andyw8-seeing_is_believing 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/test.yml +60 -0
  3. data/.gitignore +19 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +2 -0
  6. data/README.md +70 -0
  7. data/Rakefile +88 -0
  8. data/appveyor.yml +32 -0
  9. data/bin/seeing_is_believing +7 -0
  10. data/docs/example.gif +0 -0
  11. data/docs/frog-brown.png +0 -0
  12. data/docs/sib-streaming.gif +0 -0
  13. data/features/deprecated-flags.feature +91 -0
  14. data/features/errors.feature +155 -0
  15. data/features/examples.feature +423 -0
  16. data/features/flags.feature +852 -0
  17. data/features/regression.feature +898 -0
  18. data/features/support/env.rb +102 -0
  19. data/features/xmpfilter-style.feature +471 -0
  20. data/lib/seeing_is_believing/binary/align_chunk.rb +47 -0
  21. data/lib/seeing_is_believing/binary/align_file.rb +24 -0
  22. data/lib/seeing_is_believing/binary/align_line.rb +25 -0
  23. data/lib/seeing_is_believing/binary/annotate_end_of_file.rb +56 -0
  24. data/lib/seeing_is_believing/binary/annotate_every_line.rb +52 -0
  25. data/lib/seeing_is_believing/binary/annotate_marked_lines.rb +179 -0
  26. data/lib/seeing_is_believing/binary/comment_lines.rb +36 -0
  27. data/lib/seeing_is_believing/binary/commentable_lines.rb +126 -0
  28. data/lib/seeing_is_believing/binary/config.rb +455 -0
  29. data/lib/seeing_is_believing/binary/data_structures.rb +58 -0
  30. data/lib/seeing_is_believing/binary/engine.rb +161 -0
  31. data/lib/seeing_is_believing/binary/format_comment.rb +79 -0
  32. data/lib/seeing_is_believing/binary/interline_align.rb +57 -0
  33. data/lib/seeing_is_believing/binary/remove_annotations.rb +113 -0
  34. data/lib/seeing_is_believing/binary/rewrite_comments.rb +62 -0
  35. data/lib/seeing_is_believing/binary.rb +73 -0
  36. data/lib/seeing_is_believing/code.rb +139 -0
  37. data/lib/seeing_is_believing/compatibility.rb +28 -0
  38. data/lib/seeing_is_believing/debugger.rb +32 -0
  39. data/lib/seeing_is_believing/error.rb +17 -0
  40. data/lib/seeing_is_believing/evaluate_by_moving_files.rb +195 -0
  41. data/lib/seeing_is_believing/event_stream/consumer.rb +221 -0
  42. data/lib/seeing_is_believing/event_stream/events.rb +193 -0
  43. data/lib/seeing_is_believing/event_stream/handlers/debug.rb +61 -0
  44. data/lib/seeing_is_believing/event_stream/handlers/record_exit_events.rb +26 -0
  45. data/lib/seeing_is_believing/event_stream/handlers/stream_json_events.rb +23 -0
  46. data/lib/seeing_is_believing/event_stream/handlers/update_result.rb +41 -0
  47. data/lib/seeing_is_believing/event_stream/producer.rb +178 -0
  48. data/lib/seeing_is_believing/hard_core_ensure.rb +58 -0
  49. data/lib/seeing_is_believing/hash_struct.rb +206 -0
  50. data/lib/seeing_is_believing/result.rb +89 -0
  51. data/lib/seeing_is_believing/safe.rb +112 -0
  52. data/lib/seeing_is_believing/swap_files.rb +90 -0
  53. data/lib/seeing_is_believing/the_matrix.rb +97 -0
  54. data/lib/seeing_is_believing/version.rb +3 -0
  55. data/lib/seeing_is_believing/wrap_expressions.rb +265 -0
  56. data/lib/seeing_is_believing/wrap_expressions_with_inspect.rb +19 -0
  57. data/lib/seeing_is_believing.rb +69 -0
  58. data/seeing_is_believing.gemspec +84 -0
  59. data/spec/binary/alignment_specs.rb +27 -0
  60. data/spec/binary/comment_lines_spec.rb +852 -0
  61. data/spec/binary/config_spec.rb +831 -0
  62. data/spec/binary/engine_spec.rb +114 -0
  63. data/spec/binary/format_comment_spec.rb +210 -0
  64. data/spec/binary/marker_spec.rb +71 -0
  65. data/spec/binary/remove_annotations_spec.rb +342 -0
  66. data/spec/binary/rewrite_comments_spec.rb +106 -0
  67. data/spec/code_spec.rb +233 -0
  68. data/spec/debugger_spec.rb +45 -0
  69. data/spec/evaluate_by_moving_files_spec.rb +204 -0
  70. data/spec/event_stream_spec.rb +762 -0
  71. data/spec/hard_core_ensure_spec.rb +120 -0
  72. data/spec/hash_struct_spec.rb +514 -0
  73. data/spec/seeing_is_believing_spec.rb +1094 -0
  74. data/spec/sib_spec_helpers/version.rb +17 -0
  75. data/spec/spec_helper.rb +26 -0
  76. data/spec/spec_helper_spec.rb +16 -0
  77. data/spec/wrap_expressions_spec.rb +1013 -0
  78. metadata +340 -0
