File: parser_spec.rb

package info (click to toggle)
ruby-eim-xml 0.0.4-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 192 kB
  • ctags: 171
  • sloc: ruby: 1,993; makefile: 7
file content (102 lines) | stat: -rw-r--r-- 3,002 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require "eim_xml/parser"

module Module.new::M
	include EimXML

	describe Parser do
		def parse(src)
			EimXML::Parser.new(src).parse
		end

		it "'parser' method for test" do
			s = " <e /> "
			parse(s).should == Parser.new(s).parse
		end

		it "#parse with empty element" do
			parse("<e />").should == Element.new("e")
			parse("<e/>").should == Element.new("e")

			parse(%[<e key="value"/>]).should == Element.new("e", :key=>"value")
			parse(%[<e key='value'/>]).should == Element.new("e", :key=>"value")
			parse(%[<e key="value" />]).should == Element.new("e", :key=>"value")
			parse(%[<e key='value' />]).should == Element.new("e", :key=>"value")

			parse(%[<e key="value" key2="value2"/>]).should == Element.new("e", :key=>"value", :key2=>"value2")
			parse(%[<e key="value" key2="value2" />]).should == Element.new("e", :key=>"value", :key2=>"value2")

			s = " <e1 /> <e2 /> "
			p = Parser.new(s)
			p.parse.should == PCString.new(" ")
			p.parse.should == Element.new("e1")
			p.parse.should == PCString.new(" ")
			p.parse.should == Element.new("e2")
		end

		it "#parse with nonempty element" do
			parse("<super><sub /></super>").should == Element.new("super") << Element.new("sub")

			parse("<out><in></in></out>").should == Element.new("out") << Element.new("in")

			lambda{parse("<el></e>")}.should raise_error(ParseError, "End tag mismatched.")
			lambda{parse("<el><></el>")}.should raise_error(ParseError, "Syntax error.")
		end

		it "#parse with string" do
			e = parse("string&amp;")
			e.should be_kind_of(PCString)
			e.to_s.should == "string&amp;"
			e = parse(" string &amp; ")
			e.should be_kind_of(PCString)
			e.to_s.should == " string &amp; "

			e = Element.new("e")
			e << PCString.new(" string ")
			parse("<e> string </e>").should == e

			e = Element.new("e")
			e << PCString.new("string")
			parse("<e>string</e>").should == e
		end

		it "#parse escaped characters" do
			e = parse("&amp;&quot;&apos;&lt;&gt;")
			e.to_s.should == "&amp;&quot;&apos;&lt;&gt;"
			e.src.should == "&\"'<>"
		end

		it "#parse with holding space" do
			s = "<e> string with space\n</e>"
			e = Element.new("e")
			e << PCString.new(" string with space\n")
			parse(s).should == e
			parse(s).to_s.should == s

			s = "<ns:e> string with space\n</ns:e>"
			e = Element.new("ns:e")
			e << PCString.new(" string with space\n")
			parse(s).should == e
			parse(s).to_s.should == s

			s = "<a> string without space <b> string with space <a> string with space 2 </a> </b>  </a>"
			oa = Element.new("a") << PCString.new(" string without space ")
			b = Element.new("b")
			b << PCString.new(" string with space ")
			ia = Element.new("a")
			ia << PCString.new(" string with space 2 ")
			b << ia
			b << PCString.new(" ")
			oa << b
			oa << PCString.new("  ")
			parse(s).should == oa
			parse(s).to_s.should == s

			s = "<a><b/></a>"
			a = Element.new("a")
			b = Element.new("b")
			a << b
			parse(s).should == a
			parse(s).to_s.should == "<a><b /></a>"
		end
	end
end