1
+ require "test/test_helper"
2
+
3
+ require "wrong/assert"
4
+
5
+ apropos "failures" do
6
+
7
+ before do
8
+ @m = Module . new do
9
+ extend Wrong ::Assert
10
+ end
11
+ end
12
+
13
+ def get_error
14
+ error = nil
15
+ begin
16
+ yield
17
+ rescue Exception => e
18
+ error = e
19
+ end
20
+ e
21
+ end
22
+
23
+ apropos "simple" do
24
+ test "raw boolean failure" do
25
+ assert_match "false" , get_error { @m . assert { false } } . message
26
+ assert_match "true" , get_error { @m . deny { true } } . message
27
+ end
28
+
29
+ test "equality failure" do
30
+ assert_match "1 is not equal to 2" , get_error { @m . assert { 1 ==2 } } . message
31
+ assert_match "1 is equal to 1" , get_error { @m . deny { 1 ==1 } } . message
32
+ end
33
+
34
+ test "failure of basic operations" do
35
+ assert_match "1 is not greater than 2" , get_error { @m . assert { 1 >2 } } . message
36
+ assert_match "2 is not less than 1" , get_error { @m . assert { 2 <1 } } . message
37
+ assert_match "1 is not greater than or equal to 2" , get_error { @m . assert { 1 >=2 } } . message
38
+ assert_match "2 is not less than or equal to 1" , get_error { @m . assert { 2 <=1 } } . message
39
+
40
+ assert_match "2 is greater than 1" , get_error { @m . deny { 2 >1 } } . message
41
+ assert_match "1 is less than 2" , get_error { @m . deny { 1 <2 } } . message
42
+ assert_match "2 is greater than or equal to 1" , get_error { @m . deny { 2 >=1 } } . message
43
+ assert_match "1 is less than or equal to 2" , get_error { @m . deny { 1 <=2 } } . message
44
+ end
45
+
46
+ class Color
47
+ attr_reader :name
48
+ def initialize ( name )
49
+ @name = name
50
+ end
51
+
52
+ def ==( other )
53
+ other . is_a? ( Color ) && @name == other . name
54
+ end
55
+
56
+ def inspect
57
+ "Color:#{ @name } "
58
+ end
59
+ end
60
+
61
+ test "object failure" do
62
+ assert_match "'Color:red' is not equal to 2" , get_error { @m . assert { Color . new ( "red" ) ==2 } } . message
63
+ end
64
+
65
+ test %{multiline assert block shouldn't look any different
66
+ than when there everything is on one line} do
67
+ assert_match ( "1 is not equal to 2" , get_error { @m . assert {
68
+ 1 ==
69
+ 2
70
+ } } . message )
71
+ end
72
+
73
+ end
74
+
75
+ apropos "accessing and printing values set outside of the assert" do
76
+ test "use a value in the assert defined outside of it" do
77
+ a = 1
78
+ assert_match "1 is not equal to 2" , get_error { @m . assert { a ==2 } } . message
79
+ assert_match "1 is equal to 1" , get_error { @m . deny { a ==1 } } . message
80
+ end
81
+ end
82
+
83
+ apropos "the assert block has many statements" do
84
+ test "only pay attention to the final statement" do
85
+ assert_match ( "1 is not equal to 2" , get_error { @m . assert {
86
+ a = "aaa"
87
+ b = 1 + 2
88
+ c = [ "foo" , "bar" ] . length / 3
89
+ if a =="aaa"
90
+ b = 4
91
+ end ; 1 ==2
92
+ } } . message )
93
+ end
94
+ end
95
+ end
0 commit comments