thumblemonks-protest 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/protest/assertion.rb +26 -7
- data/lib/protest/context.rb +6 -5
- data/lib/protest/macros.rb +8 -1
- data/lib/protest.rb +1 -1
- data/protest.gemspec +1 -1
- data/test/assertion_test.rb +10 -20
- data/test/context_test.rb +21 -3
- metadata +1 -1
data/lib/protest/assertion.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
module Protest
|
2
2
|
class Failure < Exception; end
|
3
|
+
class Error < Exception
|
4
|
+
def initialize(message, e)
|
5
|
+
super(message)
|
6
|
+
set_backtrace(e.backtrace)
|
7
|
+
end
|
8
|
+
end
|
3
9
|
|
4
10
|
class Assertion
|
5
11
|
def initialize(description, &block)
|
6
12
|
@description = description
|
7
|
-
@
|
13
|
+
@evaluated_expectation = true
|
14
|
+
@expects_raise = false
|
8
15
|
expectation(true, &block)
|
9
16
|
end
|
10
17
|
|
@@ -13,7 +20,7 @@ module Protest
|
|
13
20
|
end
|
14
21
|
|
15
22
|
def not(&block)
|
16
|
-
@
|
23
|
+
@evaluated_expectation = false
|
17
24
|
expectation(@expectation, &block)
|
18
25
|
end
|
19
26
|
|
@@ -21,9 +28,19 @@ module Protest
|
|
21
28
|
expectation(expectation, &block)
|
22
29
|
end
|
23
30
|
|
24
|
-
def
|
25
|
-
|
26
|
-
|
31
|
+
def raises(expectation, &block)
|
32
|
+
@expects_raise = true
|
33
|
+
expectation(expectation, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def run(binding_scope)
|
37
|
+
begin
|
38
|
+
actual = binding_scope.instance_eval(&@block)
|
39
|
+
rescue Exception => e
|
40
|
+
errored("#{binding_scope} asserted #{self}, but errored with: #{e.to_s}", e) unless @expects_raise
|
41
|
+
actual = e.class
|
42
|
+
end
|
43
|
+
assert(@expectation == actual, "#{binding_scope} asserted #{self}, but received [#{actual}] instead")
|
27
44
|
end
|
28
45
|
private
|
29
46
|
def expectation(expectation, &block)
|
@@ -32,8 +49,10 @@ module Protest
|
|
32
49
|
self
|
33
50
|
end
|
34
51
|
|
35
|
-
def
|
36
|
-
|
52
|
+
def errored(message, e); raise Error.new(message, e); end
|
53
|
+
|
54
|
+
def assert(evaluation, message)
|
55
|
+
@evaluated_expectation == evaluation || raise(Failure, message)
|
37
56
|
end
|
38
57
|
end # Assertion
|
39
58
|
end # Protest
|
data/lib/protest/context.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Protest
|
2
2
|
class Context
|
3
|
-
attr_reader :assertions
|
3
|
+
attr_reader :assertions, :errors
|
4
4
|
def initialize(description, parent=nil)
|
5
5
|
@description = description
|
6
6
|
@assertions = []
|
7
|
-
@
|
7
|
+
@errors = []
|
8
8
|
@parent = parent
|
9
9
|
@setup = nil
|
10
10
|
end
|
@@ -32,11 +32,12 @@ module Protest
|
|
32
32
|
assertion.run(self)
|
33
33
|
writer.print '.'
|
34
34
|
rescue Protest::Failure => e
|
35
|
-
writer.print 'F'
|
36
|
-
|
35
|
+
writer.print 'F'; @errors << e
|
36
|
+
rescue Protest::Error => e
|
37
|
+
writer.print 'E'; @errors << e
|
37
38
|
end
|
38
39
|
end
|
39
|
-
@
|
40
|
+
@errors
|
40
41
|
end
|
41
42
|
|
42
43
|
def bootstrap(binder)
|
data/lib/protest/macros.rb
CHANGED
@@ -4,6 +4,13 @@ module Protest
|
|
4
4
|
asserts(description, &block).not
|
5
5
|
end
|
6
6
|
end # ContextMacros
|
7
|
+
|
8
|
+
module AssertionMacros
|
9
|
+
def nil(&block)
|
10
|
+
equals(nil, &block)
|
11
|
+
end
|
12
|
+
end # ContextMacros
|
7
13
|
end # Protest
|
8
14
|
|
9
|
-
Protest::Context.instance_eval { include Protest::ContextMacros }
|
15
|
+
Protest::Context.instance_eval { include Protest::ContextMacros }
|
16
|
+
Protest::Assertion.instance_eval { include Protest::AssertionMacros }
|
data/lib/protest.rb
CHANGED
@@ -26,7 +26,7 @@ module Protest
|
|
26
26
|
writer.puts "\n\n"
|
27
27
|
failures.each_with_index { |failure, idx|
|
28
28
|
message = ["##{idx + 1} - #{failure.to_s}"]
|
29
|
-
|
29
|
+
message += failure.backtrace
|
30
30
|
writer.puts message.join("\n") + "\n\n"
|
31
31
|
} unless failures.empty?
|
32
32
|
assertions = @contexts.inject(0) { |acc, context| acc + context.assertions.length }
|
data/protest.gemspec
CHANGED
data/test/assertion_test.rb
CHANGED
@@ -7,35 +7,25 @@ context "any assertion" do
|
|
7
7
|
end # any assertion
|
8
8
|
|
9
9
|
context "passing assertion" do
|
10
|
-
asserts("true is expected")
|
11
|
-
|
12
|
-
|
10
|
+
asserts("true is expected") { Protest::Assertion.new("i will pass") { true }.run(Object.new) }
|
11
|
+
asserts("false on denial") { Protest::Assertion.new("i will fail").not { false }.run(Object.new) }
|
12
|
+
asserts("actual result is nil") { Protest::Assertion.new("i will fail").nil { nil }.run(Object.new) }
|
13
13
|
|
14
14
|
asserts("provided block was executed and returned true") do
|
15
15
|
Protest::Assertion.new("i will pass").equals("foo bar") { "foo bar" }.run(Object.new)
|
16
16
|
end
|
17
17
|
|
18
|
-
asserts("false on denial") do
|
19
|
-
Protest::Assertion.new("i will fail").not { false }.run(Object.new)
|
20
|
-
end
|
21
|
-
|
22
18
|
asserts("expectation does not equal actual result") do
|
23
19
|
Protest::Assertion.new("i will fail").not.equals("foo") { "bar" }.run(Object.new)
|
24
20
|
end
|
25
21
|
end # passing assertion
|
26
22
|
|
27
|
-
context "failing
|
28
|
-
|
29
|
-
|
30
|
-
assertion = Protest::Assertion.new("failure") { false }
|
31
|
-
begin
|
32
|
-
assertion.run(Protest::Context.new("test context"))
|
33
|
-
rescue Protest::Failure => e
|
34
|
-
@result = e
|
35
|
-
end
|
23
|
+
context "failing assertions:" do
|
24
|
+
asserts("a Failure error is thrown").raises(Protest::Failure) do
|
25
|
+
Protest::Assertion.new("failure") { false }.run(Object.new)
|
36
26
|
end
|
37
|
-
|
38
|
-
asserts("
|
39
|
-
|
27
|
+
|
28
|
+
asserts("an Error error is thrown").raises(Protest::Error) do
|
29
|
+
Protest::Assertion.new("error") { raise Exception, "blah" }.run(Object.new)
|
40
30
|
end
|
41
|
-
end # failing
|
31
|
+
end # failing assertions
|
data/test/context_test.rb
CHANGED
@@ -2,10 +2,28 @@ require 'protest'
|
|
2
2
|
require 'stringio'
|
3
3
|
|
4
4
|
context "any context" do
|
5
|
-
|
6
|
-
Protest::Context.new("a")
|
5
|
+
setup do
|
6
|
+
@context = Protest::Context.new("a")
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
|
+
denies("two contexts with same name are the same").equals(@context) { Protest::Context.new("a") }
|
10
|
+
|
11
|
+
context "that doesn't have passing tests" do
|
12
|
+
setup do
|
13
|
+
@context.asserts("b") { false }
|
14
|
+
@context.asserts("c") { raise Exception, "blah" }
|
15
|
+
@context.run(StringIO.new)
|
16
|
+
end
|
17
|
+
|
18
|
+
asserts("that failures are captured").equals(1) do
|
19
|
+
@context.errors.select{|e| e.kind_of?(Protest::Failure)}.length
|
20
|
+
end
|
21
|
+
|
22
|
+
asserts("that unexpected errors are captured").equals(1) do
|
23
|
+
@context.errors.select{|e| e.kind_of?(Protest::Error)}.length
|
24
|
+
end
|
25
|
+
end # that doesn't have passing tests
|
26
|
+
end # any context
|
9
27
|
|
10
28
|
#
|
11
29
|
# Test Context
|