test_bench-isolated 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 +328 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2f36ae682bd15d3af0f189c7df3337bcdf5acf84e8e809ce2f05d273a5d8129a
4
+ data.tar.gz: 42d939f0f13bde2fe3808a9dfbbe4415c9026480bf6fa0f90b977682615f8274
5
+ SHA512:
6
+ metadata.gz: 35eb7d285e7d39f4b690191053915a347062d60d40e659080329a582ef7d85ddbea59342085ca4b3b0fbb849482739c33a77fd281216b1ef60255fe6abc6e605
7
+ data.tar.gz: 1c3da22303afc2fc9f674381edb53de250b4e4746afdeef47952ebe2cad13b7e4aceb8821eb8a841b9bb58ffc6da6502e73f00cb09b5d2c1c9f79a73171adb4f
@@ -0,0 +1,328 @@
1
+ module TestBench
2
+ module Bootstrap
3
+ def self.activate
4
+ Object.include(Fixture)
5
+ end
6
+
7
+ module Backtrace
8
+ if RUBY_ENGINE != 'mruby'
9
+ def self.frame(frame_index)
10
+ frame_index += 1
11
+
12
+ caller[frame_index]
13
+ end
14
+ end
15
+ end
16
+
17
+ class AssertionFailure < RuntimeError
18
+ def self.build(frame_index=nil)
19
+ frame_index ||= 0
20
+
21
+ frame = Backtrace.frame(frame_index)
22
+
23
+ instance = new
24
+ instance.set_backtrace([frame])
25
+ instance
26
+ end
27
+
28
+ def message
29
+ "Assertion failed"
30
+ end
31
+ end
32
+
33
+ module Path
34
+ if RUBY_ENGINE != 'mruby'
35
+ def self.match?(pattern, string)
36
+ ::File.fnmatch?(pattern, string)
37
+ end
38
+
39
+ def self.search(path, include_pattern=nil, exclude_pattern=nil)
40
+ files = []
41
+
42
+ if ::File.directory?(path)
43
+ search_directory(path, files, include_pattern, exclude_pattern)
44
+ elsif ::File.exist?(path)
45
+ files << path
46
+ else
47
+ raise LoadError, "no such file or directory -- #{path}"
48
+ end
49
+
50
+ files
51
+ end
52
+
53
+ def self.search_directory(dir, files, include_pattern=nil, exclude_pattern=nil)
54
+ include_pattern ||= '*.rb'
55
+
56
+ ::Dir[::File.join(dir, '**', '*')].each do |path|
57
+ next if ::File.directory?(path)
58
+
59
+ if match?(include_pattern, path)
60
+ if exclude_pattern.nil? || !match?(exclude_pattern, path)
61
+ files << path
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ module Fixture
70
+ def assert(value)
71
+ unless value
72
+ raise AssertionFailure.build(1)
73
+ end
74
+ end
75
+
76
+ def assert_raises(error_class=nil, &block)
77
+ begin
78
+ block.()
79
+
80
+ rescue (error_class || StandardError) => error
81
+ return if error_class.nil?
82
+
83
+ if error.instance_of?(error_class)
84
+ return
85
+ else
86
+ raise error
87
+ end
88
+ end
89
+
90
+ raise AssertionFailure.build(1)
91
+ end
92
+
93
+ def refute(value)
94
+ if value
95
+ raise AssertionFailure.build(1)
96
+ end
97
+ end
98
+
99
+ def refute_raises(error_class=nil, &block)
100
+ block.()
101
+
102
+ rescue (error_class || StandardError) => error
103
+ unless error.instance_of?(error_class)
104
+ raise error
105
+ end
106
+
107
+ raise AssertionFailure.build(1)
108
+ end
109
+
110
+ def context(title=nil, &block)
111
+ if block.nil?
112
+ Output.write(title || 'Context', sgr_code: 0x33)
113
+ return
114
+ end
115
+
116
+ unless title.nil?
117
+ Output.indent(title, sgr_code: 0x32) do
118
+ context(&block)
119
+ end
120
+ return
121
+ end
122
+
123
+ block.()
124
+ end
125
+ alias_method :context!, :context
126
+
127
+ def _context(title=nil, &block)
128
+ context(title)
129
+ end
130
+
131
+ def test(title=nil, &block)
132
+ if block.nil?
133
+ Output.write(title || 'Test', sgr_code: 0x33)
134
+ return
135
+ end
136
+
137
+ begin
138
+ block.()
139
+
140
+ Output.indent(title, sgr_code: 0x32)
141
+
142
+ rescue => error
143
+ Output.indent(title || 'Test', sgr_codes: [0x1, 0x31])
144
+
145
+ raise error
146
+ end
147
+ end
148
+ alias_method :test!, :test
149
+
150
+ def _test(title=nil, &block)
151
+ test(title)
152
+ end
153
+
154
+ def comment(...)
155
+ detail(...)
156
+ end
157
+
158
+ def detail(detail, *additional_details, quote: nil, heading: nil)
159
+ details = [detail, *additional_details]
160
+
161
+ if quote.nil?
162
+ if additional_details == ['']
163
+ quote = true
164
+ else
165
+ quote = details.last.end_with?("\n")
166
+ end
167
+ end
168
+
169
+ if quote
170
+ if heading.nil?
171
+ heading = !detail.end_with?("\n")
172
+ end
173
+ end
174
+
175
+ if heading
176
+ heading_text = details.shift
177
+ Output.indent(heading_text, sgr_codes: [0x1, 0x4])
178
+ end
179
+
180
+ details.each do |detail|
181
+ if detail.empty?
182
+ Output.indent("\e[2;3m(empty)\e[23;22m")
183
+ next
184
+ end
185
+
186
+ if quote
187
+ detail.each_line do |line|
188
+ line.chomp!
189
+
190
+ Output.indent("\e[2m> \e[22m#{line}")
191
+ end
192
+ else
193
+ Output.indent(detail)
194
+ end
195
+ end
196
+ end
197
+
198
+ def fixture(cls, *args, **kwargs, &block)
199
+ fixture = TestBench::Fixture.(cls, *args, **kwargs, &block)
200
+
201
+ passed = !fixture.test_session.failed?
202
+
203
+ assert(passed)
204
+ end
205
+ end
206
+
207
+ module Output
208
+ extend self
209
+
210
+ def write(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil)
211
+ indent(text, device: device, sgr_code: sgr_code, sgr_codes: sgr_codes, tab_indent: tab_indent)
212
+ end
213
+
214
+ def newline
215
+ write('')
216
+ end
217
+
218
+ def indent(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil, &block)
219
+ unless text.nil?
220
+ sgr_codes ||= []
221
+ unless sgr_code.nil?
222
+ sgr_codes << sgr_code
223
+ end
224
+
225
+ unless sgr_codes.empty?
226
+ sgr_codes.map! do |sgr_code|
227
+ sgr_code.to_s(16)
228
+ end
229
+
230
+ text = "\e[#{sgr_codes.join(';')}m#{text}\e[0m"
231
+ end
232
+
233
+ text = "#{"\t" if tab_indent}#{' ' * indentation}#{text}\n"
234
+
235
+ raw_write(text, device)
236
+ end
237
+
238
+ return if block.nil?
239
+
240
+ self.indentation += 1 unless text.nil?
241
+
242
+ begin
243
+ block.()
244
+ ensure
245
+ self.indentation -= 1 unless text.nil?
246
+ end
247
+ end
248
+
249
+ def raw_write(text, device=nil)
250
+ device ||= self.device
251
+
252
+ device.write(text)
253
+ end
254
+
255
+ def indentation
256
+ @indentation ||= 0
257
+ end
258
+ attr_writer :indentation
259
+
260
+ def device
261
+ @device ||= Defaults.output_device
262
+ end
263
+ end
264
+
265
+ module Run
266
+ def self.call(paths=nil, exclude_pattern: nil)
267
+ paths ||= []
268
+ exclude_pattern ||= Defaults.exclude_file_pattern
269
+
270
+ if paths.is_a?(String)
271
+ paths = [paths]
272
+ end
273
+
274
+ if paths.empty?
275
+ paths << Defaults.tests_dir
276
+ end
277
+
278
+ paths.each do |path|
279
+ Path.search(path, '*.rb', exclude_pattern).each do |file|
280
+ Output.write "Running #{file}"
281
+
282
+ begin
283
+ load(file)
284
+ ensure
285
+ Output.newline
286
+ end
287
+ end
288
+ end
289
+ end
290
+ end
291
+
292
+ module Defaults
293
+ def self.get(env_var, default)
294
+ if env.key?(env_var)
295
+ env[env_var]
296
+ else
297
+ default
298
+ end
299
+ end
300
+
301
+ def self.exclude_file_pattern
302
+ get('TEST_BENCH_EXCLUDE_FILE_PATTERN', '*_init.rb')
303
+ end
304
+
305
+ def self.omit_backtrace_pattern
306
+ get('TEST_BENCH_OMIT_BACKTRACE_PATTERN', '*/test_bench/bootstrap.rb')
307
+ end
308
+
309
+ def self.tests_dir
310
+ get('TEST_BENCH_TESTS_DIRECTORY', 'test/automated')
311
+ end
312
+
313
+ if RUBY_ENGINE == 'mruby'
314
+ def self.env
315
+ {}
316
+ end
317
+ else
318
+ def self.output_device
319
+ $stdout
320
+ end
321
+
322
+ def self.env
323
+ ::ENV
324
+ end
325
+ end
326
+ end
327
+ end
328
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test_bench-isolated
3
+ version: !ruby/object:Gem::Version
4
+ version: '0'
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Ladd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-24 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.4.10
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A minimal test framework for testing TestBench
43
+ test_files: []