1# frozen_string_literal: false 2require_relative "rexml_test_utils" 3require "rexml/light/node" 4require "rexml/parsers/lightparser" 5 6module REXMLTests 7 class LightTester < Test::Unit::TestCase 8 include REXMLTestUtils 9 include REXML::Light 10 11 def test_parse_large 12 xml_string = fixture_path("documentation.xml") 13 parser = REXML::Parsers::LightParser.new(xml_string) 14 tag, content = parser.parse 15 assert_equal([:document, :text], [tag, content.first]) 16 end 17 18 # FIXME INCOMPLETE 19 # This is because the light API is not yet ready to be used to produce 20 # trees. 21=begin 22 def test_add_element 23 doc = Node.new 24 foo = doc.add_element( 'foo' ) 25 assert_equal( "foo", foo.name ) 26 end 27 28 def test_add_attribute 29 foo = Node.new( "a" ) 30 foo["attr"] = "bar" 31 assert_equal( "bar", foo["attr"] ) 32 end 33 34 def test_write_document 35 r = make_small_document 36 assert_equal( "<a><b/><c/></a>", r.to_s ) 37 end 38 39 def test_add_attribute_under_namespace 40 foo = Node.new("a") 41 foo["attr", "a"] = "1" 42 foo["attr", "b"] = "2" 43 foo["attr"] = "3" 44 assert_equal( '1', foo['attr', 'a'] ) 45 assert_equal( '2', foo['attr', 'b'] ) 46 assert_equal( '3', foo['attr'] ) 47 end 48 49 def test_change_namespace_of_element 50 foo = Node.new 51 assert_equal( '', foo.namespace ) 52 foo.namespace = 'a' 53 assert_equal( 'a', foo.namespace ) 54 end 55 56 def test_access_child_elements 57 foo = make_small_document 58 assert_equal( 1, foo.size ) 59 a = foo[0] 60 assert_equal( 2, a.size ) 61 assert_equal( 'b', a[0].name ) 62 assert_equal( 'c', a[1].name ) 63 end 64 65 def test_itterate_over_children 66 foo = make_small_document 67 ctr = 0 68 foo[0].each { ctr += 1 } 69 assert_equal( 2, ctr ) 70 end 71 72 def test_add_text 73 foo = Node.new( "a" ) 74 foo.add_text( "Sean" ) 75 sean = foo[0] 76 assert( sean.node_type == :text ) 77 end 78 79 def test_add_instruction 80 foo = Node.new( "a" ) 81 foo.add_instruction( "target", "value" ) 82 assert( foo[0].node_type == :processing_instruction ) 83 end 84 85 def test_add_comment 86 foo = Node.new( "a" ) 87 foo.add_comment( "target", "value" ) 88 assert( foo[0].node_type == :comment ) 89 end 90 91 def test_get_root 92 foo = Node.new( 'a' ) 93 10.times { foo = foo.add_element('b') } 94 assert_equals( 'b', foo.name ) 95 assert_equals( 'a', foo.root.name ) 96 end 97 98 def make_small_document 99 r = Node.new 100 a = r.add_element( "a" ) 101 a.add_element( 'b' ) 102 a.add_element( 'c' ) 103 r 104 end 105=end 106 end 107end 108