wrong 0.4.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +300 -0
- data/lib/wrong.rb +27 -0
- data/lib/wrong/adapters/minitest.rb +14 -0
- data/lib/wrong/adapters/rspec.rb +21 -0
- data/lib/wrong/adapters/test_unit.rb +9 -0
- data/lib/wrong/assert.rb +105 -0
- data/lib/wrong/chunk.rb +233 -0
- data/lib/wrong/close_to.rb +9 -0
- data/lib/wrong/config.rb +29 -0
- data/lib/wrong/d.rb +42 -0
- data/lib/wrong/failure_message.rb +43 -0
- data/lib/wrong/helpers.rb +66 -0
- data/lib/wrong/irb.rb +16 -0
- data/lib/wrong/message/array_diff.rb +69 -0
- data/lib/wrong/message/string_comparison.rb +88 -0
- data/lib/wrong/message/test_context.rb +28 -0
- data/lib/wrong/rainbow.rb +127 -0
- data/lib/wrong/ruby2ruby_patch.rb +37 -0
- data/lib/wrong/sexp_ext.rb +49 -0
- data/lib/wrong/version.rb +3 -0
- data/test/adapters/minitest_test.rb +97 -0
- data/test/adapters/rspec1/failing_spec.rb +23 -0
- data/test/adapters/rspec2/failing_spec.rb +26 -0
- data/test/adapters/rspec_test.rb +104 -0
- data/test/adapters/test_unit_test.rb +59 -0
- data/test/assert_advanced_test.rb +51 -0
- data/test/assert_test.rb +76 -0
- data/test/capturing_test.rb +59 -0
- data/test/chunk_test.rb +264 -0
- data/test/close_to_test.rb +39 -0
- data/test/config_test.rb +89 -0
- data/test/d_test.rb +64 -0
- data/test/failure_message_test.rb +40 -0
- data/test/failures_test.rb +157 -0
- data/test/message/array_diff_test.rb +79 -0
- data/test/message/test_context_test.rb +69 -0
- data/test/rescuing_test.rb +17 -0
- data/test/separate.rb +4 -0
- data/test/sexp_ext_test.rb +80 -0
- data/test/string_comparison_test.rb +159 -0
- data/test/suite.rb +7 -0
- data/test/test_helper.rb +64 -0
- data/test/wrong_test.rb +60 -0
- metadata +215 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
require "wrong/close_to"
|
3
|
+
require "wrong"
|
4
|
+
|
5
|
+
describe "#close_to? (monkey patch for float comparison)" do
|
6
|
+
include Wrong
|
7
|
+
|
8
|
+
it "says two equal floats are equal" do
|
9
|
+
assert { 5.0.close_to? 5.0 }
|
10
|
+
end
|
11
|
+
|
12
|
+
it "says two unequal floats are unequal" do
|
13
|
+
deny { 5.0.close_to? 6.0 }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "has a default tolerance of 0.001" do
|
17
|
+
assert { 5.0.close_to? 5.0001 }
|
18
|
+
assert { 5.0.close_to? 5.0009 }
|
19
|
+
deny { 5.0.close_to? 5.001 }
|
20
|
+
deny { 5.0.close_to? 5.01 }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "takes a tolerance parameter" do
|
24
|
+
assert { 5.0.close_to? 5.01, 0.1 }
|
25
|
+
end
|
26
|
+
|
27
|
+
it "excludes the tolerance maximum" do
|
28
|
+
assert { 5.0.close_to? 5.9999, 1.0 }
|
29
|
+
deny { 5.0.close_to? 6.00, 1.0 }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "works for integers too" do
|
33
|
+
assert { 5.close_to? 5 }
|
34
|
+
assert { 5.close_to? 5.0001 }
|
35
|
+
deny { 5.close_to? 5.1 }
|
36
|
+
assert { 5.close_to? 5.1, 0.5 }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/test/config_test.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
|
3
|
+
require "wrong"
|
4
|
+
require "wrong/config"
|
5
|
+
require "wrong/message/string_comparison"
|
6
|
+
|
7
|
+
describe Wrong::Config do
|
8
|
+
|
9
|
+
# hope this doesn't blow up, but I'll try to use Wrong to test the Config object
|
10
|
+
include Wrong
|
11
|
+
|
12
|
+
before do
|
13
|
+
Wrong.config.clear
|
14
|
+
end
|
15
|
+
|
16
|
+
it "singleton" do
|
17
|
+
c = Wrong.config
|
18
|
+
assert { c.is_a?(Wrong::Config) }
|
19
|
+
c2 = Wrong.config
|
20
|
+
assert { c.object_id == c2.object_id }
|
21
|
+
end
|
22
|
+
|
23
|
+
# it "reads from a .wrong file"
|
24
|
+
|
25
|
+
it "getting an undeclared setting" do
|
26
|
+
assert { Wrong.config[:foo].nil? }
|
27
|
+
end
|
28
|
+
|
29
|
+
it "setting and getting" do
|
30
|
+
Wrong.config[:foo] = "bar"
|
31
|
+
assert { Wrong.config[:foo] == "bar" }
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "adding aliases for assert" do
|
35
|
+
before do
|
36
|
+
Wrong.config.alias_assert(:is)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "succeeds" do
|
40
|
+
is { 2 + 2 == 4 }
|
41
|
+
end
|
42
|
+
|
43
|
+
it "fails" do
|
44
|
+
e = rescuing {
|
45
|
+
is("math is hard") { 2 + 2 == 5 }
|
46
|
+
}
|
47
|
+
expected = <<-FAIL
|
48
|
+
math is hard: Expected ((2 + 2) == 5), but 4 is not equal to 5
|
49
|
+
(2 + 2) is 4
|
50
|
+
FAIL
|
51
|
+
assert { e.message == expected }
|
52
|
+
end
|
53
|
+
|
54
|
+
it "doesn't keep aliasing the same word" do
|
55
|
+
Wrong.config.alias_assert(:is)
|
56
|
+
Wrong.config.alias_assert(:is)
|
57
|
+
assert { Wrong.config.assert_method_names == [:assert, :is] }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "adding aliases for deny" do
|
62
|
+
before do
|
63
|
+
Wrong.config.alias_deny(:aint)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "succeeds" do
|
67
|
+
aint { 2 + 2 == 5 }
|
68
|
+
end
|
69
|
+
|
70
|
+
it "fails" do
|
71
|
+
e = rescuing {
|
72
|
+
aint("math is hard") { 2 + 2 == 4 }
|
73
|
+
}
|
74
|
+
expected = <<-FAIL
|
75
|
+
math is hard: Didn't expect ((2 + 2) == 4), but 4 is equal to 4
|
76
|
+
(2 + 2) is 4
|
77
|
+
FAIL
|
78
|
+
assert { e.message == expected }
|
79
|
+
end
|
80
|
+
|
81
|
+
it "doesn't keep aliasing the same word" do
|
82
|
+
Wrong.config.alias_deny(:aint)
|
83
|
+
Wrong.config.alias_deny(:aint)
|
84
|
+
assert { Wrong.config.deny_method_names == [:deny, :aint] }
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
data/test/d_test.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
require "wrong"
|
3
|
+
require "wrong/d"
|
4
|
+
require "wrong/adapters/minitest"
|
5
|
+
|
6
|
+
describe "d" do
|
7
|
+
include Wrong::D
|
8
|
+
|
9
|
+
it "prints its argument's name and its value" do
|
10
|
+
x = 5
|
11
|
+
output = capturing do
|
12
|
+
d { x }
|
13
|
+
end
|
14
|
+
assert { output == "x is 5\n" }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "inspects the value" do
|
18
|
+
x = "one\ttwo"
|
19
|
+
output = capturing do
|
20
|
+
d { x }
|
21
|
+
end
|
22
|
+
assert { output == "x is \"one\\ttwo\"\n" }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "works on an expression" do
|
26
|
+
x = 5
|
27
|
+
output = capturing do
|
28
|
+
d { x + 2 }
|
29
|
+
end
|
30
|
+
assert { output == "(x + 2) is 7\n" }
|
31
|
+
end
|
32
|
+
|
33
|
+
it "works even if it's not the only thing on the line" do
|
34
|
+
x = 8
|
35
|
+
output = capturing do
|
36
|
+
x; d { x }
|
37
|
+
end
|
38
|
+
assert { output == "x is 8\n" }
|
39
|
+
end
|
40
|
+
|
41
|
+
it "works even if it's nested in another block on the same line" do
|
42
|
+
x = 8
|
43
|
+
output = capturing do
|
44
|
+
assert { d { x }; true }
|
45
|
+
end
|
46
|
+
assert { output == "x is 8\n" }
|
47
|
+
end
|
48
|
+
|
49
|
+
it "works when called on an extending module" do
|
50
|
+
module Something
|
51
|
+
extend Wrong::D
|
52
|
+
end
|
53
|
+
x = 99
|
54
|
+
output = capturing { Something.d { x }}
|
55
|
+
assert { output == "x is 99\n" }
|
56
|
+
end
|
57
|
+
|
58
|
+
it "works when called on the D module" do
|
59
|
+
x = 9
|
60
|
+
output = capturing { Wrong::D.d { x }}
|
61
|
+
assert { output == "x is 9\n" }
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
require "wrong/assert"
|
3
|
+
require "wrong/failure_message"
|
4
|
+
|
5
|
+
module Wrong
|
6
|
+
|
7
|
+
class BogusFormatter < FailureMessage::Formatter
|
8
|
+
def match?
|
9
|
+
predicate.is_a? BogusPredicate
|
10
|
+
end
|
11
|
+
|
12
|
+
def describe
|
13
|
+
"bogus #{predicate.object_id}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class BogusPredicate < Predicated::Predicate
|
18
|
+
end
|
19
|
+
|
20
|
+
describe FailureMessage::Formatter do
|
21
|
+
include Wrong::Assert
|
22
|
+
|
23
|
+
it "describes a predicate" do
|
24
|
+
predicate = BogusPredicate.new
|
25
|
+
formatter = BogusFormatter.new(predicate)
|
26
|
+
assert { formatter.describe == "bogus #{predicate.object_id}" }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe FailureMessage do
|
31
|
+
include Wrong::Assert
|
32
|
+
|
33
|
+
it "can register a formatter class for a predicate pattern" do
|
34
|
+
FailureMessage.register_formatter(BogusFormatter)
|
35
|
+
assert { FailureMessage.formatter_for(BogusPredicate.new).is_a? BogusFormatter }
|
36
|
+
assert { FailureMessage.formatters.include?(BogusFormatter)}
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
|
3
|
+
require "wrong/assert"
|
4
|
+
|
5
|
+
describe "failures" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@m = Module.new do
|
9
|
+
extend Wrong::Assert
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "simple" do
|
14
|
+
|
15
|
+
it "raw boolean assert failure" do
|
16
|
+
error = get_error { @m.assert { false } }
|
17
|
+
assert_match "false", error.message
|
18
|
+
end
|
19
|
+
|
20
|
+
it "raw boolean deny failure" do
|
21
|
+
error = get_error {
|
22
|
+
@m.deny { true }
|
23
|
+
}
|
24
|
+
assert_match "true", error.message
|
25
|
+
end
|
26
|
+
|
27
|
+
it "equality failure" do
|
28
|
+
assert_match "1 is not equal to 2", get_error {
|
29
|
+
@m.assert { 1==2 }
|
30
|
+
}.message
|
31
|
+
assert_match "1 is equal to 1", get_error {
|
32
|
+
@m.deny { 1==1 }
|
33
|
+
}.message
|
34
|
+
end
|
35
|
+
|
36
|
+
it "failure of basic operations" do
|
37
|
+
assert_match "1 is not greater than 2", get_error {
|
38
|
+
@m.assert { 1>2 }
|
39
|
+
}.message
|
40
|
+
assert_match "2 is not less than 1", get_error {
|
41
|
+
@m.assert { 2<1 }
|
42
|
+
}.message
|
43
|
+
assert_match "1 is not greater than or equal to 2", get_error {
|
44
|
+
@m.assert { 1>=2 }
|
45
|
+
}.message
|
46
|
+
assert_match "2 is not less than or equal to 1", get_error {
|
47
|
+
@m.assert { 2<=1 }
|
48
|
+
}.message
|
49
|
+
|
50
|
+
assert_match "2 is greater than 1", get_error {
|
51
|
+
@m.deny { 2>1 }
|
52
|
+
}.message
|
53
|
+
assert_match "1 is less than 2", get_error {
|
54
|
+
@m.deny { 1<2 }
|
55
|
+
}.message
|
56
|
+
assert_match "2 is greater than or equal to 1", get_error {
|
57
|
+
@m.deny { 2>=1 }
|
58
|
+
}.message
|
59
|
+
assert_match "1 is less than or equal to 2", get_error {
|
60
|
+
@m.deny { 1<=2 }
|
61
|
+
}.message
|
62
|
+
end
|
63
|
+
|
64
|
+
it "object failure" do
|
65
|
+
assert_match "Color:red is not equal to 2", get_error {
|
66
|
+
@m.assert { Color.new("red")==2 }
|
67
|
+
}.message
|
68
|
+
end
|
69
|
+
|
70
|
+
it %{multiline assert block shouldn't look any different
|
71
|
+
than when there everything is on one line} do
|
72
|
+
assert_match("1 is not equal to 2", get_error {
|
73
|
+
@m.assert {
|
74
|
+
1==
|
75
|
+
2
|
76
|
+
}
|
77
|
+
}.message)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "accessing and printing values set outside of the assert" do
|
83
|
+
it "use a value in the assert defined outside of it" do
|
84
|
+
a = 1
|
85
|
+
assert_match "1 is not equal to 2", get_error {
|
86
|
+
@m.assert { a==2 }
|
87
|
+
}.message
|
88
|
+
assert_match "1 is equal to 1", get_error {
|
89
|
+
@m.deny { a==1 }
|
90
|
+
}.message
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "conjunctions (and and or)" do
|
95
|
+
it "omit a primary failure message since 'This is not true etc.' is more obscuring than clarifying" do
|
96
|
+
m = get_error {
|
97
|
+
x = 5
|
98
|
+
@m.assert { x == 5 && x != 5 }
|
99
|
+
}.message
|
100
|
+
assert m == "Expected ((x == 5) and (not (x == 5))), but \n (x == 5) is true\n x is 5\n (not (x == 5)) is false\n"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "the assert block has many statements" do
|
105
|
+
it "only pay attention to the final statement" do
|
106
|
+
assert_match("1 is not equal to 2", get_error {
|
107
|
+
@m.assert {
|
108
|
+
a = "aaa"
|
109
|
+
b = 1 + 2
|
110
|
+
c = ["foo", "bar"].length / 3
|
111
|
+
if a=="aaa"
|
112
|
+
b = 4
|
113
|
+
end; 1==2
|
114
|
+
}
|
115
|
+
}.message)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "works even if the assertion is based on stuff set previously in the block" do
|
119
|
+
assert_match("\"aaa\" is not equal to \"bbb\"", get_error {
|
120
|
+
@m.assert {
|
121
|
+
a = "aaa"
|
122
|
+
a=="bbb"
|
123
|
+
}
|
124
|
+
}.message)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "array comparisons" do
|
129
|
+
it "basic" do
|
130
|
+
assert_match %{[1, 2] is not equal to ["a", "b"]}, get_error {
|
131
|
+
@m.assert { [1, 2]==%w{a b} }
|
132
|
+
}.message
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "hash comparisons" do
|
137
|
+
it "basic" do
|
138
|
+
e = get_error {
|
139
|
+
@m.assert { {1=>2}=={"a"=>"b"} }
|
140
|
+
}
|
141
|
+
assert_match '{1=>2} is not equal to {"a"=>"b"}',
|
142
|
+
e.message
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "methods that result in a boolean. this might be hard." do
|
147
|
+
it "string include" do
|
148
|
+
assert_match "\"abc\" does not include \"cd\"", get_error {
|
149
|
+
@m.assert { "abc".include?("cd") }
|
150
|
+
}.message
|
151
|
+
assert_match "\"abc\" includes \"bc\"", get_error {
|
152
|
+
@m.deny { "abc".include?("bc") }
|
153
|
+
}.message
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "./test/test_helper"
|
2
|
+
require "wrong/assert"
|
3
|
+
require "wrong/helpers"
|
4
|
+
require "wrong/adapters/minitest"
|
5
|
+
|
6
|
+
require "wrong/message/array_diff"
|
7
|
+
|
8
|
+
describe "when you're comparing strings and they don't match, show me the diff message" do
|
9
|
+
|
10
|
+
def assert_string_diff_message(first_array, second_array, expected_error_message)
|
11
|
+
e = rescuing {
|
12
|
+
Wrong.assert { first_array == second_array }
|
13
|
+
}
|
14
|
+
assert {
|
15
|
+
e.message.include?(expected_error_message.strip)
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
it "don't attempt to do this if the assertion is not of the form a_array==b_array" do
|
20
|
+
deny {
|
21
|
+
rescuing {
|
22
|
+
assert { [1]==2 }
|
23
|
+
}.message.include?("^")
|
24
|
+
}
|
25
|
+
deny {
|
26
|
+
rescuing {
|
27
|
+
assert { nil==[1] }
|
28
|
+
}.message.include?("^")
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
it "simple" do
|
33
|
+
e = rescuing {
|
34
|
+
assert { ["a"]==["b"] }
|
35
|
+
}
|
36
|
+
assert {
|
37
|
+
e.message.include?("^")
|
38
|
+
}
|
39
|
+
|
40
|
+
assert_string_diff_message(["a", "b"], ["a", "c", "c"], %{
|
41
|
+
["a", "b"]
|
42
|
+
["a", "c", "c"]
|
43
|
+
^ ^
|
44
|
+
})
|
45
|
+
end
|
46
|
+
|
47
|
+
it "elements align properly" do
|
48
|
+
assert_string_diff_message(["a", "b", "c"], ["a", "cccc", "c"], %{
|
49
|
+
["a", "b" , "c"]
|
50
|
+
["a", "cccc", "c"]
|
51
|
+
^
|
52
|
+
})
|
53
|
+
|
54
|
+
assert_string_diff_message(["a", "b", "c", "d"], ["a", "cccc", "xxx", "d"], %{
|
55
|
+
["a", "b" , "c" , "d"]
|
56
|
+
["a", "cccc", "xxx", "d"]
|
57
|
+
^ ^
|
58
|
+
})
|
59
|
+
end
|
60
|
+
|
61
|
+
it "different primitive types" do
|
62
|
+
assert_string_diff_message([1, true], [2, true, nil], %{
|
63
|
+
[1, true]
|
64
|
+
[2, true, nil]
|
65
|
+
^ ^
|
66
|
+
})
|
67
|
+
end
|
68
|
+
|
69
|
+
it "2d array - just inspects the inner array like it would any other element" do
|
70
|
+
assert { [1, [2, 3]] == [1, [2, 3]] }
|
71
|
+
assert_string_diff_message([1, [2]], [1, [2, 3]], %{
|
72
|
+
[1, [2] ]
|
73
|
+
[1, [2, 3]]
|
74
|
+
^
|
75
|
+
})
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|