livetext 0.9.26 → 0.9.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/lib/livetext/funcall.rb +13 -94
  3. data/lib/livetext/lineparser.rb +165 -31
  4. data/lib/livetext/parser/string.rb +14 -5
  5. data/lib/livetext/skeleton.rb +0 -4
  6. data/lib/livetext/standard.rb +2 -2
  7. data/lib/livetext/userapi.rb +0 -1
  8. data/lib/livetext/version.rb +1 -1
  9. data/lib/livetext.rb +0 -1
  10. data/test/snapshots/basic_formatting/actual-output.txt +8 -8
  11. data/test/snapshots/basic_formatting/out-sdiff.txt +8 -8
  12. data/test/snapshots/functions/actual-error.txt +19 -0
  13. data/test/snapshots/functions/actual-output.txt +0 -0
  14. data/test/snapshots/functions/err-sdiff.txt +20 -0
  15. data/test/snapshots/more_complex_vars/actual-error.txt +0 -0
  16. data/test/snapshots/more_complex_vars/actual-output.txt +4 -0
  17. data/test/snapshots/more_complex_vars/err-sdiff.txt +1 -0
  18. data/test/snapshots/more_complex_vars/out-sdiff.txt +5 -0
  19. data/test/snapshots/more_functions/actual-error.txt +19 -0
  20. data/test/snapshots/more_functions/actual-output.txt +0 -37
  21. data/test/snapshots/more_functions/err-sdiff.txt +19 -0
  22. data/test/snapshots/raw_lines/actual-error.txt +22 -0
  23. data/test/snapshots/raw_lines/actual-output.txt +0 -0
  24. data/test/snapshots/raw_lines/err-sdiff.txt +23 -0
  25. data/test/snapshots/simple_vars/actual-output.txt +2 -2
  26. data/test/snapshots/simple_vars/out-sdiff.txt +2 -2
  27. data/test/snapshots/var_into_func/actual-error.txt +19 -0
  28. data/test/snapshots/var_into_func/actual-output.txt +0 -16
  29. data/test/snapshots/var_into_func/err-sdiff.txt +19 -0
  30. data/test/unit/all.rb +0 -1
  31. data/test/unit/lineparser.rb +62 -353
  32. data/test/unit/new_lineparser.rb +359 -0
  33. data/test/unit/tokenizer.rb +2 -1
  34. metadata +13 -6
  35. data/lib/livetext/formatline.rb +0 -408
  36. data/test/snapshots/more_functions/out-sdiff.txt +0 -38
  37. data/test/testlines.rb +0 -37
  38. data/test/unit/formatline.rb +0 -638
