ClsRuby 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. data/LICENSE +26 -0
  2. data/README +55 -0
  3. data/THANKS +0 -0
  4. data/docs/base_formatting_methods +89 -0
  5. data/docs/base_parsing_methods +79 -0
  6. data/docs/constructor_params +131 -0
  7. data/docs/examples/log_single_line_format +3 -0
  8. data/docs/examples/service_description +3 -0
  9. data/docs/examples/sms-hist +3 -0
  10. data/docs/examples/tag_any +3 -0
  11. data/docs/fragments/custom_tag_field.rb +20 -0
  12. data/docs/fragments/custom_tag_include.rb +21 -0
  13. data/docs/fragments/field.cls +2 -0
  14. data/docs/fragments/include.cls +4 -0
  15. data/docs/fragments/inherit_tag_params.rb +21 -0
  16. data/docs/fragments/message.cls +6 -0
  17. data/docs/fragments/tag_field.rb +24 -0
  18. data/docs/fragments/tag_message.rb +74 -0
  19. data/docs/fragments/tags_order.rb +41 -0
  20. data/docs/principles +402 -0
  21. data/docs/std_tags_short_description +278 -0
  22. data/docs/syntax +227 -0
  23. data/docs/why_cls +178 -0
  24. data/examples/hex_stream.txt +1 -0
  25. data/examples/log_single_line_formatter.rb +79 -0
  26. data/examples/service_description.day_time.cfg +11 -0
  27. data/examples/service_description.rb +119 -0
  28. data/examples/sms-hist.rb +164 -0
  29. data/examples/space_concat.txt +3 -0
  30. data/examples/tag_any.rb +28 -0
  31. data/lib/cls-ruby/basic_scalars.rb +270 -0
  32. data/lib/cls-ruby/constraints/one_of.rb +36 -0
  33. data/lib/cls-ruby/default_formatter.rb +50 -0
  34. data/lib/cls-ruby/ex.rb +121 -0
  35. data/lib/cls-ruby/formatter.rb +31 -0
  36. data/lib/cls-ruby/lexers/char_classifier.rb +157 -0
  37. data/lib/cls-ruby/lexers/first_stage.rb +112 -0
  38. data/lib/cls-ruby/lexers/lexer.rb +74 -0
  39. data/lib/cls-ruby/oneline_formatter.rb +35 -0
  40. data/lib/cls-ruby/parser.rb +249 -0
  41. data/lib/cls-ruby/tag.rb +428 -0
  42. data/lib/cls-ruby/tag_any.rb +111 -0
  43. data/lib/cls-ruby/tag_no_value.rb +55 -0
  44. data/lib/cls-ruby/tag_scalar.rb +197 -0
  45. data/lib/cls-ruby/tag_scalar_helpers.rb +70 -0
  46. data/lib/cls-ruby/tag_scalar_vector.rb +148 -0
  47. data/lib/cls-ruby/tag_vector_of_different_tags.rb +172 -0
  48. data/lib/cls-ruby/tag_vector_of_tags.rb +116 -0
  49. data/lib/cls-ruby/vector_of_tags_impl.rb +129 -0
  50. data/lib/cls-ruby.rb +5 -0
  51. data/tests/tc_child_tag.rb +51 -0
  52. data/tests/tc_constraint_one_of.rb +47 -0
  53. data/tests/tc_format_helper.rb +27 -0
  54. data/tests/tc_formatters.rb +125 -0
  55. data/tests/tc_lexer.rb +124 -0
  56. data/tests/tc_lexer_char_classifier.rb +121 -0
  57. data/tests/tc_lexer_first_stage.rb +72 -0
  58. data/tests/tc_parser_simple.rb +93 -0
  59. data/tests/tc_scalar_parsers.rb +189 -0
  60. data/tests/tc_tag.rb +68 -0
  61. data/tests/tc_tag_no_value.rb +46 -0
  62. data/tests/tc_tag_scalar_int.rb +83 -0
  63. data/tests/tc_tag_scalar_nonspace_string.rb +46 -0
  64. data/tests/tc_tag_scalar_string.rb +47 -0
  65. data/tests/tc_tag_scalar_vector_int.rb +88 -0
  66. data/tests/tc_tag_scalar_vector_string.rb +48 -0
  67. data/tests/tc_tag_scalar_vector_string_empty.rb +40 -0
  68. data/tests/tc_tag_vector_of_different_tags.rb +109 -0
  69. data/tests/tc_tag_vector_of_tags.rb +103 -0
  70. data/tests/ts_cls_ruby.rb +7 -0
  71. metadata +140 -0
