wrong 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.markdown +300 -0
  2. data/lib/wrong.rb +27 -0
  3. data/lib/wrong/adapters/minitest.rb +14 -0
  4. data/lib/wrong/adapters/rspec.rb +21 -0
  5. data/lib/wrong/adapters/test_unit.rb +9 -0
  6. data/lib/wrong/assert.rb +105 -0
  7. data/lib/wrong/chunk.rb +233 -0
  8. data/lib/wrong/close_to.rb +9 -0
  9. data/lib/wrong/config.rb +29 -0
  10. data/lib/wrong/d.rb +42 -0
  11. data/lib/wrong/failure_message.rb +43 -0
  12. data/lib/wrong/helpers.rb +66 -0
  13. data/lib/wrong/irb.rb +16 -0
  14. data/lib/wrong/message/array_diff.rb +69 -0
  15. data/lib/wrong/message/string_comparison.rb +88 -0
  16. data/lib/wrong/message/test_context.rb +28 -0
  17. data/lib/wrong/rainbow.rb +127 -0
  18. data/lib/wrong/ruby2ruby_patch.rb +37 -0
  19. data/lib/wrong/sexp_ext.rb +49 -0
  20. data/lib/wrong/version.rb +3 -0
  21. data/test/adapters/minitest_test.rb +97 -0
  22. data/test/adapters/rspec1/failing_spec.rb +23 -0
  23. data/test/adapters/rspec2/failing_spec.rb +26 -0
  24. data/test/adapters/rspec_test.rb +104 -0
  25. data/test/adapters/test_unit_test.rb +59 -0
  26. data/test/assert_advanced_test.rb +51 -0
  27. data/test/assert_test.rb +76 -0
  28. data/test/capturing_test.rb +59 -0
  29. data/test/chunk_test.rb +264 -0
  30. data/test/close_to_test.rb +39 -0
  31. data/test/config_test.rb +89 -0
  32. data/test/d_test.rb +64 -0
  33. data/test/failure_message_test.rb +40 -0
  34. data/test/failures_test.rb +157 -0
  35. data/test/message/array_diff_test.rb +79 -0
  36. data/test/message/test_context_test.rb +69 -0
  37. data/test/rescuing_test.rb +17 -0
  38. data/test/separate.rb +4 -0
  39. data/test/sexp_ext_test.rb +80 -0
  40. data/test/string_comparison_test.rb +159 -0
  41. data/test/suite.rb +7 -0
  42. data/test/test_helper.rb +64 -0
  43. data/test/wrong_test.rb +60 -0
  44. metadata +215 -0
