xmlcodec 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,10 +1,11 @@
1
1
  PKG_NAME = 'xmlcodec'
2
- PKG_VERSION = '0.0.5'
2
+ PKG_VERSION = '0.1.0'
3
3
 
4
4
  require 'rake'
5
5
  require 'rake/testtask'
6
6
  require 'rake/rdoctask'
7
7
  require 'rake/gempackagetask'
8
+ require 'rcov/rcovtask'
8
9
  require 'rubygems'
9
10
 
10
11
  task :default => ['test']
@@ -46,10 +47,7 @@ Rake::GemPackageTask.new(spec) do |pkg|
46
47
  end
47
48
 
48
49
  Rake::TestTask.new do |t|
49
- t.libs << "test"
50
- t.test_files = FileList['test/Test*.rb']
51
- t.libs = ['../lib','..']
52
- t.ruby_opts = ['-xtest']
50
+ t.test_files = FileList['test/*_test.rb']
53
51
  t.verbose = true
54
52
  end
55
53
 
@@ -74,6 +72,13 @@ task :stats do
74
72
  puts "Code to test ratio: 1:%.2f" % ratio
75
73
  end
76
74
 
75
+ Rcov::RcovTask.new do |t|
76
+ t.libs << "test"
77
+ t.test_files = FileList['test/*_test.rb']
78
+ t.output_dir = 'test/coverage'
79
+ t.verbose = true
80
+ end
81
+
77
82
  def count_lines(files)
78
83
  code = 0
79
84
  comments = 0
@@ -1,7 +1,7 @@
1
- require 'XMLStreamObjectParser'
2
- require 'XMLSubElements'
3
-
4
1
  module XMLCodec
2
+ class ElementClassNotFound < RuntimeError
3
+ end
4
+
5
5
  # This class should be inherited from to create classes that are able to
6
6
  # import and export XML elements and their children. It provides three main
7
7
  # functions: xmlattr, xmlsubel and xmlsubel_mult.
@@ -61,8 +61,7 @@ module XMLCodec
61
61
  attr_reader name
62
62
  define_method((name.to_s+"=").to_sym) { |value|
63
63
  if value.is_a? String or value.is_a? Fixnum
64
- elclass = XMLCodec::XMLElement.get_element_class(name)
65
- value = elclass.new(value)
64
+ value = self.class.get_element_class(name).new(value)
66
65
  end
67
66
  value.__parent = self if value
68
67
  instance_variable_set "@#{name}", value
@@ -182,6 +181,18 @@ module XMLCodec
182
181
  end
183
182
  @subelements
184
183
  }
184
+ define_method('<<') {|value|
185
+ subelements << value
186
+ }
187
+ define_method(:find_first_named) {|name|
188
+ subelements.find_first_named(name)
189
+ }
190
+ define_method('[]') {|name|
191
+ subelements.find_first_named(name)
192
+ }
193
+ define_method(:find_all_named) {|name|
194
+ subelements.find_all_named(name)
195
+ }
185
196
  define_method(:has_subelements?) {true}
186
197
  end
187
198
 
@@ -191,8 +202,15 @@ module XMLCodec
191
202
  attr_accessor name
192
203
  end
193
204
 
194
- # This is the hash that gives classes for the element names that are declared.
195
- ElClasses = {}
205
+ # Defines a new xml format (like XHTML or DocBook). This should be used in
206
+ # a class that's the super class of all the elements of a format
207
+ def self.xmlformat(name=nil)
208
+ class_variable_set('@@elclasses', {})
209
+ end
210
+
211
+ def self.elclasses
212
+ class_variable_get('@@elclasses')
213
+ end
196
214
 
197
215
  # Sets the element name for the element
198
216
  def self.elname(name)
@@ -202,10 +220,10 @@ module XMLCodec
202
220
  # Sets several element names for the element
203
221
  def self.elnames(*names)
204
222
  define_method(:elname){names[0].to_sym}
205
-
223
+
206
224
  eln = get_elnames
207
225
  names.each {|n| eln << n}
208
- names.each {|n| ElClasses[n.to_sym] = self}
226
+ names.each {|n| elclasses[n.to_sym] = self}
209
227
  end
210
228
 
211
229
  # Returns the list of element names for the element
@@ -218,7 +236,11 @@ module XMLCodec
218
236
  # be used for elements that contain only text and no subelements
219
237
  def self.elwithvalue
220
238
  define_method(:hasvalue?){true}