@@ -0,0 +1,359 @@
1
+ require 'minitest/autorun'
2
+
3
+ require_relative 'lib/livetext'
4
+
5
+ class TestingLivetext < MiniTest::Test
6
+
7
+ LineParser = Livetext::LineParser
8
+
9
+ def perform_test(recv, sym, msg, src, exp)
10
+ actual = recv.send(sym, src)
11
+ if exp[0] == "/"
12
+ exp = Regexp.compile(exp[1..-2]) # skip slashes
13
+ $testme = false
14
+ assert_match(exp, actual, msg)
15
+ else
16
+ $testme = false
17
+ assert_equal(exp, actual, msg)
18
+ end
19
+ end
20
+
21
+ def invoke_test(msg, src, exp)
22
+ actual = LineParser.parse!(src)
23
+ if exp[0] == "/"
24
+ exp = Regexp.compile(exp[1..-2]) # skip slashes
25
+ $testme = false
26
+ assert_match(exp, actual, msg)
27
+ else
28
+ $testme = false
29
+ assert_equal(exp, actual, msg)
30
+ end
31
+ end
32
+
33
+ # Some (most) methods were generated via the code
34
+ # seen in the comment at the bottom of this file...
35
+
36
+ def test_simple_string
37
+ parse = LineParser.new("only testing")
38
+ tokens = parse.parse_variables # .tokenize
39
+ assert_equal tokens, [[:str, "only testing"]], "Tokens were: #{tokens.inspect}"
40
+ # expected = "only testing"
41
+ # result = parse.evaluate
42
+ # assert_equal expected, result
43
+ end
44
+
45
+ def test_variable_interpolation
46
+ $testme = true
47
+ parse = LineParser.new("File is $File and user is $User")
48
+ tokens = parse.parse_variables # tokenize
49
+ expected_tokens = [[:str, "File is "],
50
+ [:var, "File"],
51
+ [:str, " and user is "],
52
+ [:var, "User"]]
53
+ assert_equal expected_tokens, tokens
54
+ # result = parse.evaluate
55
+ # expected = "File is [File is undefined] and user is Hal" # FIXME
56
+ # assert_equal expected, result
57
+ $testme = false
58
+ end
59
+
60
+ def test_NEW_var_expansion
61
+ parse = LineParser.new("File is $File and user is $User")
62
+ expected = "File is [File is undefined] and user is Hal" # FIXME
63
+ str = parse.parse_variables
64
+ assert_equal expected, str
65
+ end
66
+
67
+ def test_func_expansion
68
+ parse = LineParser.new("myfunc() results in $$myfunc apparently.")
69
+ tokens = parse.parse_functions # .tokenize
70
+ expected_tokens = [[:str, "myfunc() results in "],
71
+ [:func, "myfunc", nil, nil],
72
+ [:str, " apparently."]]
73
+ assert_equal expected_tokens, tokens
74
+ # result = parse.evaluate
75
+ # expected = "myfunc() results in [Error evaluating $$myfunc()] apparently."
76
+ # assert_equal expected, result
77
+ end
78
+
79
+ # These tests follow this form:
80
+ #
81
+ # def xtest_func_SUFFIX
82
+ # str = "WHATEVER"
83
+ # parse = LineParser.new(str)
84
+ # tokens_expected = [[], [], ...]
85
+ # tokens = parse.tokenize
86
+ # assert_equal tokens_expected, tokens
87
+ # result = parse.evaluate
88
+ # regex_expected = /Today is ....-..-../
89
+ # assert_match regex_expected, result, "Found unexpected: #{result.inspect}"
90
+ # end
91
+
92
+ def test_func_2
93
+ str = "Today is $$date"
94
+ parse = LineParser.new(str)
95
+ tokens_expected = [[:str, "Today is "], [:func, "date", nil, nil]]
96
+ tokens = parse.parse_functions # tokenize
97
+ assert_equal tokens_expected, tokens, "Tokens were: #{tokens.inspect}"
98
+ result = parse.evaluate
99
+ regex_expected = /Today is ....-..-../
100
+ assert_match regex_expected, result, "Found unexpected: #{result.inspect}"
101
+ end
102
+
103
+ def test_var_before_comma
104
+ str = "User name is $User, and all is well"
105
+ parse = LineParser.new(str)
106
+ tokens_expected = [[:str, "User name is "], [:var, "User"], [:str, ", and all is well"]]
107
+ tokens = parse.parse_variables # tokenize
108
+ assert_equal tokens_expected, tokens, "Tokens were: #{tokens.inspect}"
109
+ result = parse.evaluate
110
+ regex_expected = /User name is .*, /
111
+ assert_match regex_expected, result, "Found unexpected: #{result.inspect}"
112
+ end
113
+
114
+ def test_var_at_EOS
115
+ str = "File name is $File"
116
+ parse = LineParser.new(str)
117
+ tokens_expected = [[:str, "File name is "], [:var, "File"]]
118
+ tokens = parse.parse_variables # tokenize
119
+ assert_equal tokens_expected, tokens
120
+ result = parse.evaluate
121
+ string_expected = "File name is [File is undefined]"
122
+ assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
123
+ end
124
+
125
+ def test_var_starts_string
126
+ str = "$File is my file name"
127
+ parse = LineParser.new(str)
128
+ tokens_expected = [[:var, "File"], [:str, " is my file name"]]
129
+ tokens = parse.parse_variables # tokenize
130
+ assert_equal tokens_expected, tokens
131
+ result = parse.evaluate
132
+ string_expected = "[File is undefined] is my file name"
133
+ assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
134
+ end
135
+
136
+ # Next one is/will be a problem...
137
+ # I permit periods *inside* variable names
138
+
139
+ def test_var_before_period
140
+ str = "This is $File\\." # FIXME escaped for now...
141
+ parse = LineParser.new(str)
142
+ tokens_expected = [[:str, "This is "], [:var, "File"], [:str, "."]]
143
+ tokens = parse.tokenize
144
+ assert_equal tokens_expected, tokens
145
+ result = parse.evaluate
146
+ string_expected = "This is [File is undefined]."
147
+ assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
148
+ end
149
+
150
+ def test_func_needing_parameter_colon_eos # colon, param, EOS
151
+ str = "Square root of 225 is $$isqrt:225"
152
+ parse = LineParser.new(str)
153
+ tokens_expected = [[:str, "Square root of 225 is "], [:func, "isqrt", :colon, "225"]]
154
+ tokens = parse.parse_functions # tokenize
155
+ assert_equal tokens_expected, tokens
156
+ # result = parse.evaluate
157
+ # string_expected = "Square root of 225 is 15"
158
+ # assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
159
+ end
160
+
161
+ def test_func_needing_parameter_colon # colon, param, more chars
162
+ str = "Answer is $$isqrt:225 today"
163
+ parse = LineParser.new(str)
164
+ tokens_expected = [[:str, "Answer is "],
165
+ [:func, "isqrt", :colon, "225"],
166
+ [:str, " today"]]
167
+ tokens = parse.parse_functions # tokenize
168
+ assert_equal tokens_expected, tokens
169
+ # result = parse.evaluate
170
+ # string_expected = "Answer is 15 today"
171
+ # assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
172
+ end
173
+
174
+ # isqrt: Not real tests??
175
+
176
+ def test_isqrt_empty_colon_param
177
+ str = "Calculate $$isqrt:"
178
+ parse = LineParser.new(str)
179
+ tokens_expected = [[:str, "Calculate "],
180
+ [:func, "isqrt", :colon, ""]]
181
+ tokens = parse.parse_functions # tokenize
182
+ assert_equal tokens_expected, tokens
183
+ # result = parse.evaluate
184
+ # string_expected = "Calculate [Error evaluating $$isqrt(NO PARAM)]"
185
+ # assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
186
+ end
187
+
188
+ def test_isqrt_empty_bracket_param
189
+ str = "Calculate $$isqrt[]"
190
+ parse = LineParser.new(str)
191
+ tokens_expected = [[:str, "Calculate "],
192
+ [:func, "isqrt", :brackets, ""] # , [:colon, ""]
193
+ ]
194
+ # If param is null, we don't get [:colon, value]!
195
+ # ^ FIXME function should be more like: [:func, name, param]
196
+ tokens = parse.parse_functions # tokenize
197
+ assert_equal tokens_expected, tokens
198
+ # result = parse.evaluate
199
+ # string_expected = "Calculate [Error evaluating $$isqrt(NO PARAM)]"
200
+ # assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
201
+ end
202
+
203
+ def test_isqrt_malformed_number
204
+ str = "Calculate $$isqrt[3a5]"
205
+ parse = LineParser.new(str)
206
+ tokens_expected = [[:str, "Calculate "],
207
+ [:func, "isqrt", :brackets, "3a5"]
208
+ ]
209
+ # ^ FIXME function should be more like: [:func, name, param]
210
+ tokens = parse.parse_functions # tokenize
211
+ assert_equal tokens_expected, tokens
212
+ # result = parse.evaluate
213
+ # string_expected = "Calculate [Error evaluating $$isqrt(3a5)]"
214
+ # assert_equal string_expected, result, "Found unexpected: #{result.inspect}"
215
+ end
216
+
217
+ # ...end of this group
218
+
219
+ def test_func_with_colon
220
+ parse = LineParser.new("Calling $$myfunc:foo here.")
221
+ tokens = parse.parse_functions # tokenize
222
+ assert_equal tokens, [[:str, "Calling "],
223
+ [:func, "myfunc", :colon, "foo"],
224
+ [:str, " here."]]
225
+ # result = parse.evaluate
226
+ # expected = "Calling [Error evaluating $$myfunc(foo)] here."
227
+ # assert_equal expected, result
228
+ end
229
+
230
+ def test_func_with_brackets
231
+ parse = LineParser.new("Calling $$myfunc2[foo bar] here.")
232
+ tokens = parse.parse_functions # .tokenize
233
+ expected_tokens = [[:str, "Calling "],
234
+ [:func, "myfunc2", :brackets, "foo bar"],
235
+ [:str, " here."]]
236
+ assert_equal expected_tokens, tokens
237
+ # result = parse.evaluate
238
+ # expected = "Calling [Error evaluating $$myfunc2(foo bar)] here."
239
+ # assert_equal expected, result
240
+ end
241
+
242
+ def test_parse_formatting
243
+ msg, src, exp = <<~STUFF.split("\n")
244
+ Check simple formatting
245
+ This is *bold and _italics ...
246
+ This is <b>bold</b> and <i>italics</i> ...
247
+ STUFF
248
+ invoke_test(msg, src, exp)
249
+ end
250
+
251
+ def test_formatting_01 # Check output of $$date
252
+ msg, src, exp = <<~STUFF.split("\n")
253
+ Check output of $$date
254
+ Today is $$date, I guess
255
+ /Today is \\d\\d\\d\\d-\\d\\d-\\d\\d, I guess/
256
+ STUFF
257
+ invoke_test(msg, src, exp)
258
+ end
259
+
260
+ def test_formatting_32 # Check "real" dollar signs
261
+ msg, src, exp = <<~STUFF.split("\n")
262
+ Check "real" dollar signs
263
+ You paid $75 for that item.
264
+ You paid $75 for that item.
265
+ STUFF
266
+ invoke_test(msg, src, exp)
267
+ end
268
+
269
+ def test_formatting_33 # Check dollar-space
270
+ msg, src, exp = <<~STUFF.split("\n")
271
+ Check dollar-space
272
+ He paid $ 76 for it...
273
+ He paid $ 76 for it...
274
+ STUFF
275
+ invoke_test(msg, src, exp)
276
+ end
277
+
278
+ def test_formatting_34 # Check escaped dollar signs
279
+ msg, src, exp = <<~STUFF.split("\n")
280
+ Check escaped dollar signs
281
+ Paid \\$78 yo
282
+ Paid $78 yo
283
+ STUFF
284
+ invoke_test(msg, src, exp)
285
+ end
286
+
287
+ def test_formatting_35 # Check ignored function param (bug or feature?)
288
+ msg, src, exp = <<~STUFF.split("\n")
289
+ Check ignored function param (bug or feature?)
290
+ Today is $$date:foobar, apparently.
291
+ /Today is \\d\\d\\d\\d.\\d\\d.\\d\\d apparently./
292
+ STUFF
293
+ invoke_test(msg, src, exp)
294
+ end
295
+
296
+ def test_formatting_36 # Check ignored function bracket param (bug or feature?)
297
+ msg, src, exp = <<~STUFF.split("\n")
298
+ Check ignored function bracket param (bug or feature?)
299
+ Today is $$date[a useless parameter], apparently.
300
+ /Today is \\d\\\d\\d\\d.\\d\\d.\\d\\d, apparently./
301
+ STUFF
302
+ invoke_test(msg, src, exp)
303
+ end
304
+
305
+ end
306
+
307
+ # Test generation logic:
308
+
309
+ =begin
310
+ TestLines = []
311
+
312
+ items = []
313
+ formatting_tests = File.open("test/snapshots/formatting-tests.txt")
314
+ loop do
315
+ 4.times { items << formatting_tests.gets.chomp }
316
+ # Blank line terminates each "stanza"
317
+ raise "Oops? #{items.inspect}" unless items.last.empty?
318
+ TestLines << items
319
+ break if formatting_tests.eof?
320
+ end
321
+
322
+ STDERR.puts <<~RUBY
323
+ require 'minitest/autorun'
324
+
325
+ require_relative '../lib/livetext'
326
+
327
+ # Just another testing class. Chill.
328
+
329
+ class TestingLivetext < MiniTest::Test
330
+ RUBY
331
+
332
+ TestLines.each.with_index do |item, num|
333
+ msg, src, exp, blank = *item
334
+ # generate tests...
335
+ name = "test_formatting_#{'%02d' % (num + 1)}"
336
+ method_source = <<~RUBY
337
+ def #{name} # #{msg}
338
+ msg, src, exp = <<~STUFF.split("\\n")
339
+ #{msg}
340
+ #{src}
341
+ #{exp}
342
+ STUFF
343
+
344
+ actual = LineParser.parse!(src)
345
+ # FIXME could simplify assert logic?
346
+ if exp[0] == "/"
347
+ exp = Regexp.compile(exp[1..-2]) # skip slashes
348
+ assert_match(exp, actual, msg)
349
+ else
350
+ assert_equal(exp, actual, msg)
351
+ end
352
+ end
353
+
354
+ RUBY
355
+ STDERR.puts method_source
356
+ end
357
+ STDERR.puts "\nend"
358
+ end
359
+ =end
@@ -91,6 +91,7 @@ class TestingLivetext < MiniTest::Test
91
91
  # I permit periods *inside* variable names
