livetext 0.9.27 → 0.9.32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.lt3 +3 -2
  3. data/imports/bookish.rb +1 -1
  4. data/lib/livetext/expansion.rb +108 -0
  5. data/lib/livetext/formatter.rb +223 -0
  6. data/lib/livetext/functions.rb +9 -0
  7. data/lib/livetext/helpers.rb +41 -29
  8. data/lib/livetext/html.rb +73 -0
  9. data/lib/livetext/more.rb +33 -13
  10. data/lib/livetext/parser/general.rb +1 -1
  11. data/lib/livetext/parser/set.rb +10 -3
  12. data/lib/livetext/processor.rb +5 -0
  13. data/lib/livetext/standard.rb +67 -67
  14. data/lib/livetext/userapi.rb +41 -19
  15. data/lib/livetext/version.rb +1 -1
  16. data/lib/livetext.rb +1 -1
  17. data/plugin/bookish.rb +1 -1
  18. data/plugin/bootstrap_menu.rb +140 -0
  19. data/plugin/misc/navbar.rb +162 -0
  20. data/test/extra/README.txt +149 -0
  21. data/test/extra/bracketed.rb +121 -0
  22. data/test/extra/bracketed.txt +44 -0
  23. data/test/extra/double.rb +121 -0
  24. data/test/extra/double.txt +44 -0
  25. data/test/extra/functions.rb +148 -0
  26. data/test/extra/functions.txt +58 -0
  27. data/test/extra/single.rb +139 -0
  28. data/test/extra/single.txt +52 -0
  29. data/test/extra/testgen.rb +104 -0
  30. data/test/extra/variables.rb +94 -0
  31. data/test/extra/variables.txt +35 -0
  32. data/test/snapshots/basic_formatting/expected-output.txt +2 -2
  33. data/test/snapshots/simple_vars/source.lt3 +1 -1
  34. data/test/snapshots/subset.txt +49 -49
  35. data/test/unit/all.rb +1 -2
  36. data/test/unit/parser/general.rb +2 -2
  37. data/test/unit/parser/set.rb +0 -9
  38. metadata +18 -34
  39. data/lib/livetext/funcall.rb +0 -87
  40. data/lib/livetext/lineparser.rb +0 -575
  41. data/test/snapshots/basic_formatting/actual-error.txt +0 -0
  42. data/test/snapshots/basic_formatting/actual-output.txt +0 -13
  43. data/test/snapshots/basic_formatting/err-sdiff.txt +0 -1
  44. data/test/snapshots/basic_formatting/out-sdiff.txt +0 -14
  45. data/test/snapshots/error_inc_line_num/README.txt +0 -20
  46. data/test/snapshots/error_invalid_name/foo +0 -5
  47. data/test/snapshots/functions/actual-error.txt +0 -19
  48. data/test/snapshots/functions/actual-output.txt +0 -0
  49. data/test/snapshots/functions/err-sdiff.txt +0 -20
  50. data/test/snapshots/more_complex_vars/actual-error.txt +0 -0
  51. data/test/snapshots/more_complex_vars/actual-output.txt +0 -4
  52. data/test/snapshots/more_complex_vars/err-sdiff.txt +0 -1
  53. data/test/snapshots/more_complex_vars/out-sdiff.txt +0 -5
  54. data/test/snapshots/more_functions/actual-error.txt +0 -19
  55. data/test/snapshots/more_functions/actual-output.txt +0 -0
  56. data/test/snapshots/more_functions/err-sdiff.txt +0 -20
  57. data/test/snapshots/raw_lines/actual-error.txt +0 -22
  58. data/test/snapshots/raw_lines/actual-output.txt +0 -0
  59. data/test/snapshots/raw_lines/err-sdiff.txt +0 -23
  60. data/test/snapshots/simple_vars/actual-error.txt +0 -0
  61. data/test/snapshots/simple_vars/actual-output.txt +0 -6
  62. data/test/snapshots/simple_vars/err-sdiff.txt +0 -1
  63. data/test/snapshots/simple_vars/out-sdiff.txt +0 -7
  64. data/test/snapshots/var_into_func/actual-error.txt +0 -19
  65. data/test/snapshots/var_into_func/actual-output.txt +0 -0
  66. data/test/snapshots/var_into_func/err-sdiff.txt +0 -20
  67. data/test/snapshots/var_into_func/out-sdiff.txt +0 -17
  68. data/test/unit/lineparser.rb +0 -359
  69. data/test/unit/new_lineparser.rb +0 -359
  70. data/test/unit/tokenizer.rb +0 -535