221
- define_method(:initialize){|value| @value = value}
239
+ self.class_eval do
240
+ def initialize(value=nil)
241
+ @value = value
242
+ end
243
+ end
222
244
  attr_accessor :value
223
245
  end
224
246
 
@@ -247,17 +269,20 @@ module XMLCodec
247
269
  end
248
270
 
249
271
  # Which level of indentation are we in?
250
- def indent_level
251
- if not self.instance_variables.index '@indent_level'
252
- curr = self
253
- level = 0
254
- while curr = curr.__parent
255
- level +=1
256
- end
257
- @indent_level = level
258
- end
259
- @indent_level
260
- end
272
+ #
273
+ # This is currently disabled until I get around to implementing proper and
274
+ # tested indent support.
275
+ #def indent_level
276
+ # if not self.instance_variables.index '@indent_level'
277
+ # curr = self
278
+ # level = 0
279
+ # while curr = curr.__parent
280
+ # level +=1
281
+ # end
282
+ # @indent_level = level
283
+ # end
284
+ # @indent_level
285
+ #end
261
286
 
262
287
  # Iterate all of the subelements
263
288
  # We copy everything into an array and iterate that because #each doesn't
@@ -303,23 +328,25 @@ module XMLCodec
303
328
  # with the whitespace that should precede every line. Extra levels of
304
329
  # indentation can be passed so that a caller can calculate the whitespace
305
330
  # to indent an element X levels deeper than this one.
306
- def indentation(extra=0)
307
- INDENT_STR*(indent_level+extra)
308
- end
331
+ #
332
+ # This is currently disabled until I get around to implementing proper and
333
+ # tested indent support.
334
+ #def indentation(extra=0)
335
+ # INDENT_STR*(indent_level+extra)
336
+ #end
309
337
 
310
338
  # Gets the class for a certain element name.
311
339
  def self.get_element_class(name)
312
- cl = ElClasses[name.to_sym]
340
+ cl = elclasses[name.to_sym]
313
341
  if not cl
314
- raise "No class defined for element type: '" + name.to_s + "'"
342
+ raise ElementClassNotFound, "No class defined for element type: '" + name.to_s + "'"
315
343
  end
316
344
  cl
317
345
  end
318
346
 
319
347
  # Gets the possible element names for a certain element.
320
348
  def self.get_element_names(name)
321
- cl = get_element_class(name)
322
- [name.to_s]+cl.get_elnames
349
+ get_element_class(name).get_elnames
323
350
  end
324
351
 
325
352
  # Method that checks if a given class has subelements. This is usually only
@@ -378,7 +405,7 @@ module XMLCodec
378
405
  # Import the XML directly from the text. This call receives the text and the
379
406
  # classes that should be used to import the subelements.
380
407
  def self.import_xml_text(text)
381
- parser = XMLStreamObjectParser.new(ElClasses)
408
+ parser = XMLStreamObjectParser.new(self)
382
409
  parser.parse(text)
383
410
  parser.top_element
384
411
  end
@@ -1,5 +1,4 @@
1
1
  require "rexml/document"
2
- require "XMLUtils"
3
2
 
4
3
  module XMLCodec
5
4
  # This class is used internally by the parser to store the information about
@@ -62,7 +61,8 @@ module XMLCodec
62
61
  # XMLSOParserElement#consume.
63
62
  class XMLStreamObjectParser
64
63
  # Create a new parser with a listener.
65
- def initialize(listener=nil)
64
+ def initialize(base_element, listener=nil)
65
+ @base_element = base_element
66
66
  @listener = listener
67
67
  @children = Hash.new([])
68
68
  @currel = 0
@@ -77,7 +77,7 @@ module XMLCodec
77
77
  end
78
78
 
79
79
  def get_elclass(name)
80
- XMLCodec::XMLElement.get_element_class(name)
80
+ @base_element.get_element_class(name)
81
81
  end
82
82
 
83
83
  def curr_element
@@ -1,5 +1,4 @@
1
1
  require "rexml/document"
2
- require "XMLUtils"
3
2
 
4
3
  module XMLUtils
5
4
  class XMLSParserContents
@@ -15,10 +14,6 @@ module XMLUtils
15
14
  add(content)
16
15
  end
17
16
 
18
- def array_from(start)
19
- @contents[start..-1]
20
- end
21
-
22
17
  def text_from(start)
23
18
  @contents[start..-1].join
24
19
  end
