require "test/unit/testcase"

require 'rexml/pullparser'

class PullParserTester < Test::Unit::TestCase
	def test_basics
		source = '<?xml version="1.0"?>
		<!DOCTYPE blah>
		<a>foo &lt;<b attribute="value">bar</b> nooo</a>'
		parser = REXML::PullParser.new(source)
		res = { :text=>0 }
		until parser.empty?
			results = parser.next
			res[ :xmldecl ] = true if results.xmldecl?
			res[ :doctype ] = true if results.doctype?
			res[ :a ] = true if results.start_element? and results[0] == 'a'
			if results.start_element? and results[0] == 'b'
				res[ :b ] = true
				assert_equal 'value', results[1]['attribute']
			end
			res[ :text ] += 1 if results.text?
		end
		[ :xmldecl, :doctype, :a, :b ].each { |tag| 
			assert res[tag] , "#{tag} wasn't processed"
		}
		assert_equal 3, res[ :text ]	
	rescue REXML::ParseException 
		puts $!
	end

	def test_bad_document
		source = "<a><b></a>"
		parser = REXML::PullParser.new(source)
		assert_raises(REXML::ParseException, "Parsing should have failed") { 
			results = parser.next while parser.has_next? 
		}
	end

	def test_peek_unshift
		source = "<a><b/></a>"
		pp = REXML::PullParser.new(source)
		# FINISH ME!
	end
end
