wrong 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.markdown +77 -11
  2. data/lib/wrong.rb +20 -0
  3. data/lib/wrong/adapters/minitest.rb +6 -18
  4. data/lib/wrong/adapters/rspec.rb +18 -7
  5. data/lib/wrong/adapters/test_unit.rb +2 -2
  6. data/lib/wrong/assert.rb +45 -90
  7. data/lib/wrong/chunk.rb +89 -27
  8. data/lib/wrong/close_to.rb +7 -11
  9. data/lib/wrong/d.rb +42 -0
  10. data/lib/wrong/failure_message.rb +43 -0
  11. data/lib/wrong/helpers.rb +66 -0
  12. data/lib/wrong/irb.rb +1 -1
  13. data/lib/wrong/message/array_diff.rb +57 -75
  14. data/lib/wrong/message/string_comparison.rb +88 -0
  15. data/lib/wrong/message/test_context.rb +2 -0
  16. data/lib/{predicated/lib/predicated/sexp_patch.rb → wrong/ruby2ruby_patch.rb} +3 -5
  17. data/lib/wrong/sexp_ext.rb +12 -4
  18. data/lib/wrong/version.rb +1 -1
  19. data/test/adapters/rspec1/failing_spec.rb +23 -0
  20. data/test/adapters/rspec2/failing_spec.rb +26 -0
  21. data/test/adapters/rspec_test.rb +65 -4
  22. data/test/adapters/test_unit_test.rb +6 -1
  23. data/test/assert_advanced_test.rb +51 -0
  24. data/test/assert_test.rb +4 -48
  25. data/test/capturing_test.rb +4 -2
  26. data/test/chunk_test.rb +36 -11
  27. data/test/close_to_test.rb +2 -2
  28. data/test/config_test.rb +5 -5
  29. data/test/d_test.rb +64 -0
  30. data/test/failure_message_test.rb +40 -0
  31. data/test/failures_test.rb +6 -7
  32. data/test/message/array_diff_test.rb +18 -14
  33. data/test/message/{test_context_text.rb → test_context_test.rb} +2 -1
  34. data/test/rescuing_test.rb +5 -4
  35. data/test/separate.rb +4 -0
  36. data/test/sexp_ext_test.rb +2 -2
  37. data/test/string_comparison_test.rb +159 -0
  38. data/test/suite.rb +7 -4
  39. data/test/test_helper.rb +2 -0
  40. data/test/wrong_test.rb +60 -0
  41. metadata +92 -46
  42. data/lib/wrong/message/string_diff.rb +0 -42
  43. data/test/message/string_diff_test.rb +0 -89
@@ -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
@@ -14,7 +14,6 @@ describe "failures" do
14
14
 
15
15
  it "raw boolean assert failure" do
16
16
  error = get_error { @m.assert { false } }
17
- # puts error.message
18
17
  assert_match "false", error.message
19
18
  end
20
19
 
@@ -22,7 +21,6 @@ describe "failures" do
22
21
  error = get_error {
23
22
  @m.deny { true }
24
23
  }
25
- # puts error.message
26
24
  assert_match "true", error.message
27
25
  end
28
26
 
@@ -74,7 +72,7 @@ describe "failures" do
74
72
  assert_match("1 is not equal to 2", get_error {
75
73
  @m.assert {
76
74
  1==
77
- 2
75
+ 2
78
76
  }
79
77
  }.message)
80
78
  end
@@ -97,7 +95,7 @@ describe "failures" do
97
95
  it "omit a primary failure message since 'This is not true etc.' is more obscuring than clarifying" do
98
96
  m = get_error {
99
97
  x = 5
100
- @m.assert { x == 5 && x != 5}
98
+ @m.assert { x == 5 && x != 5 }
101
99
  }.message
102
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"
103
101
  end
@@ -137,10 +135,11 @@ describe "failures" do
137
135
 
138
136
  describe "hash comparisons" do
139
137
  it "basic" do