@@ -0,0 +1,121 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'livetext'
4
+
5
+ # Just another testing class. Chill.
6
+
7
+ class TestingLivetextDouble < MiniTest::Test
8
+
9
+ def setup
10
+ @live = Livetext.new
11
+ end
12
+
13
+ def check_match(exp, actual)
14
+ if exp.is_a? Regexp
15
+ assert_match(exp, actual, "actual does not match expected")
16
+ else
17
+ assert_equal(exp, actual, "actual != expected")
18
+ end
19
+ end
20
+
21
+ def test_double_001_double_marker_plus_end_of_line
22
+ # Double marker plus end of line
23
+ # No special initialization
24
+ src = "**abc"
25
+ exp = "<b>abc</b>"
26
+ actual = @live.api.format(src)
27
+ check_match(exp, actual)
28
+ end
29
+
30
+ def test_double_002_embedded_double_marker_is_ignored
31
+ # Embedded double marker is ignored
32
+ # No special initialization
33
+ src = "abc**d"
34
+ exp = "abc**d"
35
+ actual = @live.api.format(src)
36
+ check_match(exp, actual)
37
+ end
38
+
39
+ def test_double_003_double_marker_at_end_of_line_is_ignored
40
+ # Double marker at end of line is ignored
41
+ # No special initialization
42
+ src = "abc**"
43
+ exp = "abc**"
44
+ actual = @live.api.format(src)
45
+ check_match(exp, actual)
46
+ end
47
+
48
+ def test_double_004_two_valid_double_markers
49
+ # Two valid double markers
50
+ # No special initialization
51
+ src = "**A, **B, C"
52
+ exp = "<b>A</b>, <b>B</b>, C"
53
+ actual = @live.api.format(src)
54
+ check_match(exp, actual)
55
+ end
56
+
57
+ def test_double_005_one_valid_double_marker
58
+ # One valid double marker
59
+ # No special initialization
60
+ src = "Just a **test..."
61
+ exp = "Just a <b>test</b>..."
62
+ actual = @live.api.format(src)
63
+ check_match(exp, actual)
64
+ end
65
+
66
+ def test_double_006_double_marker_by_itself_is_ignored
67
+ # Double marker by itself is ignored
68
+ # No special initialization
69
+ src = " ** "
70
+ exp = " ** "
71
+ actual = @live.api.format(src)
72
+ check_match(exp, actual)
73
+ end
74
+
75
+ def test_double_007_double_marker_terminated_by_comma_period_end_of_line
76
+ # Double marker terminated by comma, period, end of line
77
+ # No special initialization
78
+ src = "**ab, **c. d **e"
79
+ exp = "<b>ab</b>, <b>c</b>. d <b>e</b>"
80
+ actual = @live.api.format(src)
81
+ check_match(exp, actual)
82
+ end
83
+
84
+ def test_double_008_embedded_double_markers_are_ignored
85
+ # Embedded double markers are ignored
86
+ # No special initialization
87
+ src = "x**y**z"
88
+ exp = "x**y**z"
89
+ actual = @live.api.format(src)
90
+ check_match(exp, actual)
91
+ end
92
+
93
+ def test_double_009_double_markers_terminated_by_space_or_end_of_line
94
+ # Double markers terminated by space or end of line
95
+ # No special initialization
96
+ src = "**a **b"
97
+ exp = "<b>a</b> <b>b</b>"
98
+ actual = @live.api.format(src)
99
+ check_match(exp, actual)
100
+ end
101
+
102
+ def test_double_010_single_valid_double_marker
103
+ # Single valid double marker
104
+ # No special initialization
105
+ src = "**A"
106
+ exp = "<b>A</b>"
107
+ actual = @live.api.format(src)
108
+ check_match(exp, actual)
109
+ end
110
+
111
+ def test_double_011_double_marker_by_itself_is_ignored
112
+ # Double marker by itself is ignored
113
+ # No special initialization
114
+ src = "**"
115
+ exp = "**"
116
+ actual = @live.api.format(src)
117
+ check_match(exp, actual)
118
+ end
119
+
120
+
121
+ end
@@ -0,0 +1,44 @@
1
+ Double marker plus end of line
2
+ "**abc"
3
+ "<b>abc</b>"
4
+ -----------------------------------
5
+ Embedded double marker is ignored
6
+ "abc**d"
7
+ "abc**d"
8
+ -----------------------------------
9
+ Double marker at end of line is ignored
10
+ "abc**"
11
+ "abc**"
12
+ -----------------------------------
13
+ Two valid double markers
14
+ "**A, **B, C"
15
+ "<b>A</b>, <b>B</b>, C"
16
+ -----------------------------------
17
+ One valid double marker
18
+ "Just a **test..."
19
+ "Just a <b>test</b>..."
20
+ -----------------------------------
21
+ Double marker by itself is ignored
22
+ " ** "
23
+ " ** "
24
+ -----------------------------------
25
+ Double marker terminated by comma, period, end of line
26
+ "**ab, **c. d **e"
27
+ "<b>ab</b>, <b>c</b>. d <b>e</b>"
28
+ -----------------------------------
29
+ Embedded double markers are ignored
30
+ "x**y**z"
31
+ "x**y**z"
32
+ -----------------------------------
33
+ Double markers terminated by space or end of line
34
+ "**a **b"
35
+ "<b>a</b> <b>b</b>"
36
+ -----------------------------------
37
+ Single valid double marker
38
+ "**A"
39
+ "<b>A</b>"
40
+ -----------------------------------
41
+ Double marker by itself is ignored
42
+ "**"
43
+ "**"
44
+ -----------------------------------
@@ -0,0 +1,148 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'livetext'
4
+
5
+ # Just another testing class. Chill.
6
+
7
+ class TestingLivetextFunctions < MiniTest::Test
8
+
9
+ def setup
10
+ @live = Livetext.new
11
+ end
12
+
13
+ def check_match(exp, actual)
14
+ if exp.is_a? Regexp
15
+ assert_match(exp, actual, "actual does not match expected")
16
+ else
17
+ assert_equal(exp, actual, "actual != expected")
18
+ end
19
+ end
20
+
21
+ def test_functions_001_simple_function_call
22
+ # Simple function call
23
+ # No special initialization
24
+ src = "Today is $$date"
25
+ exp = /Today is [[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}/
26
+ actual = @live.api.format(src)
27
+ check_match(exp, actual)
28
+ end
29
+
30
+ def test_functions_002_function_call_with_colon_parameter
31
+ # Function call with colon parameter
32
+ # No special initialization
33
+ src = "Square root of 225 is $$isqrt:225"
34
+ exp = /is 15$/
35
+ actual = @live.api.format(src)
36
+ check_match(exp, actual)
37
+ end
38
+
39
+ def test_functions_003_function_call_with_empty_colon_parameter
40
+ # Function call with empty colon parameter
41
+ # No special initialization
42
+ src = "Calculate $$isqrt:"
43
+ exp = /Error evaluating/
44
+ actual = @live.api.format(src)
45
+ check_match(exp, actual)
46
+ end
47
+
48
+ def test_functions_004_function_call_with_empty_bracket_parameter
49
+ # Function call with empty bracket parameter
50
+ # No special initialization
51
+ src = "Calculate $$isqrt[]"
52
+ exp = /Error evaluating/
53
+ actual = @live.api.format(src)
54
+ check_match(exp, actual)
55
+ end
56
+
57
+ def test_functions_005_function_detects_invalid_bracket_parameter
58
+ # Function detects invalid bracket parameter
59
+ # No special initialization
60
+ src = "Calculate $$isqrt[3a5]"
61
+ exp = /Error evaluating/
62
+ actual = @live.api.format(src)
63
+ check_match(exp, actual)
64
+ end
65
+
66
+ def test_functions_006_function_call_followed_by_comma
67
+ # Function call followed by comma
68
+ # No special initialization
69
+ src = "Today is $$date, I think"
70
+ exp = /Today is [[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}, I think/
71
+ actual = @live.api.format(src)
72
+ check_match(exp, actual)
73
+ end
74
+
75
+ def test_functions_007_undefined_function
76
+ # Undefined function
77
+ # No special initialization
78
+ src = "I am calling an $$unknown.function here"
79
+ exp = /evaluating/
80
+ actual = @live.api.format(src)
81
+ check_match(exp, actual)
82
+ end
83
+
84
+ def test_functions_008_functions_date_time_pwd
85
+ # Functions date, time, pwd
86
+ # No special initialization
87
+ src = "Today is $$date at $$time, and I am in $$pwd"
88
+ exp = /Today is [[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} at [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}, and I am in (\/[[:alnum:]]+)+\/?/
89
+ actual = @live.api.format(src)
90
+ check_match(exp, actual)
91
+ end
92
+
93
+ def test_functions_009_function_detects_missing_parameter
94
+ # Function detects missing parameter
95
+ # No special initialization
96
+ src = "Here I call $$reverse with no parameters"
97
+ exp = "Here I call (reverse: No parameter) with no parameters"
98
+ actual = @live.api.format(src)
99
+ check_match(exp, actual)
100
+ end
101
+
102
+ def test_functions_010_simple_function_test
103
+ # Simple function test
104
+ # No special initialization
105
+ src = "'animal' spelled backwards is '$$reverse[animal]'"
106
+ exp = "'animal' spelled backwards is 'lamina'"
107
+ actual = @live.api.format(src)
108
+ check_match(exp, actual)
109
+ end
110
+
111
+ def test_functions_011_simple_function_with_colon_parameter
112
+ # Simple function with colon parameter
113
+ # No special initialization
114
+ src = "'lamina' spelled backwards is $$reverse:lamina"
115
+ exp = "'lamina' spelled backwards is animal"
116
+ actual = @live.api.format(src)
117
+ check_match(exp, actual)
118
+ end
119
+
120
+ def test_functions_012_variable_inside_function_bracket_parameter
121
+ # Variable inside function bracket parameter
122
+ @live.api.setvar(:whatever, "some var value")
123
+ src = "$whatever backwards is $$reverse[$whatever]"
124
+ exp = "some var value backwards is eulav rav emos"
125
+ actual = @live.api.format(src)
126
+ check_match(exp, actual)
127
+ end
128
+
129
+ def test_functions_013_function_with_variable_in_colon_param_is_nonhygienic
130
+ # Function with variable in colon param is nonhygienic
131
+ @live.api.setvar(:whatever, "some var value")
132
+ src = "Like non-hygienic macros: $whatever backwards != $$reverse:$whatever"
133
+ exp = "Like non-hygienic macros: some var value backwards != emos var value"
134
+ actual = @live.api.format(src)
135
+ check_match(exp, actual)
136
+ end
137
+
138
+ def test_functions_014_function_call_with_variable_in_bracket_param
139
+ # Function call with variable in bracket param
140
+ # No special initialization
141
+ src = "User $User backwards is $$reverse[$User]"
142
+ exp = /User [[:alnum:]]+ backwards is [[:alnum:]]+$/
143
+ actual = @live.api.format(src)
144
+ check_match(exp, actual)
145
+ end
146
+
147
+
148
+ end
@@ -0,0 +1,58 @@
1
+ Simple function call
2
+ "Today is $$date"
3
+ /Today is RX_DATE/
4
+ -------------------------------------------------
5
+ Function call with colon parameter
6
+ "Square root of 225 is $$isqrt:225"
7
+ /is 15$/
8
+ -------------------------------------------------
9
+ Function call with empty colon parameter
10
+ "Calculate $$isqrt:"
11
+ /Error evaluating/
12
+ -------------------------------------------------
13
+ Function call with empty bracket parameter
14
+ "Calculate $$isqrt[]"
15
+ /Error evaluating/
16
+ -------------------------------------------------
17
+ Function detects invalid bracket parameter
18
+ "Calculate $$isqrt[3a5]"
19
+ /Error evaluating/
20
+ -------------------------------------------------
21
+ Function call followed by comma
22
+ "Today is $$date, I think"
23
+ /Today is RX_DATE, I think/
24
+ -------------------------------------------------
25
+ Undefined function
26
+ "I am calling an $$unknown.function here"
27
+ /evaluating/
28
+ -------------------------------------------------
29
+ Functions date, time, pwd
30
+ "Today is $$date at $$time, and I am in $$pwd"
31
+ /Today is RX_DATE at RX_TIME, and I am in RX_PATH/
32
+ -------------------------------------------------
33
+ Function detects missing parameter
34
+ "Here I call $$reverse with no parameters"
35
+ "Here I call (reverse: No parameter) with no parameters"
36
+ -------------------------------------------------
37
+ Simple function test
38
+ "'animal' spelled backwards is '$$reverse[animal]'"
39
+ "'animal' spelled backwards is 'lamina'"
40
+ -------------------------------------------------
41
+ Simple function with colon parameter
42
+ "'lamina' spelled backwards is $$reverse:lamina"
43
+ "'lamina' spelled backwards is animal"
44
+ -------------------------------------------------
45
+ Variable inside function bracket parameter
46
+ init: @live.api.setvar(:whatever, "some var value")
47
+ "$whatever backwards is $$reverse[$whatever]"
48
+ "some var value backwards is eulav rav emos"
49
+ -------------------------------------------------
50
+ Function with variable in colon param is nonhygienic
51
+ init: @live.api.setvar(:whatever, "some var value")
52
+ "Like non-hygienic macros: $whatever backwards != $$reverse:$whatever"
53
+ "Like non-hygienic macros: some var value backwards != emos var value"
54
+ -------------------------------------------------
55
+ Function call with variable in bracket param
56
+ "User $User backwards is $$reverse[$User]"
57
+ /User RX_USER backwards is RX_USER$/
58
+ -------------------------------------------------
@@ -0,0 +1,139 @@
1
+ require 'minitest/autorun'
2
+
3
+ require 'livetext'
4
+
5
+ # Just another testing class. Chill.
6
+
7
+ class TestingLivetextSingle < MiniTest::Test
8
+
9
+ def setup
10
+ @live = Livetext.new
11
+ end
12
+
13
+ def check_match(exp, actual)
14
+ if exp.is_a? Regexp
15
+ assert_match(exp, actual, "actual does not match expected")
16
+ else
17
+ assert_equal(exp, actual, "actual != expected")
18
+ end
19
+ end
20
+
21
+ def test_single_001_no_marker_at_all
22
+ # No marker at all
23
+ # No special initialization
24
+ src = "abc"
25
+ exp = "abc"
26
+ actual = @live.api.format(src)
27
+ check_match(exp, actual)
28
+ end
29
+
30
+ def test_single_002_single_marker_at_front
31
+ # Single marker at front
32
+ # No special initialization
33
+ src = "*abc"
34
+ exp = "<b>abc</b>"
35
+ actual = @live.api.format(src)
36
+ check_match(exp, actual)
37
+ end
38
+
39
+ def test_single_003_embedded_marker_is_ignored
40
+ # Embedded marker is ignored
41
+ # No special initialization
42
+ src = "abc*d"
43
+ exp = "abc*d"
44
+ actual = @live.api.format(src)
45
+ check_match(exp, actual)
46
+ end
47
+
48
+ def test_single_004_trailing_marker_is_ignored
49
+ # Trailing marker is ignored
50
+ # No special initialization
51
+ src = "abc*"
52
+ exp = "abc*"
53
+ actual = @live.api.format(src)
54
+ check_match(exp, actual)
55
+ end
56
+
57
+ def test_single_005_two_valid_markers
58
+ # Two valid markers
59
+ # No special initialization
60
+ src = "*A *B C"
61
+ exp = "<b>A</b> <b>B</b> C"
62
+ actual = @live.api.format(src)
63
+ check_match(exp, actual)
64
+ end
65
+
66
+ def test_single_006_one_valid_marker
67
+ # One valid marker
68
+ # No special initialization
69
+ src = "Just a little *test here"
70
+ exp = "Just a little <b>test</b> here"
71
+ actual = @live.api.format(src)
72
+ check_match(exp, actual)
73
+ end
74
+
75
+ def test_single_007_marker_surrounded_by_spaces_is_ignored
76
+ # Marker surrounded by spaces is ignored
77
+ # No special initialization
78
+ src = " * "
79
+ exp = " * "
80
+ actual = @live.api.format(src)
81
+ check_match(exp, actual)
82
+ end
83
+
84
+ def test_single_008_multiple_valid_markers
85
+ # Multiple valid markers
86
+ # No special initialization
87
+ src = "*abc *d ef *gh i"
88
+ exp = "<b>abc</b> <b>d</b> ef <b>gh</b> i"
89
+ actual = @live.api.format(src)
90
+ check_match(exp, actual)
91
+ end
92
+
93
+ def test_single_009_valid_markers_are_ignored
94
+ # Valid markers are ignored
95
+ # No special initialization
96
+ src = "x*y*z"
97
+ exp = "x*y*z"
98
+ actual = @live.api.format(src)
99
+ check_match(exp, actual)
100
+ end
101
+
102
+ def test_single_010_valid_markers_at_start_end_of_string
103
+ # Valid markers at start+end of string
104
+ # No special initialization
105
+ src = "*a *b"
106
+ exp = "<b>a</b> <b>b</b>"
107
+ actual = @live.api.format(src)
108
+ check_match(exp, actual)
109
+ end
110
+
111
+ def test_single_011_marker_by_itself_on_line_is_ignored
112
+ # Marker by itself on line is ignored
113
+ # No special initialization
114
+ src = "*"
115
+ exp = "*"
116
+ actual = @live.api.format(src)
117
+ check_match(exp, actual)
118
+ end
119
+
120
+ def test_single_012_marker_at_end_unaffected_by_newline
121
+ # Marker at end unaffected by newline
122
+ # No special initialization
123
+ src = "This is *bold\n"
124
+ exp = "This is <b>bold</b>"
125
+ actual = @live.api.format(src)
126
+ check_match(exp, actual)
127
+ end
128
+
129
+ def test_single_013_escaped_marker_is_ignored
130
+ # Escaped marker is ignored
131
+ # No special initialization
132
+ src = "\\\\*escaped"
133
+ exp = "*escaped"
134
+ actual = @live.api.format(src)
135
+ check_match(exp, actual)
136
+ end
137
+
138
+
139
+ end
@@ -0,0 +1,52 @@
1
+ No marker at all
2
+ "abc"
3
+ "abc"
4
+ ------------------------------------
5
+ Single marker at front
6
+ "*abc"
7
+ "<b>abc</b>"
8
+ ------------------------------------
9
+ Embedded marker is ignored
10
+ "abc*d"
11
+ "abc*d"
12
+ ------------------------------------
13
+ Trailing marker is ignored
14
+ "abc*"
15
+ "abc*"
16
+ ------------------------------------
17
+ Two valid markers
18
+ "*A *B C"
19
+ "<b>A</b> <b>B</b> C"
20
+ ------------------------------------
21
+ One valid marker
22
+ "Just a little *test here"
23
+ "Just a little <b>test</b> here"
24
+ ------------------------------------
25
+ Marker surrounded by spaces is ignored
26
+ " * "
27
+ " * "
28
+ ------------------------------------
29
+ Multiple valid markers
30
+ "*abc *d ef *gh i"
31
+ "<b>abc</b> <b>d</b> ef <b>gh</b> i"
32
+ ------------------------------------
33
+ Valid markers are ignored
34
+ "x*y*z"
35
+ "x*y*z"
36
+ ------------------------------------
37
+ Valid markers at start+end of string
38
+ "*a *b"
39
+ "<b>a</b> <b>b</b>"
40
+ ------------------------------------
41
+ Marker by itself on line is ignored
42
+ "*"
43
+ "*"
44
+ ------------------------------------
45
+ Marker at end unaffected by newline
46
+ "This is *bold\n"
47
+ "This is <b>bold</b>"
48
+ ------------------------------------
49
+ Escaped marker is ignored
50
+ "\\*escaped"
51
+ "*escaped"
52
+ ------------------------------------
@@ -0,0 +1,104 @@
1
+ # In theory, test run could cross midnight...
2
+ RX_DATE = "[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}"
3
+ RX_TIME = "[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}"
4
+ RX_USER = "[[:alnum:]]+"
5
+ RX_PATH = "(\\\/[[:alnum:]]+)+\\\/?"
6
+ RX_VERS = "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$"
7
+
8
+ TestLines = []
9
+
10
+ def fix_regex(str)
11
+ str.gsub!(/RX_DATE/, RX_DATE)
12
+ str.gsub!(/RX_TIME/, RX_TIME)
13
+ str.gsub!(/RX_USER/, RX_USER)
14
+ str.gsub!(/RX_PATH/, RX_PATH)
15
+ str.gsub!(/RX_VERS/, RX_VERS)
16
+ str
17
+ end
18
+
19
+ abort "Need filename.txt" unless ARGV.size == 1
20
+
21
+ filename = ARGV.first
22
+ basename = filename.sub(/\.txt$/, "")
23
+ classname = basename.capitalize
24
+ rubycode = basename + ".rb"
25
+
26
+ puts "Writing: #{rubycode}"
27
+ output = File.new(rubycode, "w")
28
+
29
+ test_stanzas = File.new(filename)
30
+ loop do
31
+ items = []
32
+ loop do
33
+ items << test_stanzas.gets.chomp
34
+ break if items.last =~ /^-----/
35
+ end
36
+
37
+ TestLines << items
38
+ break if test_stanzas.eof?
39
+ end
40
+
41
+ output.puts <<~RUBY
42
+ require 'minitest/autorun'
43
+
44
+ require 'livetext'
45
+
46
+ # Just another testing class. Chill.
47
+
48
+ class TestingLivetext#{classname} < MiniTest::Test
49
+
50
+ def setup
51
+ @live = Livetext.new
52
+ end
53
+
54
+ def check_match(exp, actual)
55
+ if exp.is_a? Regexp
56
+ assert_match(exp, actual, "actual does not match expected")
57
+ else
58
+ assert_equal(exp, actual, "actual != expected")
59
+ end
60
+ end
61
+
62
+ RUBY
63
+
64
+ TestLines.each.with_index do |stanza, num|
65
+ init = nil
66
+ ix = stanza.find_index {|x| x =~ /^init: / }
67
+ if ix
68
+ init = stanza[ix].sub(/^init: /, "")
69
+ stanza.delete_at(ix)
70
+ end
71
+
72
+ desc, src, exp = *stanza
73
+ slug = desc.downcase
74
+ slug.gsub!(/[^[[:alpha:]]]/, "_")
75
+ slug.gsub!(/__/, "_")
76
+ src = src[1..-2] # strip off quotes
77
+ src.gsub!("\\n", "\n")
78
+ exp.gsub!("\\n", "\n")
79
+ if exp[0] == "/"
80
+ exp = fix_regex(exp)
81
+ exp = Regexp.compile(exp[1..-2])
82
+ else
83
+ exp = exp[1..-2]
84
+ end
85
+ init = "# No special initialization" unless init
86
+
87
+ # Generate tests...
88
+ name = "test_#{basename}_#{'%03d' % (num + 1)}_#{slug}"
89
+ method_source = <<~RUBY
90
+ def #{name}
91
+ # #{desc}
92
+ #{init}
93
+ src = #{src.inspect}
94
+ exp = #{exp.inspect}
95
+ actual = @live.api.format(src)
96
+ check_match(exp, actual)
97
+ end
98
+ RUBY
99
+ lines = method_source.split("\n")
100
+ lines.map! {|x| " " + x }
101
+ method_source = lines.join("\n")
102
+ output.puts method_source + "\n "
103
+ end
104
+ output.puts "\nend"