sdl4r 0.9.9 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/CHANGELOG +70 -1
  2. data/LICENSE +499 -497
  3. data/Rakefile +38 -28
  4. data/TODO +194 -45
  5. data/doc/classes/SDL4R/AbbreviationTimezoneProxy.html +151 -0
  6. data/doc/classes/SDL4R/ConstantTimezone.html +129 -0
  7. data/doc/classes/SDL4R/Element.html +148 -0
  8. data/doc/classes/SDL4R/Reader.html +683 -0
  9. data/doc/classes/SDL4R/RelativeTimezone.html +187 -0
  10. data/doc/classes/SDL4R/SdlBinary.html +188 -0
  11. data/doc/classes/SDL4R/SdlParseError.html +110 -0
  12. data/doc/classes/SDL4R/SdlTimeSpan.html +651 -0
  13. data/doc/classes/SDL4R/Serializer.html +557 -0
  14. data/doc/classes/SDL4R/Serializer/Ref.html +138 -0
  15. data/doc/classes/SDL4R/TZAbbreviationDB/Record.html +117 -0
  16. data/doc/classes/SDL4R/Tag.html +1274 -0
  17. data/doc/classes/SDL4R/Token.html +131 -0
  18. data/doc/created.rid +1 -0
  19. data/doc/files/CHANGELOG.html +290 -0
  20. data/doc/files/LICENSE.html +53 -0
  21. data/doc/files/README.html +405 -0
  22. data/doc/files/lib/sdl4r/abbreviation_timezone_proxy_rb.html +63 -0
  23. data/doc/files/lib/sdl4r/constant_timezone_rb.html +54 -0
  24. data/doc/files/lib/sdl4r/element_rb.html +54 -0
  25. data/doc/files/lib/sdl4r/reader_rb.html +68 -0
  26. data/doc/files/lib/sdl4r/relative_timezone_rb.html +62 -0
  27. data/doc/files/lib/sdl4r/sdl4r_rb.html +66 -0
  28. data/doc/files/lib/sdl4r/sdl4r_tzinfo_rb.html +64 -0
  29. data/doc/files/lib/sdl4r/sdl4r_version_rb.html +54 -0
  30. data/doc/files/lib/sdl4r/sdl_binary_rb.html +54 -0
  31. data/doc/files/lib/sdl4r/sdl_parse_error_rb.html +54 -0
  32. data/doc/files/lib/sdl4r/sdl_time_span_rb.html +54 -0
  33. data/doc/files/lib/sdl4r/serializer_rb.html +62 -0
  34. data/doc/files/lib/sdl4r/tag_rb.html +66 -0
  35. data/doc/files/lib/sdl4r/token_rb.html +54 -0
  36. data/doc/files/lib/sdl4r/tokenizer_rb.html +64 -0
  37. data/doc/files/lib/sdl4r/tz_abbreviation_db_rb.html +67 -0
  38. data/doc/files/lib/sdl4r_rb.html +54 -0
  39. data/doc/files/lib/sdl4r_tzinfo_rb.html +54 -0
  40. data/doc/fr_class_index.html +23 -0
  41. data/doc/fr_file_index.html +40 -0
  42. data/doc/fr_method_index.html +4711 -0
  43. data/doc/index.html +15 -0
  44. data/doc/rdoc-style.css +328 -0
  45. data/lib/sdl4r.rb +3 -1
  46. data/lib/sdl4r/abbreviation_timezone_proxy.rb +38 -0
  47. data/lib/sdl4r/constant_timezone.rb +58 -0
  48. data/{test/sdl4r/sdl_test.rb → lib/sdl4r/element.rb} +19 -14
  49. data/lib/sdl4r/reader.rb +772 -0
  50. data/lib/sdl4r/relative_timezone.rb +156 -0
  51. data/lib/sdl4r/sdl4r.rb +187 -45
  52. data/lib/sdl4r/sdl4r_tzinfo.rb +75 -0
  53. data/lib/sdl4r/sdl4r_version.rb +24 -0
  54. data/lib/sdl4r/sdl_parse_error.rb +1 -1
  55. data/lib/sdl4r/sdl_time_span.rb +5 -1
  56. data/lib/sdl4r/serializer.rb +473 -60
  57. data/lib/sdl4r/tag.rb +126 -51
  58. data/lib/sdl4r/token.rb +49 -0
  59. data/lib/sdl4r/tokenizer.rb +431 -0
  60. data/lib/sdl4r/tz_abbreviation_db.rb +266 -0
  61. data/read_jprof.html +16934 -0
  62. data/read_jprof_pull.html +14451 -0
  63. data/read_prof.html +4983 -0
  64. data/read_prof_pull.html +4896 -0
  65. data/test/sdl4r/parser_test.rb +577 -503
  66. data/test/sdl4r/read_jprof.rb +58 -0
  67. data/test/sdl4r/read_prof.rb +70 -0
  68. data/test/sdl4r/reader_test.rb +173 -0
  69. data/test/sdl4r/relative_timezone_test.rb +102 -0
  70. data/test/sdl4r/sdl4r_test.rb +611 -528
  71. data/test/sdl4r/sdl4r_tzinfo_test.rb +108 -0
  72. data/test/sdl4r/sdl_test_case.rb +60 -0
  73. data/test/sdl4r/serializer_test.rb +448 -11
  74. data/test/sdl4r/tag_test.rb +84 -5
  75. data/test/sdl4r/tokenizer_test.rb +128 -0
  76. metadata +69 -11
  77. data/lib/sdl4r/parser.rb +0 -648
  78. data/lib/sdl4r/parser/reader.rb +0 -184
  79. data/lib/sdl4r/parser/time_span_with_zone.rb +0 -57
  80. data/lib/sdl4r/parser/token.rb +0 -138
  81. data/lib/sdl4r/parser/tokenizer.rb +0 -507
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ #
6
+ # Simple Declarative Language (SDL) for Ruby
7
+ # Copyright 2005 Ikayzo, inc.
8
+ #
9
+ # This program is free software. You can distribute or modify it under the
10
+ # terms of the GNU Lesser General Public License version 2.1 as published by
11
+ # the Free Software Foundation.
12
+ #
13
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
14
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
+ # See the GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
19
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
+ #++
21
+
22
+ # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653)
23
+ if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
24
+ $:[0] = File.join(File.dirname(__FILE__),'../../lib')
25
+ $:.unshift(File.join(File.dirname(__FILE__),'../../test'))
26
+ end
27
+
28
+ require 'rubygems'
29
+ require 'jruby-prof'
30
+
31
+ if RUBY_VERSION < '1.9.0'
32
+ $KCODE = 'u'
33
+ require 'jcode'
34
+ end
35
+
36
+ module SDL4R
37
+
38
+ require 'sdl4r'
39
+
40
+ # SDL4R::use_datetime = false
41
+
42
+ file1 = Pathname.new(File.dirname(__FILE__) + '/test_basic_types.sdl').read
43
+ file2 = Pathname.new(File.dirname(__FILE__) + '/test_structures.sdl').read
44
+
45
+ # Do all the loading once
46
+ SDL4R::read(file1)
47
+ SDL4R::read(file2)
48
+
49
+ result = JRubyProf.profile {
50
+ 100.times {
51
+ SDL4R::read(file1)
52
+ SDL4R::read(file2)
53
+ }
54
+ }
55
+
56
+ JRubyProf.print_graph_html(result, "read_jprof_pull.html")
57
+
58
+ end
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ #
6
+ # Simple Declarative Language (SDL) for Ruby
7
+ # Copyright 2005 Ikayzo, inc.
8
+ #
9
+ # This program is free software. You can distribute or modify it under the
10
+ # terms of the GNU Lesser General Public License version 2.1 as published by
11
+ # the Free Software Foundation.
12
+ #
13
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
14
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
+ # See the GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
19
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
+ #++
21
+
22
+ # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653)
23
+ if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
24
+ $:[0] = File.join(File.dirname(__FILE__),'../../lib')
25
+ $:.unshift(File.join(File.dirname(__FILE__),'../../test'))
26
+ end
27
+
28
+ require 'rubygems'
29
+ require 'ruby-prof'
30
+
31
+ if RubyProf.running?
32
+ RubyProf.stop
33
+ end
34
+ RubyProf.start
35
+ RubyProf.pause
36
+
37
+ if RUBY_VERSION < '1.9.0'
38
+ $KCODE = 'u'
39
+ require 'jcode'
40
+ end
41
+
42
+ module SDL4R
43
+
44
+ require 'sdl4r'
45
+
46
+ file1 = Pathname.new(File.dirname(__FILE__) + '/test_basic_types.sdl').read
47
+ file2 = Pathname.new(File.dirname(__FILE__) + '/test_structures.sdl').read
48
+
49
+ # Do all the loading once
50
+ SDL4R::read(file1)
51
+ SDL4R::read(file2)
52
+
53
+ puts "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
54
+ puts ""
55
+
56
+ RubyProf.resume {
57
+ 100.times {
58
+ SDL4R::read(file1)
59
+ SDL4R::read(file2)
60
+ }
61
+ }
62
+
63
+ result = RubyProf.stop
64
+
65
+ open("read_prof_pull.html", "w") do |io|
66
+ printer = RubyProf::GraphHtmlPrinter.new(result)
67
+ printer.print(io, :min_percent => 1)
68
+ end
69
+
70
+ end
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ # Simple Declarative Language (SDL) for Ruby
6
+ # Copyright 2005 Ikayzo, inc.
7
+ #
8
+ # This program is free software. You can distribute or modify it under the
9
+ # terms of the GNU Lesser General Public License version 2.1 as published by
10
+ # the Free Software Foundation.
11
+ #
12
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
13
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
14
+ # See the GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
18
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
+ #++
20
+
21
+ # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653)
22
+ if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
23
+ $:[0] = File.join(File.dirname(__FILE__),'../../../lib')
24
+ $:.unshift(File.join(File.dirname(__FILE__),'../../../test'))
25
+ end
26
+
27
+ if RUBY_VERSION < '1.9.0'
28
+ $KCODE = 'u'
29
+ require 'jcode'
30
+ end
31
+
32
+ module SDL4R
33
+ module Parser
34
+
35
+ require 'stringio'
36
+ require 'test/unit'
37
+
38
+ require 'sdl4r/sdl4r'
39
+ require 'sdl4r/reader'
40
+
41
+ require 'sdl4r/sdl_test_case'
42
+
43
+ class ReaderTest < Test::Unit::TestCase
44
+ include SdlTestCase
45
+
46
+ def read(sdl)
47
+ node_types = []
48
+ node_values = []
49
+ Reader.from_memory(sdl).each { |node|
50
+ node_types << node.node_type
51
+ node_values << node.value if node.node_type == Reader::TYPE_ELEMENT
52
+ }
53
+ return node_types, node_values
54
+ end
55
+ private :read
56
+
57
+ def test_read_empty
58
+ reader = Reader.from_memory("")
59
+ assert_nil reader.read
60
+ end
61
+
62
+ def test_read_values
63
+ types, values = read("tag1 `toto`")
64
+ assert_equal [Reader::TYPE_ELEMENT], types
65
+ assert_equal [["toto"]], values
66
+
67
+ types, values = read("tag1 \"toto\"")
68
+ assert_equal [Reader::TYPE_ELEMENT], types
69
+ assert_equal [["toto"]], values
70
+
71
+ types, values = read("tag1 \"toto\\t123\"")
72
+ assert_equal [Reader::TYPE_ELEMENT], types
73
+ assert_equal [["toto\t123"]], values
74
+
75
+ types, values = read(<<-EOS)
76
+ tag1 {
77
+ "text"
78
+ tag2 123
79
+ }
80
+ EOS
81
+ assert_equal(
82
+ [Reader::TYPE_ELEMENT, Reader::TYPE_ELEMENT, Reader::TYPE_ELEMENT, Reader::TYPE_END_ELEMENT],
83
+ types)
84
+ assert_equal [nil, ["text"], [123]], values
85
+ end
86
+
87
+ def test_accessors
88
+ reader = Reader.from_memory(<<-EOS)
89
+ tag1 "I've seen this before."
90
+ tag2 party="today" ever:work=false {
91
+ ns:tag3 "time is of the essence" attr1=-145.99
92
+ "Taratatata"
93
+ tag4
94
+ }
95
+ EOS
96
+
97
+ node = reader.read
98
+ assert_equal Reader::TYPE_ELEMENT, node.node_type
99
+ assert_equal "", node.prefix
100
+ assert_equal "tag1", node.name
101
+ assert_equal true, node.self_closing?
102
+ assert_equal ["I've seen this before."], node.value
103
+ assert_equal ["I've seen this before."], node.values
104
+ assert_equal true, node.value?
105
+ assert_equal true, node.values?
106
+ assert_equal false, node.attributes?
107
+ assert_equal 0, node.attribute_count
108
+ assert_equal [], node.attributes
109
+ assert_equal nil, node.attribute("something")
110
+
111
+ node = reader.read
112
+ assert_equal Reader::TYPE_ELEMENT, node.node_type
113
+ assert_equal "", node.prefix
114
+ assert_equal "tag2", node.name
115
+ assert_equal false, node.self_closing?
116
+ assert_equal nil, node.values
117
+ assert_equal false, node.values?
118
+ assert_equal true, node.attributes?
119
+ assert_equal 2, node.attribute_count
120
+ assert_equal [[["", "party"], "today"], [["ever", "work"], false]], node.attributes
121
+ assert_equal [["", "party"], "today"], node.attribute_at(0)
122
+ assert_equal [["ever", "work"], false], node.attribute_at(1)
123
+ assert_equal "today", node.attribute("party")
124
+ assert_equal false, node.attribute("ever", "work")
125
+ assert_equal nil, node.attribute("work")
126
+
127
+ node = reader.read
128
+ assert_equal Reader::TYPE_ELEMENT, node.node_type
129
+ assert_equal "ns", node.prefix
130
+ assert_equal "tag3", node.name
131
+ assert_equal true, node.self_closing?
132
+ assert_equal ["time is of the essence"], node.values
133
+ assert_equal true, node.values?
134
+ assert_equal true, node.attributes?
135
+ assert_equal 1, node.attribute_count
136
+ assert_equal [[["", "attr1"], -145.99]], node.attributes
137
+ assert_equal [["", "attr1"], -145.99], node.attribute_at(0)
138
+ assert_equal(-145.99, node.attribute("attr1"))
139
+ assert_equal(-145.99, node.attribute("", "attr1"))
140
+ assert_equal nil, node.attribute("kraken", "attr1")
141
+
142
+ node = reader.read
143
+ assert_equal Reader::TYPE_ELEMENT, node.node_type
144
+ assert_equal "", node.prefix
145
+ assert_equal SDL4R::ANONYMOUS_TAG_NAME, node.name
146
+ assert_equal true, node.self_closing?
147
+ assert_equal ["Taratatata"], node.value
148
+ assert_equal true, node.value?
149
+ assert_equal false, node.attributes?
150
+ assert_equal 0, node.attribute_count
151
+ assert_equal [], node.attributes
152
+
153
+ node = reader.read
154
+ assert_equal Reader::TYPE_ELEMENT, node.node_type
155
+ assert_equal "", node.prefix
156
+ assert_equal "tag4", node.name
157
+ assert_equal true, node.self_closing?
158
+ assert_equal nil, node.value
159
+ assert_equal false, node.value?
160
+ assert_equal false, node.attributes?
161
+ assert_equal 0, node.attribute_count
162
+ assert_equal [], node.attributes
163
+
164
+ node = reader.read
165
+ assert_equal Reader::TYPE_END_ELEMENT, node.node_type
166
+
167
+ assert_nil reader.read
168
+ assert_nil reader.read
169
+ end
170
+ end
171
+
172
+ end
173
+ end
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ #
6
+ # Simple Declarative Language (SDL) for Ruby
7
+ # Copyright 2005 Ikayzo, inc.
8
+ #
9
+ # This program is free software. You can distribute or modify it under the
10
+ # terms of the GNU Lesser General Public License version 2.1 as published by
11
+ # the Free Software Foundation.
12
+ #
13
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
14
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
+ # See the GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
19
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
+ #++
21
+
22
+ module SDL4R
23
+
24
+ require 'tzinfo'
25
+ require 'test/unit'
26
+
27
+ require 'sdl4r/relative_timezone'
28
+
29
+ require "sdl4r/sdl_test_case"
30
+
31
+ class RelativeTimezoneTest < Test::Unit::TestCase
32
+ include SdlTestCase
33
+
34
+ def test_get
35
+ TZInfo::Timezone.get("GMT")
36
+ tz = SDL4R::RelativeTimezone.get("GMT")
37
+
38
+ assert_equal "GMT", tz.identifier
39
+ assert !tz.is_a?(SDL4R::RelativeTimezone)
40
+
41
+ tz = SDL4R::RelativeTimezone.get("GMT+01:30")
42
+ assert_equal "GMT+01:30", tz.identifier
43
+ assert_kind_of SDL4R::RelativeTimezone, tz
44
+ assert_equal 5400, tz.relative_offset
45
+
46
+ tz_period = tz.period_for_utc(Time.utc(2001, 1, 25, 21, 45, 50, 123))
47
+ assert_equal 5400, tz_period.utc_offset
48
+ assert_equal 0, tz_period.std_offset
49
+ assert_equal :"GMT+01:30", tz_period.abbreviation
50
+
51
+ tz = SDL4R::RelativeTimezone.get("CET-02:00")
52
+ assert_equal "CET-2", tz.identifier
53
+ assert_kind_of SDL4R::RelativeTimezone, tz
54
+ assert_equal(-7200, tz.relative_offset)
55
+
56
+ tz_period = tz.period_for_utc(Time.utc(2001, 1, 25, 21, 45, 50, 123))
57
+ assert_equal 3600 - 7200, tz_period.utc_offset
58
+ assert_equal 0, tz_period.std_offset
59
+ assert_equal :"CET-2", tz_period.abbreviation
60
+
61
+ tz = SDL4R::RelativeTimezone.get("Europe/Paris+01:40")
62
+ assert_equal "Europe/Paris+01:40", tz.identifier
63
+ assert_kind_of SDL4R::RelativeTimezone, tz
64
+ assert_equal 6000, tz.relative_offset
65
+
66
+ tz_period = tz.period_for_local(Time.utc(2001, 1, 1, 21, 45, 50, 123))
67
+ assert_equal 3600 + 6000, tz_period.utc_offset
68
+ assert_equal 3600 + 6000, tz_period.utc_total_offset
69
+ assert_equal 0, tz_period.std_offset
70
+ assert_equal :"CET+01:40", tz_period.abbreviation
71
+
72
+ tz_period = tz.period_for_local(Time.utc(2001, 7, 1, 21, 45, 50, 123))
73
+ assert_equal 3600 + 6000, tz_period.utc_offset
74
+ assert_equal 7200 + 6000, tz_period.utc_total_offset
75
+ assert_equal 3600, tz_period.std_offset
76
+ assert_equal :"CEST+01:40", tz_period.abbreviation
77
+
78
+ tz = SDL4R::RelativeTimezone.get("GMT+7")
79
+ assert_equal "GMT+7", tz.identifier
80
+
81
+ tz = SDL4R::RelativeTimezone.get("GMT-07")
82
+ assert_equal "GMT-7", tz.identifier
83
+
84
+ tz = SDL4R::RelativeTimezone.get("GMT+7:32")
85
+ assert_equal "GMT+07:32", tz.identifier
86
+
87
+ tz = SDL4R::RelativeTimezone.get("GMT-7:32")
88
+ assert_equal "GMT-07:32", tz.identifier
89
+
90
+ # Test abbreviations
91
+ tz = SDL4R::RelativeTimezone.get("JST")
92
+ assert_equal "JST", tz.identifier
93
+
94
+ tz = SDL4R::RelativeTimezone.get("JST+8")
95
+ assert_equal "JST+8", tz.identifier
96
+
97
+ assert_raise TZInfo::InvalidTimezoneIdentifier do
98
+ SDL4R::RelativeTimezone.get("LMT")
99
+ end
100
+ end
101
+ end
102
+ end
@@ -1,529 +1,612 @@
1
- #!/usr/bin/env ruby -w
2
- # encoding: UTF-8
3
-
4
- #--
5
- #
6
- # Simple Declarative Language (SDL) for Ruby
7
- # Copyright 2005 Ikayzo, inc.
8
- #
9
- # This program is free software. You can distribute or modify it under the
10
- # terms of the GNU Lesser General Public License version 2.1 as published by
11
- # the Free Software Foundation.
12
- #
13
- # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
14
- # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
- # See the GNU Lesser General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU Lesser General Public License
18
- # along with this program; if not, contact the Free Software Foundation, Inc.,
19
- # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
- #++
21
-
22
-
23
- if RUBY_VERSION < '1.9.0'
24
- $KCODE = 'u'
25
- require 'jcode'
26
- end
27
-
28
- module SDL4R
29
-
30
- require 'fileutils'
31
- require 'pathname'
32
- require 'date'
33
-
34
- require 'test/unit'
35
- require 'sdl4r'
36
-
37
- # SDL unit tests.
38
- #
39
- # @author Daniel Leuck
40
- #
41
- class SDL4RTest < Test::Unit::TestCase
42
-
43
- # Tag datastructure tests
44
- TAG = "Tag"
45
- TAG_WRITE_PARSE = "Tag Write Parse"
46
-
47
- # Basic Types Tests
48
- STRING_DECLARATIONS = "String Declarations"
49
- CHARACTER_DECLARATIONS = "Character Declarations"
50
- NUMBER_DECLARATIONS = "Number Declarations"
51
- BOOLEAN_DECLARATIONS = "Boolean Declarations"
52
- NULL_DECLARATION = "Null Declaration"
53
- DATE_DECLARATIONS = "Date Declarations"
54
- TIME_SPAN_DECLARATIONS = "Time Span Declarations"
55
- DATE_TIME_DECLARATIONS = "Date Time Declarations"
56
- BINARY_DECLARATIONS = "Binary Declarations";
57
-
58
- # Structure Tests
59
- EMPTY_TAG = "Empty Tag"
60
- VALUES = "Values"
61
- ATTRIBUTES = "Attributes"
62
- VALUES_AND_ATTRIBUTES = "Values and Attributes"
63
- CHILDREN = "Children"
64
- NAMESPACES = "Namespaces"
65
-
66
-
67
- def assert_tags_equal(expected, actual, message)
68
- if expected != actual
69
- assert_equal(expected.to_s, actual.to_s, message)
70
- end
71
- end
72
-
73
- # Returns a Pathname giving the location of the specified relative +filename+.
74
- #
75
- # +filename+:: path to a file relatively to this source file
76
- #
77
- def get_test_sdl_file_path(filename)
78
- dir = File.dirname(__FILE__)
79
- return Pathname.new(dir + '/' + filename)
80
- end
81
-
82
- @@root_basic_types = nil
83
- @@root_structures = nil
84
-
85
- def setup
86
- super
87
-
88
- @@root_basic_types ||= SDL4R::read(get_test_sdl_file_path("test_basic_types.sdl"))
89
- @@root_structures ||= SDL4R::read(get_test_sdl_file_path("test_structures.sdl"))
90
- end
91
-
92
- ######################################
93
- # Tag Tests
94
- ######################################
95
- def test_tag
96
- # Test to make sure Tag ignores the order in which attributes are
97
- # added.
98
- t1 = Tag.new("test")
99
- t1.set_attribute("foo", "bar")
100
- t1.set_attribute("john", "doe")
101
-
102
- t2 = Tag.new("test")
103
- t2.set_attribute("john", "doe")
104
- t2.set_attribute("foo", "bar")
105
-
106
- assert_tags_equal(t1, t2, TAG)
107
-
108
- # Making sure tags with different structures return false from .equals
109
- t2.value = "item"
110
- assert_not_equal(t1, t2, TAG)
111
-
112
- t2.remove_value("item")
113
- t2.set_attribute("another", "attribute")
114
- assert_not_equal(t1, t2, TAG)
115
-
116
- # Checking attributes namespaces
117
- t2.set_attribute("name", "bill")
118
- t2.set_attribute("private", "smoker", true)
119
- t2.set_attribute("public", "hobby", "hiking")
120
- t2.set_attribute("private", "nickname", "tubby")
121
-
122
- assert_equal(
123
- t2.attributes("private"),
124
- { "smoker" => true, "nickname" => "tubby" },
125
- "attributes()")
126
- end
127
-
128
- def test_tag_write_parse_basic_types
129
- check_tag_write_parse @@root_basic_types
130
- end
131
-
132
- def test_tag_write_parse_structures
133
- check_tag_write_parse @@root_structures
134
- end
135
-
136
- #
137
- # Does a to_s/parse test on the specified Tag (+root+)
138
- #
139
- def check_tag_write_parse(root)
140
- # puts '========================================'
141
- # puts root.to_s
142
- # puts '========================================'
143
-
144
- write_parse_root = Tag.new("test").read(root.to_s).child("root");
145
-
146
- # File.open("d:\\dev\\tmp\\root.sdl", "w") { |io| io.write(root.to_string) }
147
- # File.open("d:\\dev\\tmp\\root_reparsed.sdl", "w") { |io| io.write(write_parse_root.to_string) }
148
-
149
- assert_tags_equal(root, write_parse_root, "write/parse")
150
- end
151
-
152
- ######################################
153
- # Basic Types Tests
154
- ######################################
155
-
156
- def test_strings
157
- root = @@root_basic_types
158
-
159
- # Doing String tests...
160
- # Doing basic tests including new line handling...
161
- assert_equal(root.child("string1").value, "hello", STRING_DECLARATIONS)
162
- assert_equal(root.child("string2").value, "hi", STRING_DECLARATIONS)
163
- assert_equal(root.child("string3").value, "aloha", STRING_DECLARATIONS)
164
- assert_equal(root.child("string4").value, "hi there", STRING_DECLARATIONS)
165
- assert_equal(root.child("string5").value, "hi there joe", STRING_DECLARATIONS)
166
- assert_equal(root.child("string6").value, "line1\nline2", STRING_DECLARATIONS)
167
- assert_equal(root.child("string7").value, "line1\nline2", STRING_DECLARATIONS)
168
- assert_equal(root.child("string8").value, "line1\nline2\nline3", STRING_DECLARATIONS)
169
- assert_equal(
170
- root.child("string9").value,
171
- "Anything should go in this line without escapes \\ \\\\ \\n " +
172
- "\\t \" \"\" ' ''", STRING_DECLARATIONS)
173
- assert_equal(root.child("string10").value, "escapes \"\\\n\t", STRING_DECLARATIONS)
174
-
175
- # Checking unicode strings...
176
- assert_equal(root.child("japanese.hello").value, "日本語", STRING_DECLARATIONS)
177
- assert_equal(root.child("korean.hello").value, "여보세요", STRING_DECLARATIONS)
178
- assert_equal(root.child("russian.hello").value, "здравствулте", STRING_DECLARATIONS)
179
-
180
- # More new line tests...
181
- assert(root.child("xml").value.index("<text>Hi there!</text>") >= 0, STRING_DECLARATIONS)
182
- assert_equal(root.child("line_test").value, "\nnew line above and below\n", STRING_DECLARATIONS)
183
- end
184
-
185
- def test_characters
186
- root = @@root_basic_types
187
-
188
- assert_equal(root.child("char1").value, 'a', CHARACTER_DECLARATIONS)
189
- assert_equal(root.child("char2").value, 'A', CHARACTER_DECLARATIONS)
190
- assert_equal(root.child("char3").value, '\\', CHARACTER_DECLARATIONS)
191
- assert_equal(root.child("char4").value, "\n", CHARACTER_DECLARATIONS)
192
- assert_equal(root.child("char5").value, "\t", CHARACTER_DECLARATIONS)
193
- assert_equal(root.child("char6").value, '\'', CHARACTER_DECLARATIONS)
194
- assert_equal(root.child("char7").value, '"', CHARACTER_DECLARATIONS)
195
- end
196
-
197
- def test_characters_unicode
198
- root = @@root_basic_types
199
-
200
- assert_equal(root.child("char8").value, "日", CHARACTER_DECLARATIONS) # \u65e5
201
- assert_equal(root.child("char9").value, "여", CHARACTER_DECLARATIONS) # \uc5ec
202
- assert_equal(root.child("char10").value, "з", CHARACTER_DECLARATIONS) # \u0437
203
- end
204
-
205
- def test_numbers
206
- root = @@root_basic_types
207
-
208
- # Testing ints...
209
- assert_equal(root.child("int1").value, 0, NUMBER_DECLARATIONS)
210
- assert_equal(root.child("int2").value, 5, NUMBER_DECLARATIONS)
211
- assert_equal(root.child("int3").value, -100, NUMBER_DECLARATIONS)
212
- assert_equal(root.child("int4").value, 234253532, NUMBER_DECLARATIONS)
213
-
214
- # Testing longs...
215
- assert_equal(root.child("long1").value, 0, NUMBER_DECLARATIONS)
216
- assert_equal(root.child("long2").value, 5, NUMBER_DECLARATIONS)
217
- assert_equal(root.child("long3").value, 5, NUMBER_DECLARATIONS)
218
- assert_equal(root.child("long4").value, 3904857398753453453, NUMBER_DECLARATIONS)
219
-
220
- # Testing floats...
221
- assert_equal(root.child("float1").value, 1, NUMBER_DECLARATIONS)
222
- assert_equal(root.child("float2").value, 0.23, NUMBER_DECLARATIONS)
223
- assert_equal(root.child("float3").value, -0.34, NUMBER_DECLARATIONS)
224
-
225
- # Testing doubles..."
226
- assert_equal(root.child("double1").value, 2, NUMBER_DECLARATIONS)
227
- assert_equal(root.child("double2").value, -0.234, NUMBER_DECLARATIONS)
228
- assert_equal(root.child("double3").value, 2.34, NUMBER_DECLARATIONS)
229
-
230
- # Testing decimals (BigDouble in Java)...
231
- assert_equal(
232
- root.child("decimal1").value, 0, NUMBER_DECLARATIONS);
233
- assert_equal(
234
- root.child("decimal2").value, 11.111111, NUMBER_DECLARATIONS);
235
- assert_equal(
236
- root.child("decimal3").value, 234535.3453453453454345345341242343, NUMBER_DECLARATIONS);
237
- end
238
-
239
- def test_booleans
240
- root = @@root_basic_types
241
-
242
- assert_equal(root.child("light-on").value, true, BOOLEAN_DECLARATIONS)
243
- assert_equal(root.child("light-off").value, false, BOOLEAN_DECLARATIONS)
244
- assert_equal(root.child("light1").value, true, BOOLEAN_DECLARATIONS)
245
- assert_equal(root.child("light2").value, false, BOOLEAN_DECLARATIONS)
246
- end
247
-
248
- def test_null
249
- root = @@root_basic_types
250
-
251
- assert_equal(root.child("nothing").value, nil, NULL_DECLARATION);
252
- end
253
-
254
- def test_dates
255
- root = @@root_basic_types
256
-
257
- assert_equal(root.child("date1").value, Date.civil(2005, 12, 31), DATE_DECLARATIONS)
258
- assert_equal(root.child("date2").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
259
- assert_equal(root.child("date3").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
260
- assert_equal(root.child("_way_back").value, Date.civil(582, 9, 16), DATE_DECLARATIONS)
261
- end
262
-
263
- def test_time_spans
264
- root = @@root_basic_types
265
-
266
- assert_equal(
267
- root.child("time1").value, SdlTimeSpan.new(0,12,30,0,0), TIME_SPAN_DECLARATIONS)
268
- assert_equal(
269
- root.child("time2").value, SdlTimeSpan.new(0,24,0,0,0), TIME_SPAN_DECLARATIONS)
270
- assert_equal(
271
- root.child("time3").value, SdlTimeSpan.new(0,1,0,0,0), TIME_SPAN_DECLARATIONS)
272
- assert_equal(
273
- root.child("time4").value, SdlTimeSpan.new(0,1,0,0,0), TIME_SPAN_DECLARATIONS)
274
- assert_equal(
275
- root.child("time5").value, SdlTimeSpan.new(0,12,30,2,0), TIME_SPAN_DECLARATIONS)
276
- assert_equal(
277
- root.child("time6").value, SdlTimeSpan.new(0,12,30,23,0), TIME_SPAN_DECLARATIONS)
278
- assert_equal(
279
- root.child("time7").value, SdlTimeSpan.new(0,12,30,23,100), TIME_SPAN_DECLARATIONS)
280
- assert_equal(
281
- root.child("time8").value, SdlTimeSpan.new(0,12,30,23,120), TIME_SPAN_DECLARATIONS)
282
- assert_equal(
283
- root.child("time9").value, SdlTimeSpan.new(0,12,30,23,123), TIME_SPAN_DECLARATIONS)
284
-
285
- # Checking time spans with days...
286
- assert_equal(
287
- root.child("time10").value, SdlTimeSpan.new(34,12,30,23,100), TIME_SPAN_DECLARATIONS)
288
- assert_equal(
289
- root.child("time11").value, SdlTimeSpan.new(1,12,30,0,0), TIME_SPAN_DECLARATIONS)
290
- assert_equal(
291
- root.child("time12").value, SdlTimeSpan.new(5,12,30,23,123), TIME_SPAN_DECLARATIONS)
292
-
293
- # Checking negative time spans...
294
- assert_equal(
295
- root.child("time13").value, SdlTimeSpan.new(0,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
296
- assert_equal(
297
- root.child("time14").value, SdlTimeSpan.new(-5,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
298
- end
299
-
300
- def test_date_times
301
- root = @@root_basic_types
302
- local_offset = DateTime.now.offset
303
-
304
- assert_equal(root.child("date_time1").value,
305
- DateTime.civil(2005,12,31,12,30,0, local_offset), DATE_TIME_DECLARATIONS)
306
- assert_equal(root.child("date_time2").value,
307
- DateTime.civil(1882,5,2,12,30,0, local_offset), DATE_TIME_DECLARATIONS)
308
- assert_equal(root.child("date_time3").value,
309
- DateTime.civil(2005,12,31,1,0,0, local_offset), DATE_TIME_DECLARATIONS)
310
- assert_equal(root.child("date_time4").value,
311
- DateTime.civil(1882,5,2,1,0,0, local_offset), DATE_TIME_DECLARATIONS)
312
- assert_equal(root.child("date_time5").value,
313
- DateTime.civil(2005,12,31,12,30,2312.to_r/100, local_offset), DATE_TIME_DECLARATIONS)
314
- assert_equal(root.child("date_time6").value,
315
- DateTime.civil(1882,5,2,12,30,23123.to_r/1000, local_offset), DATE_TIME_DECLARATIONS)
316
-
317
- # Checking timezones...
318
- assert_equal(root.child("date_time7").value,
319
- DateTime.civil(1882,5,2,12,30,23123.to_r/1000,"JST"), DATE_TIME_DECLARATIONS)
320
- assert_equal(root.child("date_time8").value,
321
- DateTime.civil(985,04,11,12,30,23123.to_r/1000,"PST"), DATE_TIME_DECLARATIONS)
322
- end
323
-
324
- def test_binaries
325
- root = @@root_basic_types
326
-
327
- assert_equal(SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
328
- assert_equal(
329
- SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
330
- assert_equal(
331
- SdlBinary.decode64(
332
- "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAKnRFWHRDcmVhdGlvbiBUaW1l" +
333
- "AERpIDQgTXJ6IDIwMDMgMDA6MjQ6MDQgKzAxMDDdSQ6OAAAAB3RJTUUH0wMEAAcllPlrJgAA" +
334
- "AAlwSFlzAAAK8AAACvABQqw0mAAAAARnQU1BAACxjwv8YQUAAADQSURBVHjaY2CgEDCCyZn/" +
335
- "3YHkDhL1ejCkM+5kgXJ2zDQmXueShwwMh9+ALWSEGcCQfhZIvHlDnAk8PAwMHBxgJtyAa7bX" +
336
- "UdT8/cvA8Ps3hP7zB4FBYn/+vGbweqyJaoCmpiaKASDFv35BNMBoZMzwGKKOidJYoNgAuBdm" +
337
- "naXQgHRKDfgagxD89w8S+iAaFICwGIHFAgjrHUczAByySAaAMEgDLBphhv7/D8EYLgDZhAxA" +
338
- "mkAKYYbAMMwwDAOQXYDuDXRXgDC6AR7SW8jITNQAACjZgdj4VjlqAAAAAElFTkSuQmCC"
339
- ),
340
- root.child("png").value,
341
- BINARY_DECLARATIONS)
342
- end
343
-
344
- ######################################
345
- # Structure Tests (values, attributes, children)
346
- ######################################
347
-
348
- def test_empty_tag
349
- root = @@root_structures
350
- assert_equal(root.child("empty_tag"), Tag.new("empty_tag"), EMPTY_TAG)
351
- end
352
-
353
- def test_values
354
- root = @@root_structures
355
- local_offset = DateTime.now.offset
356
-
357
- assert_equal(root.child("values1").values, ["hi"], VALUES)
358
- assert_equal(root.child("values2").values, ["hi","ho"], VALUES)
359
- assert_equal(root.child("values3").values, [1, "ho"], VALUES)
360
- assert_equal(root.child("values4").values, ["hi",5], VALUES)
361
- assert_equal(root.child("values5").values, [1,2], VALUES)
362
- assert_equal(root.child("values6").values, [1,2,3], VALUES)
363
- assert_equal(
364
- root.child("values7").values,
365
- [nil,"foo",false,Date.civil(1980,12,5)],
366
- VALUES)
367
- assert_equal(
368
- root.child("values8").values,
369
- [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
370
- "there", SdlTimeSpan.new(0,15,23,12,234)],
371
- VALUES)
372
- assert_equal(
373
- root.child("values9").values,
374
- [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
375
- "there", DateTime.civil(1989,8,12,15,23,12234.to_r/1000,"JST")],
376
- VALUES)
377
- assert_equal(
378
- root.child("values10").values,
379
- [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
380
- "there", SdlTimeSpan.new(0,15,23,12,234), "more stuff"],
381
- VALUES)
382
- assert_equal(
383
- root.child("values11").values,
384
- [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
385
- "there", SdlTimeSpan.new(123,15,23,12,234), "more stuff here"],
386
- VALUES)
387
- assert_equal(root.child("values12").values, [1,3], VALUES)
388
- assert_equal(root.child("values13").values, [1,3], VALUES)
389
- assert_equal(root.child("values14").values, [1,3], VALUES)
390
- assert_equal(root.child("values15").values, [1,2,4,5,6], VALUES)
391
- assert_equal(root.child("values16").values, [1,2,5], VALUES)
392
- assert_equal(root.child("values17").values, [1,2,5], VALUES)
393
- assert_equal(root.child("values18").values, [1,2,7], VALUES)
394
- assert_equal(root.child("values19").values, [1,3,5,7], VALUES)
395
- assert_equal(root.child("values20").values, [1,3,5], VALUES)
396
- assert_equal(root.child("values21").values, [1,3,5], VALUES)
397
- assert_equal(root.child("values22").values, ["hi","ho","ho",5,"hi"], VALUES)
398
- end
399
-
400
- def test_attributes
401
- root = @@root_structures
402
-
403
- assert_equal(
404
- root.child("atts1").attributes, {"name" => "joe"}, ATTRIBUTES);
405
- assert_equal(root.child("atts2").attributes, {"size" => 5}, ATTRIBUTES);
406
- assert_equal(
407
- root.child("atts3").attributes, {"name" => "joe","size" => 5}, ATTRIBUTES);
408
- assert_equal(
409
- root.child("atts4").attributes, {"name"=>"joe","size"=>5,"smoker"=>false},
410
- ATTRIBUTES);
411
- assert_equal(
412
- root.child("atts5").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES);
413
- assert_equal(
414
- root.child("atts6").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES);
415
- assert_equal(root.child("atts7").attributes, {"name"=>"joe"}, ATTRIBUTES);
416
- assert_equal(
417
- root.child("atts8").attributes,
418
- {"name"=>"joe","size"=>5,"smoker"=>false,
419
- "text"=>"hi","birthday"=>Date.civil(1972,5,23)},
420
- ATTRIBUTES);
421
- assert_equal(
422
- root.child("atts9").attribute("key"), SDL4R::SdlBinary("mykey"), ATTRIBUTES)
423
- end
424
-
425
- def test_values_and_attributes
426
- root = @@root_structures
427
-
428
- assert_equal(root.child("valatts1").values, ["joe"], VALUES_AND_ATTRIBUTES)
429
- assert_equal(
430
- root.child("valatts1").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
431
-
432
- assert_equal(root.child("valatts2").values, ["joe"], VALUES_AND_ATTRIBUTES)
433
- assert_equal(root.child("valatts2").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
434
-
435
- assert_equal(root.child("valatts3").values, ["joe"], VALUES_AND_ATTRIBUTES)
436
- assert_equal(root.child("valatts3").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
437
-
438
- assert_equal(root.child("valatts4").values, ["joe"], VALUES_AND_ATTRIBUTES)
439
- assert_equal(
440
- root.child("valatts4").attributes,
441
- {"size"=>5, "weight"=>160, "hat"=>"big"},
442
- VALUES_AND_ATTRIBUTES)
443
-
444
- assert_equal(
445
- root.child("valatts5").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
446
- assert_equal(
447
- root.child("valatts5").attributes,
448
- {"size"=>5, "smoker"=>false},
449
- VALUES_AND_ATTRIBUTES);
450
-
451
- assert_equal(
452
- root.child("valatts6").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
453
- assert_equal(
454
- root.child("valatts6").attributes,
455
- {"size"=>5, "house"=>"big and\n blue"},
456
- VALUES_AND_ATTRIBUTES)
457
-
458
- #####
459
-
460
- assert_equal(
461
- root.child("valatts7").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
462
- assert_equal(
463
- root.child("valatts7").attributes,
464
- {"size"=>5, "smoker"=>false},
465
- VALUES_AND_ATTRIBUTES)
466
-
467
- assert_equal(
468
- root.child("valatts8").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
469
- assert_equal(
470
- root.child("valatts8").attributes,
471
- {"size"=>5, "smoker"=>false},
472
- VALUES_AND_ATTRIBUTES);
473
-
474
- assert_equal(
475
- root.child("valatts9").values,["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
476
- assert_equal(
477
- root.child("valatts9").attributes,
478
- {"size"=>5, "smoker"=>false},
479
- VALUES_AND_ATTRIBUTES)
480
- end
481
-
482
- def test_children
483
- root = @@root_structures
484
- parent = root.child("parent")
485
-
486
- assert_equal(parent.children.size, 2, CHILDREN)
487
- assert_equal(parent.children[1].name, "daughter", CHILDREN)
488
-
489
- grandparent = root.child("grandparent")
490
-
491
- assert_equal(grandparent.children.size, 2, CHILDREN)
492
- # recursive fetch of children
493
- assert_equal(grandparent.children(true).size, 6, CHILDREN)
494
- assert_equal(grandparent.children(true, "son").size, 2, CHILDREN)
495
-
496
- grandparent2 = root.child("grandparent2")
497
- assert_equal(grandparent2.children(true, "child").size, 5, CHILDREN)
498
- assert_equal(
499
- grandparent2.child(true, "daughter").attribute("birthday"),
500
- Date.civil(1976,04,18),
501
- CHILDREN)
502
-
503
- files = root.child("files")
504
-
505
- assert_equal(
506
- ["c:/file1.txt", "c:/file2.txt", "c:/folder"],
507
- files.children_values("content"),
508
- CHILDREN)
509
-
510
- matrix = root.child("matrix")
511
-
512
- assert_equal([[1,2,3],[4,5,6]], matrix.children_values("content"), CHILDREN);
513
- end
514
-
515
- def test_namespaces
516
- root = @@root_structures
517
- assert_equal(8, root.children(true, "person", nil).size, NAMESPACES);
518
-
519
- grandparent2 = root.child("grandparent3");
520
-
521
- # get only the attributes for Akiko in the public namespace
522
- assert_equal(
523
- grandparent2.child(true, "daughter").attributes("public"),
524
- {"name"=>"Akiko", "birthday"=>Date.civil(1976,04,18)},
525
- NAMESPACES);
526
- end
527
-
528
- end
1
+ #!/usr/bin/env ruby -w
2
+ # encoding: UTF-8
3
+
4
+ #--
5
+ #
6
+ # Simple Declarative Language (SDL) for Ruby
7
+ # Copyright 2005 Ikayzo, inc.
8
+ #
9
+ # This program is free software. You can distribute or modify it under the
10
+ # terms of the GNU Lesser General Public License version 2.1 as published by
11
+ # the Free Software Foundation.
12
+ #
13
+ # This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
14
+ # INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
15
+ # See the GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with this program; if not, contact the Free Software Foundation, Inc.,
19
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
+ #++
21
+
22
+ # Work-around a bug in NetBeans (http://netbeans.org/bugzilla/show_bug.cgi?id=188653)
23
+ if ENV["NB_EXEC_EXTEXECUTION_PROCESS_UUID"]
24
+ $:[0] = File.join(File.dirname(__FILE__),'../../lib')
25
+ $:.unshift(File.join(File.dirname(__FILE__),'../../test'))
26
+ end
27
+
28
+ if RUBY_VERSION < '1.9.0'
29
+ $KCODE = 'u'
30
+ require 'jcode'
31
+ end
32
+
33
+ module SDL4R
34
+
35
+ require 'fileutils'
36
+ require 'pathname'
37
+ require 'date'
38
+ require 'test/unit'
39
+
40
+ require 'sdl4r'
41
+
42
+ require 'sdl4r/sdl_test_case'
43
+
44
+ class SDL4RTest < Test::Unit::TestCase
45
+ include SdlTestCase
46
+
47
+ # Tag datastructure tests
48
+ TAG = "Tag"
49
+ TAG_WRITE_PARSE = "Tag Write Parse"
50
+
51
+ # Basic Types Tests
52
+ STRING_DECLARATIONS = "String Declarations"
53
+ CHARACTER_DECLARATIONS = "Character Declarations"
54
+ NUMBER_DECLARATIONS = "Number Declarations"
55
+ BOOLEAN_DECLARATIONS = "Boolean Declarations"
56
+ NULL_DECLARATION = "Null Declaration"
57
+ DATE_DECLARATIONS = "Date Declarations"
58
+ TIME_SPAN_DECLARATIONS = "Time Span Declarations"
59
+ DATE_TIME_DECLARATIONS = "Date Time Declarations"
60
+ BINARY_DECLARATIONS = "Binary Declarations"
61
+
62
+ # Structure Tests
63
+ EMPTY_TAG = "Empty Tag"
64
+ VALUES = "Values"
65
+ ATTRIBUTES = "Attributes"
66
+ VALUES_AND_ATTRIBUTES = "Values and Attributes"
67
+ CHILDREN = "Children"
68
+ NAMESPACES = "Namespaces"
69
+
70
+
71
+ def assert_tags_equal(expected, actual, message)
72
+ if expected != actual
73
+ assert_equal(expected.to_s, actual.to_s, message)
74
+ end
75
+ end
76
+
77
+ # Returns a Pathname giving the location of the specified relative +filename+.
78
+ #
79
+ # +filename+:: path to a file relatively to this source file
80
+ #
81
+ def get_test_sdl_file_path(filename)
82
+ dir = File.dirname(__FILE__)
83
+ return Pathname.new(dir + '/' + filename)
84
+ end
85
+
86
+ @@root_basic_types = nil
87
+ @@root_structures = nil
88
+
89
+ def setup
90
+ super
91
+
92
+ @@root_basic_types ||= SDL4R::read(get_test_sdl_file_path("test_basic_types.sdl"))
93
+ @@root_structures ||= SDL4R::read(get_test_sdl_file_path("test_structures.sdl"))
94
+ end
95
+
96
+ ######################################
97
+ # Tag Tests
98
+ ######################################
99
+ def test_tag
100
+ # Test to make sure Tag ignores the order in which attributes are
101
+ # added.
102
+ t1 = Tag.new("test")
103
+ t1.set_attribute("foo", "bar")
104
+ t1.set_attribute("john", "doe")
105
+
106
+ t2 = Tag.new("test")
107
+ t2.set_attribute("john", "doe")
108
+ t2.set_attribute("foo", "bar")
109
+
110
+ assert_tags_equal(t1, t2, TAG)
111
+
112
+ # Making sure tags with different structures return false from .equals
113
+ t2.value = "item"
114
+ assert_not_equal(t1, t2, TAG)
115
+
116
+ t2.remove_value("item")
117
+ t2.set_attribute("another", "attribute")
118
+ assert_not_equal(t1, t2, TAG)
119
+
120
+ # Checking attributes namespaces
121
+ t2.set_attribute("name", "bill")
122
+ t2.set_attribute("private", "smoker", true)
123
+ t2.set_attribute("public", "hobby", "hiking")
124
+ t2.set_attribute("private", "nickname", "tubby")
125
+
126
+ assert_equal(
127
+ t2.attributes("private"),
128
+ { "smoker" => true, "nickname" => "tubby" },
129
+ "attributes()")
130
+ end
131
+
132
+ def test_tag_write_parse_basic_types
133
+ check_tag_write_parse @@root_basic_types
134
+ end
135
+
136
+ def test_tag_write_parse_structures
137
+ check_tag_write_parse @@root_structures
138
+ end
139
+
140
+ #
141
+ # Does a to_s/parse test on the specified Tag (+root+)
142
+ #
143
+ def check_tag_write_parse(root)
144
+ # puts '========================================'
145
+ # puts root.to_s
146
+ # puts '========================================'
147
+
148
+ write_parse_root = Tag.new("test").read(root.to_s).child("root")
149
+
150
+ # File.open("d:\\dev\\tmp\\root.sdl", "w") { |io| io.write(root.to_string) }
151
+ # File.open("d:\\dev\\tmp\\root_reparsed.sdl", "w") { |io| io.write(write_parse_root.to_string) }
152
+
153
+ assert_tags_equal(root, write_parse_root, "write/parse")
154
+ end
155
+
156
+ ######################################
157
+ # Basic Types Tests
158
+ ######################################
159
+
160
+ def test_strings
161
+ root = @@root_basic_types
162
+
163
+ # Doing String tests...
164
+ # Doing basic tests including new line handling...
165
+ assert_equal(root.child("string1").value, "hello", STRING_DECLARATIONS)
166
+ assert_equal(root.child("string2").value, "hi", STRING_DECLARATIONS)
167
+ assert_equal(root.child("string3").value, "aloha", STRING_DECLARATIONS)
168
+ assert_equal(root.child("string4").value, "hi there", STRING_DECLARATIONS)
169
+ assert_equal(root.child("string5").value, "hi there joe", STRING_DECLARATIONS)
170
+ assert_equal(root.child("string6").value, "line1\nline2", STRING_DECLARATIONS)
171
+ assert_equal(root.child("string7").value, "line1\nline2", STRING_DECLARATIONS)
172
+ assert_equal(root.child("string8").value, "line1\nline2\nline3", STRING_DECLARATIONS)
173
+ assert_equal(
174
+ root.child("string9").value,
175
+ "Anything should go in this line without escapes \\ \\\\ \\n " +
176
+ "\\t \" \"\" ' ''", STRING_DECLARATIONS)
177
+ assert_equal(root.child("string10").value, "escapes \"\\\n\t", STRING_DECLARATIONS)
178
+
179
+ # Checking unicode strings...
180
+ assert_equal(root.child("japanese.hello").value, "日本語", STRING_DECLARATIONS)
181
+ assert_equal(root.child("korean.hello").value, "여보세요", STRING_DECLARATIONS)
182
+ assert_equal(root.child("russian.hello").value, "здравствулте", STRING_DECLARATIONS)
183
+
184
+ # More new line tests...
185
+ assert(root.child("xml").value.index("<text>Hi there!</text>") >= 0, STRING_DECLARATIONS)
186
+ assert_equal(root.child("line_test").value, "\nnew line above and below\n", STRING_DECLARATIONS)
187
+ end
188
+
189
+ def test_characters
190
+ root = @@root_basic_types
191
+
192
+ assert_equal(root.child("char1").value, 'a', CHARACTER_DECLARATIONS)
193
+ assert_equal(root.child("char2").value, 'A', CHARACTER_DECLARATIONS)
194
+ assert_equal(root.child("char3").value, '\\', CHARACTER_DECLARATIONS)
195
+ assert_equal(root.child("char4").value, "\n", CHARACTER_DECLARATIONS)
196
+ assert_equal(root.child("char5").value, "\t", CHARACTER_DECLARATIONS)
197
+ assert_equal(root.child("char6").value, '\'', CHARACTER_DECLARATIONS)
198
+ assert_equal(root.child("char7").value, '"', CHARACTER_DECLARATIONS)
199
+ end
200
+
201
+ def test_characters_unicode
202
+ root = @@root_basic_types
203
+
204
+ assert_equal(root.child("char8").value, "日", CHARACTER_DECLARATIONS) # \u65e5
205
+ assert_equal(root.child("char9").value, "여", CHARACTER_DECLARATIONS) # \uc5ec
206
+ assert_equal(root.child("char10").value, "з", CHARACTER_DECLARATIONS) # \u0437
207
+ end
208
+
209
+ def test_numbers
210
+ root = @@root_basic_types
211
+
212
+ # Testing ints...
213
+ assert_equal(root.child("int1").value, 0, NUMBER_DECLARATIONS)
214
+ assert_equal(root.child("int2").value, 5, NUMBER_DECLARATIONS)
215
+ assert_equal(root.child("int3").value, -100, NUMBER_DECLARATIONS)
216
+ assert_equal(root.child("int4").value, 234253532, NUMBER_DECLARATIONS)
217
+
218
+ # Testing longs...
219
+ assert_equal(root.child("long1").value, 0, NUMBER_DECLARATIONS)
220
+ assert_equal(root.child("long2").value, 5, NUMBER_DECLARATIONS)
221
+ assert_equal(root.child("long3").value, 5, NUMBER_DECLARATIONS)
222
+ assert_equal(root.child("long4").value, 3904857398753453453, NUMBER_DECLARATIONS)
223
+
224
+ # Testing floats...
225
+ assert_equal(root.child("float1").value, 1, NUMBER_DECLARATIONS)
226
+ assert_equal(root.child("float2").value, 0.23, NUMBER_DECLARATIONS)
227
+ assert_equal(root.child("float3").value, -0.34, NUMBER_DECLARATIONS)
228
+
229
+ # Testing doubles..."
230
+ assert_equal(root.child("double1").value, 2, NUMBER_DECLARATIONS)
231
+ assert_equal(root.child("double2").value, -0.234, NUMBER_DECLARATIONS)
232
+ assert_equal(root.child("double3").value, 2.34, NUMBER_DECLARATIONS)
233
+
234
+ # Testing decimals (BigDouble in Java)...
235
+ assert_equal(
236
+ root.child("decimal1").value, 0, NUMBER_DECLARATIONS)
237
+ assert_equal(
238
+ root.child("decimal2").value, 11.111111, NUMBER_DECLARATIONS)
239
+ assert_equal(
240
+ root.child("decimal3").value, 234535.3453453453454345345341242343, NUMBER_DECLARATIONS)
241
+ end
242
+
243
+ def test_booleans
244
+ root = @@root_basic_types
245
+
246
+ assert_equal(root.child("light-on").value, true, BOOLEAN_DECLARATIONS)
247
+ assert_equal(root.child("light-off").value, false, BOOLEAN_DECLARATIONS)
248
+ assert_equal(root.child("light1").value, true, BOOLEAN_DECLARATIONS)
249
+ assert_equal(root.child("light2").value, false, BOOLEAN_DECLARATIONS)
250
+ end
251
+
252
+ def test_null
253
+ root = @@root_basic_types
254
+
255
+ assert_equal(root.child("nothing").value, nil, NULL_DECLARATION)
256
+ end
257
+
258
+ def test_dates
259
+ root = @@root_basic_types
260
+
261
+ assert_equal(root.child("date1").value, Date.civil(2005, 12, 31), DATE_DECLARATIONS)
262
+ assert_equal(root.child("date2").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
263
+ assert_equal(root.child("date3").value, Date.civil(1882, 5, 2), DATE_DECLARATIONS)
264
+ assert_equal(root.child("_way_back").value, Date.civil(582, 9, 16), DATE_DECLARATIONS)
265
+ end
266
+
267
+ def test_time_spans
268
+ root = @@root_basic_types
269
+
270
+ assert_equal(
271
+ root.child("time1").value, SdlTimeSpan.new(0,12,30,0,0), TIME_SPAN_DECLARATIONS)
272
+ assert_equal(
273
+ root.child("time2").value, SdlTimeSpan.new(0,24,0,0,0), TIME_SPAN_DECLARATIONS)
274
+ assert_equal(
275
+ root.child("time3").value, SdlTimeSpan.new(0,1,0,0,0), TIME_SPAN_DECLARATIONS)
276
+ assert_equal(
277
+ root.child("time4").value, SdlTimeSpan.new(0,1,0,0,0), TIME_SPAN_DECLARATIONS)
278
+ assert_equal(
279
+ root.child("time5").value, SdlTimeSpan.new(0,12,30,2,0), TIME_SPAN_DECLARATIONS)
280
+ assert_equal(
281
+ root.child("time6").value, SdlTimeSpan.new(0,12,30,23,0), TIME_SPAN_DECLARATIONS)
282
+ assert_equal(
283
+ SdlTimeSpan.new(0,12,30,23,100), root.child("time7").value, TIME_SPAN_DECLARATIONS)
284
+ assert_equal(
285
+ root.child("time8").value, SdlTimeSpan.new(0,12,30,23,120), TIME_SPAN_DECLARATIONS)
286
+ assert_equal(
287
+ root.child("time9").value, SdlTimeSpan.new(0,12,30,23,123), TIME_SPAN_DECLARATIONS)
288
+
289
+ # Checking time spans with days...
290
+ assert_equal(
291
+ root.child("time10").value, SdlTimeSpan.new(34,12,30,23,100), TIME_SPAN_DECLARATIONS)
292
+ assert_equal(
293
+ root.child("time11").value, SdlTimeSpan.new(1,12,30,0,0), TIME_SPAN_DECLARATIONS)
294
+ assert_equal(
295
+ root.child("time12").value, SdlTimeSpan.new(5,12,30,23,123), TIME_SPAN_DECLARATIONS)
296
+
297
+ # Checking negative time spans...
298
+ assert_equal(
299
+ root.child("time13").value, SdlTimeSpan.new(0,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
300
+ assert_equal(
301
+ root.child("time14").value, SdlTimeSpan.new(-5,-12,-30,-23,-123), TIME_SPAN_DECLARATIONS)
302
+ end
303
+
304
+ def test_date_times
305
+ root = @@root_basic_types
306
+ local_offset = DateTime.now.offset
307
+
308
+ assert_equal(root.child("date_time1").value,
309
+ DateTime.civil(2005,12,31,12,30,0, local_offset), DATE_TIME_DECLARATIONS)
310
+ assert_equal(root.child("date_time2").value,
311
+ DateTime.civil(1882,5,2,12,30,0, local_offset), DATE_TIME_DECLARATIONS)
312
+ assert_equal(root.child("date_time3").value,
313
+ DateTime.civil(2005,12,31,1,0,0, local_offset), DATE_TIME_DECLARATIONS)
314
+ assert_equal(root.child("date_time4").value,
315
+ DateTime.civil(1882,5,2,1,0,0, local_offset), DATE_TIME_DECLARATIONS)
316
+ assert_equal(root.child("date_time5").value,
317
+ DateTime.civil(2005,12,31,12,30,2312.to_r/100, local_offset), DATE_TIME_DECLARATIONS)
318
+ assert_equal(root.child("date_time6").value,
319
+ DateTime.civil(1882,5,2,12,30,23123.to_r/1000, local_offset), DATE_TIME_DECLARATIONS)
320
+
321
+ # Checking timezones...
322
+ assert_equal(root.child("date_time7").value,
323
+ DateTime.civil(1882,5,2,12,30,23123.to_r/1000,"JST"), DATE_TIME_DECLARATIONS)
324
+ assert_equal(root.child("date_time8").value,
325
+ DateTime.civil(985,04,11,12,30,23123.to_r/1000,"PST"), DATE_TIME_DECLARATIONS)
326
+ end
327
+
328
+ def test_binaries
329
+ root = @@root_basic_types
330
+
331
+ assert_equal(SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
332
+ assert_equal(
333
+ SDL4R::SdlBinary("hi"), root.child("hi").value, BINARY_DECLARATIONS)
334
+ assert_equal(
335
+ SdlBinary.decode64(
336
+ "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAKnRFWHRDcmVhdGlvbiBUaW1l" +
337
+ "AERpIDQgTXJ6IDIwMDMgMDA6MjQ6MDQgKzAxMDDdSQ6OAAAAB3RJTUUH0wMEAAcllPlrJgAA" +
338
+ "AAlwSFlzAAAK8AAACvABQqw0mAAAAARnQU1BAACxjwv8YQUAAADQSURBVHjaY2CgEDCCyZn/" +
339
+ "3YHkDhL1ejCkM+5kgXJ2zDQmXueShwwMh9+ALWSEGcCQfhZIvHlDnAk8PAwMHBxgJtyAa7bX" +
340
+ "UdT8/cvA8Ps3hP7zB4FBYn/+vGbweqyJaoCmpiaKASDFv35BNMBoZMzwGKKOidJYoNgAuBdm" +
341
+ "naXQgHRKDfgagxD89w8S+iAaFICwGIHFAgjrHUczAByySAaAMEgDLBphhv7/D8EYLgDZhAxA" +
342
+ "mkAKYYbAMMwwDAOQXYDuDXRXgDC6AR7SW8jITNQAACjZgdj4VjlqAAAAAElFTkSuQmCC"
343
+ ),
344
+ root.child("png").value,
345
+ BINARY_DECLARATIONS)
346
+ end
347
+
348
+ ######################################
349
+ # Structure Tests (values, attributes, children)
350
+ ######################################
351
+
352
+ def test_empty_tag
353
+ root = @@root_structures
354
+ assert_equal(root.child("empty_tag"), Tag.new("empty_tag"), EMPTY_TAG)
355
+ end
356
+
357
+ def test_values
358
+ root = @@root_structures
359
+ local_offset = DateTime.now.offset
360
+
361
+ assert_equal(root.child("values1").values, ["hi"], VALUES)
362
+ assert_equal(root.child("values2").values, ["hi","ho"], VALUES)
363
+ assert_equal(root.child("values3").values, [1, "ho"], VALUES)
364
+ assert_equal(root.child("values4").values, ["hi",5], VALUES)
365
+ assert_equal(root.child("values5").values, [1,2], VALUES)
366
+ assert_equal(root.child("values6").values, [1,2,3], VALUES)
367
+ assert_equal(
368
+ root.child("values7").values,
369
+ [nil,"foo",false,Date.civil(1980,12,5)],
370
+ VALUES)
371
+ assert_equal(
372
+ root.child("values8").values,
373
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
374
+ "there", SdlTimeSpan.new(0,15,23,12,234)],
375
+ VALUES)
376
+ assert_equal(
377
+ root.child("values9").values,
378
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
379
+ "there", DateTime.civil(1989,8,12,15,23,12234.to_r/1000,"JST")],
380
+ VALUES)
381
+ assert_equal(
382
+ root.child("values10").values,
383
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
384
+ "there", SdlTimeSpan.new(0,15,23,12,234), "more stuff"],
385
+ VALUES)
386
+ assert_equal(
387
+ root.child("values11").values,
388
+ [nil, "foo", false, DateTime.civil(1980,12,5,12,30,0,local_offset),
389
+ "there", SdlTimeSpan.new(123,15,23,12,234), "more stuff here"],
390
+ VALUES)
391
+ assert_equal(root.child("values12").values, [1,3], VALUES)
392
+ assert_equal(root.child("values13").values, [1,3], VALUES)
393
+ assert_equal(root.child("values14").values, [1,3], VALUES)
394
+ assert_equal(root.child("values15").values, [1,2,4,5,6], VALUES)
395
+ assert_equal(root.child("values16").values, [1,2,5], VALUES)
396
+ assert_equal(root.child("values17").values, [1,2,5], VALUES)
397
+ assert_equal(root.child("values18").values, [1,2,7], VALUES)
398
+ assert_equal(root.child("values19").values, [1,3,5,7], VALUES)
399
+ assert_equal(root.child("values20").values, [1,3,5], VALUES)
400
+ assert_equal(root.child("values21").values, [1,3,5], VALUES)
401
+ assert_equal(root.child("values22").values, ["hi","ho","ho",5,"hi"], VALUES)
402
+ end
403
+
404
+ def test_attributes
405
+ root = @@root_structures
406
+
407
+ assert_equal(
408
+ root.child("atts1").attributes, {"name" => "joe"}, ATTRIBUTES)
409
+ assert_equal(root.child("atts2").attributes, {"size" => 5}, ATTRIBUTES)
410
+ assert_equal(
411
+ root.child("atts3").attributes, {"name" => "joe","size" => 5}, ATTRIBUTES)
412
+ assert_equal(
413
+ root.child("atts4").attributes, {"name"=>"joe","size"=>5,"smoker"=>false},
414
+ ATTRIBUTES)
415
+ assert_equal(
416
+ root.child("atts5").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES)
417
+ assert_equal(
418
+ root.child("atts6").attributes, {"name"=>"joe","smoker"=>false}, ATTRIBUTES)
419
+ assert_equal(root.child("atts7").attributes, {"name"=>"joe"}, ATTRIBUTES)
420
+ assert_equal(
421
+ root.child("atts8").attributes,
422
+ {"name"=>"joe","size"=>5,"smoker"=>false,
423
+ "text"=>"hi","birthday"=>Date.civil(1972,5,23)},
424
+ ATTRIBUTES)
425
+ assert_equal(
426
+ root.child("atts9").attribute("key"), SDL4R::SdlBinary("mykey"), ATTRIBUTES)
427
+ end
428
+
429
+ def test_values_and_attributes
430
+ root = @@root_structures
431
+
432
+ assert_equal(root.child("valatts1").values, ["joe"], VALUES_AND_ATTRIBUTES)
433
+ assert_equal(
434
+ root.child("valatts1").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
435
+
436
+ assert_equal(root.child("valatts2").values, ["joe"], VALUES_AND_ATTRIBUTES)
437
+ assert_equal(root.child("valatts2").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
438
+
439
+ assert_equal(root.child("valatts3").values, ["joe"], VALUES_AND_ATTRIBUTES)
440
+ assert_equal(root.child("valatts3").attributes, {"size"=>5}, VALUES_AND_ATTRIBUTES)
441
+
442
+ assert_equal(root.child("valatts4").values, ["joe"], VALUES_AND_ATTRIBUTES)
443
+ assert_equal(
444
+ root.child("valatts4").attributes,
445
+ {"size"=>5, "weight"=>160, "hat"=>"big"},
446
+ VALUES_AND_ATTRIBUTES)
447
+
448
+ assert_equal(
449
+ root.child("valatts5").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
450
+ assert_equal(
451
+ root.child("valatts5").attributes,
452
+ {"size"=>5, "smoker"=>false},
453
+ VALUES_AND_ATTRIBUTES)
454
+
455
+ assert_equal(
456
+ root.child("valatts6").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
457
+ assert_equal(
458
+ root.child("valatts6").attributes,
459
+ {"size"=>5, "house"=>"big and\n blue"},
460
+ VALUES_AND_ATTRIBUTES)
461
+
462
+ #####
463
+
464
+ assert_equal(
465
+ root.child("valatts7").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
466
+ assert_equal(
467
+ root.child("valatts7").attributes,
468
+ {"size"=>5, "smoker"=>false},
469
+ VALUES_AND_ATTRIBUTES)
470
+
471
+ assert_equal(
472
+ root.child("valatts8").values, ["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
473
+ assert_equal(
474
+ root.child("valatts8").attributes,
475
+ {"size"=>5, "smoker"=>false},
476
+ VALUES_AND_ATTRIBUTES)
477
+
478
+ assert_equal(
479
+ root.child("valatts9").values,["joe", "is a\n nice guy"], VALUES_AND_ATTRIBUTES)
480
+ assert_equal(
481
+ root.child("valatts9").attributes,
482
+ {"size"=>5, "smoker"=>false},
483
+ VALUES_AND_ATTRIBUTES)
484
+ end
485
+
486
+ def test_children
487
+ root = @@root_structures
488
+ parent = root.child("parent")
489
+
490
+ assert_equal(parent.children.size, 2, CHILDREN)
491
+ assert_equal(parent.children[1].name, "daughter", CHILDREN)
492
+
493
+ grandparent = root.child("grandparent")
494
+
495
+ assert_equal(grandparent.children.size, 2, CHILDREN)
496
+ # recursive fetch of children
497
+ assert_equal(grandparent.children(true).size, 6, CHILDREN)
498
+ assert_equal(grandparent.children(true, "son").size, 2, CHILDREN)
499
+
500
+ grandparent2 = root.child("grandparent2")
501
+ assert_equal(grandparent2.children(true, "child").size, 5, CHILDREN)
502
+ assert_equal(
503
+ grandparent2.child(true, "daughter").attribute("birthday"),
504
+ Date.civil(1976,04,18),
505
+ CHILDREN)
506
+
507
+ files = root.child("files")
508
+
509
+ assert_equal(
510
+ ["c:/file1.txt", "c:/file2.txt", "c:/folder"],
511
+ files.children_values("content"),
512
+ CHILDREN)
513
+
514
+ matrix = root.child("matrix")
515
+
516
+ assert_equal([[1,2,3],[4,5,6]], matrix.children_values("content"), CHILDREN)
517
+ end
518
+
519
+ def test_namespaces
520
+ root = @@root_structures
521
+ assert_equal(8, root.children(true, "person", nil).size, NAMESPACES)
522
+
523
+ grandparent3 = root.child("grandparent3")
524
+
525
+ # get only the attributes for Akiko in the public namespace
526
+ assert_equal(
527
+ {"name"=>"Akiko", "birthday"=>Date.civil(1976,04,18)},
528
+ grandparent3.child(true, "daughter").attributes("public"),
529
+ NAMESPACES)
530
+ end
531
+
532
+ def test_validate_identifier
533
+ assert_nothing_raised { SDL4R::validate_identifier('myName') }
534
+ assert_nothing_raised { SDL4R::validate_identifier('my-name') }
535
+ assert_nothing_raised { SDL4R::validate_identifier('my_name') }
536
+ assert_nothing_raised { SDL4R::validate_identifier('_my-name') }
537
+ assert_nothing_raised { SDL4R::validate_identifier('my_name_') }
538
+ assert_nothing_raised { SDL4R::validate_identifier('com.ikayzo.foo') }
539
+ assert_nothing_raised { SDL4R::validate_identifier('com.ikayzo.foo$1') }
540
+
541
+ if SDL4R::supports_unicode_identifiers?
542
+ assert_nothing_raised { SDL4R::validate_identifier('まName') }
543
+ assert_nothing_raised { SDL4R::validate_identifier('myNäme') }
544
+ assert_nothing_raised { SDL4R::validate_identifier('なまえ') }
545
+ end
546
+
547
+ assert_raise ArgumentError do SDL4R::validate_identifier('1myName') end
548
+ assert_raise ArgumentError do SDL4R::validate_identifier('$myName') end
549
+ assert_raise ArgumentError do SDL4R::validate_identifier('.myName') end
550
+ assert_raise ArgumentError do SDL4R::validate_identifier('my Name') end
551
+ assert_raise ArgumentError do SDL4R::validate_identifier(' myName') end
552
+ assert_raise ArgumentError do SDL4R::validate_identifier('myName ') end
553
+ end
554
+
555
+ def test_coerce_or_fail
556
+ # Most basic types are considered to be tested in other tests (like ParserTest)
557
+ tag = Tag.new "tag1"
558
+ assert_raise ArgumentError do tag.add_value(Object.new) end
559
+ assert_raise ArgumentError do tag.add_value([1, 2, 3]) end
560
+ assert_raise ArgumentError do tag.add_value({"a" => "b"}) end
561
+
562
+ # check translation of Rational
563
+ tag.add_value(Rational(3, 10))
564
+ assert_equal [0.3], tag.values
565
+ end
566
+
567
+ def test_format_times
568
+ # Using DateTime
569
+ date = DateTime.civil(1989, 11, 13, 14, 15, 37)
570
+ assert_equal "1989/11/13 14:15:37", SDL4R::format(date)
571
+
572
+ date = DateTime.civil(1989, 11, 13, 14, 15, 37, Rational(-5, 24))
573
+ assert_equal "1989/11/13 14:15:37-GMT-05:00", SDL4R::format(date)
574
+
575
+ date = DateTime.civil(1989, 11, 13, 14, 15, 37055.to_r/1000)
576
+ assert_equal "1989/11/13 14:15:37.055", SDL4R::format(date)
577
+
578
+ date = DateTime.civil(1989, 11, 13, 14, 15, 37055.to_r/1000, Rational(6, 24))
579
+ assert_equal "1989/11/13 14:15:37.055-GMT+06:00", SDL4R::format(date)
580
+
581
+ # Using Time
582
+ time = Time.gm(1989, 11, 13, 14, 15, 37)
583
+ assert_equal "1989/11/13 14:15:37", SDL4R::format(time)
584
+
585
+ time = Time.gm(1989, 11, 13, 14, 15, 37, 55000)
586
+ assert_equal "1989/11/13 14:15:37.055", SDL4R::format(time)
587
+ end
588
+
589
+ # Date parsing tests using Time rather than DateTime
590
+ def test_time
591
+ SDL4R::use_datetime = false
592
+ begin
593
+ # no timezone
594
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768")
595
+ assert_kind_of Time, root.child.value
596
+ assert_equal_date_time Time.local(1998, 05, 12, 12, 34, 56, 768000), root.child.value
597
+
598
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-UTC")
599
+ assert_equal_date_time Time.utc(1998, 05, 12, 12, 34, 56, 768000), root.child.value
600
+
601
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-JST")
602
+ assert_equal_date_time Time.xmlschema("1998-05-12T12:34:56.768+09:00"), root.child.value
603
+
604
+ root = SDL4R::read("tag1 1998/05/12 12:34:56.768-GMT-01:00")
605
+ assert_equal_date_time Time.xmlschema("1998-05-12T12:34:56.768-01:00"), root.child.value
606
+
607
+ ensure
608
+ SDL4R::use_datetime = true
609
+ end
610
+ end
611
+ end
529
612
  end