@@ -0,0 +1,114 @@
1
+ require 'seeing_is_believing/binary/engine'
2
+ require 'seeing_is_believing/binary/config'
3
+
4
+ class SeeingIsBelieving
5
+ module Binary
6
+ RSpec.describe Engine do
7
+ def call(body, options={})
8
+ timeout = options.fetch :timeout, 0
9
+ filename = options.fetch :filename, "program.rb"
10
+ toggle = options.fetch :toggled_mark, nil
11
+ config = Config.new body: body, timeout_seconds: timeout, toggle_mark: toggle
12
+ config.lib_options.timeout_seconds = timeout
13
+ config.lib_options.filename = filename
14
+ Engine.new config
15
+ end
16
+
17
+ def assert_must_evaluate(message)
18
+ engine = call '1+1'
19
+ expect { engine.__send__ message }.to raise_error MustEvaluateFirst, /#{message}/
20
+ engine.evaluate!
21
+ engine.__send__ message
22
+ end
23
+
24
+ context 'syntax' do
25
+ let(:valid_engine) { call "1+1", filename: "filename.rb" }
26
+ let(:invalid_engine) { call "1+", filename: "filename.rb" }
27
+
28
+ specify 'syntax_error? is true when the body was syntactically invalid' do
29
+ expect( valid_engine.syntax_error?).to eq false
30
+ expect(invalid_engine.syntax_error?).to eq true
31
+ end
32
+
33
+ specify 'syntax_error contains the syntax\'s error message with file and line information' do
34
+ expect(valid_engine.syntax_error).to eq nil
35
+
36
+ allow_any_instance_of(Code::Syntax).to receive(:error_message).and_return "ERR!!"
37
+ allow_any_instance_of(Code::Syntax).to receive(:line_number).and_return 123
38
+ expect(invalid_engine.syntax_error)
39
+ .to eq SyntaxErrorMessage.new(line_number: 123, filename: "filename.rb", explanation: "ERR!!")
40
+ expect(invalid_engine.syntax_error.to_s).to include "ERR!!"
41
+ end
42
+ end
43
+
44
+ context 'cleaned_body' do
45
+ it 'has the annotations removed' do
46
+ expect(call("1 # =>").cleaned_body).to eq "1"
47
+ end
48
+ it 'ends in a newline if the body ended in a newline' do
49
+ expect(call("1").cleaned_body).to eq "1"
50
+ expect(call("1\n").cleaned_body).to eq "1\n"
51
+ end
52
+ end
53
+
54
+ context 'toggled_mark' do
55
+ it 'has the mark toggled and doesn\'t change the newline' do
56
+ expect(call("1", toggled_mark: 1).toggled_mark).to eq "1 # => "
57
+ expect(call("1 # => ", toggled_mark: 1).toggled_mark).to eq "1"
58
+ expect(call("1\n", toggled_mark: 1).toggled_mark).to eq "1 # => \n"
59
+ expect(call("1 # =>\n", toggled_mark: 1).toggled_mark).to eq "1\n"
60
+ end
61
+ end
62
+
63
+ context 'before evaluating it raises if asked for' do
64
+ specify('result') { assert_must_evaluate :result }
65
+ specify('exitstatus') { assert_must_evaluate :exitstatus }
66
+ specify('timed_out?') { assert_must_evaluate :timed_out? }
67
+ specify('timeout_seconds') { assert_must_evaluate :timeout_seconds }
68
+ specify('annotated_body') { assert_must_evaluate :annotated_body }
69
+ end
70
+
71
+ context 'after evaluating' do
72
+ specify 'result is the result of the evaluation' do
73
+ status = call('exit 55').evaluate!.result.exitstatus
74
+ expect(status).to eq 55
75
+ end
76
+
77
+ it 'has recorded the exitstatus' do
78
+ engine = call('exit 88')
79
+ engine.evaluate!
80
+ expect(engine.exitstatus).to eq 88
81
+ end
82
+
83
+ specify 'timed_out? is true if a Timeout event was emitted' do
84
+ expect(call('', timeout: 1).evaluate!.timed_out?).to eq false
85
+ expect(call('sleep 1', timeout: 0.01).evaluate!.timed_out?).to eq true
86
+ end
87
+
88
+ specify 'timeout_seconds is nil, or the timeout duration' do
89
+ expect(call('', timeout: 1).evaluate!.timeout_seconds).to eq nil
90
+ expect(call('sleep 1', timeout: 0.01).evaluate!.timeout_seconds).to eq 0.01
91
+ end
92
+
93
+ context 'annotated_body' do
94
+ it 'is the body after being run through the annotator' do
95
+ expect(call("1").evaluate!.annotated_body).to eq "1 # => 1"
96
+ end
97
+ it 'ends in a newline if the body ended in a newline' do
98
+ expect(call("1").evaluate!.annotated_body).to eq "1 # => 1"
99
+ expect(call("1\n").evaluate!.annotated_body).to eq "1 # => 1\n"
100
+ end
101
+ end
102
+
103
+ it 'evaluation errors are raised up, and it behaves as if it was not evaluated' do
104
+ evaluated = call('1').evaluate!
105
+ unevaluated = call '1'
106
+ expect(SeeingIsBelieving).to receive(:call).exactly(2).times.and_raise(ArgumentError)
107
+ evaluated.evaluate!
108
+ expect { unevaluated.evaluate! }.to raise_error ArgumentError
109
+ expect { unevaluated.evaluate! }.to raise_error ArgumentError
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,210 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'seeing_is_believing/binary/format_comment'
5
+
6
+ RSpec.describe SeeingIsBelieving::Binary::FormatComment do
7
+ def result_for(line_length, separator, result, options={})
8
+ described_class.new(line_length, separator, result, options).call
9
+ end
10
+
11
+ specify 'it returns the consolidated result if there are no truncations' do
12
+ expect(result_for 1, '=>', '12345').to eq '=>12345'
13
+ end
14
+
15
+ specify 'max_result_length truncates a result to the specified length, using elipses up to that length if appropriate' do
16
+ line_length = 1
17
+ separator = '=>'
18
+ result = '12345'
19
+ expect(result_for line_length, separator, result, max_result_length: Float::INFINITY).to eq '=>12345'
20
+ expect(result_for line_length, separator, result, max_result_length: 7 ).to eq '=>12345'
21
+ expect(result_for line_length, separator, result, max_result_length: 6 ).to eq '=>1...'
22
+ expect(result_for line_length+1, separator, result, max_result_length: 6 ).to eq '=>1...'
23
+ expect(result_for line_length, separator, result, max_result_length: 5 ).to eq '=>...'
24
+ expect(result_for line_length, separator, result, max_result_length: 4 ).to eq ''
25
+ expect(result_for line_length, separator, result, max_result_length: 0 ).to eq ''
26
+ end
27
+
28
+ specify 'max_line_length truncates a result to the specified length, minus the length of the line' do
29
+ line_length = 1
30
+ separator = '=>'
31
+ result = '12345'
32
+ expect(result_for line_length, separator, result ).to eq '=>12345'
33
+ expect(result_for line_length, separator, result, max_line_length: Float::INFINITY).to eq '=>12345'
34
+ expect(result_for line_length, separator, result, max_line_length: 8 ).to eq '=>12345'
35
+ expect(result_for line_length, separator, result, max_line_length: 7 ).to eq '=>1...'
36
+ expect(result_for line_length+1, separator, result, max_line_length: 7 ).to eq '=>...'
37
+ expect(result_for line_length, separator, result, max_line_length: 6 ).to eq '=>...'
38
+ expect(result_for line_length, separator, result, max_line_length: 5 ).to eq ''
39
+ expect(result_for line_length, separator, result, max_line_length: 0 ).to eq ''
40
+ end
41
+
42
+ specify 'pad_to will pad the length that the line is displayed in' do
43
+ expect(result_for 1, '=>', '2', pad_to: 0).to eq '=>2'
44
+ expect(result_for 1, '=>', '2', pad_to: 1).to eq '=>2'
45
+ expect(result_for 1, '=>', '2', pad_to: 2).to eq ' =>2'
46
+ expect(result_for 2, '=>', '2', pad_to: 2).to eq '=>2'
47
+ end
48
+
49
+ specify 'pad_to is ignored when separator/result will not be printed' do
50
+ expect(result_for 1, '=>', '12345', pad_to: 2, max_line_length: 2).to eq ''
51
+ expect(result_for 1, '=>', '12345', pad_to: 2, max_result_length: 2).to eq ''
52
+ end
53
+
54
+ specify 'they can all work together' do
55
+ expect(result_for 1, '=>', '12345', max_line_length: 100, max_result_length: 100, pad_to: 2).to eq ' =>12345'
56
+ expect(result_for 1, '=>', '12345', max_line_length: 8, max_result_length: 100, pad_to: 2).to eq ' =>1...'
57
+ expect(result_for 1, '=>', '12345', max_line_length: 100, max_result_length: 6, pad_to: 2).to eq ' =>1...'
58
+ expect(result_for 1, '=>', '12345', max_line_length: 100, max_result_length: 6, pad_to: 2).to eq ' =>1...'
59
+ end
60
+
61
+ def assert_printed(c, printed)
62
+ c = c.force_encoding 'utf-8'
63
+ result = result_for 0, '', c
64
+ expect(result).to eq printed
65
+ expect(result.encoding).to eq Encoding::UTF_8
66
+ expect(result).to be_valid_encoding
67
+ end
68
+
69
+ it 'escapes any non-printable characters' do
70
+ assert_printed '©' , '©'
71
+ assert_printed 0.chr , "\\u0000"
72
+ assert_printed 1.chr , "\\u0001"
73
+ assert_printed 2.chr , "\\u0002"
74
+ assert_printed 3.chr , "\\u0003"
75
+ assert_printed 4.chr , "\\u0004"
76
+ assert_printed 5.chr , "\\u0005"
77
+ assert_printed 6.chr , "\\u0006"
78
+ assert_printed 7.chr , "\\a"
79
+ assert_printed 8.chr , "\\b"
80
+ assert_printed 9.chr , "\\t"
81
+ assert_printed 10.chr , "\\n"
82
+ assert_printed 11.chr , "\\v"
83
+ assert_printed 12.chr , "\\f"
84
+ assert_printed 13.chr , "\\r"
85
+ assert_printed 14.chr , "\\u000E"
86
+ assert_printed 15.chr , "\\u000F"
87
+ assert_printed 16.chr , "\\u0010"
88
+ assert_printed 17.chr , "\\u0011"
89
+ assert_printed 18.chr , "\\u0012"
90
+ assert_printed 19.chr , "\\u0013"
91
+ assert_printed 20.chr , "\\u0014"
92
+ assert_printed 21.chr , "\\u0015"
93
+ assert_printed 22.chr , "\\u0016"
94
+ assert_printed 23.chr , "\\u0017"
95
+ assert_printed 24.chr , "\\u0018"
96
+ assert_printed 25.chr , "\\u0019"
97
+ assert_printed 26.chr , "\\u001A"
98
+ assert_printed 27.chr , "\\e"
99
+ assert_printed 28.chr , "\\u001C"
100
+ assert_printed 29.chr , "\\u001D"
101
+ assert_printed 30.chr , "\\u001E"
102
+ assert_printed 31.chr , "\\u001F"
103
+ assert_printed 32.chr , " "
104
+ assert_printed 33.chr , "!"
105
+ assert_printed 34.chr , '"' # printable, thus not escaped
106
+ assert_printed 35.chr , "#"
107
+ assert_printed 36.chr , "$"
108
+ assert_printed 37.chr , "%"
109
+ assert_printed 38.chr , "&"
110
+ assert_printed 39.chr , "'"
111
+ assert_printed 40.chr , "("
112
+ assert_printed 41.chr , ")"
113
+ assert_printed 42.chr , "*"
114
+ assert_printed 43.chr , "+"
115
+ assert_printed 44.chr , ","
116
+ assert_printed 45.chr , "-"
117
+ assert_printed 46.chr , "."
118
+ assert_printed 47.chr , "/"
119
+ assert_printed 48.chr , "0"
120
+ assert_printed 49.chr , "1"
121
+ assert_printed 50.chr , "2"
122
+ assert_printed 51.chr , "3"
123
+ assert_printed 52.chr , "4"
124
+ assert_printed 53.chr , "5"
125
+ assert_printed 54.chr , "6"
126
+ assert_printed 55.chr , "7"
127
+ assert_printed 56.chr , "8"
128
+ assert_printed 57.chr , "9"
129
+ assert_printed 58.chr , ":"
130
+ assert_printed 59.chr , ";"
131
+ assert_printed 60.chr , "<"
132
+ assert_printed 61.chr , "="
133
+ assert_printed 62.chr , ">"
134
+ assert_printed 63.chr , "?"
135
+ assert_printed 64.chr , "@"
136
+ assert_printed 65.chr , "A"
137
+ assert_printed 66.chr , "B"
138
+ assert_printed 67.chr , "C"
139
+ assert_printed 68.chr , "D"
140
+ assert_printed 69.chr , "E"
141
+ assert_printed 70.chr , "F"
142
+ assert_printed 71.chr , "G"
143
+ assert_printed 72.chr , "H"
144
+ assert_printed 73.chr , "I"
145
+ assert_printed 74.chr , "J"
146
+ assert_printed 75.chr , "K"
147
+ assert_printed 76.chr , "L"
148
+ assert_printed 77.chr , "M"
149
+ assert_printed 78.chr , "N"
150
+ assert_printed 79.chr , "O"
151
+ assert_printed 80.chr , "P"
152
+ assert_printed 81.chr , "Q"
153
+ assert_printed 82.chr , "R"
154
+ assert_printed 83.chr , "S"
155
+ assert_printed 84.chr , "T"
156
+ assert_printed 85.chr , "U"
157
+ assert_printed 86.chr , "V"
158
+ assert_printed 87.chr , "W"
159
+ assert_printed 88.chr , "X"
160
+ assert_printed 89.chr , "Y"
161
+ assert_printed 90.chr , "Z"
162
+ assert_printed 91.chr , "["
163
+ assert_printed 92.chr , "\\" # printable, thus not escaped
164
+ assert_printed 93.chr , "]"
165
+ assert_printed 94.chr , "^"
166
+ assert_printed 95.chr , "_"
167
+ assert_printed 96.chr , "`"
168
+ assert_printed 97.chr , "a"
169
+ assert_printed 98.chr , "b"
170
+ assert_printed 99.chr , "c"
171
+ assert_printed 100.chr , "d"
172
+ assert_printed 101.chr , "e"
173
+ assert_printed 102.chr , "f"
174
+ assert_printed 103.chr , "g"
175
+ assert_printed 104.chr , "h"
176
+ assert_printed 105.chr , "i"
177
+ assert_printed 106.chr , "j"
178
+ assert_printed 107.chr , "k"
179
+ assert_printed 108.chr , "l"
180
+ assert_printed 109.chr , "m"
181
+ assert_printed 110.chr , "n"
182
+ assert_printed 111.chr , "o"
183
+ assert_printed 112.chr , "p"
184
+ assert_printed 113.chr , "q"
185
+ assert_printed 114.chr , "r"
186
+ assert_printed 115.chr , "s"
187
+ assert_printed 116.chr , "t"
188
+ assert_printed 117.chr , "u"
189
+ assert_printed 118.chr , "v"
190
+ assert_printed 119.chr , "w"
191
+ assert_printed 120.chr , "x"
192
+ assert_printed 121.chr , "y"
193
+ assert_printed 122.chr , "z"
194
+ assert_printed 123.chr , "{"
195
+ assert_printed 124.chr , "|"
196
+ assert_printed 125.chr , "}"
197
+ assert_printed 126.chr , "~"
198
+ assert_printed 127.chr, "\u007F"
199
+ end
200
+
201
+ it 'can be given a list of characters to not escape' do
202
+ expect(result_for 0, '', "\r\n", dont_escape: ["\n"]).to eq "\\r\n"
203
+ expect(result_for 0, '', "\r\n", dont_escape: ["\r"]).to eq "\r\\n"
204
+ end
205
+
206
+ it 'escapes them before running through the other calculations' do
207
+ expect(result_for 1, '=>', "\r\n", max_line_length: 7).to eq '=>\r\n'
208
+ expect(result_for 1, '=>', "\r\n", max_line_length: 6).to eq '=>...'
209
+ end
210
+ end
@@ -0,0 +1,71 @@
1
+ require 'seeing_is_believing/binary/data_structures'
2
+
3
+ RSpec.describe SeeingIsBelieving::Binary::Marker do
4
+
5
+ describe 'to_regex' do
6
+ def assert_parses(input, regex)
7
+ expect(described_class.to_regex input).to eq regex
8
+ end
9
+
10
+ it 'doesn\'t change existing regexes' do
11
+ assert_parses /^.$/ix, /^.$/ix
12
+ end
13
+
14
+ it 'converts strings into regexes' do
15
+ assert_parses '', %r()
16
+ assert_parses 'a', %r(a)
17
+ end
18
+
19
+ it 'ignores surrounding slashes' do
20
+ assert_parses '//', %r()
21
+ assert_parses '/a/', %r(a)
22
+ end
23
+
24
+ it 'respects flags after the trailing slash in surrounding slashes' do
25
+ assert_parses '/a/', %r(a)
26
+ assert_parses '/a//', %r(a/)
27
+ assert_parses '//a/', %r(/a)
28
+ assert_parses '/a/i', %r(a)i
29
+ assert_parses '/a/im', %r(a)im
30
+ assert_parses '/a/xim', %r(a)xim
31
+ assert_parses '/a/mix', %r(a)mix
32
+ assert_parses '/a/mixi', %r(a)mixi
33
+ end
34
+
35
+ it 'isn\'t fooled by strings that kinda look regexy' do
36
+ assert_parses '/a', %r(/a)
37
+ assert_parses 'a/', %r(a/)
38
+ assert_parses '/', %r(/)
39
+ assert_parses '/i', %r(/i)
40
+ end
41
+
42
+ it 'does not escape the content' do
43
+ assert_parses 'a\\s+', %r(a\s+)
44
+ assert_parses '/a\\s+/', %r(a\s+)
45
+ end
46
+ end
47
+
48
+ it 'requires prefix and a regex' do
49
+ described_class.new prefix: '', regex: //
50
+ expect { described_class.new }.to raise_error ArgumentError
51
+ expect { described_class.new prefix: '' }.to raise_error ArgumentError
52
+ expect { described_class.new regex: // }.to raise_error ArgumentError
53
+ end
54
+
55
+ it 'stores the prefix and a regex' do
56
+ marker = described_class.new(prefix: 't', regex: /r/)
57
+ expect(marker.prefix).to eq 't'
58
+ expect(marker.regex).to eq /r/
59
+ end
60
+
61
+ it 'converts strings to rgexes when they are set' do
62
+ marker = described_class.new prefix: 't', regex: 'r1'
63
+ expect(marker[:regex]).to eq /r1/
64
+
65
+ marker.regex = '/r2/i'
66
+ expect(marker.regex).to eq /r2/i
67
+
68
+ marker[:regex] = 'r3'
69
+ expect(marker.regex).to eq /r3/
70
+ end
71
+ end