138
+ e = get_error {
139
+ @m.assert { {1=>2}=={"a"=>"b"} }
140
+ }
140
141
  assert_match '{1=>2} is not equal to {"a"=>"b"}',
141
- get_error {
142
- @m.assert { {1=>2}=={"a"=>"b"} }
143
- }.message
142
+ e.message
144
143
  end
145
144
  end
146
145
 
@@ -1,15 +1,18 @@
1
1
  require "./test/test_helper"
2
2
  require "wrong/assert"
3
- require "wrong/message/array_diff"
3
+ require "wrong/helpers"
4
4
  require "wrong/adapters/minitest"
5
5
 
6
+ require "wrong/message/array_diff"
7
+
6
8
  describe "when you're comparing strings and they don't match, show me the diff message" do
7
9
 
8
10
  def assert_string_diff_message(first_array, second_array, expected_error_message)
11
+ e = rescuing {
12
+ Wrong.assert { first_array == second_array }
13
+ }
9
14
  assert {
10
- rescuing {
11
- assert { first_array == second_array }
12
- }.message.include?(expected_error_message)
15
+ e.message.include?(expected_error_message.strip)
13
16
  }
14
17
  end
15
18
 
@@ -17,26 +20,27 @@ describe "when you're comparing strings and they don't match, show me the diff m
17
20
  deny {
18
21
  rescuing {
19
22
  assert { [1]==2 }
20
- }.message.include?("diff")
23
+ }.message.include?("^")
21
24
  }
22
25
  deny {
23
26
  rescuing {
24
27
  assert { nil==[1] }
25
- }.message.include?("diff")
28
+ }.message.include?("^")
26
29
  }
27
30
  end
28
31
 
29
32
  it "simple" do
33
+ e = rescuing {
34
+ assert { ["a"]==["b"] }
35
+ }
30
36
  assert {
31
- rescuing {
32
- assert { ["a"]==["b"] }
33
- }.message.include?("diff")
37
+ e.message.include?("^")
34
38
  }
35
39
 
36
40
  assert_string_diff_message(["a", "b"], ["a", "c", "c"], %{
37
41
  ["a", "b"]
38
42
  ["a", "c", "c"]
39
- ^ ^
43
+ ^ ^
40
44
  })
41
45
  end
42
46
 
@@ -44,13 +48,13 @@ describe "when you're comparing strings and they don't match, show me the diff m
44
48
  assert_string_diff_message(["a", "b", "c"], ["a", "cccc", "c"], %{
45
49
  ["a", "b" , "c"]
46
50
  ["a", "cccc", "c"]
47
- ^
51
+ ^
48
52
  })
49
53
 
50
54
  assert_string_diff_message(["a", "b", "c", "d"], ["a", "cccc", "xxx", "d"], %{
51
55
  ["a", "b" , "c" , "d"]
52
56
  ["a", "cccc", "xxx", "d"]
53
- ^ ^
57
+ ^ ^
54
58
  })
55
59
  end
56
60
 
@@ -58,7 +62,7 @@ describe "when you're comparing strings and they don't match, show me the diff m
58
62
  assert_string_diff_message([1, true], [2, true, nil], %{
59
63
  [1, true]
60
64
  [2, true, nil]
61
- ^ ^
65
+ ^ ^
62
66
  })
63
67
  end
64
68
 
@@ -67,7 +71,7 @@ describe "when you're comparing strings and they don't match, show me the diff m
67
71
  assert_string_diff_message([1, [2]], [1, [2, 3]], %{
68
72
  [1, [2] ]
69
73
  [1, [2, 3]]
70
- ^
74
+ ^
71
75
  })
72
76
 
73
77
  end
@@ -2,7 +2,7 @@ require "./test/test_helper"
2
2
  require "wrong/assert"
3
3
  require "wrong/message/test_context"
4
4
 
5
- describe "showing the lines just above where the failure occurs, so you have some context" do
5
+ xdescribe "showing the lines just above where the failure occurs, so you have some context" do
6
6
 
