livetext 0.9.11 → 0.9.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/lib/errors.rb +13 -0
  3. data/lib/formatline.rb +3 -5
  4. data/lib/functions.rb +6 -2
  5. data/lib/helpers.rb +21 -0
  6. data/lib/html.rb +32 -0
  7. data/lib/livetext/importable.rb +2 -0
  8. data/lib/livetext.rb +44 -42
  9. data/lib/parser/general.rb +38 -0
  10. data/lib/parser/import.rb +17 -0
  11. data/lib/parser/mixin.rb +46 -0
  12. data/lib/parser/set.rb +136 -0
  13. data/lib/parser/string.rb +55 -0
  14. data/lib/parser.rb +5 -0
  15. data/lib/processor.rb +22 -16
  16. data/lib/standard.rb +144 -292
  17. data/lib/userapi.rb +2 -1
  18. data/livetext.gemspec +1 -2
  19. data/test/all.rb +3 -0
  20. data/test/formatting.rb +2 -9
  21. data/test/{data → snapshots}/basic_formatting/expected-error.txt +0 -0
  22. data/test/{data → snapshots}/basic_formatting/expected-output.txt +0 -0
  23. data/test/{data → snapshots}/basic_formatting/source.lt3 +0 -0
  24. data/test/{data → snapshots}/block_comment/expected-error.txt +0 -0
  25. data/test/{data → snapshots}/block_comment/expected-output.txt +0 -0
  26. data/test/{data → snapshots}/block_comment/source.lt3 +0 -0
  27. data/test/{data → snapshots}/comments_ignored_1/expected-error.txt +0 -0
  28. data/test/{data → snapshots}/comments_ignored_1/expected-output.txt +0 -0
  29. data/test/{data → snapshots}/comments_ignored_1/source.lt3 +0 -0
  30. data/test/{data → snapshots}/copy_is_raw/expected-error.txt +0 -0
  31. data/test/{data → snapshots}/copy_is_raw/expected-output.txt +0 -0
  32. data/test/{data → snapshots}/copy_is_raw/rawtext.inc +0 -0
  33. data/test/{data → snapshots}/copy_is_raw/source.lt3 +0 -0
  34. data/test/{data → snapshots}/crap +0 -0
  35. data/test/{data → snapshots}/def_method/expected-error.txt +0 -0
  36. data/test/{data → snapshots}/def_method/expected-output.txt +0 -0
  37. data/test/{data → snapshots}/def_method/source.lt3 +0 -0
  38. data/test/{data/error_inc_line_num/expected-err-line1match.txt → snapshots/error_inc_line_num/actual-error.txt} +0 -0
  39. data/test/{data/error_inc_line_num/expected-output.txt → snapshots/error_inc_line_num/actual-output.txt} +0 -0
  40. data/test/{data/error_line_num → snapshots/error_inc_line_num}/expected-err-line1match.txt +0 -0
  41. data/test/snapshots/error_inc_line_num/expected-output.txt +13 -0
  42. data/test/{data → snapshots}/error_inc_line_num/file2.lt3 +0 -0
  43. data/test/snapshots/error_inc_line_num/out-sdiff.txt +14 -0
  44. data/test/{data → snapshots}/error_inc_line_num/source.lt3 +0 -0
  45. data/test/snapshots/error_invalid_name/actual-error.txt +10 -0
  46. data/test/{data/error_no_such_mixin/expected-output.txt → snapshots/error_invalid_name/actual-output.txt} +0 -0
  47. data/test/{data → snapshots}/error_invalid_name/expected-err-line1match.txt +0 -0
  48. data/test/{data → snapshots}/error_invalid_name/expected-output.txt +0 -0
  49. data/test/snapshots/error_invalid_name/out-sdiff.txt +6 -0
  50. data/test/{data → snapshots}/error_invalid_name/source.lt3 +0 -0
  51. data/test/snapshots/error_line_num/actual-error.txt +1 -0
  52. data/test/snapshots/error_line_num/actual-output.txt +5 -0
  53. data/test/snapshots/error_line_num/expected-err-line1match.txt +1 -0
  54. data/test/{data → snapshots}/error_line_num/expected-output.txt +0 -0
  55. data/test/snapshots/error_line_num/out-sdiff.txt +6 -0
  56. data/test/{data → snapshots}/error_line_num/source.lt3 +0 -0
  57. data/test/snapshots/error_mismatched_end/actual-error.txt +1 -0
  58. data/test/{data/error_mismatched_end/expected-output.txt → snapshots/error_mismatched_end/actual-output.txt} +0 -0
  59. data/test/{data → snapshots}/error_mismatched_end/expected-err-line1match.txt +0 -0
  60. data/test/snapshots/error_mismatched_end/expected-output.txt +8 -0
  61. data/test/snapshots/error_mismatched_end/out-sdiff.txt +9 -0
  62. data/test/{data → snapshots}/error_mismatched_end/source.lt3 +0 -0
  63. data/test/{data/error_missing_end/expected-err-line1match.txt → snapshots/error_missing_end/actual-error.txt} +0 -0
  64. data/test/{data/error_missing_end/expected-output.txt → snapshots/error_missing_end/actual-output.txt} +0 -0
  65. data/test/snapshots/error_missing_end/expected-err-line1match.txt +1 -0
  66. data/test/snapshots/error_missing_end/expected-output.txt +5 -0
  67. data/test/snapshots/error_missing_end/out-sdiff.txt +6 -0
  68. data/test/{data → snapshots}/error_missing_end/source.lt3 +0 -0
  69. data/test/{data/error_name_not_permitted/expected-output.txt → snapshots/error_name_not_permitted/OLD-exp-out} +0 -0
  70. data/test/{data/example_alpha/expected-error.txt → snapshots/error_name_not_permitted/expected-output.txt} +0 -0
  71. data/test/snapshots/error_name_not_permitted/match-error.txt +1 -0
  72. data/test/{data → snapshots}/error_name_not_permitted/source.lt3 +0 -0
  73. data/test/snapshots/error_no_such_copy/actual-error.txt +10 -0
  74. data/test/{data/example_alpha2/expected-error.txt → snapshots/error_no_such_copy/actual-output.txt} +0 -0
  75. data/test/snapshots/error_no_such_copy/expected-err-line1match.txt +1 -0
  76. data/test/{data → snapshots}/error_no_such_copy/expected-output.txt +0 -5
  77. data/test/snapshots/error_no_such_copy/out-sdiff.txt +5 -0
  78. data/test/{data → snapshots}/error_no_such_copy/source.lt3 +0 -0
  79. data/test/snapshots/error_no_such_inc/actual-error.txt +10 -0
  80. data/test/{data/functions/expected-error.txt → snapshots/error_no_such_inc/actual-output.txt} +0 -0
  81. data/test/snapshots/error_no_such_inc/expected-err-line1match.txt +1 -0
  82. data/test/{data → snapshots}/error_no_such_inc/expected-output.txt +0 -4
  83. data/test/snapshots/error_no_such_inc/out-sdiff.txt +6 -0
  84. data/test/{data → snapshots}/error_no_such_inc/source.lt3 +0 -0
  85. data/test/snapshots/error_no_such_mixin/actual-error.txt +1 -0
  86. data/test/snapshots/error_no_such_mixin/actual-output.txt +11 -0
  87. data/test/{data → snapshots}/error_no_such_mixin/expected-err-line1match.txt +0 -0
  88. data/test/snapshots/error_no_such_mixin/expected-output.txt +5 -0
  89. data/test/snapshots/error_no_such_mixin/out-sdiff.txt +12 -0
  90. data/test/{data → snapshots}/error_no_such_mixin/source.lt3 +0 -0
  91. data/test/{data/hello_world → snapshots/example_alpha}/expected-error.txt +0 -0
  92. data/test/{data → snapshots}/example_alpha/expected-output.txt +0 -0
  93. data/test/{data → snapshots}/example_alpha/source.lt3 +0 -0
  94. data/test/{data/more_complex_vars → snapshots/example_alpha2}/expected-error.txt +0 -0
  95. data/test/{data → snapshots}/example_alpha2/expected-output.txt +0 -0
  96. data/test/{data → snapshots}/example_alpha2/source.lt3 +0 -0
  97. data/test/{data → snapshots}/fixit +0 -0
  98. data/test/{data/predef_vars → snapshots/functions}/expected-error.txt +0 -0
  99. data/test/{data → snapshots}/functions/expected-output.txt +0 -0
  100. data/test/{data → snapshots}/functions/source.lt3 +0 -0
  101. data/test/{data/raw_lines → snapshots/hello_world}/expected-error.txt +0 -0
  102. data/test/{data → snapshots}/hello_world/expected-output.txt +0 -0
  103. data/test/{data → snapshots}/hello_world/source.lt3 +0 -0
  104. data/test/{data → snapshots}/lines.txt +0 -0
  105. data/test/{data/raw_text_block → snapshots/more_complex_vars}/expected-error.txt +0 -0
  106. data/test/{data → snapshots}/more_complex_vars/expected-output.txt +0 -0
  107. data/test/{data → snapshots}/more_complex_vars/source.lt3 +0 -0
  108. data/test/{data/simple_copy/expected-error.txt → snapshots/predef_vars/actual-error.txt} +0 -0
  109. data/test/snapshots/predef_vars/actual-output.txt +6 -0
  110. data/test/{data/simple_include → snapshots/predef_vars}/expected-error.txt +0 -0
  111. data/test/{data → snapshots}/predef_vars/expected-output.txt +1 -1
  112. data/test/snapshots/predef_vars/out-sdiff.txt +7 -0
  113. data/test/{data → snapshots}/predef_vars/source.lt3 +0 -0
  114. data/test/{data/simple_mixin → snapshots/raw_lines}/expected-error.txt +0 -0
  115. data/test/{data → snapshots}/raw_lines/expected-output.txt +0 -0
  116. data/test/{data → snapshots}/raw_lines/source.lt3 +0 -0
  117. data/test/{data/simple_vars → snapshots/raw_text_block}/expected-error.txt +0 -0
  118. data/test/{data → snapshots}/raw_text_block/expected-output.txt +0 -0
  119. data/test/{data → snapshots}/raw_text_block/rawtext.inc +0 -0
  120. data/test/{data → snapshots}/raw_text_block/source.lt3 +0 -0
  121. data/test/{data/single_raw_line → snapshots/simple_copy}/expected-error.txt +0 -0
  122. data/test/{data → snapshots}/simple_copy/expected-output.txt +0 -0
  123. data/test/{data → snapshots}/simple_copy/simplefile.inc +0 -0
  124. data/test/{data → snapshots}/simple_copy/source.lt3 +0 -0
  125. data/test/{data/table_with_heredocs → snapshots/simple_include}/expected-error.txt +0 -0
  126. data/test/{data → snapshots}/simple_include/expected-output.txt +0 -0
  127. data/test/{data → snapshots}/simple_include/simplefile.inc +0 -0
  128. data/test/{data → snapshots}/simple_include/source.lt3 +0 -0
  129. data/test/snapshots/simple_mixin/actual-error.txt +2 -0
  130. data/test/snapshots/simple_mixin/actual-output.txt +4 -0
  131. data/test/{data/subset.txt → snapshots/simple_mixin/expected-error.txt} +0 -0
  132. data/test/{data → snapshots}/simple_mixin/expected-output.txt +0 -0
  133. data/test/snapshots/simple_mixin/out-sdiff.txt +6 -0
  134. data/test/{data → snapshots}/simple_mixin/simple_mixin.rb +0 -0
  135. data/test/{data → snapshots}/simple_mixin/source.lt3 +0 -0
  136. data/test/snapshots/simple_vars/expected-error.txt +0 -0
  137. data/test/{data → snapshots}/simple_vars/expected-output.txt +0 -0
  138. data/test/{data → snapshots}/simple_vars/source.lt3 +0 -0
  139. data/test/snapshots/single_raw_line/expected-error.txt +0 -0
  140. data/test/{data → snapshots}/single_raw_line/expected-output.txt +0 -0
  141. data/test/{data → snapshots}/single_raw_line/source.lt3 +0 -0
  142. data/test/snapshots/subset.txt +0 -0
  143. data/test/snapshots/table_with_heredocs/expected-error.txt +0 -0
  144. data/test/{data → snapshots}/table_with_heredocs/expected-output.txt +0 -0
  145. data/test/{data → snapshots}/table_with_heredocs/source.lt3 +0 -0
  146. data/test/snapshots.rb +219 -0
  147. data/test/testlines.rb +2 -2
  148. data/test/unit/all.rb +3 -0
  149. data/test/unit/html.rb +38 -0
  150. data/test/unit/parse_misc.rb +60 -0
  151. data/test/unit/parse_set.rb +157 -0
  152. data/test/unit/parser/all.rb +3 -0
  153. data/test/unit/parser/general.rb +59 -0
  154. data/test/unit/parser/importable.rb +19 -0
  155. data/test/unit/parser/mixin.rb +19 -0
  156. data/test/unit/parser/set.rb +157 -0
  157. data/test/unit/parser/string.rb +130 -0
  158. data/test/unit/parser.rb +4 -0
  159. data/test/unit/standard.rb +23 -0
  160. data/test/unit/stringparser.rb +140 -0
  161. metadata +152 -99
  162. data/test/data/error_inc_line_num/FOO +0 -21
  163. data/test/data/error_missing_end/ERR +0 -32
  164. data/test/data/error_name_not_permitted/expected-error.txt +0 -1
  165. data/test/data/error_no_such_copy/expected-err-line1match.txt +0 -1
  166. data/test/data/error_no_such_inc/expected-err-line1match.txt +0 -1
  167. data/test/test.rb +0 -140