92
92
 
93
93
  def test_tokenizer_var_before_period
94
+ skip
94
95
  str = "This is $File\\." # FIXME escaped for now...
95
96
  parse = LineParser.new(str)
96
97
  tokens_expected = [[:str, "This is "], [:var, "File"], [:str, "."]]
@@ -503,7 +504,7 @@ class TestingLivetext < MiniTest::Test
503
504
  invoke_test(msg, src, exp)
504
505
  end
505
506
 
506
- def test_tokenizer_formatting_34 # Check escaped dollar signs
507
+ def xtest_tokenizer_formatting_34 # Check escaped dollar signs
507
508
  msg, src, exp = <<~STUFF.split("\n")
508
509
  Check escaped dollar signs
509
510
  Paid \\$78 yo
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: livetext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.26
4
+ version: 0.9.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hal Fulton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-05 00:00:00.000000000 Z
11
+ date: 2022-04-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A smart text processor extensible in Ruby
14
14
  email: rubyhacker@gmail.com
@@ -29,7 +29,6 @@ files:
29
29
  - lib/cmdargs.rb
30
30
  - lib/livetext.rb
31
31
  - lib/livetext/errors.rb
32
- - lib/livetext/formatline.rb
33
32
  - lib/livetext/funcall.rb
34
33
  - lib/livetext/functions.rb