@@ -1,5 +1,3 @@
1
- require 'XMLUtils'
2
-
3
1
  module XMLCodec
4
2
  # A simple element that has only text inside. This is used inside
5
3
  # XMLSubElements to be able to directly add strings to it and have them be
@@ -38,6 +36,10 @@ module XMLCodec
38
36
  start_partial_export(file)
39
37
  end_partial_export(file)
40
38
  end
39
+
40
+ def elname
41
+ '__TEXT__'
42
+ end
41
43
  end
42
44
 
43
45
  # This is the container class used to hold the elements for the xmlsubelements
@@ -51,13 +53,17 @@ module XMLCodec
51
53
  @parent = parent
52
54
  end
53
55
 
54
- private
55
- # Get the class for a given element name
56
- def elclass(name)
57
- XMLElement.get_element_class(name)
56
+ public
57
+ # Returns the first element in the collection with the given element name
58
+ def find_first_named(name)
59
+ self.find {|el| el.elname.to_s == name.to_s}
60
+ end
61
+
62
+ # Returns the elements in the collection with the given element name
63
+ def find_all_named(name)
64
+ self.find_all {|el| el.elname.to_s == name.to_s}
58
65
  end
59
66
 
60
- public
61
67
  # Adds a value to the collection. The value may be either a String or a
62
68
  # descendant of XMLElement. If it's a string it's converted into a
63
69
  # XMLTextElement
@@ -84,17 +90,6 @@ module XMLCodec
84
90
  @elements.each{|e| e.create_xml(parent)}
85
91
  end
86
92
 
87
- # Import the XML of all the elements
88
- def import_xml(xmlelements)
89
- xmlelements.each do |xmlel|
90
- if xmlel.kind_of? REXML::Text
91
- self << xmlel.to_s
92
- else
93
- self << elclass(xmlel.name.to_sym).import_xml(xmlel)
94
- end
95
- end
96
- end
97
-
98
93
  # Iterate all the values in the collection
99
94
  def each
100
95
  @elements.dup.each {|e| yield e}
data/lib/xmlcodec.rb CHANGED
@@ -1,4 +1,5 @@
1
- require 'XMLElement'
2
- require 'XMLUtils'
3
- require 'XMLStreamObjectParser'
4
- require 'XMLStreamParser'
1
+ require File.dirname(__FILE__) + '/XMLUtils'
2
+ require File.dirname(__FILE__) + '/element'
3
+ require File.dirname(__FILE__) + '/subelements'
4
+ require File.dirname(__FILE__) + '/stream_object_parser'
5
+ require File.dirname(__FILE__) + '/stream_parser'
@@ -1,7 +1,5 @@
1
- require 'test/unit'
2
- require 'xmlcodec'
1
+ require File.dirname(__FILE__) + '/test_helper'
3
2
  require 'rexml/document'
4
- require 'simple_objects'
5
3
 
6
4
  class TestXMLElement < Test::Unit::TestCase
7
5
  def test_xmlsubel
@@ -83,6 +81,50 @@ class TestXMLElement < Test::Unit::TestCase
83
81
  assert_equal value, el.subelements[0].value
84
82
  end
85
83
 
84
+ # Test using "somelement << addsomething" instead of
85
+ # "somelement.subelements << addsomething"
86
+ def test_xmlsubelements_direct
87
+ value = 'test'
88
+ el = SubelElement.new
89
+ sel = SimpleElement.new(value)
90
+
91
+ el << sel
92
+ assert_equal(sel, el.subelements[0])
93
+ end
94
+
95
+ def test_find_first
96
+ value = 'test'
97
+ el = SubelElement.new
98
+ sel = SimpleElement.new(value)
99
+
100
+ el << sel
101
+ assert_equal(sel, el.find_first_named('abc'))
102
+ assert_equal(sel, el['abc'])
103
+ end
104
+
105
+ def test_find_all
106
+ value = 'test'
107
+ el = SubelElement.new
108
+ sel = SimpleElement.new(value)
109
+ sel2 = SimpleElement.new(value)
110
+
111
+ el << sel
112
+ el << sel2
113
+ assert_equal([sel,sel2], el.find_all_named('abc'))
114
+ end
115
+
116
+ def test_find_all_with_texts
117
+ value = 'test'
118
+ el = SubelElement.new
119
+ sel = SimpleElement.new(value)
120
+ sel2 = SimpleElement.new(value)
121
+
122
+ el << sel
123
+ el << 'some text'
124
+ el << sel2
125
+ assert_equal([sel,sel2], el.find_all_named('abc'))
126
+ end
127
+
86
128
  def test_xmlsubel_mult
