-
Notifications
You must be signed in to change notification settings - Fork 63
/
spec.rb
1773 lines (1479 loc) · 42.5 KB
/
spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require_relative 'formatters/default_formatter'
require_relative 'formatters/yaml_formatter'
require_relative 'formatters/spec_formatter'
require_relative 'mspec'
require_relative 'mspecscript'
require_relative 'platform_guard'
require_relative 'version'
require_relative 'spec_helpers/fs'
require_relative 'spec_helpers/io'
require_relative 'spec_helpers/mock_to_path'
require_relative 'spec_helpers/tmp'
require_relative 'spec_utils/warnings'
require_relative 'nat_binary'
require 'tempfile'
class SpecFailedException < StandardError
end
class NatalieFixMeException < SpecFailedException
end
class UnknownFormatterException < StandardError
end
TOLERANCE = 0.00003
TIME_TOLERANCE = 20.0
FORMATTERS = %w[default yaml spec]
@formatter_name = ARGV[ARGV.index('-f') + 1] if ARGV.include?('-f')
@formatter_name ||= 'default'
unless FORMATTERS.include?(@formatter_name)
raise UnknownFormatterException, "#{@formatter_name} is not supported! Use #{FORMATTERS.join(', ')}"
end
$context = []
@shared = {}
@specs = []
$natfixme_depth = 0 # if > 0 then we are in a NATFIXME
$expectations = []
class Context
def initialize(description, skip: false)
@description = description
@before_each = []
@before_all = []
@after_each = []
@after_all = []
@skip = skip
end
attr_reader :description, :before_each, :before_all, :after_each, :after_all, :skip
def add_before_each(block)
@before_each << block
end
def add_before_all(block)
@before_all << block
end
def add_after_each(block)
@after_each << block
end
def add_after_all(block)
@after_all << block
end
def to_s
@description
end
def run_before_all(done = [])
before_all.each do |b|
unless done.include?(b)
b.call
done << b
end
end
end
def run_after_all(done = [])
after_all.reverse_each do |b|
unless done.include?(b)
b.call
done << b
end
end
end
end
class ScratchPad
def self.record(item)
@record = item
end
def self.recorded
@record
end
def self.clear
@record = nil
end
def self.<<(item)
@record << item
end
end
class SpecEvaluate
# NATFIXME: Implement this method
def self.desc=(description)
end
end
def ci?
!!ENV['CI']
end
def describe(description, shared: false, &block)
if shared
@shared[description] = block
else
parent = $context.last
$context << Context.new(description, skip: parent && parent.skip)
yield
$context.pop
end
end
alias context describe
def xdescribe(description, &block)
$context << Context.new(description, skip: true)
yield
$context.pop
end
alias xcontext xdescribe
def it(test, &block)
return xit(test, &block) if $context.last.skip || block.nil?
@specs << [$context.dup, test, block]
end
def fit(test, &block)
raise 'Focused spec in CI detected. Please remove it!' if ci?
@specs << [$context.dup, test, block, :focus]
end
def xit(test, &block)
@specs << [$context.dup, test, nil]
end
def skip(test = nil, &block)
xit(test, &block)
end
def it_behaves_like(behavior, method, obj = :zzzz_not_given)
before :all do
@method = method if method
@object = obj unless obj == :zzzz_not_given
end
block = @shared[behavior]
if block
block.call
else
raise "Cannot find shared behavior: #{behavior.inspect} (available: #{@shared.keys.inspect})"
end
end
def it_should_behave_like(*shared_groups)
shared_groups.each do |behavior|
block = @shared[behavior]
if block
block.call
else
raise "Cannot find shared behavior: #{behavior.inspect} (available: #{@shared.keys.inspect})"
end
end
end
def specify(test = nil, &block)
return xit(test, &block) if $context.last.skip
@specs << [$context.dup, test, block]
end
# NATFIXME: implement this guard
def with_block_device
nil
end
def with_feature(*names)
yield if names.all? { |name| MSpec.features[name] }
end
def with_timezone(zone, offset = nil)
old_tz = ENV['TZ']
if offset
zone += "#{(-offset).to_s}:00:00"
end
ENV['TZ'] = zone
begin
yield
ensure
# Natalie does not allow setting ENV values that are not strings?
if old_tz.is_a?(String)
ENV['TZ'] = old_tz
end
end
end
def nan_value
0 / 0.0
end
def infinity_value
1 / 0.0
end
def bignum_value(plus = 0)
(2 ** 64) + plus
end
def fixnum_max
9_223_372_036_854_775_807
end
def fixnum_min
-9_223_372_036_854_775_807
end
def max_long
2**(0.size * 8 - 1) - 1
end
def min_long
-(2**(0.size * 8 - 1))
end
def ruby_exe(code = nil, options: nil, args: nil, escape: true, exit_status: 0, env: {})
env = env.map { |key, value| "#{key}=#{value} " }.join
binary = if RUBY_ENGINE == 'ruby'
'ruby'
else
ENV.fetch('NAT_BINARY', 'bin/natalie')
end
if code.nil?
return binary if args.nil?
return `#{env}#{binary} #{options} #{args}`
end
output = if !escape
`#{env}#{binary} #{options} -e #{code.inspect} #{args}`
elsif File.readable?(code)
`#{binary} #{options} #{code} #{args}`
else
Tempfile.create('ruby_exe.rb') do |file|
file.write(code)
file.rewind
`#{env}#{binary} #{options} #{file.path} #{args}`
end
end
if exit_status.is_a?(Symbol) || exit_status.is_a?(String)
exit_status = 128 + Signal.list.fetch(exit_status.to_s.delete_prefix('SIG'))
end
if $?.exitstatus != exit_status
raise SpecFailedException, "Expected exit status #{exit_status} but actual is #{$?.exitstatus}"
end
output
end
def ruby_cmd(code, options: nil, args: nil)
binary = ENV['NAT_BINARY'] || 'bin/natalie'
"#{binary} #{options} -e #{code.inspect} #{args}"
end
def fixture(source, filename)
dirname = File.dirname(File.realpath(source))
dirname.delete_suffix!('/shared')
dirname = File.join(dirname, 'fixtures') unless dirname.end_with?('/fixtures')
File.join(dirname, filename)
end
def ruby_version_is(version, &block)
version_is(RUBY_VERSION, version, &block)
end
def _version_is(version, requirement_version)
version = SpecVersion.new(version)
case requirement_version
when String
requirement = SpecVersion.new requirement_version
return(version >= requirement)
when Range
a = SpecVersion.new requirement_version.begin
b = SpecVersion.new requirement_version.end
return(version >= a && (requirement_version.exclude_end? ? version < b : version <= b))
else
raise "version must be a String or Range but was a #{requirement_version.class}"
end
false
end
def version_is(*args)
matched = _version_is(*args)
return matched unless block_given?
if matched
yield
end
end
def ruby_bug(bug_id, _version, &block)
describe("We do not reproduce bug #{bug_id}", &block)
end
def slow_test
yield if ENV['ENABLE_SLOW_TESTS']
end
def _platform_match(*args)
options, platforms = if args.last.is_a?(Hash)
[args.last, args[0..-2]]
else
[{}, args]
end
return true if options[:wordsize] == 64 || options[:pointer_size] == 64
return true if platforms.include?(:windows) && RUBY_PLATFORM =~ /(mswin|mingw)/
return true if platforms.include?(:linux) && RUBY_PLATFORM =~ /linux/
return true if platforms.include?(:darwin) && RUBY_PLATFORM =~ /darwin/i
return true if platforms.include?(:openbsd) && RUBY_PLATFORM =~ /openbsd/i
return true if platforms.include?(:freebsd) && RUBY_PLATFORM =~ /freebsd/i
return true if platforms.include?(:netbsd) && RUBY_PLATFORM =~ /netbsd/i
return true if platforms.include?(:bsd) && RUBY_PLATFORM =~ /(bsd|darwin)/i
# TODO: cygwin, android, solaris and aix are currently uncovered
false
end
def platform_is(*args)
matched = _platform_match(*args)
return matched unless block_given?
if matched
yield
end
end
def platform_is_not(*args)
not_matched = !_platform_match(*args)
return not_matched unless block_given?
if not_matched
yield
end
end
def not_supported_on(*)
yield
end
# NATFIXME
def kernel_version_is(*)
false
end
def as_user
if Process.euid != 0
yield
end
end
def as_superuser
if Process.euid == 0
yield
end
end
def as_real_superuser
if Process.uid == 0
yield
end
end
def little_endian
yield
end
def big_endian
end
def suppress_warning
old_stderr = $stderr
$stderr = IOStub.new
yield
ensure
$stderr = old_stderr
end
alias suppress_keyword_warning suppress_warning
def before(type = :each, &block)
if type == :each
$context.last.add_before_each(block)
elsif type == :all
$context.last.add_before_all(block)
else
raise "I don't know how to do before(#{type.inspect})"
end
end
def after(type = :each, &block)
if type == :each
$context.last.add_after_each(block)
elsif type == :all
$context.last.add_after_all(block)
else
raise "I don't know how to do after(#{type.inspect})"
end
end
def guard(proc)
yield if proc.call
rescue
nil
end
def guard_not(proc)
yield unless proc.call
end
def quarantine!
# do nothing
end
def flunk
raise SpecFailedException
end
class Matcher
def initialize(subject, inverted, args)
@subject = subject
@inverted = inverted
@args = args
match_expectation(@args.first) if @args.any?
end
def ==(other)
@inverted ? neq(other) : eq(other)
end
MIN_STRING_SIZE_TO_RUN_DIFF = 100
MIN_ARRAY_SIZE_TO_RUN_DIFF = 2
def eq(other)
if @subject != other
if @subject.is_a?(String) && other.is_a?(String) && (@subject.size >= MIN_STRING_SIZE_TO_RUN_DIFF || other.size >= MIN_STRING_SIZE_TO_RUN_DIFF) && $natfixme_depth == 0
diff(other, @subject)
raise SpecFailedException, 'two strings should match'
elsif @subject.is_a?(Array) && other.is_a?(Array) && (@subject.size >= MIN_ARRAY_SIZE_TO_RUN_DIFF || other.size >= MIN_ARRAY_SIZE_TO_RUN_DIFF) && $natfixme_depth == 0
diff(
"[\n" + other.map(&:inspect).join("\n") + "\n]",
"[\n" + @subject.map(&:inspect).join("\n") + "\n]",
)
raise SpecFailedException, @subject.inspect + ' should be == to ' + other.inspect
else
raise SpecFailedException, @subject.inspect + ' should be == to ' + other.inspect
end
end
end
def diff(actual, expected)
return if ENV['CI']
actual_file = Tempfile.create('actual')
actual_file.write(actual)
actual_file.close
expected_file = Tempfile.create('expected')
expected_file.write(expected)
expected_file.close
puts `diff #{expected_file.path} #{actual_file.path}`
File.unlink(actual_file.path)
File.unlink(expected_file.path)
end
def !=(other)
@inverted ? eq(other) : neq(other)
end
def neq(other)
raise SpecFailedException, @subject.inspect + ' should not (!) be == to ' + other.inspect if @subject == other
end
def =~(pattern)
@inverted ? not_regex_match(pattern) : regex_match(pattern)
end
def regex_match(pattern)
raise SpecFailedException, @subject.inspect + ' should match ' + pattern.inspect if @subject !~ pattern
end
def !~(pattern)
@inverted ? regex_match(pattern) : not_regex_match(pattern)
end
def not_regex_match(pattern)
raise SpecFailedException, @subject.inspect + ' should not (!) match ' + pattern.inspect if @subject =~ pattern
end
def match_expectation(expectation)
@inverted ? expectation.inverted_match(@subject) : expectation.match(@subject)
end
def method_missing(method, *args)
target = if args.any?
case method
when :<, :<=, :>, :>=
"be #{method} than #{args.first.inspect}"
else
"#{method} #{args.first.inspect}"
end
else
"be #{method.to_s}"
end
send = Kernel.instance_method(:send)
if !@inverted
raise SpecFailedException, "#{@subject.inspect} should #{target}" if !send.bind_call(@subject, method, *args)
else
raise SpecFailedException, "#{@subject.inspect} should not #{target}" if send.bind_call(@subject, method, *args)
end
end
undef_method :equal?
end
class BeNilExpectation
def match(subject)
raise SpecFailedException, subject.inspect + ' should be nil' if subject != nil
end
def inverted_match(subject)
raise SpecFailedException, subject.inspect + ' should not be nil' if subject == nil
end
end
class BeKindOfExpectation
def initialize(klass)
@klass = klass
end
def match(subject)
raise SpecFailedException, subject.inspect + ' should be a kind of ' + @klass.inspect if !(@klass === subject)
end
def inverted_match(subject)
raise SpecFailedException, subject.inspect + ' should not be a kind of ' + @klass.inspect if @klass === subject
end
end
class BeAncestorOfExpectation
def initialize(klass)
@klass = klass
end
def match(subject)
unless @klass.ancestors.include?(subject)
raise SpecFailedException, subject.inspect + ' should be an ancestor of ' + @klass.inspect
end
end
def inverted_match(subject)
if @klass.ancestors.include?(subject)
raise SpecFailedException, subject.inspect + ' should not to be an ancestor of ' + @klass.inspect
end
end
end
class BlockCallerExpectation
def match(subject)
unless check(subject)
raise SpecFailedException, subject.inspect + ' should have blocked, but it did not'
end
end
def inverted_match(subject)
if check(subject)
raise SpecFailedException, subject.inspect + ' should have not have blocked, but it did'
end
end
private
# I borrowed this from https://github.com/ruby/mspec/blob/master/lib/mspec/matchers/block_caller.rb
# Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
# Licensed Under the MIT license.
def check(subject)
t = Thread.new { subject.call }
loop do
case t.status
when 'sleep' # blocked
t.kill
t.join
return true
when false # terminated normally, so never blocked
t.join
return false
when nil # terminated exceptionally
t.value
else
Thread.pass
end
end
end
end
class EqlExpectation
def initialize(other)
@other = other
end
def match(subject)
if !subject.eql?(@other)
raise SpecFailedException, subject.inspect + ' should be eql (same type) to ' + @other.inspect
end
end
def inverted_match(subject)
if subject.eql?(@other)
raise SpecFailedException, subject.inspect + ' should not be eql (same type) to ' + @other.inspect
end
end
end
class BeEmptyExpectation
def match(subject)
if (subject.length > 0)
raise SpecFailedException, subject.inspect + ' should be empty but has size ' + subject.length
end
end
def inverted_match(subject)
raise SpecFailedException, subject.inspect + ' should not be empty' if (subject.length == 0)
end
end
class EqualExpectation
def initialize(other)
@other = other
end
def match(subject)
raise SpecFailedException, subject.inspect + ' should be equal to ' + @other.inspect if !subject.equal?(@other)
end
def inverted_match(subject)
raise SpecFailedException, subject.inspect + ' should not be equal to ' + @other.inspect if subject.equal?(@other)
end
end
# Largely taken from
# https://github.com/ruby/ruby/blob/master/spec/mspec/lib/mspec/matchers/equal_element.rb#L76
class EqualElementExpectation
def initialize(element, attributes = nil, content = nil, options = {})
@element = element
@attributes = attributes
@content = content
@options = options
end
def match(subject)
@actual = subject
unless matches?
raise SpecFailedException, "Expected #{@actual} to be a '#{@element}' element with #{attributes_for_failure_message} and #{content_for_failure_message}"
end
end
def inverted_match(subject)
@actual = subject
if matches?
raise SpecFailedException "Expected #{@actual} not to be a '#{@element}' element with #{attributes_for_failure_message} and #{content_for_failure_message}"
end
end
private
def matches?
actual = @actual
matched = true
if @options[:not_closed]
matched &&= actual =~ /^#{Regexp.quote("<" + @element)}.*#{Regexp.quote(">" + (@content || ''))}$/
else
matched &&= actual =~ /^#{Regexp.quote("<" + @element)}/
matched &&= actual =~ /#{Regexp.quote("</" + @element + ">")}$/
matched &&= actual =~ /#{Regexp.quote(">" + @content + "</")}/ if @content
end
if @attributes
if @attributes.empty?
matched &&= actual.scan(/\w+\=\"(.*)\"/).size == 0
else
@attributes.each do |key, value|
if value == true
matched &&= (actual.scan(/#{Regexp.quote(key)}(\s|>)/).size == 1)
else
matched &&= (actual.scan(%Q{ #{key}="#{value}"}).size == 1)
end
end
end
end
!!matched
end
def attributes_for_failure_message
if @attributes
if @attributes.empty?
"no attributes"
else
@attributes.inject([]) { |memo, n| memo << %Q{#{n[0]}="#{n[1]}"} }.join(" ")
end
else
"any attributes"
end
end
def content_for_failure_message
if @content
if @content.empty?
"no content"
else
"#{@content.inspect} as content"
end
else
"any content"
end
end
end
class TrueFalseExpectation
def match(subject)
unless subject == true || subject == false
raise SpecFailedException, subject.inspect + ' should be true or false'
end
end
def inverted_match(subject)
if subject == true || subject == false
raise SpecFailedException, subject.inspect + ' should not be true or false'
end
end
end
class BeCloseExpectation
def initialize(target, tolerance = TOLERANCE)
@target = target
@tolerance = tolerance
end
def match(subject)
if (subject - @target).abs > @tolerance
raise SpecFailedException, "#{subject.inspect} should be within #{@tolerance} of #{@target}"
end
end
def inverted_match(subject)
if (subject - @target).abs <= @tolerance
raise SpecFailedException, "#{subject.inspect} should not be within #{@tolerance} of #{@target}"
end
end
end
class BeComputedByExpectation
def initialize(method, args)
@method = method
@args = args
end
def match(subject)
subject.each do |(target, *args, expected)|
actual = target.send(@method, *(@args + args))
if actual != expected
expected_bits =
if expected.methods.include?(:bytes)
expected.bytes.map { |b| lzpad(b.to_s(2), 8) }.join(' ')
else
expected.inspect
end
actual_bits =
actual.methods.include?(:bytes) ? actual.bytes.map { |b| lzpad(b.to_s(2), 8) }.join(' ') : actual.inspect
raise SpecFailedException, "#{target.inspect} should compute to #{expected_bits}, but it was #{actual_bits}"
end
end
end
def inverted_match(subject)
subject.each do |target, expected|
actual = target.send(@method, *@args)
if actual == expected
expected_bits = expected.bytes.map { |b| lzpad(b.to_s(2), 8) }.join(' ')
raise SpecFailedException, "#{target.inspect} should not compute to #{expected_bits}"
end
end
end
private
# TODO: Add % formatting to Natalie :-)
def lzpad(str, length)
str = '0' + str until str.length == length
str
end
end
class BeNanExpectation
def match(subject)
raise SpecFailedException, "#{subject.inspect} should be NaN" if !subject.nan?
end
def inverted_match(subject)
raise SpecFailedException, "#{subject.inspect} should not be NaN" if subject.nan?
end
end
class BeInfinityExpectation
def initialize(sign_of_infinity)
@sign_of_infinity = sign_of_infinity
end
def match(subject)
raise SpecFailedException, "#{subject.inspect} should be #{"-" if negative?}Infinity" unless expected_infinity?(subject)
end
private
def expected_infinity?(subject)
subject.kind_of?(Float) && subject.infinite? == @sign_of_infinity
end
def negative?
@sign_of_infinity == -1
end
end
class OutputExpectation
def initialize(expected_out, expected_err)
@expected_out = expected_out
@expected_err = expected_err
end
def match(subject)
actual_out, actual_err = capture_output do
subject.call
end
if @expected_out && !(@expected_out === actual_out)
raise SpecFailedException, "expected $stdout to get #{@expected_out.inspect} but it got #{actual_out.inspect} instead"
elsif @expected_err && !(@expected_err === actual_err)
raise SpecFailedException, "expected $stderr to get #{@expected_err.inspect} but it got #{actual_err.inspect} instead"
end
end
def inverted_match(subject)
actual_out, actual_err = capture_output do
subject.call
end
if @expected_out && @expected_out === actual_out
raise SpecFailedException, "expected $stdout not to get #{@expected_out.inspect} but it did"
elsif @expected_err && @expected_err === actual_err
raise SpecFailedException, "expected $stderr not to get #{@expected_err.inspect} but it did"
end
end
private
def capture_output
stub_out = IOStub.new
stub_err = IOStub.new
old_stdout = $stdout
old_stderr = $stderr
begin
$stdout = stub_out
$stderr = stub_err
yield
ensure
$stdout = old_stdout
$stderr = old_stderr
end
[stub_out.to_s, stub_err.to_s]
end
end
class RaiseErrorExpectation
def initialize(klass, message = nil, &block)
@klass = klass
@message = message
@block = block
end
def match(subject)
if @block
# given a block, we rescue everything!
begin
subject.call
rescue Exception => e
@block.call(e)
else
raise SpecFailedException, "#{subject.inspect} should have raised an error, but instead raised nothing"
end
return
end
begin
subject.call
rescue @klass => e
if @message.nil?
nil # good
elsif @message == e.message
nil # good
elsif @message.is_a?(Regexp) && @message =~ e.message
nil # good
else
raise SpecFailedException,
"#{subject.inspect} should have raised #{@klass.inspect} with message #{@message.inspect}, but the message was #{e.message.inspect}"
end
rescue => e
# FIXME: I don't think this `raise e if` line is needed in MRI -- smells like a Natalie bug
raise e if e.is_a?(SpecFailedException)
raise SpecFailedException,
"#{subject.inspect} should have raised #{@klass.inspect}, but instead raised #{e.inspect}"
else
raise SpecFailedException, "#{subject.inspect} should have raised #{@klass.inspect}, but instead raised nothing"
end
end
def inverted_match(subject)
if @klass
begin
subject.call
rescue @klass => e
message = subject.inspect + ' should not have raised ' + @klass.inspect
message << " (was #{e.class}: #{e.message})" if e.class != @klass
raise SpecFailedException, message
end
else
begin
subject.call
rescue => e
# FIXME: same bug as above
raise e if e.is_a?(SpecFailedException)
raise SpecFailedException, "#{subject.inspect} should not have raised any errors"
end
end
end
end
class ComplainExpectation
def initialize(message = nil, kwargs = {}, verbose: false)
@message = message
if kwargs.key?(:verbose)
@verbose = kwargs[:verbose]
else
@verbose = verbose
end
end
def match(subject)
out = run(subject)
case @message
when nil
raise SpecFailedException, 'Expected a warning but received none' if out.empty?
when Regexp
unless out =~ @message
raise SpecFailedException,
"#{subject.inspect} should have printed a warning #{@message.inspect}, but the output was #{out.inspect}"
end
when String
unless out == @message
raise SpecFailedException,
"#{subject.inspect} should have printed a warning #{@message.inspect}, but the output was #{out.inspect}"
end
else
puts "Expected a Regexp to complain but got #{@message.inspect}"
end
end
def inverted_match(subject)
out = run(subject)
case @message
when nil