@@ -0,0 +1,121 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+
3
+ require 'stringio'
4
+ require 'test/unit'
5
+
6
+ require 'cls-ruby/lexers/char_classifier'
7
+
8
+ class TC_LexerCharClassifier < Test::Unit::TestCase
9
+ EOF = ClsRuby::Lexers::CharClassifier::EOF
10
+
11
+ def test_empty
12
+ parse_and_compare( "", [] )
13
+ end
14
+
15
+ def test_simple
16
+ parse_and_compare( 'abc',
17
+ [
18
+ [ 'a', :tok_nonspace ],
19
+ [ 'b', :tok_nonspace ],
20
+ [ 'c', :tok_nonspace ]
21
+ ] )
22
+ end
23
+
24
+ def test_comments
25
+ parse_and_compare( '||',
26
+ [ [ '||', :tok_space ] ] )
27
+ parse_and_compare( "||abc",
28
+ [ [ '||abc', :tok_space ] ] )
29
+ parse_and_compare( "||abc\n1 2",
30
+ [ [ "||abc\n", :tok_space ],
31
+ [ '1', :tok_nonspace ],
32
+ [ ' ', :tok_space ],
33
+ [ '2', :tok_nonspace ]
34
+ ] )
35
+
36
+ parse_and_compare( "|##|",
37
+ [ [ '|##|', :tok_space ] ] )
38
+ parse_and_compare( "|###|",
39
+ [ [ '|###|', :tok_space ] ] )
40
+ parse_and_compare( "|#\n#|",
41
+ [ [ "|#\n#|", :tok_space ] ] )
42
+ parse_and_compare( "|######\n\t######|",
43
+ [ [ "|######\n\t######|", :tok_space ] ] )
44
+ parse_and_compare( "|#\\n#|",
45
+ [ [ "|#\\n#|", :tok_space ] ] )
46
+ parse_and_compare( "|#\\x0A#|",
47
+ [ [ "|#\n#|", :tok_space ] ] )
48
+ end
49
+
50
+ def test_escapes
51
+ parse_and_compare(
52
+ '\\\\ \\{ \\} \\| \\n \\t \\r { }',
53
+ [ [ '\\', :tok_nonspace ],
54
+ [ ' ', :tok_space ],
55
+ [ '{', :tok_nonspace ],
56
+ [ ' ', :tok_space ],
57
+ [ '}', :tok_nonspace ],
58
+ [ ' ', :tok_space ],
59
+ [ '|', :tok_nonspace ],
60
+ [ ' ', :tok_space ],
61
+ [ "\n", :tok_space ],
62
+ [ ' ', :tok_space ],
63
+ [ "\t", :tok_space ],
64
+ [ ' ', :tok_space ],
65
+ [ "\r", :tok_space ],
66
+ [ ' ', :tok_space ],
67
+ [ '{', :tok_open_block ],
68
+ [ ' ', :tok_space ],
69
+ [ '}', :tok_close_block ] ] )
70
+ end
71
+
72
+ def test_strings
73
+ parse_and_compare( '""',
74
+ [ [ '', :tok_string ] ] )
75
+
76
+ parse_and_compare( '"abc"',
77
+ [ [ 'abc', :tok_string ] ] )
78
+
79
+ parse_and_compare(
80
+ '"ab|cd"' + '"ab\\|cd"' + '"a{b}"' + '"a\\{b\\}"' +
81
+ '"a\"b"',
82
+ [ [ 'ab|cd', :tok_string ],
83
+ [ 'ab|cd', :tok_string ],
84
+ [ 'a{b}', :tok_string ],
85
+ [ 'a{b}', :tok_string ],
86
+ [ 'a"b', :tok_string ] ] )
87
+ end
88
+
89
+ def test_exceptions
90
+ assert_raise( ClsRuby::InvalidVerticalBarSeqEx ) do
91
+ parse_string( "|-" )
92
+ end
93
+ assert_raise( ClsRuby::UnclosedVerticalBarSeqEx ) do
94
+ parse_string( "|" )
95
+ end
96
+ assert_raise( ClsRuby::UnclosedMultilineCommentEx ) do
97
+ parse_string( "|# # #" )
98
+ end
99
+
100
+ assert_raise( ClsRuby::InvalidEscapeSeqEx ) do
101
+ parse_string( "\\d" )
102
+ end
103
+ end
104
+
105
+ private
106
+ def parse_and_compare( source, expected )
107
+ result = parse_string( source )
108
+ assert_equal( expected, result )
109
+ end
110
+
111
+ def parse_string( string )
112
+ result = []
113
+ lexer = ClsRuby::Lexers::CharClassifier.new( StringIO.new( string ), '-' )
114
+ while EOF != ( n = lexer.next )
115
+ result << n
116
+ end
117
+
118
+ result
119
+ end
120
+ end
121
+
@@ -0,0 +1,72 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+
3
+ require 'stringio'
4
+ require 'test/unit'
5
+
6
+ require 'cls-ruby/lexers/first_stage'
7
+
8
+ class TC_LexerFirstStage < Test::Unit::TestCase
9
+ def test_empty
10
+ parse_and_compare( "", "" )
11
+ end
12
+
13
+ def test_simple
14
+ parse_and_compare( 'abc', 'abc' )
15
+ end
16
+
17
+ def test_non_digital_escapes
18
+ source = '\\\\ \n \r \t \| \{ \}'
19
+ parse_and_compare( source, source )
20
+ end
21
+
22
+ def test_hex_escapes
23
+ parse_and_compare(
24
+ '\\xaa \\xAA \\Xaa \\XAA \\xA \\x0 \\xAAA',
25
+ "\xaa \xAA \xaa \xAA \xa \x0 \xAAA" )
26
+ end
27
+
28
+ def test_oct_escapes
29
+ parse_and_compare(
30
+ '\\o123 \\O123 \\o12 \\O12 \\o1 \\O1 \\o1234',
31
+ "\123 \123 \12 \12 \1 \1 \1234" )
32
+ end
33
+
34
+ def test_bin_escapes
35
+ parse_and_compare(
36
+ '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011',
37
+ "\x11 \x11 \x1 \x1 \x111" )
38
+ end
39
+
40
+ def test_unclosed_escape_seq
41
+ assert_raise( ClsRuby::UnclosedEscapeSeqEx ) do
42
+ parse_string( "\\" )
43
+ end
44
+ end
45
+
46
+ def test_empty_digital_escape_seq
47
+ assert_raise( ClsRuby::UnclosedEscapeSeqEx ) do
48
+ parse_string( "\\x" )
49
+ end
50
+
51
+ assert_raise( ClsRuby::UnclosedEscapeSeqEx ) do
52
+ parse_string( "\\x----" )
53
+ end
54
+ end
55
+
56
+ private
57
+ def parse_and_compare( source, expected )
58
+ result = parse_string( source )
59
+ assert_equal( expected, result )
60
+ end
61
+
62
+ def parse_string( string )
63
+ result = ''
64
+ lexer = ClsRuby::Lexers::FirstStage.new( StringIO.new( string ), '-' )
65
+ while n = lexer.next
66
+ result << n
67
+ end
68
+
69
+ result
70
+ end
71
+ end
72
+
@@ -0,0 +1,93 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+
3
+ require 'stringio'
4
+ require 'test/unit'
5
+
6
+ require 'cls-ruby/parser'
7
+
8
+ class TC_ParserSimple < Test::Unit::TestCase
9
+ def test_empty
10
+ assert_nothing_raised do ClsRuby.parse_string( '' ) end
11
+ end
12
+
13
+ def test_empty_with_one_optional_tag
14
+ assert_nothing_raised do
15
+ ClsRuby.parse_string(
16
+ '',
17
+ ClsRuby::Tag.new( :name => 'sample' ) )
18
+ end
19
+ end
20
+
21
+ def test_empty_with_one_mandatory_tag
22
+ assert_raise( ClsRuby::ParsingErrorEx ) do
23
+ ClsRuby.parse_string( '',
24
+ ClsRuby::Tag.new(
25
+ :name => 'sample',
26
+ :mandatory => true ) )
27
+ end
28
+ end
29
+
30
+ def test_one_tag
31
+ assert_nothing_raised do
32
+ ClsRuby.parse_string(
33
+ ' {sample} ',
34
+ ClsRuby::Tag.new(
35
+ :name => 'sample',
36
+ :mandatory => true ) )
37
+ end
38
+ end
39
+
40
+ def test_string_at_top_level
41
+ assert_raise( ClsRuby::ParsingErrorEx ) do
42
+ ClsRuby.parse_string( ' "ssnns" ' )
43
+ end
44
+ end
45
+
46
+ def test_nonspace_at_top_level
47
+ assert_raise( ClsRuby::ParsingErrorEx ) do
48
+ ClsRuby.parse_string( ' ssnns ' )
49
+ end
50
+ end
51
+
52
+ def test_nested_tag_when_disabled
53
+ assert_raise( ClsRuby::ParsingErrorEx ) do
54
+ ClsRuby.parse_string( '{sample {some-tag}}',
55
+ ClsRuby::Tag.new(
56
+ :name => 'sample',
57
+ :mandatory => true ) )
58
+ end
59
+ end
60
+
61
+ def test_nested_tags_when_enabled
62
+ parent = ClsRuby::Tag.new( :name => 'parent' )
63
+ first = ClsRuby::Tag.new( :name => 'first', :owner => parent )
64
+ second = ClsRuby::Tag.new( :name => 'second', :owner => parent )
65
+
66
+ assert_nothing_raised do
67
+ ClsRuby.parse_string( '{parent {first} {second}}', parent )
68
+ end
69
+
70
+ assert_equal( true, parent.tag_defined? )
71
+ assert_equal( true, first.tag_defined? )
72
+ assert_equal( true, second.tag_defined? )
73
+ end
74
+
75
+ def test_unclosed_tag
76
+ assert_raise( ClsRuby::ParsingErrorEx ) do
77
+ ClsRuby.parse_string( '{sample',
78
+ ClsRuby::Tag.new(
79
+ :name => 'sample',
80
+ :mandatory => true ) )
81
+ end
82
+ end
83
+
84
+ def test_to_many_closing_tags
85
+ assert_raise( ClsRuby::ParsingErrorEx ) do
86
+ ClsRuby.parse_string( '{sample}}',
87
+ ClsRuby::Tag.new(
88
+ :name => 'sample',
89
+ :mandatory => true ) )
90
+ end
91
+ end
92
+ end
93
+
@@ -0,0 +1,189 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+
3
+ require 'test/unit'
4
+
5
+ require 'cls-ruby/basic_scalars'
6
+
7
+ class TC_IntegerScalars < Test::Unit::TestCase
8
+ def initialize( *args )
9
+ super( *args )
10
+
11
+ @format = ClsRuby::SCALAR_INT
12
+ end
13
+
14
+ def test_zero
15
+ assert_equal( 0, @format.on_tok_nonspace( '0' ) )
16
+ assert_equal( 0, @format.on_tok_nonspace( '00' ) )
17
+ assert_equal( 0, @format.on_tok_nonspace( '+0' ) )
18
+ assert_equal( 0, @format.on_tok_nonspace( '-0' ) )
19
+ end
20
+
21
+ def test_sign
22
+ assert_equal( 1, @format.on_tok_nonspace( '1' ) )
23
+ assert_equal( -1, @format.on_tok_nonspace( '-1') )
24
+ assert_equal( +1, @format.on_tok_nonspace( '+1') )
25
+ end
26
+
27
+ def test_decimal
28
+ data = { '1' => 1, '10' => 10, '01' => 1,
29
+ '010' => 10, '11' => 11, '110' => 110 }
30
+
31
+ data.each do |k, v|
32
+ assert_equal( v, @format.on_tok_nonspace( k ), "parsing #{k}" )
33
+ assert_equal( -v, @format.on_tok_nonspace( '-' + k ), "parsing -#{k}" )
34
+ assert_equal( v, @format.on_tok_nonspace( k.upcase ),
35
+ "parsing #{k.upcase}" )
36
+ assert_equal( -v, @format.on_tok_nonspace( '-' + k.upcase ),
37
+ "parsing -#{k.upcase}" )
38
+ end
39
+ end
40
+
41
+ def test_binary
42
+ data = { '0b0' => 0, '0b00' => 0, '0b01' => 1,
43
+ '0b10' => 2, '0b11' => 3, '0b110' => 6 }
44
+
45
+ data.each do |k, v|
46
+ assert_equal( v, @format.on_tok_nonspace( k ), "parsing #{k}" )
47
+ assert_equal( v, @format.on_tok_nonspace( k.upcase ),
48
+ "parsing #{k.upcase}" )
49
+ end
50
+ end
51
+
52
+ def test_octal
53
+ data = { '0o0' => 0, '0o00' => 0, '0o01' => 1,
54
+ '0o10' => 8, '0o11' => 9, '0o110' => 72 }
55
+
56
+ data.each do |k, v|
57
+ assert_equal( v, @format.on_tok_nonspace( k ), "parsing #{k}" )
58
+ assert_equal( v, @format.on_tok_nonspace( k.upcase ),
59
+ "parsing #{k.upcase}" )
60
+ end
61
+ end
62
+
63
+ def test_hex
64
+ data = { '0x0' => 0, '0x00' => 0, '0x01' => 1,
65
+ '0x10' => 16, '0x11' => 17, '0x110' => 272 }
66
+
67
+ data.each do |k, v|
68
+ assert_equal( v, @format.on_tok_nonspace( k ), "parsing #{k}" )
69
+ assert_equal( v, @format.on_tok_nonspace( k.upcase ),
70
+ "parsing #{k.upcase}" )
71
+ end
72
+ end
73
+
74
+ def test_failures
75
+ data = [ '0b', '0o', '0x', '0x1G', '0o778' ]
76
+
77
+ data.each do |v|
78
+ assert_raise( ClsRuby::InvalidValueEx, "parsing #{v}" ) do
79
+ @format.on_tok_nonspace( v )
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ class TC_UnsignedIntegerScalars < Test::Unit::TestCase
86
+ def initialize( *args )
87
+ super( *args )
88
+
89
+ @format = ClsRuby::SCALAR_UINT
90
+ end
91
+
92
+ def test_zero
93
+ assert_equal( 0, @format.on_tok_nonspace( '0' ) )
94
+ assert_equal( 0, @format.on_tok_nonspace( '00' ) )
95
+ end
96
+
97
+ def test_decimal
98
+ data = { '1' => 1, '10' => 10, '01' => 1,
99
+ '010' => 10, '11' => 11, '110' => 110 }
100
+
101
+ data.each do |k, v|
102
+ assert_equal( v, @format.on_tok_nonspace( k ), "parsing #{k}" )
103
+ assert_equal( v, @format.on_tok_nonspace( k.upcase ),
104
+ "parsing #{k.upcase}" )
105
+ end
106
+ end
107
+ end
108
+
109
+ class TC_FloatingPointScalars < Test::Unit::TestCase
110
+ def initialize( *args )
111
+ super( *args )
112
+
113
+ @format = ClsRuby::SCALAR_FLOAT
114
+ end
115
+
116
+ def test_zero
117
+ values = [ '0', '0.0', '.0', '0.0e0', '.0e0', '0.0e+0', '0.0e-0' ]
118
+
119
+ values.each do |v|
120
+ assert_equal( 0.0, @format.on_tok_nonspace( v ), "parsing #{v}" )
121
+
122
+ n = '-' + v
123
+ assert_equal( 0.0, @format.on_tok_nonspace( n ), "parsing #{n}" )
124
+
125
+ n = '+' + v
126
+ assert_equal( 0.0, @format.on_tok_nonspace( n ), "parsing #{n}" )
127
+ end
128
+ end
129
+
130
+ def test_values
131
+ data = { '45' => 45.0, '4.5e1' => 45.0, '0.45e2' => 45.0,
132
+ '4.5e+1' => 45.0, '0.45e+2' => 45.0, '.45e+2' => 45.0,
133
+ '45e-1' => 4.5, '45.0e-1' => 4.5 }
134
+
135
+ data.each do |k, v|
136
+ assert_equal( v, @format.on_tok_nonspace( k ), "parsing #{k}" )
137
+
138
+ assert_equal( -v, @format.on_tok_nonspace( '-' + k ), "parsing -#{k}" )
139
+ end
140
+ end
141
+ end
142
+
143
+ class TC_TimeXmlSchemaScalars < Test::Unit::TestCase
144
+ def initialize( *args )
145
+ super( *args )
146
+
147
+ @format_no_fraction = ClsRuby::TimeXmlSchemaScalarParser.new
148
+ @format_fraction_3 = ClsRuby::TimeXmlSchemaScalarParser.new( 3 )
149
+ end
150
+
151
+ def test_normal
152
+ time_initializers = [ 2006, 12, 23, 11, 43, 15, 345000 ]
153
+ t = Time.utc( *time_initializers )
154
+
155
+ v_no_fraction = @format_no_fraction.format( t )
156
+ assert_equal( '"2006-12-23T11:43:15Z"', v_no_fraction )
157
+
158
+ v_fraction_3 = @format_fraction_3.format( t )
159
+ assert_equal( '"2006-12-23T11:43:15.345Z"', v_fraction_3 )
160
+
161
+ assert_equal(
162
+ Time.utc( *(time_initializers[ 0, time_initializers.size - 1 ]) ),
163
+ @format_no_fraction.on_tok_string(
164
+ v_no_fraction[ 1, v_no_fraction.size - 2 ] ) )
165
+
166
+ assert_equal(
167
+ t,
168
+ @format_no_fraction.on_tok_string(
169
+ v_fraction_3[ 1, v_fraction_3.size - 2 ] ) )
170
+ assert_equal(
171
+ t,
172
+ @format_fraction_3.on_tok_string(
173
+ v_fraction_3[ 1, v_fraction_3.size - 2 ] ) )
174
+ end
175
+
176
+ def test_localtime
177
+ time_initializers = [ 2006, 12, 23, 11, 43, 15, 345000 ]
178
+ t = Time.utc( *time_initializers ).getlocal
179
+
180
+ v_fraction_3 = @format_fraction_3.format( t )
181
+ assert_equal( '"2006-12-23T11:43:15.345Z"', v_fraction_3 )
182
+
183
+ assert_equal(
184
+ t,
185
+ @format_fraction_3.on_tok_string(
186
+ v_fraction_3[ 1, v_fraction_3.size - 2 ] ) )
187
+ end
188
+ end
189
+
data/tests/tc_tag.rb ADDED
@@ -0,0 +1,68 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+
3
+ require 'test/unit'
4
+
5
+ require 'cls-ruby/tag'
6
+
7
+ class TC_TagDefaultParams < Test::Unit::TestCase
8
+ class First < ClsRuby::Tag
9
+ default_tag_params :name => 'first'
10
+ end
11
+
12
+ class Second < First
13
+ default_tag_params :name => 'second', :mandatory => true
14
+ end
15
+
16
+ class Third < Second
17
+ default_tag_params :mandatory => false
18
+ end
19
+
20
+ class HandleOptParam < ClsRuby::Tag
21
+ attr_reader :value
22
+
23
+ def initialize( params = {} )
24
+ super( params )
25
+
26
+ tag_handle_opt_param( :value ) do |v|
27
+ @value = v
28
+ tag_make_defined
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_first
34
+ t1 = First.new( {} )
35
+ t2 = First.new( :name => 't2' )
36
+
37
+ assert_equal( 'first', t1.tag_name )
38
+ assert_equal( 't2', t2.tag_name )
39
+ end
40
+
41
+ def test_second
42
+ t1 = Second.new( {} )
43
+ t2 = Second.new( :name => 't2', :mandatory => false )
44
+
45
+ assert_equal( 'second', t1.tag_name )
46
+ assert_equal( true, t1.tag_mandatory? )
47
+
48
+ assert_equal( 't2', t2.tag_name )
49
+ assert_equal( false, t2.tag_mandatory? )
50
+ end
51
+
52
+ def test_third
53
+ t = Third.new( {} )
54
+
55
+ assert_equal( 'second', t.tag_name )
56
+ assert_equal( false, t.tag_mandatory? )
57
+ end
58
+
59
+ def test_handle_opt_param
60
+ t1 = HandleOptParam.new
61
+ assert_equal( false, t1.tag_defined? )
62
+
63
+ t2 = HandleOptParam.new( :value => 'some-value' )
64
+ assert_equal( true, t2.tag_defined? )
65
+ assert_equal( 'some-value', t2.value )
66
+ end
67
+ end
68
+
@@ -0,0 +1,46 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+
3
+ require 'test/unit'
4
+
5
+ require 'cls-ruby/parser'
6
+ require 'cls-ruby/tag_no_value'
7
+
8
+ class TC_TagNoValue < Test::Unit::TestCase
9
+ def test_string_token
10
+ tag = ClsRuby::TagNoValue.new( :name => 'sample' )
11
+ assert_raise( ClsRuby::ParsingErrorEx ) do
12
+ ClsRuby::parse_string( '{sample "value"}', tag )
13
+ end
14
+ end
15
+
16
+ def test_nonspace_token
17
+ tag = ClsRuby::TagNoValue.new( :name => 'sample' )
18
+ assert_raise( ClsRuby::ParsingErrorEx ) do
19
+ ClsRuby::parse_string( '{sample value}', tag )
20
+ end
21
+ end
22
+
23
+ def test_no_entries
24
+ tag = ClsRuby::TagNoValue.new( :name => 'sample' )
25
+ assert_nothing_raised do
26
+ ClsRuby::parse_string( ' ', tag )
27
+ end
28
+ assert_equal( false, tag.tag_defined? )
29
+ end
30
+
31
+ def test_multiple_entries_no_once
32
+ tag = ClsRuby::TagNoValue.new( :name => 'sample' )
33
+ assert_nothing_raised do
34
+ ClsRuby::parse_string( '{sample} {sample} {sample}', tag )
35
+ end
36
+ assert_equal( true, tag.tag_defined? )
37
+ end
38
+
39
+ def test_multiple_entries_once
40
+ tag = ClsRuby::TagNoValue.new( :name => 'sample', :once => true )
41
+ assert_raise( ClsRuby::ParsingErrorEx ) do
42
+ ClsRuby::parse_string( '{sample} {sample} {sample}', tag )
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,83 @@
1
+ $:.unshift( File.join( File.dirname( __FILE__ ), '../lib' ) )
2
+ require 'test/unit'
3
+
4
+ require 'cls-ruby/parser'
5
+ require 'cls-ruby/tag_scalar'
6
+ require 'cls-ruby/basic_scalars'
7
+
8
+ class TC_TagScalarInt < Test::Unit::TestCase
9
+ def setup
10
+ @tag = ClsRuby::TagScalar.new :name => 'test',
11
+ :format => ClsRuby::SCALAR_INT,
12
+ :constraint => 1..1000
13
+ end
14
+
15
+ def test_normal
16
+ assert_nothing_raised do
17
+ ClsRuby::parse_string '{test 234}', @tag
18
+ end
19
+
20
+ assert_equal( 234, @tag.value )
21
+ end
22
+
23
+ def test_fetch
24
+ assert_nothing_raised do
25
+ ClsRuby::parse_string '', @tag
26
+ end
27
+
28
+ assert_equal( false, @tag.tag_defined? )
29
+ assert_equal( nil, @tag.value )
30
+ assert_equal( 345, @tag.fetch( 345 ) )
31
+ end
32
+
33
+ def test_no_value
34
+ assert_raise( ClsRuby::ParsingErrorEx ) do
35
+ ClsRuby::parse_string( '{test}', @tag )
36
+ end
37
+ end
38
+
39
+ def test_two_tags
40
+ assert_raise( ClsRuby::ParsingErrorEx ) do
41
+ ClsRuby::parse_string( '{test 1} {test 2}', @tag )
42
+ end
43
+ end
44
+
45
+ def test_two_values
46
+ assert_raise( ClsRuby::ParsingErrorEx ) do
47
+ ClsRuby::parse_string( '{test 1 2}', @tag )
48
+ end
49
+ end
50
+
51
+ def test_string_as_value
52
+ assert_raise( ClsRuby::ParsingErrorEx ) do
53
+ ClsRuby::parse_string( '{test "1"}', @tag )
54
+ end
55
+ end
56
+
57
+ def test_reset
58
+ test_normal
59
+ @tag.tag_reset
60
+ test_normal
61
+ end
62
+
63
+ def test_out_of_range_min
64
+ assert_raise( ClsRuby::ParsingErrorEx ) do
65
+ ClsRuby::parse_string( '{test 0}', @tag )
66
+ end
67
+ end
68
+
69
+ def test_out_of_range_max
70
+ assert_raise( ClsRuby::ParsingErrorEx ) do
71
+ ClsRuby::parse_string( '{test 1001}', @tag )
72
+ end
73
+ end
74
+ end
75
+
76
+ class TC_AutoDefineTagIntScalar < Test::Unit::TestCase
77
+ def test_autodefine
78
+ t = ClsRuby::TagIntScalar.new( :name => 'n', :value => 1 )
79
+ assert_equal( true, t.tag_defined? )
80
+ assert_equal( 1, t.value )
81
+ end
82
+ end
83
+