35
34
  - lib/livetext/global_helpers.rb
@@ -117,6 +116,9 @@ files:
117
116
  - test/snapshots/example_alpha2/expected-error.txt
118
117
  - test/snapshots/example_alpha2/expected-output.txt
119
118
  - test/snapshots/example_alpha2/source.lt3
119
+ - test/snapshots/functions/actual-error.txt
120
+ - test/snapshots/functions/actual-output.txt
121
+ - test/snapshots/functions/err-sdiff.txt
120
122
  - test/snapshots/functions/expected-error.txt
121
123
  - test/snapshots/functions/expected-output.txt
122
124
  - test/snapshots/functions/source.lt3
@@ -139,19 +141,25 @@ files:
139
141
  - test/snapshots/mixin_bookish/expected-output.txt
140
142
  - test/snapshots/mixin_bookish/source.lt3
141
143
  - test/snapshots/mixin_bookish/toc.tmp
144
+ - test/snapshots/more_complex_vars/actual-error.txt
145
+ - test/snapshots/more_complex_vars/actual-output.txt
146
+ - test/snapshots/more_complex_vars/err-sdiff.txt
142
147
  - test/snapshots/more_complex_vars/expected-error.txt
143
148
  - test/snapshots/more_complex_vars/expected-output.txt
149
+ - test/snapshots/more_complex_vars/out-sdiff.txt
144
150
  - test/snapshots/more_complex_vars/source.lt3