87
129
  value1 = 'test'
88
130
  value2 = 'test2'
@@ -174,4 +216,35 @@ class TestXMLElement < Test::Unit::TestCase
174
216
  assert_equal 0, el.subelements.size
175
217
  assert_nil el.subelements[0]
176
218
  end
219
+
220
+ def test_elwithvalue_methods
221
+ v = 'Some Value'
222
+
223
+ el = ValueElement.new
224
+ el.value = v
225
+ assert_equal v, el.value
226
+
227
+ el = ValueElement.new(v)
228
+ assert_equal v, el.value
229
+ end
230
+
231
+ def test_auto_conversion_from_string
232
+ v = 'Some Value'
233
+ vparent = ValueParent.new
234
+ vparent.valueelement = v
235
+ assert_kind_of ValueElement, vparent.valueelement
236
+ assert_equal vparent.valueelement.value, v
237
+ end
238
+
239
+ def test_get_element_class
240
+ assert_equal SimpleElement, BaseFormat.get_element_class('abc')
241
+ assert_equal SimpleElement, BaseFormat.get_element_class(:abc)
242
+ assert_raise ElementClassNotFound do
243
+ BaseFormat.get_element_class(:nosuchclass)
244
+ end
245
+ end
246
+
247
+ def test_get_element_names
248
+ assert_equal ['abc'], BaseFormat.get_element_names('abc')
249
+ end
177
250
  end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'rexml/document'
3
+
4
+ class FirstFormat < XMLCodec::XMLElement
5
+ xmlformat 'First Format'
6
+ end
7
+
8
+ class FirstRoot < FirstFormat
9
+ elname "root"
10
+ xmlsubel :child
11
+ end
12
+
13
+ class FirstChild < FirstFormat
14
+ elname "child"
15
+ xmlattr :value
16
+ end
17
+
18
+ class SecondFormat < XMLCodec::XMLElement
19
+ xmlformat 'Second Format'
20
+ end
21
+
22
+ class SecondRoot < SecondFormat
23
+ elname "root"
24
+ xmlsubel :child
25
+ xmlsubel :child2
26
+ end
27
+
28
+ class SecondChild < SecondFormat
29
+ elname "child"
30
+ elwithvalue
31
+ end
32
+
33
+ class SecondChild2 < SecondChild
34
+ elname "child2"
35
+ xmlattr :value2
36
+ end
37
+
38
+ # Test what happens when we define two different XML formats with xmlcodec that
39
+ # have clashing element names
40
+ class TestMultiFormat < Test::Unit::TestCase
41
+ def test_import_first
42
+ value = 'somevalue'
43
+ text = '<root><child value="'+value+'"></child></root>'
44
+ root = FirstFormat.import_xml_text(text)
45
+ assert_kind_of(FirstRoot, root)
46
+ assert_kind_of(FirstChild, root.child)
47
+ assert_equal value, root.child.value
48
+ end
49
+
50
+ def test_import_second
51
+ value = 'somevalue'
52
+ text = '<root><child>'+value+'</child></root>'
53
+ root = SecondFormat.import_xml_text(text)
54
+ assert_kind_of(SecondRoot, root)
55
+ assert_kind_of(SecondChild, root.child)
56
+ assert_equal value, root.child.value
57
+ end
58
+
59
+ def test_double_inheritance
60
+ value = 'somevalue'
61
+ text = '<root><child2 value2="'+value+'">'+value+'</child2></root>'
62
+ root = SecondFormat.import_xml_text(text)
63
+ assert_kind_of(SecondRoot, root)
64
+ assert_kind_of(SecondChild2, root.child2)
65
+ assert_equal value, root.child2.value
66
+ assert_equal value, root.child2.value2
67
+ end
68
+ end
@@ -1,6 +1,4 @@
1
- require 'test/unit'
2
- require 'XMLUtils'
3
- require 'simple_objects'
1
+ require File.dirname(__FILE__) + '/test_helper'
4
2
 
5
3
  class TestPartialExport < Test::Unit::TestCase
6
4
  def validate_well_formed(filename)
@@ -12,23 +10,23 @@ class TestPartialExport < Test::Unit::TestCase
12
10
  end
13
11
 
14
12
  def test_simple
13
+ @filename = 'test_partial_export_simple.xml'
15
14
  value = "somevalue"
