eimxml 0.0.2 → 0.0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +24 -95
- data/Rakefile.utirake +374 -0
- data/lib/eim_xml.rb +95 -220
- data/lib/eim_xml/dsl.rb +105 -0
- data/lib/eim_xml/formatter.rb +123 -0
- data/lib/eim_xml/formatter/element_wrapper.rb +7 -0
- data/lib/eim_xml/parser.rb +12 -40
- data/lib/eim_xml/xhtml.rb +84 -56
- data/lib/eim_xml/xhtml/dsl.rb +18 -0
- data/{test → spec}/assertions_test.rb +1 -0
- data/spec/dsl_spec.rb +217 -0
- data/spec/eim_xml_spec.rb +441 -0
- data/spec/formatter_spec.rb +260 -0
- data/spec/parser_spec.rb +102 -0
- data/spec/xhtml_spec.rb +524 -0
- metadata +52 -38
- data/README +0 -58
- data/test/eim_xml_test.rb +0 -648
- data/test/parser_test.rb +0 -102
- data/test/xhtml_test.rb +0 -340
metadata
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: eimxml
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
4
|
+
hash: 65
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 3
|
|
10
|
+
- 1
|
|
11
|
+
version: 0.0.3.1
|
|
5
12
|
platform: ruby
|
|
6
13
|
authors:
|
|
7
14
|
- KURODA Hiraku
|
|
@@ -9,8 +16,7 @@ autorequire:
|
|
|
9
16
|
bindir: bin
|
|
10
17
|
cert_chain: []
|
|
11
18
|
|
|
12
|
-
date:
|
|
13
|
-
default_executable:
|
|
19
|
+
date: 2011-07-19 00:00:00 Z
|
|
14
20
|
dependencies: []
|
|
15
21
|
|
|
16
22
|
description:
|
|
@@ -19,57 +25,65 @@ executables: []
|
|
|
19
25
|
|
|
20
26
|
extensions: []
|
|
21
27
|
|
|
22
|
-
extra_rdoc_files:
|
|
23
|
-
|
|
28
|
+
extra_rdoc_files: []
|
|
29
|
+
|
|
24
30
|
files:
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
- lib/eim_xml
|
|
28
|
-
- lib/eim_xml/parser.rb
|
|
31
|
+
- Rakefile.utirake
|
|
32
|
+
- Rakefile
|
|
29
33
|
- lib/eim_xml/matcher.rb
|
|
30
|
-
- lib/eim_xml/assertions.rb
|
|
31
34
|
- lib/eim_xml/xhtml.rb
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
|
|
35
|
+
- lib/eim_xml/dsl.rb
|
|
36
|
+
- lib/eim_xml/parser.rb
|
|
37
|
+
- lib/eim_xml/formatter.rb
|
|
38
|
+
- lib/eim_xml/xhtml/dsl.rb
|
|
39
|
+
- lib/eim_xml/assertions.rb
|
|
40
|
+
- lib/eim_xml/formatter/element_wrapper.rb
|
|
41
|
+
- lib/eim_xml.rb
|
|
42
|
+
- spec/assertions_test.rb
|
|
43
|
+
- spec/formatter_spec.rb
|
|
44
|
+
- spec/eim_xml_spec.rb
|
|
45
|
+
- spec/dsl_spec.rb
|
|
46
|
+
- spec/xhtml_spec.rb
|
|
47
|
+
- spec/parser_spec.rb
|
|
40
48
|
homepage: http://eimxml.rubyforge.org/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
licenses: []
|
|
50
|
+
|
|
51
|
+
post_install_message: |-
|
|
52
|
+
************************************************************
|
|
53
|
+
[DEPRECATION]
|
|
54
|
+
"eimxml" gem is deprecated.
|
|
55
|
+
Please use "eim_xml" gem instead.
|
|
56
|
+
************************************************************
|
|
57
|
+
rdoc_options: []
|
|
58
|
+
|
|
50
59
|
require_paths:
|
|
51
60
|
- lib
|
|
52
61
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
none: false
|
|
53
63
|
requirements:
|
|
54
64
|
- - ">="
|
|
55
65
|
- !ruby/object:Gem::Version
|
|
66
|
+
hash: 3
|
|
67
|
+
segments:
|
|
68
|
+
- 0
|
|
56
69
|
version: "0"
|
|
57
|
-
version:
|
|
58
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
none: false
|
|
59
72
|
requirements:
|
|
60
|
-
- - "
|
|
73
|
+
- - ">"
|
|
61
74
|
- !ruby/object:Gem::Version
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
hash: 25
|
|
76
|
+
segments:
|
|
77
|
+
- 1
|
|
78
|
+
- 3
|
|
79
|
+
- 1
|
|
80
|
+
version: 1.3.1
|
|
64
81
|
requirements: []
|
|
65
82
|
|
|
66
83
|
rubyforge_project: eimxml
|
|
67
|
-
rubygems_version: 1.
|
|
84
|
+
rubygems_version: 1.8.5
|
|
68
85
|
signing_key:
|
|
69
|
-
specification_version:
|
|
86
|
+
specification_version: 3
|
|
70
87
|
summary: Easy IMplemented XML
|
|
71
|
-
test_files:
|
|
72
|
-
|
|
73
|
-
- test/eim_xml_test.rb
|
|
74
|
-
- test/assertions_test.rb
|
|
75
|
-
- test/parser_test.rb
|
|
88
|
+
test_files: []
|
|
89
|
+
|
data/README
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
= Easy IMplemented XML
|
|
2
|
-
:lang:ja
|
|
3
|
-
Easy IMplemented XML(EimXML)は、RubyスクリプトからXMLを作成するためのライブラリです。
|
|
4
|
-
動作が軽くなるように、単純さを心がけて実装しています。
|
|
5
|
-
|
|
6
|
-
:lang:en
|
|
7
|
-
Easy IMplemented XML(EimXML) is library to construct XML on Ruby.
|
|
8
|
-
For light weight performance, this library is implemented simply.
|
|
9
|
-
|
|
10
|
-
:lang:
|
|
11
|
-
= URLs
|
|
12
|
-
* Document(Japanese)[http://eimxml.rubyforge.org/ja/]
|
|
13
|
-
* Document(English)[http://eimxml.rubyforge.org/en/]
|
|
14
|
-
* MercurialRepository[https://hg.hinet.mydns.jp/eim_xml/]
|
|
15
|
-
|
|
16
|
-
= Sample
|
|
17
|
-
:lang:ja
|
|
18
|
-
例えば、次のようなHTMLを作成する場合は
|
|
19
|
-
|
|
20
|
-
:lang:en
|
|
21
|
-
For example, to construct html like bellow
|
|
22
|
-
|
|
23
|
-
:lang:
|
|
24
|
-
<html>
|
|
25
|
-
<head>
|
|
26
|
-
<title>TITLE</title>
|
|
27
|
-
</head>
|
|
28
|
-
<body>
|
|
29
|
-
<p>Texts...</p>
|
|
30
|
-
</body>
|
|
31
|
-
</html>
|
|
32
|
-
|
|
33
|
-
:lang:ja
|
|
34
|
-
スクリプトは以下のように記述します。
|
|
35
|
-
|
|
36
|
-
:lang:en
|
|
37
|
-
write script like bellow.
|
|
38
|
-
|
|
39
|
-
:lang:
|
|
40
|
-
#!/usr/bin/ruby
|
|
41
|
-
# require "rubygems"
|
|
42
|
-
require "eim_xml/xhtml"
|
|
43
|
-
|
|
44
|
-
include EimXML::XHTML
|
|
45
|
-
html = HTML.new do |html|
|
|
46
|
-
html << Head.new do |head|
|
|
47
|
-
head << Title.new do |title|
|
|
48
|
-
title << "TITLE"
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
html << Body.new do |body|
|
|
52
|
-
body << P.new do |par|
|
|
53
|
-
par << "Text..."
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
html.write($stdout, false)
|
data/test/eim_xml_test.rb
DELETED
|
@@ -1,648 +0,0 @@
|
|
|
1
|
-
# Test for eim_xml.rb
|
|
2
|
-
#
|
|
3
|
-
# Copyright (C) 2006, KURODA Hiraku <hiraku@hinet.mydns.jp>
|
|
4
|
-
# You can redistribute it and/or modify it under GPL2.
|
|
5
|
-
|
|
6
|
-
require "test/unit"
|
|
7
|
-
require "eim_xml"
|
|
8
|
-
require "spec"
|
|
9
|
-
|
|
10
|
-
class PCStringTest < Test::Unit::TestCase
|
|
11
|
-
include EimXML
|
|
12
|
-
|
|
13
|
-
def test_encode
|
|
14
|
-
assert_equal("<>"'&", PCString.encode("<>\"'&"))
|
|
15
|
-
assert_equal("&test;", PCString.encode("&test;"))
|
|
16
|
-
assert_equal("&amp;", PCString.encode("&"))
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def test_new
|
|
20
|
-
assert_equal("&", PCString.new("&").encoded_string)
|
|
21
|
-
assert_equal("&", PCString.new("&", true).encoded_string)
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def test_equal
|
|
25
|
-
p1 = PCString.new("str")
|
|
26
|
-
p2 = PCString.new("str")
|
|
27
|
-
assert_equal(p1, p2)
|
|
28
|
-
|
|
29
|
-
assert_not_equal(p1, "str")
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def test_to_xml
|
|
33
|
-
assert_equal("&amp;", PCString.new("&").to_xml)
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
class ElementTest < Test::Unit::TestCase
|
|
38
|
-
include EimXML
|
|
39
|
-
|
|
40
|
-
class Dummy < Element
|
|
41
|
-
def chgname(name)
|
|
42
|
-
self.name = name
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def test_name
|
|
47
|
-
e = Element.new("el")
|
|
48
|
-
assert_equal(:el, e.name)
|
|
49
|
-
assert_raises(NoMethodError){e.name="changed"}
|
|
50
|
-
|
|
51
|
-
d = Dummy.new("el1")
|
|
52
|
-
assert_equal(:el1, d.name)
|
|
53
|
-
d.chgname(:el2)
|
|
54
|
-
assert_equal(:el2, d.name)
|
|
55
|
-
d.chgname("el3")
|
|
56
|
-
assert_equal(:el3, d.name)
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def test_attributes
|
|
60
|
-
e = Element.new("el", {"a1"=>"v1", "a2"=>"v2", "a3"=>nil})
|
|
61
|
-
assert_equal(:el, e.name)
|
|
62
|
-
assert_equal({:a1=>"v1", :a2=>"v2", :a3=>nil}, e.attributes)
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def test_bracket
|
|
66
|
-
e = Element.new(:el, :attr=>"value")
|
|
67
|
-
e << "test"
|
|
68
|
-
assert_equal("value", e[:attr])
|
|
69
|
-
assert_equal("test", e[0])
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def test_add_attribute
|
|
73
|
-
e = Element.new("el")
|
|
74
|
-
e.add_attribute("key_str", "value1")
|
|
75
|
-
e.add_attribute(:key_sym, "value2")
|
|
76
|
-
assert_equal({:key_str=>"value1", :key_sym=>"value2"}, e.attributes)
|
|
77
|
-
e.add_attribute(:nil, nil)
|
|
78
|
-
assert_equal({:key_str=>"value1", :key_sym=>"value2", :nil=>nil}, e.attributes)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def test_del_attribute
|
|
82
|
-
e = Element.new("el", {:a1=>"v1", :a2=>"v2"})
|
|
83
|
-
e.del_attribute("a1")
|
|
84
|
-
assert_equal({:a2=>"v2"}, e.attributes)
|
|
85
|
-
e.del_attribute(:a2)
|
|
86
|
-
assert_equal({}, e.attributes)
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def test_contents
|
|
90
|
-
sub = Element.new("sub")
|
|
91
|
-
e = Element.new("el") << "String1" << "String2" << sub
|
|
92
|
-
assert_equal(["String1", "String2", sub], e.contents)
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def test_add
|
|
96
|
-
e = Element.new("el").add(Element.new("sub"))
|
|
97
|
-
assert_instance_of(Element, e, "add Element")
|
|
98
|
-
assert_equal(:el, e.name)
|
|
99
|
-
|
|
100
|
-
e = Element.new("el")
|
|
101
|
-
e.add(Element.new("sub1"))
|
|
102
|
-
e.add([Element.new("sub2").add("text"), "string"])
|
|
103
|
-
assert_equal([Element.new("sub1"), Element.new("sub2").add("text"), "string"], e.contents, "add Array")
|
|
104
|
-
|
|
105
|
-
e = Element.new("el")
|
|
106
|
-
e.add(nil)
|
|
107
|
-
assert_equal(0, e.contents.size, "add nil")
|
|
108
|
-
|
|
109
|
-
e = Element.new("el").add(:symbol)
|
|
110
|
-
assert_equal([:symbol], e.contents, "add anything(which has to_s)")
|
|
111
|
-
assert_equal("<el>symbol</el>", e.to_xml)
|
|
112
|
-
|
|
113
|
-
e = Element.new("super") << Element.new("sub")
|
|
114
|
-
assert_equal(:super, e.name)
|
|
115
|
-
assert_equal([Element.new("sub")], e.contents)
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def test_to_xml_with_indent
|
|
119
|
-
e = Element.new("el")
|
|
120
|
-
s = String.new
|
|
121
|
-
assert_equal(s.object_id, e.to_xml_with_indent(s).object_id)
|
|
122
|
-
assert_equal("<el />", s)
|
|
123
|
-
|
|
124
|
-
e = Element.new("super")
|
|
125
|
-
e << Element.new("sub")
|
|
126
|
-
assert_equal("<super><sub /></super>", e.to_xml_with_indent)
|
|
127
|
-
e << Element.new("sub2")
|
|
128
|
-
assert_equal("<super>\n <sub />\n <sub2 />\n</super>", e.to_xml_with_indent)
|
|
129
|
-
assert_equal(" <super>\n <sub />\n <sub2 />\n </super>", e.to_xml_with_indent("", 1))
|
|
130
|
-
assert_equal("<super>\n <sub />\n <sub2 />\n </super>", e.to_xml_with_indent("", 1, false))
|
|
131
|
-
|
|
132
|
-
s = Element.new("supersuper")
|
|
133
|
-
s << e
|
|
134
|
-
assert_equal("<supersuper><super>\n <sub />\n <sub2 />\n</super></supersuper>", s.to_xml_with_indent)
|
|
135
|
-
|
|
136
|
-
e = Element.new("el") << "str"
|
|
137
|
-
s = Element.new("sub")
|
|
138
|
-
s << "inside"
|
|
139
|
-
e << s
|
|
140
|
-
assert_equal("<el>\n str\n <sub>inside</sub>\n</el>", e.to_xml_with_indent)
|
|
141
|
-
|
|
142
|
-
e = Element.new("el")
|
|
143
|
-
e.attributes["a1"] = "v1"
|
|
144
|
-
e.attributes["a2"] = "'\"<>&"
|
|
145
|
-
s = e.to_xml_with_indent
|
|
146
|
-
assert_match(/\A<el ([^>]*) \/>\z/, s)
|
|
147
|
-
assert_match(/a1='v1'/, s)
|
|
148
|
-
assert_match(/a2=''"<>&'/, s)
|
|
149
|
-
|
|
150
|
-
e = Element.new("el", {"a1"=>nil})
|
|
151
|
-
assert_equal("<el a1='a1' />", e.to_xml_with_indent)
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
def test_to_xml
|
|
155
|
-
el = Element.new("el")
|
|
156
|
-
ex = "<el />"
|
|
157
|
-
assert_equal(ex, el.to_xml)
|
|
158
|
-
|
|
159
|
-
s = ""
|
|
160
|
-
r = el.to_xml(s)
|
|
161
|
-
assert_equal(ex, r)
|
|
162
|
-
assert_equal(s.object_id, r.object_id)
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
def test_spcial_string
|
|
166
|
-
e = Element.new("el") << "&\"'<>"
|
|
167
|
-
e << PCString.new("&\"'<>", true)
|
|
168
|
-
e.attributes["key"] = PCString.new("&\"'<>", true)
|
|
169
|
-
assert_equal(%[<el key='&\"'<>'>\n &"'<>\n &\"'<>\n</el>], e.to_s)
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
def test_dup
|
|
173
|
-
e = Element.new("el")
|
|
174
|
-
e.attributes["key"] = "value"
|
|
175
|
-
e << "String"
|
|
176
|
-
e << "Freeze".freeze
|
|
177
|
-
s = Element.new("sub")
|
|
178
|
-
s.attributes["subkey"] = "subvalue"
|
|
179
|
-
e << s
|
|
180
|
-
f = e.dup
|
|
181
|
-
|
|
182
|
-
assert_equal(e.attributes.object_id, f.attributes.object_id)
|
|
183
|
-
assert_equal(e.contents.object_id, f.contents.object_id)
|
|
184
|
-
|
|
185
|
-
assert_equal(e.to_s, f.to_s)
|
|
186
|
-
|
|
187
|
-
e = Element.new("el")
|
|
188
|
-
e.hold_space
|
|
189
|
-
f = e.dup
|
|
190
|
-
assert(f.hold_space?)
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
def test_clone
|
|
194
|
-
e = Element.new("el")
|
|
195
|
-
e.attributes["key"] = "value"
|
|
196
|
-
e << "String"
|
|
197
|
-
e << "Freeze".freeze
|
|
198
|
-
s = Element.new("sub")
|
|
199
|
-
s.attributes["subkey"] = "subvalue"
|
|
200
|
-
e << s
|
|
201
|
-
f = e.clone
|
|
202
|
-
|
|
203
|
-
assert_equal(e.attributes.object_id, f.attributes.object_id)
|
|
204
|
-
assert_equal(e.contents.object_id, f.contents.object_id)
|
|
205
|
-
|
|
206
|
-
assert_equal(e.to_s, f.to_s)
|
|
207
|
-
|
|
208
|
-
e = Element.new("el")
|
|
209
|
-
e.hold_space
|
|
210
|
-
f = e.clone
|
|
211
|
-
assert(f.hold_space?)
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
def test_hold_space
|
|
215
|
-
e = Element.new("el") << "Line1" << "Line2"
|
|
216
|
-
s = Element.new("sub") << "Sub1" << "Sub2"
|
|
217
|
-
ss = Element.new("subsub") << "ss1" << "ss2"
|
|
218
|
-
s << ss
|
|
219
|
-
e << s
|
|
220
|
-
e.hold_space
|
|
221
|
-
assert_equal("<el>Line1Line2<sub>Sub1Sub2<subsub>ss1ss2</subsub></sub></el>", e.to_s)
|
|
222
|
-
|
|
223
|
-
e.unhold_space
|
|
224
|
-
assert_equal("<el>\n Line1\n Line2\n <sub>\n Sub1\n Sub2\n <subsub>\n ss1\n ss2\n </subsub>\n </sub>\n</el>", e.to_s)
|
|
225
|
-
|
|
226
|
-
e = Element.new("e")
|
|
227
|
-
assert_equal(e.object_id, e.hold_space.object_id)
|
|
228
|
-
assert_equal(e.object_id, e.unhold_space.object_id)
|
|
229
|
-
end
|
|
230
|
-
|
|
231
|
-
def test_equal
|
|
232
|
-
e1 = Element.new("el")
|
|
233
|
-
e1.attributes["key"] = "value"
|
|
234
|
-
s = Element.new("sub")
|
|
235
|
-
s << "String"
|
|
236
|
-
e1 << s
|
|
237
|
-
e2 = e1.dup
|
|
238
|
-
assert_equal(e1, e2)
|
|
239
|
-
|
|
240
|
-
e3 = Element.new("e")
|
|
241
|
-
e3.attributes["key"] = "value"
|
|
242
|
-
s = Element.new("sub")
|
|
243
|
-
s << "String"
|
|
244
|
-
e3 << s
|
|
245
|
-
assert_not_equal(e1, e3)
|
|
246
|
-
|
|
247
|
-
e3 = Element.new("e")
|
|
248
|
-
e3.attributes["k"] = "value"
|
|
249
|
-
s = Element.new("sub")
|
|
250
|
-
s << "String"
|
|
251
|
-
e3 << s
|
|
252
|
-
assert_not_equal(e1, e3)
|
|
253
|
-
|
|
254
|
-
e3 = Element.new("e")
|
|
255
|
-
e3.attributes["key"] = "v"
|
|
256
|
-
s = Element.new("sub")
|
|
257
|
-
s << "String"
|
|
258
|
-
e3 << s
|
|
259
|
-
assert_not_equal(e1, e3)
|
|
260
|
-
|
|
261
|
-
e3 = Element.new("e")
|
|
262
|
-
e3.attributes["key"] = "value"
|
|
263
|
-
s = Element.new("sub")
|
|
264
|
-
s << "S"
|
|
265
|
-
e3 << s
|
|
266
|
-
assert_not_equal(e1, e3)
|
|
267
|
-
|
|
268
|
-
e3 = Element.new("e")
|
|
269
|
-
e3.attributes["key"] = "value"
|
|
270
|
-
s = Element.new("s")
|
|
271
|
-
s << "String"
|
|
272
|
-
e3 << s
|
|
273
|
-
assert_not_equal(e1, e3)
|
|
274
|
-
|
|
275
|
-
assert_not_equal(e1, "string")
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
def test_new_with_block
|
|
279
|
-
base = nil
|
|
280
|
-
e = Element.new("base") do |b|
|
|
281
|
-
b["attr"]="value"
|
|
282
|
-
b << Element.new("sub")
|
|
283
|
-
base = b
|
|
284
|
-
end
|
|
285
|
-
assert_same(e, base)
|
|
286
|
-
e2 = Element.new("base", "attr"=>"value")
|
|
287
|
-
e2 << Element.new("sub")
|
|
288
|
-
assert_equal(e, e2)
|
|
289
|
-
|
|
290
|
-
e = Element.new("base") do |e|
|
|
291
|
-
e <<= Element.new("sub1") do |e|
|
|
292
|
-
e <<= Element.new("sub12")
|
|
293
|
-
end
|
|
294
|
-
e <<= Element.new("sub2")
|
|
295
|
-
end
|
|
296
|
-
base = Element.new("base")
|
|
297
|
-
sub1 = Element.new("sub1")
|
|
298
|
-
sub1 << Element.new("sub12")
|
|
299
|
-
sub2 = Element.new("sub2")
|
|
300
|
-
base << sub1 << sub2
|
|
301
|
-
assert_equal(base, e)
|
|
302
|
-
end
|
|
303
|
-
|
|
304
|
-
def test_symbol_string_compatible
|
|
305
|
-
sym = Element.new(:tag, :attr=>"value")
|
|
306
|
-
str = Element.new("tag", "attr"=>"value")
|
|
307
|
-
|
|
308
|
-
assert_equal(sym.to_s, str.to_s)
|
|
309
|
-
assert_equal(sym, str)
|
|
310
|
-
end
|
|
311
|
-
|
|
312
|
-
def test_match
|
|
313
|
-
e = Element.new(:tag, :attr=>"value")
|
|
314
|
-
assert(e.match?(:tag))
|
|
315
|
-
assert(e.match?(:tag, :attr=>"value"))
|
|
316
|
-
assert(! e.match?(:t))
|
|
317
|
-
assert(! e.match?(:tag, :attr2=>"value"))
|
|
318
|
-
assert(! e.match?(:tag, :attr=>"value2"))
|
|
319
|
-
|
|
320
|
-
assert(e.match?(Element.new(:tag)))
|
|
321
|
-
assert(e.match?(Element.new(:tag, :attr=>"value")))
|
|
322
|
-
assert(! e.match?(Element.new(:t)))
|
|
323
|
-
assert(! e.match?(Element.new(:tag, :attr2=>"value")))
|
|
324
|
-
assert(! e.match?(Element.new(:tag, :attr=>"value2")))
|
|
325
|
-
|
|
326
|
-
assert(e.match?(/ag/))
|
|
327
|
-
assert(e.match?(/ag/, /tt/=>/al/))
|
|
328
|
-
assert(! e.match?(/elem/))
|
|
329
|
-
assert(! e.match?(/tag/, /attr2/=>/val/))
|
|
330
|
-
assert(! e.match?(/tag/, /attr/=>/v2/))
|
|
331
|
-
|
|
332
|
-
assert(e.match?(:tag, :attr=>/val/))
|
|
333
|
-
assert(e.match?(/t/, /at/=>"value"))
|
|
334
|
-
|
|
335
|
-
e = Element.new(:tag, :attr1=>"value", :attr2=>"test")
|
|
336
|
-
assert(e.match?(:tag, /attr/=>"value"))
|
|
337
|
-
assert(e.match?(:tag, /attr/=>/t/))
|
|
338
|
-
|
|
339
|
-
assert(e.match?(Element))
|
|
340
|
-
assert(!e.match?(Dummy))
|
|
341
|
-
assert(!e.match?(String))
|
|
342
|
-
|
|
343
|
-
e1 = Element.new(:tag, :attr=>:value)
|
|
344
|
-
e2 = Element.new(:tag, :attr=>"value")
|
|
345
|
-
assert(e1.match?(e2))
|
|
346
|
-
assert(e2.match?(e1))
|
|
347
|
-
end
|
|
348
|
-
|
|
349
|
-
def test_match_by_array
|
|
350
|
-
e = Element.new(:tag, :attr=>"value", :a2=>"v2")
|
|
351
|
-
assert(e.match?([:tag]))
|
|
352
|
-
assert(e.match?([:tag, {:attr=>"value"}]))
|
|
353
|
-
assert(e.match?([:tag, {:attr=>"value", :a2=>"v2"}]))
|
|
354
|
-
assert(e.match?([/tag/, {/a/=>/v/}]))
|
|
355
|
-
end
|
|
356
|
-
|
|
357
|
-
def test_match_operator
|
|
358
|
-
e = Element.new(:tag, :attr=>"value", :a2=>"v2")
|
|
359
|
-
assert_match(:tag, e)
|
|
360
|
-
assert_match(Element.new(:tag), e)
|
|
361
|
-
assert_match([:tag], e)
|
|
362
|
-
assert_match([:tag, {:attr=>"value"}], e)
|
|
363
|
-
assert_match(Element.new(:tag, :a2=>"v2"), e)
|
|
364
|
-
assert_match([/t/, {/a/=>/v/}], e)
|
|
365
|
-
|
|
366
|
-
assert(e !~ :t)
|
|
367
|
-
assert(e !~ Element.new(:t))
|
|
368
|
-
assert(e !~ [:t])
|
|
369
|
-
assert(e !~ [:tag, {:a=>"v"}])
|
|
370
|
-
assert(e !~ Element.new(:tag, :a=>"v"))
|
|
371
|
-
end
|
|
372
|
-
|
|
373
|
-
def test_has?
|
|
374
|
-
e = Element.new(:base) do |b|
|
|
375
|
-
b <<= Element.new(:sub) do |s|
|
|
376
|
-
s <<= Element.new(:deep) do |d|
|
|
377
|
-
d << "text"
|
|
378
|
-
end
|
|
379
|
-
end
|
|
380
|
-
b <<= Element.new(:sub, :attr=>"value")
|
|
381
|
-
end
|
|
382
|
-
|
|
383
|
-
assert(e.has?(:base))
|
|
384
|
-
assert(e.has?(:sub))
|
|
385
|
-
assert(e.has?(:sub, :attr=>"value"))
|
|
386
|
-
assert(!e.has?(:sub, :attr=>"value", :attr2=>""))
|
|
387
|
-
assert(e.has?(:deep))
|
|
388
|
-
assert(! e.has?(:deep, {}, false))
|
|
389
|
-
|
|
390
|
-
assert(e.has?(String))
|
|
391
|
-
|
|
392
|
-
e = DSL.element(:base) do
|
|
393
|
-
element(:sub, :sym=>:v1, "string"=>"v2")
|
|
394
|
-
end
|
|
395
|
-
assert(e.has?(Element.new(:sub, :sym=>"v1")))
|
|
396
|
-
assert(e.has?(Element.new(:sub, "sym"=>"v1")))
|
|
397
|
-
assert(e.has?(Element.new(:sub, "string"=>:v2)))
|
|
398
|
-
assert(e.has?(Element.new(:sub, :string=>:v2)))
|
|
399
|
-
end
|
|
400
|
-
|
|
401
|
-
def test_has_element?
|
|
402
|
-
e = Element.new(:base) do |b|
|
|
403
|
-
b <<= Element.new(:sub) do |s|
|
|
404
|
-
s <<= Element.new(:deep) do |d|
|
|
405
|
-
d << "text"
|
|
406
|
-
end
|
|
407
|
-
end
|
|
408
|
-
b <<= Element.new(:sub, :attr=>"value")
|
|
409
|
-
end
|
|
410
|
-
|
|
411
|
-
assert(e.has_element?(:base))
|
|
412
|
-
assert(e.has_element?(:sub))
|
|
413
|
-
assert(e.has_element?(:sub, :attr=>"value"))
|
|
414
|
-
assert(!e.has_element?(:sub, :attr=>"value", :attr2=>""))
|
|
415
|
-
assert(e.has_element?(:deep))
|
|
416
|
-
assert(! e.has_element?(:deep, {}, false))
|
|
417
|
-
|
|
418
|
-
assert(e.has_element?(String))
|
|
419
|
-
|
|
420
|
-
e = DSL.element(:base) do
|
|
421
|
-
element(:sub, :sym=>:v1, "string"=>"v2")
|
|
422
|
-
end
|
|
423
|
-
assert(e.has_element?(Element.new(:sub, :sym=>"v1")))
|
|
424
|
-
assert(e.has_element?(Element.new(:sub, "sym"=>"v1")))
|
|
425
|
-
assert(e.has_element?(Element.new(:sub, "string"=>:v2)))
|
|
426
|
-
assert(e.has_element?(Element.new(:sub, :string=>:v2)))
|
|
427
|
-
end
|
|
428
|
-
|
|
429
|
-
def test_find
|
|
430
|
-
s1 = Element.new(:sub)
|
|
431
|
-
d = Element.new(:deep)
|
|
432
|
-
d << "3rd"
|
|
433
|
-
s1 << "2nd" << d
|
|
434
|
-
s2 = Element.new(:sub, :attr=>"value")
|
|
435
|
-
e = Element.new(:base)
|
|
436
|
-
e << "1st" << s1 << s2
|
|
437
|
-
|
|
438
|
-
assert_equal([d], e.find(:deep))
|
|
439
|
-
assert_equal([s1, s2], e.find(:sub))
|
|
440
|
-
assert_equal([e, s1, d, s2], e.find(//))
|
|
441
|
-
|
|
442
|
-
assert_equal(["1st", "2nd", "3rd"], e.find(String))
|
|
443
|
-
end
|
|
444
|
-
end
|
|
445
|
-
|
|
446
|
-
class SymbolKeyHashTest < Test::Unit::TestCase
|
|
447
|
-
SKH = EimXML::SymbolKeyHash
|
|
448
|
-
|
|
449
|
-
def test_new
|
|
450
|
-
s = SKH.new({"key1"=>"value1", :key2=>"value2"})
|
|
451
|
-
assert_equal({:key1=>"value1", :key2=>"value2"}, s)
|
|
452
|
-
end
|
|
453
|
-
|
|
454
|
-
def test_update
|
|
455
|
-
h = {"key"=>"value"}
|
|
456
|
-
s = SKH.new
|
|
457
|
-
s.update(h)
|
|
458
|
-
assert_equal({:key=>"value"}, s)
|
|
459
|
-
|
|
460
|
-
s2 = SKH.new
|
|
461
|
-
s2.update(s)
|
|
462
|
-
assert_equal(s, s2)
|
|
463
|
-
end
|
|
464
|
-
|
|
465
|
-
def test_merge
|
|
466
|
-
s = SKH.new
|
|
467
|
-
s2 = s.merge({"key"=>"value"})
|
|
468
|
-
assert_equal({}, s)
|
|
469
|
-
assert_equal({:key=>"value"}, s2)
|
|
470
|
-
end
|
|
471
|
-
|
|
472
|
-
def test_merge!
|
|
473
|
-
s = SKH.new
|
|
474
|
-
s2 = s.merge!({"key"=>"value"})
|
|
475
|
-
h = {:key=>"value"}
|
|
476
|
-
assert_equal(h, s)
|
|
477
|
-
assert_equal(h, s2)
|
|
478
|
-
end
|
|
479
|
-
|
|
480
|
-
def test_store
|
|
481
|
-
s = SKH.new
|
|
482
|
-
s.store(:sym1, "value1")
|
|
483
|
-
s.store("str1", "value2")
|
|
484
|
-
s[:sym2] = "value3"
|
|
485
|
-
s["str2"] = "value4"
|
|
486
|
-
|
|
487
|
-
assert_equal({:sym1=>"value1", :str1=>"value2", :sym2=>"value3", :str2=>"value4"}, s)
|
|
488
|
-
end
|
|
489
|
-
end
|
|
490
|
-
|
|
491
|
-
describe EimXML::DSL do
|
|
492
|
-
it "scope is in instance of EimXML::DSL" do
|
|
493
|
-
outer = inner = nil
|
|
494
|
-
e3 = e2 = nil
|
|
495
|
-
block_executed = false
|
|
496
|
-
e = EimXML::DSL.element(:out, :k1=>"v1") do
|
|
497
|
-
outer = self
|
|
498
|
-
e2 = element(:in, :k2=>"v2") do
|
|
499
|
-
block_executed = true
|
|
500
|
-
inner = self
|
|
501
|
-
e3 = element(:deep)
|
|
502
|
-
end
|
|
503
|
-
end
|
|
504
|
-
|
|
505
|
-
block_executed.should == true
|
|
506
|
-
outer.should be_kind_of(EimXML::DSL)
|
|
507
|
-
inner.should be_kind_of(EimXML::DSL)
|
|
508
|
-
outer.object_id.should == inner.object_id
|
|
509
|
-
|
|
510
|
-
e.name.should == :out
|
|
511
|
-
e[:k1].should == "v1"
|
|
512
|
-
e[0].name.should == :in
|
|
513
|
-
e[0][:k2].should == "v2"
|
|
514
|
-
e[0][0].name.should == :deep
|
|
515
|
-
e2.object_id.should == e[0].object_id
|
|
516
|
-
e3.object_id.should == e[0][0].object_id
|
|
517
|
-
end
|
|
518
|
-
end
|
|
519
|
-
|
|
520
|
-
describe "Subclass of EimXML::BaseDSL" do
|
|
521
|
-
class DSL1 < EimXML::BaseDSL
|
|
522
|
-
register([EimXML::Element, "call"])
|
|
523
|
-
register(Hash)
|
|
524
|
-
register(String, Array, Object)
|
|
525
|
-
end
|
|
526
|
-
|
|
527
|
-
it "register" do
|
|
528
|
-
lambda{EimXML::DSL.call(:dummy)}.should raise_error(NoMethodError)
|
|
529
|
-
lambda{EimXML::BaseDSL.call(:dummy)}.should raise_error(NoMethodError)
|
|
530
|
-
lambda{DSL1.element(:dummy)}.should raise_error(NoMethodError)
|
|
531
|
-
DSL1.call(:dummy).should be_kind_of(EimXML::Element)
|
|
532
|
-
DSL1.hash.should be_kind_of(Hash)
|
|
533
|
-
DSL1.string.should be_kind_of(String)
|
|
534
|
-
DSL1.array.should be_kind_of(Array)
|
|
535
|
-
DSL1.object.should be_kind_of(Object)
|
|
536
|
-
end
|
|
537
|
-
end
|
|
538
|
-
|
|
539
|
-
describe EimXML::OpenDSL do
|
|
540
|
-
it "scope of block is one of outside" do
|
|
541
|
-
@scope_checker_variable = 1
|
|
542
|
-
block_executed = false
|
|
543
|
-
d = EimXML::OpenDSL.new do |d|
|
|
544
|
-
block_executed = true
|
|
545
|
-
d.should be_kind_of(EimXML::OpenDSL)
|
|
546
|
-
d.container.should be_nil
|
|
547
|
-
d.element(:base, :key1=>"v1") do
|
|
548
|
-
@scope_checker_variable.should == 1
|
|
549
|
-
self.should_not be_kind_of(EimXML::Element)
|
|
550
|
-
d.container.should be_kind_of(EimXML::Element)
|
|
551
|
-
d.container.should == EimXML::Element.new(:base, :key1=>"v1")
|
|
552
|
-
d.element(:sub, :key2=>"v2") do
|
|
553
|
-
d.container.should be_kind_of(EimXML::Element)
|
|
554
|
-
d.container.should == EimXML::Element.new(:sub, :key2=>"v2")
|
|
555
|
-
end
|
|
556
|
-
d.element(:sub2).should == EimXML::Element.new(:sub2)
|
|
557
|
-
end
|
|
558
|
-
end
|
|
559
|
-
block_executed.should be_true
|
|
560
|
-
end
|
|
561
|
-
|
|
562
|
-
it "DSL methods return element" do
|
|
563
|
-
d = EimXML::OpenDSL.new
|
|
564
|
-
d.container.should be_nil
|
|
565
|
-
r = d.element(:base, :key1=>"v1") do
|
|
566
|
-
d.element(:sub, :key2=>"v2")
|
|
567
|
-
end
|
|
568
|
-
r.should == EimXML::DSL.element(:base, :key1=>"v1") do
|
|
569
|
-
element(:sub, :key2=>"v2")
|
|
570
|
-
end
|
|
571
|
-
end
|
|
572
|
-
|
|
573
|
-
it "DSL method's block given instance of OpenDSL" do
|
|
574
|
-
e = EimXML::OpenDSL.new.element(:base) do |d|
|
|
575
|
-
d.should be_kind_of(EimXML::OpenDSL)
|
|
576
|
-
d.container.name.should == :base
|
|
577
|
-
d.element(:sub) do |d2|
|
|
578
|
-
d2.object_id.should == d.object_id
|
|
579
|
-
end
|
|
580
|
-
end
|
|
581
|
-
|
|
582
|
-
e.should == EimXML::DSL.element(:base) do
|
|
583
|
-
element(:sub)
|
|
584
|
-
end
|
|
585
|
-
end
|
|
586
|
-
|
|
587
|
-
it "ensure reset container when error raised" do
|
|
588
|
-
EimXML::OpenDSL.new do |d|
|
|
589
|
-
begin
|
|
590
|
-
d.element(:base) do
|
|
591
|
-
begin
|
|
592
|
-
d.element(:sub) do
|
|
593
|
-
raise "OK"
|
|
594
|
-
end
|
|
595
|
-
rescue RuntimeError => e
|
|
596
|
-
raise unless e.message=="OK"
|
|
597
|
-
d.container.name.should == :base
|
|
598
|
-
raise
|
|
599
|
-
end
|
|
600
|
-
end
|
|
601
|
-
rescue RuntimeError => e
|
|
602
|
-
raise unless e.message=="OK"
|
|
603
|
-
d.container.should == nil
|
|
604
|
-
end
|
|
605
|
-
end
|
|
606
|
-
end
|
|
607
|
-
|
|
608
|
-
it "respond to add" do
|
|
609
|
-
r = EimXML::OpenDSL.new.element(:base) do |d|
|
|
610
|
-
d.add "text"
|
|
611
|
-
d.element(:sub) do
|
|
612
|
-
s = EimXML::Element.new(:sub)
|
|
613
|
-
s.add("sub text")
|
|
614
|
-
d.add("sub text").should == s
|
|
615
|
-
end
|
|
616
|
-
end
|
|
617
|
-
|
|
618
|
-
r.should == EimXML::DSL.element(:base) do
|
|
619
|
-
add "text"
|
|
620
|
-
element(:sub) do
|
|
621
|
-
add "sub text"
|
|
622
|
-
end
|
|
623
|
-
end
|
|
624
|
-
end
|
|
625
|
-
|
|
626
|
-
it "respond to <<" do
|
|
627
|
-
r = EimXML::OpenDSL.new.element(:base) do |d|
|
|
628
|
-
b = EimXML::Element.new(:base)
|
|
629
|
-
b << "text" << "next"
|
|
630
|
-
(d << "text" << "next").should == b
|
|
631
|
-
end
|
|
632
|
-
r.should == EimXML::DSL.element(:base) do
|
|
633
|
-
add "text"
|
|
634
|
-
add "next"
|
|
635
|
-
end
|
|
636
|
-
end
|
|
637
|
-
|
|
638
|
-
it "can call directly element method" do
|
|
639
|
-
r = EimXML::OpenDSL.element(:base) do |d|
|
|
640
|
-
d.element(:sub)
|
|
641
|
-
d.element(:sub2)
|
|
642
|
-
end
|
|
643
|
-
r.should == EimXML::DSL.element(:base) do
|
|
644
|
-
element(:sub)
|
|
645
|
-
element(:sub2)
|
|
646
|
-
end
|
|
647
|
-
end
|
|
648
|
-
end
|