@@ -0,0 +1,157 @@
1
+
2
+ require 'minitest/autorun'
3
+
4
+ require_relative '../../../lib/parser/set'
5
+
6
+ ParseSet = Livetext::ParseSet
7
+
8
+ class TestParseSet < MiniTest::Test
9
+
10
+ def setup
11
+ end
12
+
13
+ def teardown
14
+ end
15
+
16
+ def test_one_unquoted
17
+ set = ParseSet.new('my_var_123 = 789').parse
18
+ pair = set.first
19
+ assert_equal pair, %w[my_var_123 789]
20
+
21
+ set = ParseSet.new('var_234 = naked_string').parse
22
+ pair = set.first
23
+ assert_equal pair, %w[var_234 naked_string]
24
+ end
25
+
26
+ def test_one_single_quoted
27
+ set = ParseSet.new("fancy.var.name = 'simple string'").parse
28
+ pair = set.first
29
+ assert_equal pair, ["fancy.var.name", "simple string"]
30
+ end
31
+
32
+ def test_one_double_quoted
33
+ set = ParseSet.new('fancy.var2 = "another string"').parse
34
+ pair = set.first
35
+ assert_equal pair, ["fancy.var2", "another string"]
36
+ end
37
+
38
+ def test_multiple_unquoted
39
+ pair1, pair2 = ParseSet.new("this=345, that=678").parse
40
+ assert_equal pair1, %w[this 345]
41
+ assert_equal pair2, %w[that 678]
42
+ end
43
+
44
+ def test_multiple_unquoted_quoted
45
+ pair1, pair2 = ParseSet.new('alpha = 567, beta = "oh well"').parse
46
+ assert_equal pair1, %w[alpha 567]
47
+ assert_equal pair2, ["beta", "oh well"]
48
+ end
49
+
50
+ def test_quote_embedded_comma
51
+ set = ParseSet.new('gamma = "oh, well"').parse
52
+ pair = set.first
53
+ assert_equal pair, ["gamma", "oh, well"]
54
+ end
55
+
56
+ def test_get_var
57
+ @parse = ParseSet.new("foo=345")
58
+ assert_equal @parse.get_var, "foo"
59
+ @parse = ParseSet.new("foo = 345")
60
+ assert_equal @parse.get_var, "foo"
61
+ @parse = ParseSet.new("foo123 = 345")
62
+ assert_equal @parse.get_var, "foo123"
63
+ @parse = ParseSet.new("foo_bar = 345")
64
+ assert_equal @parse.get_var, "foo_bar"
65
+ @parse = ParseSet.new("Foobar = 345")
66
+ assert_equal @parse.get_var, "Foobar"
67
+ @parse = ParseSet.new("_foobar = 345")
68
+ assert_equal @parse.get_var, "_foobar"
69
+
70
+ # will not notice missing equal sign till later parsing
71
+ @parse = ParseSet.new("foo bar")
72
+ assert_equal @parse.get_var, "foo"
73
+
74
+ # can detect missing equal sign if iteration ends
75
+ @parse = ParseSet.new("foo")
76
+ assert_raises(NoEqualSign) { @parse.get_var }
77
+ @parse = ParseSet.new("foo-bar = 345")
78
+ assert_raises(BadVariableName) { @parse.get_var }
79
+ end
80
+
81
+ def test_skip_equal
82
+ @parse = ParseSet.new("=")
83
+ assert_nil @parse.skip_equal
84
+ @parse = ParseSet.new(" = ")
85
+ assert_nil @parse.skip_equal
86
+ @parse = ParseSet.new(" =")
87
+ assert_nil @parse.skip_equal
88
+ @parse = ParseSet.new(" = 345")
89
+ assert_equal @parse.skip_equal, "3"
90
+ @parse = ParseSet.new(" = 'meh'")
91
+ assert_equal @parse.skip_equal, "'"
92
+
93
+ @parse = ParseSet.new("no equal here")
94
+ assert_raises(NoEqualSign) { @parse.skip_equal }
95
+ @parse = ParseSet.new("")
96
+ assert_raises(NoEqualSign) { @parse.skip_equal }
97
+ end
98
+
99
+ def test_quoted_value
100
+ @parse = ParseSet.new(%['this'])
101
+ assert_equal @parse.quoted_value, "this"
102
+ @parse = ParseSet.new(%["that"])
103
+ assert_equal @parse.quoted_value, "that"
104
+ @parse = ParseSet.new(%[""])
105
+ assert_equal @parse.quoted_value, ""
106
+ @parse = ParseSet.new(%[''])
107
+ assert_equal @parse.quoted_value, ""
108
+
109
+ @parse = ParseSet.new(%['foo"])
110
+ assert_raises(BadQuotedString) { @parse.quoted_value }
111
+ @parse = ParseSet.new(%["bar'])
112
+ assert_raises(BadQuotedString) { @parse.quoted_value }
113
+ @parse = ParseSet.new(%['baz])
114
+ assert_raises(BadQuotedString) { @parse.quoted_value }
115
+ @parse = ParseSet.new(%["bam])
116
+ assert_raises(BadQuotedString) { @parse.quoted_value }
117
+ # LATER:
118
+ # - allow (escaped?) comma in quoted string
119
+ end
120
+
121
+ def test_unquoted_value
122
+ # Note: an unquoted value is still a string!
123
+ @parse = ParseSet.new(%[342 ])
124
+ assert_equal @parse.unquoted_value, "342"
125
+ @parse = ParseSet.new(%[343,])
126
+ assert_equal @parse.unquoted_value, "343"
127
+ @parse = ParseSet.new(%[344,678])
128
+ assert_equal @parse.unquoted_value, "344"
129
+ @parse = ParseSet.new(%[345.123])
130
+ assert_equal @parse.unquoted_value, "345.123"
131
+ @parse = ParseSet.new(%[whatever])
132
+ assert_equal @parse.unquoted_value, "whatever"
133
+
134
+ # LATER:
135
+ # - disallow comma in unquoted string
136
+ # - disallow quote trailing unquoted string
137
+ # - allow/disallow escaping??
138
+ end
139
+
140
+ # BUG: FormatLine doesn't know variables in this context!
141
+
142
+ def xtest_4
143
+ set = ParseSet.new("file = $File").parse
144
+ assert_equal set.first, "file"
145
+ assert set.last !~ /undefined/
146
+ end
147
+
148
+ # BUG: ...or functions.
149
+ # (Additional bug: Failing silently seems wrong.)
150
+
151
+ def xtest_5
152
+ set = ParseSet.new("date = $$date").parse
153
+ assert_equal set.first, "date"
154
+ assert set.last =~ /^\d\d.\d\d.\d\d/
155
+ end
156
+
157
+ end
@@ -0,0 +1,130 @@
1
+ require 'minitest/autorun'
2
+
3
+ require_relative '../../../lib/parser/string'
4
+
5
+ class TestStringParser < MiniTest::Test
6
+
7
+ def setup
8
+ # Lengths: zero, one, arbitrary
9
+ @str0 = ""
10
+ @str1 = "x"
11
+ @strN = "This is a test"
12
+ @zero = StringParser.new(@str0)
13
+ @one = StringParser.new(@str1)
14
+ @many = StringParser.new(@strN)
15
+ end
16
+
17
+ def teardown
18
+ # Line and length are invariants
19
+ assert_equal @zero.len, 0
20
+ assert_equal @one.len, 1
21
+ assert_equal @many.len, 14
22
+ end
23
+
24
+ def test_init
25
+ assert_equal @zero.line, ""
26
+ assert @zero.eos, "EOS was initially false for #{@zero.inspect}"
27
+ assert_equal @zero.i, 0
28
+
29
+ assert_equal @one.line, "x"
30
+ refute @one.eos, "EOS was initially true for #{@one.inspect}"
31
+ assert_equal @one.i, 0
32
+
33
+ assert_equal @many.line, "This is a test"
34
+ refute @many.eos, "EOS was initially true for #{@many.inspect}"
35
+ assert_equal @many.i, 0
36
+ end
37
+
38
+ def test_next
39
+ assert_nil @zero.next
40
+ assert_equal @zero.i, 0 # nothing happens
41
+
42
+ assert_equal @one.next, "x"
43
+ assert_equal @one.i, 1
44
+
45
+ assert_equal @many.next, "T"
46
+ refute @many.eos, "EOS was true for #{@many.inspect}"
47
+ assert_equal @many.i, 1
48
+ end
49
+
50
+ def test_eos
51
+ assert @zero.eos?
52
+ refute @one.eos?
53
+ refute @many.eos?
54
+ end
55
+
56
+ def test_next_eos
57
+ @zero.next
58
+ assert @zero.eos?
59
+
60
+ @one.next
61
+ assert @one.eos?
62
+ @one.next
63
+ assert @one.eos?
64
+
65
+ @many.next
66
+ refute @many.eos?
67
+ count = @many.len # doesn't make sense??
68
+ count.times { @many.next }
69
+ assert @many.eos?
70
+ end
71
+
72
+ def test_peek
73
+ assert_nil @zero.peek
74
+ assert_equal @one.peek, @str1[0]
75
+ assert_equal @many.peek, @strN[0]
76
+ end
77
+
78
+ def test_next_peek
79
+ char1 = @zero.next
80
+ char2 = @zero.peek
81
+ assert_nil char1
82
+ assert_nil char2
83
+ assert @zero.i == 0
84
+ assert @zero.last?
85
+ assert @zero.eos?
86
+
87
+ refute @one.last? # FIXME??
88
+ char1 = @one.peek
89
+ refute @one.last? # FIXME??
90
+ char2 = @one.next
91
+ char3 = @one.peek
92
+ assert char1
93
+ assert char2 == char1
94
+ assert char3 == @str1[1]
95
+ assert @one.i == 1
96
+ assert @one.last?
97
+ assert @one.eos? # FIXME??
98
+
99
+ char1 = @many.peek
100
+ char2 = @many.next
101
+ char3 = @many.peek
102
+ assert char1
103
+ assert char2 == char1
104
+ assert char3 == @strN[1]
105
+ assert @many.i == 1
106
+ refute @many.last?
107
+ refute @many.eos?
108
+ end
109
+
110
+ def test_skip_spaces
111
+ none = StringParser.new("abc")
112
+ char, index = none.peek, none.i
113
+ none.skip_spaces
114
+ refute none.peek == " "
115
+ assert_equal none.peek, char
116
+ assert_equal none.i, index
117
+
118
+ one = StringParser.new(" def")
119
+ one.skip_spaces
120
+ refute one.peek == " "
121
+ assert_equal one.peek, "d"
122
+ assert_equal one.i, 1
123
+
124
+ some = StringParser.new(" xyz")
125
+ some.skip_spaces
126
+ refute some.peek == " "
127
+ assert_equal some.peek, "x"
128
+ assert_equal some.i, 3
129
+ end
130
+ end
@@ -0,0 +1,4 @@
1
+
2
+ require_relative 'parser/string'
3
+ require_relative 'parser/set'
4
+ require_relative 'parser/general'
@@ -0,0 +1,23 @@
1
+ require 'minitest/autorun'
2
+
3
+
4
+ require_relative '../../lib/livetext'
5
+
6
+ class TestingLivetext < MiniTest::Test
7
+ include Livetext::Standard
8
+ include Livetext::UserAPI
9
+
10
+ # Only method here "really" belongs elsewhere? FIXME
11
+
12
+ def test_onoff
13
+ refute onoff('off'), "Expected onoff('off') to be false"
14
+ assert onoff('on'), "Expected onoff('on') to be true"
15
+ refute onoff('oFf'), "Expected onoff('oFf') to be false"
16
+ assert onoff('oN'), "Expected onoff('oN') to be true"
17
+ assert onoff(nil), "Expected onoff(nil) to be true"
18
+
19
+ assert_raises(ExpectedOnOff, "Should raise ExpectedOnOff") { onoff("") }
20
+ assert_raises(ExpectedOnOff, "Should raise ExpectedOnOff") { onoff(345) }
21
+ end
22
+
23
+ end
@@ -0,0 +1,140 @@
1
+ require 'minitest/autorun'
2
+
3
+ require_relative '../../lib/stringparser'
4
+
5
+ class TestStringParser < MiniTest::Test
6
+
7
+ def setup
8
+ # Lengths: zero, one, arbitrary
9
+ @str0 = ""
10
+ @str1 = "x"
11
+ @strN = "This is a test"
12
+ @zero = StringParser.new(@str0)
13
+ @one = StringParser.new(@str1)
14
+ @many = StringParser.new(@strN)
15
+ end
16
+
17
+ def teardown
18
+ # Line and length are invariants
19
+ assert_equal @zero.len, 0
20
+ assert_equal @one.len, 1
21
+ assert_equal @many.len, 14
22
+ end
23
+
24
+ def test_init
25
+ assert_equal @zero.line, ""
26
+ assert @zero.eos, "EOS was initially false for #{@zero.inspect}"
27
+ assert_equal @zero.i, 0
28
+
29
+ assert_equal @one.line, "x"
30
+ refute @one.eos, "EOS was initially true for #{@one.inspect}"
31
+ assert_equal @one.i, 0
32
+
33
+ assert_equal @many.line, "This is a test"
34
+ refute @many.eos, "EOS was initially true for #{@many.inspect}"
35
+ assert_equal @many.i, 0
36
+ end
37
+
38
+ def test_next
39
+ assert_nil @zero.next
40
+ assert_equal @zero.i, 0 # nothing happens
41
+
42
+ assert_equal @one.next, "x"
43
+ assert_equal @one.i, 1
44
+
45
+ assert_equal @many.next, "T"
46
+ refute @many.eos, "EOS was true for #{@many.inspect}"
47
+ assert_equal @many.i, 1
48
+ end
49
+
50
+ def test_eos
51
+ assert @zero.eos?
52
+ refute @one.eos?
53
+ refute @many.eos?
54
+ end
55
+
56
+ def test_next_eos
57
+ @zero.next
58
+ assert @zero.eos?
59
+
60
+ @one.eos?
61
+ refute @one.eos?
62
+ @one.next
63
+ assert @one.eos?
64
+ @one.next # One beyond the actual end
65
+ assert @one.eos? # Still the end
66
+
67
+ @many.next
68
+ refute @many.eos?
69
+ count = @many.len # doesn't make sense??
70
+ count.times { @many.next }
71
+ assert @many.eos?
72
+ end
73
+
74
+ def test_peek
75
+ assert_nil @zero.peek
76
+ assert_equal @one.peek, @str1[0]
77
+ assert_equal @many.peek, @strN[0]
78
+ end
79
+
80
+ def test_next_peek
81
+ char1 = @zero.next
82
+ char2 = @zero.peek
83
+ assert_nil char1
84
+ assert_nil char2
85
+ assert @zero.i == 0
86
+ assert @zero.last?
87
+ assert @zero.eos?
88
+
89
+ refute @one.last?
90
+ char1 = @one.peek
91
+ refute @one.last?
92
+ char2 = @one.next
93
+ assert @one.last? # One beyond the last
94
+ char3 = @one.peek
95
+ assert char1
96
+ assert char2 == char1
97
+ assert char3 == @str1[1]
98
+ assert @one.i == 1
99
+ assert @one.last?
100
+ assert @one.eos?
101
+
102
+ char1 = @many.peek
103
+ char2 = @many.next
104
+ char3 = @many.peek
105
+ assert char1
106
+ assert char2 == char1
107
+ assert char3 == @strN[1]
108
+ assert @many.i == 1
109
+ refute @many.last?
110
+ refute @many.eos?
111
+ end
112
+
113
+ def test_skip_spaces
114
+ none = StringParser.new("abc")
115
+ char, index = none.peek, none.i
116
+ none.skip_spaces
117
+ refute none.peek == " "
118
+ assert_equal none.peek, char
119
+ assert_equal none.i, index
120
+
121
+ one = StringParser.new(" def")
122
+ one.skip_spaces
123
+ refute one.peek == " "
124
+ assert_equal one.peek, "d"
125
+ assert_equal one.i, 1
126
+
127
+ some = StringParser.new(" xyz")
128
+ some.skip_spaces
129
+ refute some.peek == " "
130
+ assert_equal some.peek, "x"
131
+ assert_equal some.i, 3
132
+ end
133
+
134
+ def test_for_parse_set
135
+ str = StringParser.new('gamma = "oh, well"')
136
+ count = str.len # doesn't make sense??
137
+ count.times { print str.next; }
138
+
139
+ end
140
+ end