16
- filename = 'test_partial_export_simple.xml'
17
15
 
18
- file = File.open(filename, "w")
16
+ file = File.open(filepath, "w")
19
17
  sel = SimpleElement.new(value)
20
18
  sel.partial_export(file)
21
19
  file.close
22
20
 
23
- validate_well_formed(filename)
24
- compare_xpath(value, filename, "/abc")
21
+ validate_well_formed(filepath)
22
+ compare_xpath(value, filepath, "/abc")
25
23
  end
26
24
 
27
25
  def test_double
28
26
  value = 'somevalue'
29
- filename = 'test_partial_export_double.xml'
27
+ @filename = 'test_partial_export_double.xml'
30
28
 
31
- file = File.open(filename, "w")
29
+ file = File.open(filepath, "w")
32
30
  sel = SimpleElement.new(value)
33
31
  el = TestElement.new
34
32
  el.abc = sel
@@ -37,15 +35,15 @@ class TestPartialExport < Test::Unit::TestCase
37
35
  el.end_partial_export(file)
38
36
  file.close
39
37
 
40
- validate_well_formed(filename)
41
- compare_xpath(value, filename, "/subel/abc")
38
+ validate_well_formed(filepath)
39
+ compare_xpath(value, filepath, "/subel/abc")
42
40
  end
43
41
 
44
42
  def test_triple
45
43
  value = 'somevalue'
46
- filename = 'test_partial_export_double.xml'
44
+ @filename = 'test_partial_export_double.xml'
47
45
 
48
- file = File.open(filename, "w")
46
+ file = File.open(filepath, "w")
49
47
  sel = SimpleElement.new(value)
50
48
  el1 = TestElement.new
51
49
  el2 = TestElement.new
@@ -56,30 +54,30 @@ class TestPartialExport < Test::Unit::TestCase
56
54
  el1.end_partial_export(file)
57
55
  file.close
58
56
 
59
- validate_well_formed(filename)
60
- compare_xpath(value, filename, "/subel/subel/abc")
57
+ validate_well_formed(filepath)
58
+ compare_xpath(value, filepath, "/subel/subel/abc")
61
59
  end
62
60
 
63
61
  def test_attr
64
62
  value = 'somevalue'
65
- filename = 'test_partial_export_double.xml'
63
+ @filename = 'test_partial_export_double.xml'
66
64
 
67
- file = File.open(filename, "w")
65
+ file = File.open(filepath, "w")
68
66
  el = TestElement.new
69
67
  el.someattr = value
70
68
 
71
69
  el.partial_export(file)
72
70
  file.close
73
71
 
74
- compare_xpath(value, filename, "/subel/@someattr")
72
+ compare_xpath(value, filepath, "/subel/@someattr")
75
73
  end
76
74
 
77
75
  def test_mult
78
76
  value1 = 'somevalue1'
79
77
  value2 = 'somevalue2'
80
- filename = 'test_partial_export_mult.xml'
78
+ @filename = 'test_partial_export_mult.xml'
81
79
 
82
- file = File.open(filename, "w")
80
+ file = File.open(filepath, "w")
83
81
  sel1 = SimpleElement.new(value1)
84
82
  sel2 = SimpleElement.new(value2)
85
83
 
@@ -93,17 +91,17 @@ class TestPartialExport < Test::Unit::TestCase
93
91
 
94
92
  file.close
95
93
 
96
- validate_well_formed(filename)
97
- compare_xpath(value1, filename, "/mult/abc[1]")
98
- compare_xpath(value2, filename, "/mult/abc[2]")
94
+ validate_well_formed(filepath)
95
+ compare_xpath(value1, filepath, "/mult/abc[1]")
96
+ compare_xpath(value2, filepath, "/mult/abc[2]")
99
97
  end
100
98
 
101
99
  def test_subelements
102
100
  value1 = 'somevalue1'
103
101
  value2 = 'somevalue2'
104
- filename = 'test_partial_export_subelements.xml'
102
+ @filename = 'test_partial_export_subelements.xml'
105
103
 
106
- file = File.open(filename, "w")
104
+ file = File.open(filepath, "w")
107
105
  sel1 = SimpleElement.new(value1)
108
106
  sel2 = SimpleElement.new(value2)
109
107
 
@@ -117,17 +115,17 @@ class TestPartialExport < Test::Unit::TestCase
117
115
 
118
116
  file.close
119
117
 