7
7
  include Wrong::Assert
8
8
 
@@ -14,6 +14,7 @@ describe "showing the lines just above where the failure occurs, so you have som
14
14
  begin
15
15
  assert{ a == b }
16
16
  rescue Wrong::Assert::AssertionFailedError => e
17
+ puts e
17
18
  assert do
18
19
  e.message.include?(
19
20
  %{ it "you can see test method all the way back to the start of the test, plus an indication of where the failure was" do
@@ -1,15 +1,16 @@
1
1
  require "./test/test_helper"
2
-
3
- require "wrong/assert"
4
2
  require "wrong/adapters/minitest"
5
3
 
6
- describe "a tool for rescuing errors" do
4
+ describe "a helper for rescuing errors" do
7
5
 
8
6
  class RedError < StandardError; end
9
7
  class BlueError < StandardError; end
10
8
 
11
- it "catch the error and assert on it" do
9
+ it "returns the error that was raised" do
12
10
  assert{ rescuing{raise RedError.new}.is_a?(RedError) }
11
+ end
12
+
13
+ it "returns nil if nothing was raised" do
13
14
  assert{ rescuing{"x"}.nil? }
14
15
  end
15
16
 
@@ -0,0 +1,4 @@
1
+ #simple (but slow) way to make sure requires are isolated
2
+ result = Dir["test/**/*_test.rb"].collect{|test_file| system("bundle exec ruby #{test_file}") }.uniq == [true]
3
+ puts "suite " + (result ? "passed" : "FAILED")
4
+ exit(result ? 0 : 1)
@@ -2,10 +2,10 @@ require "./test/test_helper"
2
2
  require "wrong/sexp_ext"
3
3
 
4
4
  describe Sexp do
5
- describe "#doop" do
5
+ describe "#deep_clone" do
6
6
  it "deeply duplicates the sexp" do
7
7
  original = RubyParser.new.parse("x == 5")
8
- duplicate = original.doop
8
+ duplicate = original.deep_clone
9
9
  assert(original.object_id != duplicate.object_id)
10
10
  assert(original[1].object_id != duplicate[1].object_id)
11
11
  assert(original[1][3].object_id != duplicate[1][3].object_id)
@@ -0,0 +1,159 @@
1
+ require "./test/test_helper"
2
+ require "wrong/adapters/minitest"
3
+ require "wrong/message/string_comparison"
4
+
5
+ module Wrong
6
+ describe StringComparison do
7
+
8
+ before do
9
+ # crank the window and prelude down for these tests
10
+ @old_window = StringComparison.window
11
+ @old_prelude = StringComparison.prelude
12
+ StringComparison.window = 16
13
+ StringComparison.prelude = 8
14
+ end
15
+
16
+ after do
17
+ StringComparison.window = @old_window
18
+ StringComparison.prelude = @old_prelude
19
+ end
20
+
21
+ describe '#same?' do
22
+ it "says identical empty strings are the same" do
23
+ comparison = StringComparison.new("", "")
24
+ assert { comparison.same? }
25
+ end
26
+
27
+ it "says identical non-empty strings are the same" do
28
+ comparison = StringComparison.new("abc", "abc")
29
+ assert { comparison.same? }
30
+ end
31
+
32
+ it "says two nils are the same" do
33
+ comparison = StringComparison.new(nil, nil)
34
+ assert { comparison.same? }
35
+ end
36
+
37
+ it "says a string is different from a different string" do
38
+ comparison = StringComparison.new("abc", "xyz")
39
+ deny { comparison.same? }
40
+ end
41
+
42
+ it "says a string is different from nil" do
43
+ comparison = StringComparison.new("abc", nil)
44
+ deny { comparison.same? }
45
+ end
46
+
47
+ it "says nil is different from a string" do
48
+ comparison = StringComparison.new(nil, "abc")
49
+ deny { comparison.same? }
50
+ end
51
+ end
52
+
53
+ describe '#different_at' do
54
+ describe "returns the location where two strings differ" do
55
+
56
+ it "at the beginning of the strings" do
57
+ assert { StringComparison.new("abc", "xyz").different_at == 0 }
58
+ end
59
+
60
+ it "at the middle of the strings" do
61
+ assert { StringComparison.new("abc", "ayz").different_at == 1 }
62
+ end
63
+
64
+ it "when the first string is longer" do
65
+ assert { StringComparison.new("abcd", "abc").different_at == 3 }
66
+ end
67
+
68
+ it "when the second string is longer" do
69
+ assert { StringComparison.new("abc", "abcd").different_at == 3 }
70
+ end
71
+
72
+ it "with nil as the first string" do
73
+ assert { StringComparison.new(nil, "abc").different_at == 0 }
74
+ end
75
+
76
+ it "with nil as the second string" do
77
+ assert { StringComparison.new("abc", nil).different_at == 0 }
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ describe '#message' do
84
+ def compare(first, second, expected_message)
85
+ expected_message.strip!
86
+ assert { StringComparison.new(first, second).message == expected_message }
87
+ end
88
+
89
+ it 'shows the whole of both strings when the difference is near the start' do
90
+ compare "abc", "xyz", <<-MESSAGE
91
+ Strings differ at position 0:
92
+ first: "abc"
93
+ second: "xyz"
94
+ MESSAGE
95
+ end
96
+
97
+ it 'shows ellipses when the difference is in the middle of a long string' do
98
+ compare "abcdefghijklmnopqrstuvwxyz", "abcdefghijkl*nopqrstuvwxyz", <<-MESSAGE
99
+ Strings differ at position 12:
100
+ first: ..."efghijklmnopqrst"...
101
+ second: ..."efghijkl*nopqrst"...
102
+ MESSAGE
103
+ end
104
+
105
+ it 'shows ellipses when the difference is near the beginning of a long string' do
106
+ compare "abcdefghijklmnopqrstuvwxyz", "a*cdefghijklmnopqrstuvwxyz", <<-MESSAGE
107
+ Strings differ at position 1:
108
+ first: "abcdefghijklmnop"...
109
+ second: "a*cdefghijklmnop"...
110
+ MESSAGE
111
+ end
112
+
113
+ it 'shows ellipses when the difference is near the end of a long string' do
114
+ compare "abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvw*yz", <<-MESSAGE
115
+ Strings differ at position 23:
116
+ first: ..."pqrstuvwxyz"
117
+ second: ..."pqrstuvw*yz"
118
+ MESSAGE
119
+ end
120
+
121
+ it 'allows user to override the default window size' do
122
+ original = StringComparison.window
123
+ begin
124
+ StringComparison.window = 10
125
+ compare "abcdefghijklmnopqrstuvwxyz", "a*cdefghijklmnopqrstuvwxyz", <<-MESSAGE
126
+ Strings differ at position 1:
127
+ first: "abcdefghij"...
128
+ second: "a*cdefghij"...
129
+ MESSAGE
130
+ ensure
131
+ StringComparison.window = original
132
+ end
133
+ end
134
+
135
+ it 'allows user to override the prelude size' do
136
+ original = StringComparison.prelude
137
+ begin
138
+ StringComparison.prelude = 2
139
+ compare "abcdefghijklmnopqrstuvwxyz", "abcdefghijkl*nopqrstuvwxyz", <<-MESSAGE
140
+ Strings differ at position 12:
141
+ first: ..."klmnopqrstuvwxyz"
142
+ second: ..."kl*nopqrstuvwxyz"
143
+ MESSAGE
144
+ ensure
145
+ StringComparison.prelude = original
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ describe "Wrong integration" do
152
+ it "works" do
153
+ error = rescuing do
154
+ assert { "xyz" == "abc" }
155
+ end
156
+ assert { error.message =~ /Strings differ/ }
157
+ end
158
+ end
159
+ end