@@ -0,0 +1,69 @@
1
+ require "./test/test_helper"
2
+ require "wrong/assert"
3
+ require "wrong/message/test_context"
4
+
5
+ xdescribe "showing the lines just above where the failure occurs, so you have some context" do
6
+
7
+ include Wrong::Assert
8
+
9
+ 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
10
+ a = 1
11
+ b = 2
12
+ c = 1
13
+ assert{ a == c }
14
+ begin
15
+ assert{ a == b }
16
+ rescue Wrong::Assert::AssertionFailedError => e
17
+ puts e
18
+ assert do
19
+ e.message.include?(
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
21
+ a = 1
22
+ b = 2
23
+ c = 1
24
+ assert{ a == c }
25
+ begin
26
+ assert{ a == b } ASSERTION FAILURE test/include_test_context_test.rb:15}
27
+ )
28
+ end
29
+
30
+ deny {e.message.include?("works with it too")}
31
+ deny {e.message.include?("test_works_with_test_undercore")}
32
+ end
33
+ end
34
+
35
+ it "works with it too" do
36
+ begin
37
+ assert{ 1 == 2 }
38
+ rescue Wrong::Assert::AssertionFailedError => e
39
+ assert do
40
+ e.message.include?(
41
+ %{ it "works with it too" do
42
+ begin
43
+ assert{ 1 == 2 } ASSERTION FAILURE test/include_test_context_test.rb:36}
44
+ )
45
+ end
46
+
47
+ deny {e.message.include?("you can see test method")}
48
+ deny {e.message.include?("test_works_with_test_undercore")}
49
+ end
50
+ end
51
+
52
+ def test_works_with_test_undercore_too
53
+ begin
54
+ assert{ 1 == 2 }
55
+ rescue Wrong::Assert::AssertionFailedError => e
56
+ assert do
57
+ e.message.include?(
58
+ %{ def test_works_with_test_undercore_too
59
+ begin
60
+ assert{ 1 == 2 } ASSERTION FAILURE test/include_test_context_test.rb:53}
61
+ )
62
+ end
63
+
64
+ deny {e.message.include?("you can see test method")}
65
+ deny {e.message.include?("works with it too")}
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,17 @@
1
+ require "./test/test_helper"
2
+ require "wrong/adapters/minitest"
3
+
4
+ describe "a helper for rescuing errors" do
5
+
6
+ class RedError < StandardError; end
7
+ class BlueError < StandardError; end
8
+
9
+ it "returns the error that was raised" do
10
+ assert{ rescuing{raise RedError.new}.is_a?(RedError) }
11
+ end
12
+
13
+ it "returns nil if nothing was raised" do
14
+ assert{ rescuing{"x"}.nil? }
15
+ end
16
+
17
+ end
data/test/separate.rb ADDED
@@ -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)
@@ -0,0 +1,80 @@
1
+ require "./test/test_helper"
2
+ require "wrong/sexp_ext"
3
+
4
+ describe Sexp do
5
+ describe "#deep_clone" do
6
+ it "deeply duplicates the sexp" do
7
+ original = RubyParser.new.parse("x == 5")
8
+ duplicate = original.deep_clone
9
+ assert(original.object_id != duplicate.object_id)
10
+ assert(original[1].object_id != duplicate[1].object_id)
11
+ assert(original[1][3].object_id != duplicate[1][3].object_id)
12
+ assert(original[3].object_id != duplicate[3].object_id)
13
+ end
14
+ end
15
+
16
+ def parse(ruby)
17
+ RubyParser.new.parse(ruby)
18
+ end
19
+
20
+ describe "#to_ruby" do
21
+ it "converts the sexp to ruby code" do
22
+ sexp = parse("x == 5")
23
+ assert sexp.to_ruby == "(x == 5)"
24
+ end
25
+
26
+ it "leaves the original sexp alone" do
27
+ sexp = parse("x == 5")
28
+ assert sexp.to_ruby == "(x == 5)"
29
+ assert sexp.to_ruby == "(x == 5)" # intended
30
+ end
31
+ end
32
+
33
+ describe "#assertion? with a question mark" do
34
+ it "matches an sexp that looks like assert { }" do
35
+ sexp = parse("assert { true }")
36
+ assert sexp.assertion?
37
+ end
38
+
39
+ it "matches an sexp that looks like assert(message) { }" do
40
+ sexp = parse("assert('hi') { true }")
41
+ assert sexp.assertion?
42
+ end
43
+
44
+ it "matches an sexp that looks like deny { }" do
45
+ sexp = parse("deny { false }")
46
+ assert sexp.assertion?
47
+ end
48
+
49
+ it "doesn't match an sexp that calls assert without a block" do
50
+ sexp = parse("assert(true)")
51
+ assert !sexp.assertion?
52
+ end
53
+
54
+ it "doesn't match a normal sexp" do
55
+ sexp = parse("x == 5")
56
+ assert !sexp.assertion?
57
+ end
58
+ end
59
+
60
+ describe "#assertion" do
61
+ it "matches a top-level sexp that looks like assert { }" do
62
+ sexp = parse("assert { true }")
63
+ code = sexp.assertion.to_ruby
64
+ assert code == "assert { true }"
65
+ end
66
+
67
+ it "matches a nested sexp that looks like assert { }" do
68
+ sexp = parse("nesting { assert { true } }")
69
+ code = sexp.assertion.to_ruby
70
+ assert code == "assert { true }"
71
+ end
72
+
73
+ it "matches the first nested sexp that looks like assert { }" do
74
+ sexp = parse("nesting { assert { true } or assert { false } }")
75
+ code = sexp.assertion.to_ruby
76
+ assert code == "assert { true }"
77
+ end
78
+ end
79
+
80
+ end
@@ -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
data/test/suite.rb ADDED
@@ -0,0 +1,7 @@
1
+ #simple (but slow) way to make sure requires are isolated
2
+ failed = Dir["test/**/*_test.rb"].collect do |test_file|
3
+ ok = system("bundle exec ruby #{test_file}")
4
+ test_file unless ok
5
+ end.compact
6
+ puts "suite " + (failed.empty? ? "passed" : "FAILED: #{failed.join(', ')}")
7
+ exit(failed.empty? ? 0 : 1)
@@ -0,0 +1,64 @@
1
+ puts "RUBY_VERSION=#{RUBY_VERSION}#{" (JRuby)" if Object.const_defined?(:JRuby)}"
2
+
3
+ dir = File.dirname(__FILE__)
4
+ $LOAD_PATH.unshift "#{dir}/../lib"
5
+
6
+ predicated_project_dir = File.expand_path("../predicated")
7
+ if File.exist?(predicated_project_dir) # if predicated project is a sibling of this project
8
+ puts "using predicated from #{predicated_project_dir}"
9
+ $LOAD_PATH.unshift "#{predicated_project_dir}/lib"
10
+ require "predicated"
11
+ end
12
+
13
+ require "rubygems"
14
+ require "minitest/spec"
15
+ require "minitest/unit"
16
+ require "pp"
17
+
18
+ # yes, this does look a lot like Wrong::Assert#rescuing :-)
19
+ def get_error
20
+ error = nil
21
+ begin
22
+ yield
23
+ rescue Exception, RuntimeError => e
24
+ error = e
25
+ end
26
+ error
27
+ end
28
+
29
+ class MiniTest::Unit::TestCase
30
+ end
31
+
32
+ module Kernel
33
+ def xdescribe(str)
34
+ puts "x'd out describe \"#{str}\""
35
+ end
36
+ end
37
+
38
+ class MiniTest::Spec
39
+ include MiniTest::Assertions
40
+
41
+ class << self
42
+ def xit(str)
43
+ puts "x'd out test \"#{str}\""
44
+ end
45
+ end
46
+ end
47
+
48
+ # dummy class for use by tests
49
+ class Color
50
+ attr_reader :name
51
+ def initialize(name)
52
+ @name = name
53
+ end
54
+
55
+ def ==(other)
56
+ other.is_a?(Color) && @name == other.name
57
+ end
58
+
59
+ def inspect
60
+ "Color:#{@name}"
61
+ end
62
+ end
63
+
64
+ MiniTest::Unit.autorun
@@ -0,0 +1,60 @@
1
+ require "./test/test_helper"
2
+ require "wrong"
3
+
4
+ describe "the Wrong module" do
5
+
6
+ class Client
7
+ include Wrong
8
+ end
9
+
10
+ describe "by itself" do
11
+ it "gets the assert method" do
12
+ Wrong.assert { true }
13
+ end
14
+
15
+ it "gets the deny method" do
16
+ Wrong.deny { false }
17
+ end
18
+
19
+ it "gets the capturing method" do
20
+ value = Wrong.capturing { puts "ok" }
21
+ assert value == "ok\n"
22
+ end
23
+
24
+ it "gets the rescuing method" do
25
+ value = Wrong.rescuing { raise "uh-oh" }
26
+ assert value.message == "uh-oh"
27
+ end
28
+
29
+ end
30
+
31
+ describe "when included" do
32
+ it "gets the assert method" do
33
+ Client.new.assert { true }
34
+ end
35
+
36
+ it "gets the deny method" do
37
+ Client.new.deny { false }
38
+ end
39
+
40
+ it "gets the capturing method" do
41
+ value = Client.new.capturing { puts "ok" }
42
+ assert value == "ok\n"
43
+ end
44
+
45
+ it "gets the rescuing method" do
46
+ value = Client.new.rescuing { raise "uh-oh" }
47
+ assert value.message == "uh-oh"
48
+ end
49
+
50
+ it "adds #close_to? to Float" do
51
+ assert 1.0.respond_to?(:close_to?)
52
+ end
53
+
54
+ it "adds #d to a global namespace" do
55
+ x = 5
56
+ output = Wrong.capturing { d { x } }
57
+ Wrong.assert { output == "x is 5\n" }
58
+ end
59
+ end
60
+ end