120
- validate_well_formed(filename)
121
- compare_xpath(value1, filename, "/subels/abc[1]")
122
- compare_xpath(value2, filename, "/subels/abc[2]")
118
+ validate_well_formed(filepath)
119
+ compare_xpath(value1, filepath, "/subels/abc[1]")
120
+ compare_xpath(value2, filepath, "/subels/abc[2]")
123
121
  end
124
122
 
125
123
  def test_subelements_multiple
126
124
  value1 = 'somevalue1'
127
125
  value2 = 'somevalue2'
128
- filename = 'test_partial_export_subelements2.xml'
126
+ @filename = 'test_partial_export_subelements2.xml'
129
127
 
130
- file = File.open(filename, "w")
128
+ file = File.open(filepath, "w")
131
129
  sel1 = SimpleElement.new(value1)
132
130
  sel2 = SimpleElement.new(value2)
133
131
 
@@ -138,14 +136,14 @@ class TestPartialExport < Test::Unit::TestCase
138
136
 
139
137
  file.close
140
138
 
141
- validate_well_formed(filename)
142
- compare_xpath(value1, filename, "/subels/abc[1]")
143
- compare_xpath(value2, filename, "/subels/abc[2]")
139
+ validate_well_formed(filepath)
140
+ compare_xpath(value1, filepath, "/subels/abc[1]")
141
+ compare_xpath(value2, filepath, "/subels/abc[2]")
144
142
  end
145
143
 
146
144
  def test_recursive
147
- filename = 'test_partial_export_recursive.xml'
148
- file = File.open(filename, "w")
145
+ @filename = 'test_partial_export_recursive.xml'
146
+ file = File.open(filepath, "w")
149
147
 
150
148
  value = 'somevalue'
151
149
 
@@ -167,6 +165,6 @@ class TestPartialExport < Test::Unit::TestCase
167
165
  rec1.end_partial_export(file)
168
166
 
169
167
  file.close
170
- validate_well_formed(filename)
168
+ validate_well_formed(filepath)
171
169
  end
172
170
  end
@@ -1,17 +1,20 @@
1
- require 'XMLElement'
2
1
  include XMLCodec
3
2
 
4
- class SimpleElement < XMLElement
3
+ class BaseFormat < XMLElement
4
+ xmlformat 'Base Format'
5
+ end
6
+
7
+ class SimpleElement < BaseFormat
5
8
  elwithvalue
6
9
  elname 'abc'
7
10
  end
8
11
 
9
- class SimpleElement2 < XMLElement
12
+ class SimpleElement2 < BaseFormat
10
13
  elwithvalue
11
14
  elname 'abc1'
12
15
  end
13
16
 
14
- class SimpleElementMultName < XMLElement
17
+ class SimpleElementMultName < BaseFormat
15
18
  elwithvalue
16
19
  elnames 'abc2', 'abc3'
17
20
 
@@ -27,7 +30,7 @@ class SimpleElementMultName < XMLElement
27
30
  end
28
31
  end
29
32
 
30
- class TestElement < XMLElement
33
+ class TestElement < BaseFormat
31
34
  elname 'subel'
32
35
  xmlsubel :abc
33
36
  xmlsubel :abc2
@@ -35,19 +38,29 @@ class TestElement < XMLElement
35
38
  xmlattr :someattr
36
39
  end
37
40
 
38
- class SubelElement < XMLElement
41
+ class SubelElement < BaseFormat
39
42
  elname 'subels'
40
43
  xmlsubelements
41
44
  end
42
45
 
43
- class SubelMultElement < XMLElement
46
+ class SubelMultElement < BaseFormat
44
47
  elname 'mult'
45
48
  xmlsubel_mult :abc
46
49
  xmlsubel_mult 'abc2' # Use a string and a symbol so that both are checked
47
50
  end
48
51
 
49
- class Recursive < XMLElement
52
+ class Recursive < BaseFormat
50
53
  elname 'recursive'
51
54
  xmlsubel :abc
52
55
  xmlsubel_mult :recursive
53
56
  end
57
+
58
+ class ValueParent < BaseFormat
59
+ elname 'valueparent'
60
+ xmlsubel 'valueelement'
61
+ end
62
+
63
+ class ValueElement < BaseFormat
64
+ elwithvalue
65
+ elname 'valueelement'
66
+ end
@@ -1,6 +1,4 @@
1
- require "test/unit"
2
- require "XMLStreamObjectParser"
3
- require 'simple_objects.rb'
1
+ require File.dirname(__FILE__) + '/test_helper'
4
2
 