145
151
  - test/snapshots/more_functions/actual-error.txt
146
152
  - test/snapshots/more_functions/actual-output.txt
147
153
  - test/snapshots/more_functions/err-sdiff.txt
148
154
  - test/snapshots/more_functions/expected-error.txt
149
155
  - test/snapshots/more_functions/expected-output.txt
150
- - test/snapshots/more_functions/out-sdiff.txt
151
156
  - test/snapshots/more_functions/source.lt3
152
157
  - test/snapshots/predef_vars/expected-error.txt
153
158
  - test/snapshots/predef_vars/match-output.txt
154
159
  - test/snapshots/predef_vars/source.lt3
160
+ - test/snapshots/raw_lines/actual-error.txt
161
+ - test/snapshots/raw_lines/actual-output.txt
162
+ - test/snapshots/raw_lines/err-sdiff.txt
155
163
  - test/snapshots/raw_lines/expected-error.txt
156
164
  - test/snapshots/raw_lines/expected-output.txt
157
165
  - test/snapshots/raw_lines/source.lt3
@@ -196,11 +204,10 @@ files:
196
204
  - test/snapshots/var_into_func/expected-output.txt
197
205
  - test/snapshots/var_into_func/out-sdiff.txt
198
206
  - test/snapshots/var_into_func/source.lt3
199
- - test/testlines.rb
200
207
  - test/unit/all.rb
201
- - test/unit/formatline.rb
202
208
  - test/unit/html.rb
203
209
  - test/unit/lineparser.rb
210
+ - test/unit/new_lineparser.rb
204
211
  - test/unit/parser.rb
205
212
  - test/unit/parser/all.rb
206
213
  - test/unit/parser/general.rb