test_bench-bootstrap 0.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/test_bench/bootstrap.rb +234 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 978ac1932ff9bd26c3df5224a36601e8c0904a86373cb0afce012719c304061a
4
+ data.tar.gz: 6f2d19a75d8ba1e3b24682768df85a29472a69d10312eb678739ba06df403d51
5
+ SHA512:
6
+ metadata.gz: e80f1d090729ae650faac76d433e9d8aa7e7812a8da986593944b1797fbdaf97dd62def14b1326ed1aff098d0113a67991d6a49d48afc232c051a813e88f01d9
7
+ data.tar.gz: d571f23a5dbed980a37788c157d2673b7c7abe697f2c64a7ff9597e772f48a8dd4c9a47d89221c49f03120dd6cea0b7b3fb67a6d9a246e1456b17172292f6b00
@@ -0,0 +1,234 @@
1
+ module TestBench
2
+ module Bootstrap
3
+ def self.activate(receiver=nil)
4
+ receiver ||= TOPLEVEL_BINDING.receiver
5
+
6
+ receiver.extend(Fixture)
7
+ end
8
+
9
+ module Fixture
10
+ def assert(value)
11
+ unless value
12
+ raise AssertionFailure.build(caller_locations.first)
13
+ end
14
+ end
15
+
16
+ def assert_raises(error_class=nil, &block)
17
+ begin
18
+ block.()
19
+
20
+ rescue (error_class || StandardError) => error
21
+ return if error_class.nil?
22
+
23
+ if error.instance_of?(error_class)
24
+ return
25
+ else
26
+ raise error
27
+ end
28
+ end
29
+
30
+ raise AssertionFailure.build(caller_locations.first)
31
+ end
32
+
33
+ def refute(value)
34
+ if value
35
+ raise AssertionFailure.build(caller_locations.first)
36
+ end
37
+ end
38
+
39
+ def refute_raises(error_class, &block)
40
+ block.()
41
+
42
+ rescue error_class => error
43
+ unless error.instance_of?(error_class)
44
+ raise error
45
+ end
46
+
47
+ raise AssertionFailure.build(caller_locations.first)
48
+ end
49
+
50
+ def context(prose=nil, &block)
51
+ if block.nil?
52
+ Output.write(prose || 'Context', sgr_code: 0x33)
53
+ return
54
+ end
55
+
56
+ unless prose.nil?
57
+ Output.indent(prose, sgr_code: 0x32) do
58
+ context(&block)
59
+ end
60
+ return
61
+ end
62
+
63
+ begin
64
+ block.()
65
+
66
+ rescue => error
67
+ Fixture.print_error(error)
68
+
69
+ raise Failure.build
70
+ end
71
+ end
72
+
73
+ def test(prose=nil, &block)
74
+ if block.nil?
75
+ Output.write(prose || 'Test', sgr_code: 0x33)
76
+ return
77
+ end
78
+
79
+ begin
80
+ block.()
81
+
82
+ Output.indent(prose, sgr_code: 0x32)
83
+
84
+ rescue => error
85
+ Output.indent(prose, sgr_codes: [0x1, 0x31]) do
86
+ Fixture.print_error(error)
87
+ end
88
+
89
+ raise Failure.build
90
+ end
91
+ end
92
+
93
+ def comment(text)
94
+ Output.write(text)
95
+ end
96
+
97
+ def self.print_error(error)
98
+ omit_backtrace_pattern = ENV['TEST_BENCH_OMIT_BACKTRACE_PATTERN']
99
+ omit_backtrace_pattern ||= %r{lib/test_bench/bootstrap\.rb}
100
+
101
+ omitting = false
102
+
103
+ error_message = error.full_message(highlight: true, order: :bottom)
104
+
105
+ error_message.each_line.with_index do |line, index|
106
+ line.chomp!
107
+
108
+ if line.start_with?("\t") && omit_backtrace_pattern.match?(line)
109
+ if omitting
110
+ next
111
+ else
112
+ omitting = true
113
+
114
+ indentation = line.index("from ")
115
+
116
+ line.slice!(indentation..-1)
117
+ line.gsub!(%r{[[:digit:]]}, '?')
118
+ line.concat("*omitted*")
119
+
120
+ Output.write(line, sgr_codes: [0x2, 0x3, 0x31])
121
+ end
122
+ else
123
+ omitting = false
124
+
125
+ Output.write(line, sgr_code: 0x31)
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ module Output
132
+ extend self
133
+
134
+ def write(text, device: nil, sgr_code: nil, sgr_codes: nil)
135
+ indent(text, device: device, sgr_code: sgr_code, sgr_codes: sgr_codes)
136
+ end
137
+
138
+ def indent(text, device: nil, sgr_code: nil, sgr_codes: nil, &block)
139
+ device ||= $stdout
140
+
141
+ unless text.nil?
142
+ sgr_codes = Array(sgr_codes)
143
+ unless sgr_code.nil?
144
+ sgr_codes << sgr_code
145
+ end
146
+
147
+ unless sgr_codes.empty?
148
+ sgr_codes.map! do |sgr_code|
149
+ sgr_code.to_s(16)
150
+ end
151
+
152
+ text = "\e[#{sgr_codes * ';'}m#{text}\e[0m"
153
+ end
154
+
155
+ text = "#{' ' * indentation}#{text}"
156
+
157
+ device.puts(text)
158
+ end
159
+
160
+ return if block.nil?
161
+
162
+ self.indentation += 1
163
+
164
+ begin
165
+ block.()
166
+ ensure
167
+ self.indentation -= 1
168
+ end
169
+ end
170
+
171
+ def indentation
172
+ @indentation ||= 0
173
+ end
174
+ attr_writer :indentation
175
+ end
176
+
177
+ class AssertionFailure < RuntimeError
178
+ def self.build(caller_location=nil)
179
+ caller_location ||= caller_locations.first
180
+
181
+ instance = new
182
+ instance.set_backtrace([caller_location.to_s])
183
+ instance
184
+ end
185
+
186
+ def message
187
+ "Assertion failed"
188
+ end
189
+ end
190
+
191
+ class Failure < SystemExit
192
+ def self.build
193
+ new(false)
194
+ end
195
+ end
196
+
197
+ module Run
198
+ def self.call(argv=nil, exclude_file_pattern: nil)
199
+ argv ||= ::ARGV
200
+
201
+ exclude_file_pattern = ENV['TEST_BENCH_EXCLUDE_FILE_PATTERN']
202
+ exclude_file_pattern ||= %r{automated_init\.rb}
203
+
204
+ if argv.empty?
205
+ file_patterns = ['test/automated/**/*.rb']
206
+ else
207
+ file_patterns = argv
208
+ end
209
+
210
+ file_patterns.each do |file_pattern|
211
+ if File.directory?(file_pattern)
212
+ file_pattern = File.join(file_pattern, '**/*.rb')
213
+ end
214
+
215
+ files = Dir[file_pattern].reject do |file|
216
+ File.basename(file).match?(exclude_file_pattern)
217
+ end
218
+
219
+ files.sort.each do |file|
220
+ puts "Running #{file}"
221
+
222
+ begin
223
+ load file
224
+ ensure
225
+ puts
226
+ end
227
+ end
228
+ end
229
+ end
230
+ end
231
+ end
232
+
233
+ Bootstrap.activate
234
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_bench-bootstrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Ladd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: nathanladd+github@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/test_bench/bootstrap.rb
20
+ homepage: https://github.com/test-bench/test-bench-bootstrap
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.0.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A minimal test framework for testing TestBench
43
+ test_files: []