5
3
  include XMLCodec
6
4
 
@@ -44,7 +42,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
44
42
  file = '<abc>'+value+'</abc>'
45
43
 
46
44
  listener = MyStreamListener.new
47
- parser = XMLStreamObjectParser.new(listener)
45
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
48
46
  parser.parse(file)
49
47
  el = listener.abc.get_object
50
48
  assert_equal el.value, value
@@ -56,7 +54,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
56
54
  file = '<subel someattr="'+attrvalue+'"><abc>'+value+'</abc></subel>'
57
55
 
58
56
  listener = MyStreamListener.new
59
- parser = XMLStreamObjectParser.new(listener)
57
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
60
58
  parser.parse(file)
61
59
 
62
60
  el = listener.abc.get_object
@@ -72,7 +70,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
72
70
  file = '<mult><abc>'+value+'</abc></mult>'
73
71
 
74
72
  listener = MyStreamListener.new
75
- parser = XMLStreamObjectParser.new(listener)
73
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
76
74
  parser.parse(file)
77
75
 
78
76
  el = listener.abc.get_object
@@ -85,7 +83,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
85
83
  file = '<subels><abc>'+value+'</abc></subels>'
86
84
 
87
85
  listener = MyStreamListener.new
88
- parser = XMLStreamObjectParser.new(listener)
86
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
89
87
  parser.parse(file)
90
88
 
91
89
  el = listener.abc.get_object
@@ -98,7 +96,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
98
96
  file = '<subel><abc>'+value+'</abc></subel>'
99
97
 
100
98
  listener = MyConsumingStreamListener.new
101
- parser = XMLStreamObjectParser.new(listener)
99
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
102
100
  parser.parse(file)
103
101
 
104
102
  el = listener.abc.get_object
@@ -112,7 +110,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
112
110
  file = '<subels><abc2>'+value1+'</abc2><abc3>'+value2+'</abc3></subels>'
113
111
 
114
112
  listener = MyStreamListener.new
115
- parser = XMLStreamObjectParser.new(listener)
113
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
116
114
  parser.parse(file)
117
115
 
118
116
  el = listener.subels.get_object
@@ -129,7 +127,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
129
127
  file = '<mult><abc2>'+value1+'</abc2><abc3>'+value2+'</abc3></mult>'
130
128
 
131
129
  listener = MyStreamListener.new
132
- parser = XMLStreamObjectParser.new(listener)
130
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
133
131
  parser.parse(file)
134
132
 
135
133
  el = listener.mult.get_object
@@ -145,7 +143,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
145
143
  file = '<subel><abc3>'+value+'</abc3></subel>'
146
144
 
147
145
  listener = MyStreamListener.new
148
- parser = XMLStreamObjectParser.new(listener)
146
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
149
147
  parser.parse(file)
150
148
 
151
149
  el = listener.subel.get_object
@@ -159,7 +157,7 @@ class TestXMLStreamObjectParser < Test::Unit::TestCase
159
157
  file = '<subel><abc>'+value+'</abc></subel>'
160
158
 
161
159
  listener = MyStreamListener.new
162
- parser = XMLStreamObjectParser.new(listener)
160
+ parser = XMLStreamObjectParser.new(BaseFormat, listener)
163
161
  parser.parse(file)
164
162
 
165
163
  el1 = listener.subel
@@ -1,12 +1,9 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..")
2
- $-w = true
3
-
4
- require 'test/unit'
5
- require 'XMLStreamParser'
1
+ require File.dirname(__FILE__) + '/test_helper'
6
2
 
7
3
  class MyXMLStreamListener
8
4
  attr_reader :def_id, :def_parent_id
9
5
  attr_reader :abc_id, :abc_parent_id
6
+ attr_reader :abc_content
10
7
 
11
8
  def el_def(el)
12
9
  @def_id, = el.element_id
@@ -17,6 +14,7 @@ class MyXMLStreamListener
17
14
  def el_abc(el)
18
15
  @abc_id, = el.element_id
19
16
  @abc_parent_id = el.parent_id
17
+ @abc_content = el.content
20
18
  el.consume
21
19
  end
22
20
  end
@@ -42,6 +40,7 @@ class TestXMLStreamParser < Test::Unit::TestCase
42
40
  assert_equal(listener.abc_id, listener.def_parent_id)
43
41
  assert_equal(0, listener.abc_parent_id)
44
42
  assert_equal(1, listener.abc_id)
