require "runit/testcase"
require "runit/cui/testrunner"

require "rexml/document"
require "rexml/parseexception"

class ContribTester < RUNIT::TestCase
	# Tobias Reif <tobiasreif@pinkjuice.com>
	def test_bad_doctype
		source = <<-EOF
		 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
				"http://www.w3.org/TR/SVG/DTD/svg10.dtd"
			 [
			 <!-- <!ENTITY % fast-slow "0 0  .5 1">-->
			 <!--<!ENTITY % slow-fast ".5 0  1 1">-->
			 <!ENTITY hover_ani
				'<animateTransform attributeName="transform"
				 type="scale" restart="whenNotActive" values="1;0.96"
				 dur="0.5s" calcMode="spline" keySplines="0 0  .5 1"
				 fill="freeze" begin="mouseover"/>
				 <animateTransform  attributeName="transform"
				 type="scale" restart="whenNotActive" values="0.96;1"
				 dur="0.5s" calcMode="spline" keySplines=".5 0  1 1"
				 fill="freeze" begin="mouseover+0.5s"/>'
			 >
			 ]
		 >
		EOF
		doc = REXML::Document.new source
		out = ""
		doc.write(out)
		assert out[/>'>/] != nil, "Couldn't find >'>" # '
		assert out[/]>/] != nil, "Couldn't find ]>"
	end

	# Peter Verhage
	def test_namespace
		source = <<-EOF
		<?xml version="1.0"?>
		<config:myprog-config xmlns:config="http://someurl/program/version">
		<!-- main options -->
			<config:main>
				<config:parameter name="name"  value="value"/>
			</config:main>
		</config:myprog-config>
		EOF
		doc = REXML::Document.new source
		assert_equal "myprog-config", doc.root.name
		count = 0
		REXML::XPath.each(doc, "x:myprog-config/x:main/x:parameter", 
			{"x","http://someurl/program/version"}) { |element|
				assert_equal "name", element.attributes["name"]
			count += 1;
		}
		assert_equal 1, count
		assert_equal "myprog-config", doc.elements["config:myprog-config"].name
	end

#=begin
	# Tobias Reif <tobiasreif@pinkjuice.com>
	def test_complex_xpath
		source = <<-EOF
		<root>
			<foo>
				<bar style="baz"/>
				<blah style="baz"/>
				<blam style="baz"/>
			</foo>
			<wax>
				<fudge>
					<noodle/>
				</fudge>
			</wax>
		</root>
		EOF
		complex_path = "*[* "+
			"and not(*/node()) "+
			"and not(*[not(@style)]) "+
			"and not(*/@style != */@style)]"
		doc = REXML::Document.new source
		results = REXML::XPath.match( doc.root, complex_path )
		assert_not_nil results
		assert_equal 1, results.size
		assert_equal "foo", results[0].name
	end
#=end

	# "Chris Morris" <chrismo@charter.net>
	def test_extra_newline_on_read
		text = 'test text'
		e = REXML::Element.new('Test')
		e.add_text(text)
		out = ''
		e.write(out)

		doc = REXML::Document.new(out)
		outtext = doc.root.text

		assert_equal(text, outtext)
	end


=begin
	# Tobias Reif <tobiasreif@pinkjuice.com>
	def test_other_xpath
		schema = <<-Delimiter
		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
			elementFormDefault="qualified">
			<xs:element name="rect">
				<xs:complexType>
					<xs:attribute name="width" type="xs:byte" use="required"/>
					<xs:attribute name="height" type="xs:byte" use="required"/>
				</xs:complexType>
			</xs:element>
			<xs:element name="svg">
				<xs:complexType>
					<xs:sequence>
						<xs:element ref="rect"/>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:schema>
		Delimiter

		doc = REXML::Document.new schema

		result = REXML::XPath.first(doc, 'xs:element[descendant::xs:element[@ref]]')
		assert_not_nil result
		assert_equal "svg", result.attributes['name']
		result = REXML::XPath.first(doc, 'element[descendant::element[@ref]]')
		assert_nil result
	end
=end
end