43
+ assert_equal(@doc_no_def, listener.abc_content)
45
44
  assert(listener.def_id > listener.def_parent_id)
46
45
  end
47
46
 
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'stringio'
3
+ require 'rexml/document'
4
+ class TestXMLTextElement < Test::Unit::TestCase
5
+ def test_base
6
+ value = 'Some Value'
7
+ el = XMLTextElement.new value
8
+ assert_equal value, el.to_s
9
+ assert_equal value, el.xml_text
10
+ end
11
+
12
+ def test_partial_export
13
+ io = StringIO.new
14
+ value = 'Some Value'
15
+ el = XMLTextElement.new value
16
+ el.partial_export(io)
17
+ io.pos = 0
18
+ assert_equal value, io.read
19
+ end
20
+
21
+ def test_elname
22
+ el = XMLTextElement.new 'abc'
23
+ assert_equal '__TEXT__', el.elname
24
+ end
25
+
26
+ def test_create_xml
27
+ xmlel = REXML::Element.new 'abc'
28
+ value = 'Some Value'
29
+ el = XMLTextElement.new value
30
+ el.create_xml(xmlel)
31
+ assert_equal '<abc>'+value+'</abc>', xmlel.to_s
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+ require File.dirname(__FILE__) + '/../lib/xmlcodec'
4
+ require File.dirname(__FILE__) + '/simple_objects'
5
+
6
+
7
+ class Test::Unit::TestCase
8
+ def filepath
9
+ File.join(Dir::tmpdir, @filename)
10
+ end
11
+ end
@@ -1,7 +1,5 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..")
2
-
3
- require 'test/unit'
4
- require 'XMLUtils'
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require 'tmpdir'
5
3
 
6
4
  class TestXMLUtils < Test::Unit::TestCase
7
5
  def test_create_open_tag
@@ -48,4 +46,22 @@ class TestXMLUtils < Test::Unit::TestCase
48
46
  assert_equal result, XMLUtils::get_xpath(path, StringIO.new(xml), opts)
49
47
  assert_equal result, XMLUtils::get_xpath(path, REXML::Document.new(xml), opts)
50
48
  end
49
+
50
+ def test_count_elements
51
+ text = '<root><abc/><abc/></root>'
52
+ filename = File.join(Dir::tmpdir, 'test_count_elements.xml')
53
+ f = File.open(filename, 'w')
54
+ f << text
55
+ f.close
56
+ assert_equal 2, XMLUtils::count_elements('//abc', filename)
57
+ end
58
+
59
+ def test_element_exists
60
+ text = '<root><abc/><abc/></root>'
61
+ filename = File.join(Dir::tmpdir, 'test_element_exists.xml')
62
+ f = File.open(filename, 'w')
63
+ f << text
64
+ f.close
65
+ assert XMLUtils::element_exists('//abc', filename)
66
+ end
51
67
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: xmlcodec
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.5
7
- date: 2006-07-20 00:00:00 +01:00
6
+ version: 0.1.0
7
+ date: 2006-07-27 00:00:00 +01:00
8
8
  summary: Generic Importer/Exporter of XML formats
9
9
  require_paths:
10
10
  - lib
@@ -29,17 +29,20 @@ post_install_message:
29
29
  authors:
30
30
  - "Pedro C\xC3\xB4rte-Real"
31
31
  files:
32
- - test/TestXMLStreamParser.rb
33
- - test/TestXMLElement.rb
34
- - test/TestPartialExport.rb
35
- - test/TestXMLUtils.rb
36
- - test/TestXMLStreamObjectParser.rb
32
+ - test/multi_format_test.rb
33
+ - test/utils_test.rb
34
+ - test/stream_parser_test.rb
35
+ - test/element_test.rb
36
+ - test/partial_export_test.rb
37
37
  - test/simple_objects.rb
38
- - lib/XMLStreamParser.rb
39
- - lib/XMLElement.rb
40
- - lib/XMLStreamObjectParser.rb
41
- - lib/XMLSubElements.rb
38
+ - test/stream_object_parser_test.rb
39
+ - test/test_helper.rb
40
+ - test/subelements_test.rb
41
+ - lib/stream_parser.rb
42
42
  - lib/XMLUtils.rb
43
+ - lib/subelements.rb
44
+ - lib/element.rb
45
+ - lib/stream_object_parser.rb
43
46
  - lib/xmlcodec.rb
44
47
  - README
45
48
  - LICENSE