riot 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +22 -0
- data/lib/riot.rb +2 -2
- data/lib/riot/assertion.rb +5 -1
- data/lib/riot/context.rb +5 -1
- data/lib/riot/macros.rb +18 -0
- data/riot.gemspec +3 -2
- data/test/assertion_test.rb +68 -20
- data/test/context_test.rb +1 -1
- data/test/teststrap.rb +3 -0
- metadata +3 -2
data/README.markdown
CHANGED
@@ -131,6 +131,28 @@ When you want to test that an expression returns an object of an expected type:
|
|
131
131
|
asserts("its balance") { @user.balance }.kind_of(Currency)
|
132
132
|
end
|
133
133
|
|
134
|
+
#### Example: Assigns
|
135
|
+
|
136
|
+
Sometimes you want to check and see if an instance variable is defined.
|
137
|
+
|
138
|
+
context "a new user" do
|
139
|
+
setup do
|
140
|
+
User.new(:email => "foo@bar.baz", :first_name => nil)
|
141
|
+
end
|
142
|
+
asserts("has an email address") { topic }.assigns(:email)
|
143
|
+
asserts("has a first name") { topic }.assigns(:first_name) # This will fail
|
144
|
+
end
|
145
|
+
|
146
|
+
While other times you also want to make sure the value of the instance variable is what you expect it to be.
|
147
|
+
|
148
|
+
context "a new user" do
|
149
|
+
setup do
|
150
|
+
User.new(:email => "foo@bar.baz", :first_name => "John")
|
151
|
+
end
|
152
|
+
asserts("has an email address") { topic }.assigns(:email, "foo@bar.baz")
|
153
|
+
asserts("has a first name") { topic }.assigns(:first_name, "Joe") # This will fail
|
154
|
+
end
|
155
|
+
|
134
156
|
#### Example: Nested contexts
|
135
157
|
|
136
158
|
Oh yeah, Riot does those, too. The `setup` from each parent is "loaded" into the context and then the context is executed as normal. Test naming is a composite of the parent contexts' names. Here, we'll do a little Sinatra testing (see below for instructions on how to make it Riot work seamlessly with Sinatra tests).
|
data/lib/riot.rb
CHANGED
@@ -39,8 +39,8 @@ module Riot
|
|
39
39
|
# Exception
|
40
40
|
|
41
41
|
class Failure < Exception
|
42
|
-
|
43
|
-
def initialize(message, assertion)
|
42
|
+
attr_accessor :assertion, :context
|
43
|
+
def initialize(message, assertion=nil)
|
44
44
|
super(message)
|
45
45
|
@assertion = assertion
|
46
46
|
end
|
data/lib/riot/assertion.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Riot
|
2
2
|
|
3
3
|
class Assertion
|
4
|
-
attr_reader :raised, :to_s, :description
|
4
|
+
attr_reader :raised, :to_s, :description, :situation
|
5
5
|
def initialize(description, situation, &block)
|
6
6
|
@description = @to_s = description
|
7
|
+
@situation = situation
|
7
8
|
actualize(situation, &block)
|
8
9
|
end
|
9
10
|
|
@@ -24,6 +25,9 @@ module Riot
|
|
24
25
|
def actualize(situation, &block)
|
25
26
|
@actual = situation.instance_eval(&block)
|
26
27
|
@default_failure = fail("expected true, not #{@actual.inspect}") unless @actual == true
|
28
|
+
rescue Failure => e
|
29
|
+
@failure = e
|
30
|
+
@failure.assertion = self
|
27
31
|
rescue Exception => e
|
28
32
|
@raised = e
|
29
33
|
end
|
data/lib/riot/context.rb
CHANGED
data/lib/riot/macros.rb
CHANGED
@@ -44,6 +44,24 @@ module Riot
|
|
44
44
|
def kind_of(expected)
|
45
45
|
actual.kind_of?(expected) || fail("expected kind of #{expected}, not #{actual.inspect}")
|
46
46
|
end
|
47
|
+
|
48
|
+
# Asserts that an instance variable is defined for the result of the assertion. Value of instance
|
49
|
+
# variable is expected to not be nil
|
50
|
+
# setup { User.new(:email => "foo@bar.baz") }
|
51
|
+
# asserts("foo") { topic }.assigns(:email)
|
52
|
+
#
|
53
|
+
# If a value is provided in addition to the variable name, the actual value of the instance variable
|
54
|
+
# must equal the expected value
|
55
|
+
# setup { User.new(:emmail => "foo@bar.baz") }
|
56
|
+
# asserts("foo") { topic }.assigns(:email, "foo@bar.baz")
|
57
|
+
def assigns(variable, expected_value=nil)
|
58
|
+
actual_value = actual.instance_variable_get("@#{variable}")
|
59
|
+
return fail("expected @#{variable} to be assigned a value") if actual_value.nil?
|
60
|
+
unless expected_value.nil? || expected_value == actual_value
|
61
|
+
return fail(%Q[expected @#{variable} to be equal to '#{expected_value}', not '#{actual_value}'])
|
62
|
+
end
|
63
|
+
true
|
64
|
+
end
|
47
65
|
end # AssertionMacros
|
48
66
|
end # Riot
|
49
67
|
|
data/riot.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "riot"
|
3
|
-
s.version = "0.9.
|
4
|
-
s.date = "2009-10-
|
3
|
+
s.version = "0.9.2"
|
4
|
+
s.date = "2009-10-05"
|
5
5
|
s.summary = "An extremely fast, expressive, and context-driven unit-testing framework"
|
6
6
|
s.email = %w[gus@gusg.us]
|
7
7
|
s.homepage = "http://github.com/thumblemonks/protest"
|
@@ -29,5 +29,6 @@ Gem::Specification.new do |s|
|
|
29
29
|
test/assertion_test.rb
|
30
30
|
test/benchmark/simple_context_and_assertions.rb
|
31
31
|
test/context_test.rb
|
32
|
+
test/teststrap.rb
|
32
33
|
]
|
33
34
|
end
|
data/test/assertion_test.rb
CHANGED
@@ -1,39 +1,49 @@
|
|
1
|
-
require '
|
1
|
+
require 'teststrap'
|
2
2
|
|
3
|
-
|
3
|
+
fake_situation = Riot::Situation.new
|
4
4
|
|
5
5
|
context "basic assertion:" do
|
6
6
|
should "have a description" do
|
7
|
-
Riot::Assertion.new("i will pass",
|
7
|
+
Riot::Assertion.new("i will pass", fake_situation).to_s
|
8
8
|
end.equals("i will pass")
|
9
9
|
|
10
10
|
asserts "pass? is true when assertion passed" do
|
11
|
-
Riot::Assertion.new("i will pass",
|
11
|
+
Riot::Assertion.new("i will pass", fake_situation) { true }.passed?
|
12
12
|
end
|
13
13
|
|
14
14
|
asserts "failure? is true when assertion does not pass" do
|
15
|
-
Riot::Assertion.new("i will pass",
|
15
|
+
Riot::Assertion.new("i will pass", fake_situation) { false }.failed?
|
16
16
|
end
|
17
17
|
|
18
18
|
asserts "error? is true when an unexpected Exception is raised" do
|
19
|
-
Riot::Assertion.new("error",
|
19
|
+
Riot::Assertion.new("error", fake_situation) { raise Exception, "blah" }.errored?
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
|
+
context "that fails while executing test" do
|
23
|
+
setup do
|
24
|
+
Riot::Assertion.new("error", fake_situation) { fail("I'm a bum") }
|
25
|
+
end
|
26
|
+
|
27
|
+
should("be considered a failing assertion") { topic.failed? }
|
28
|
+
should("use failed message in description") { topic.result.message }.matches(/I'm a bum/)
|
29
|
+
should("assign assertion to failure") { topic.result }.assigns(:assertion)
|
30
|
+
end # that fails while executing test
|
31
|
+
end # basic assertion
|
22
32
|
|
23
33
|
context "equals assertion:" do
|
24
34
|
asserts "result equals expectation" do
|
25
|
-
Riot::Assertion.new("i will pass",
|
35
|
+
Riot::Assertion.new("i will pass", fake_situation) { "foo bar" }.equals("foo bar")
|
26
36
|
end
|
27
37
|
|
28
38
|
should "raise a Failure if results don't equal each other" do
|
29
|
-
Riot::Assertion.new("failure",
|
39
|
+
Riot::Assertion.new("failure", fake_situation) { "bar" }.equals("foo")
|
30
40
|
end.kind_of(Riot::Failure)
|
31
41
|
end # equals assertion
|
32
42
|
|
33
43
|
context "nil assertion:" do
|
34
|
-
asserts("result is nil") { Riot::Assertion.new("foo",
|
44
|
+
asserts("result is nil") { Riot::Assertion.new("foo", fake_situation) { nil }.nil }
|
35
45
|
should "raise a Failure if not nil" do
|
36
|
-
Riot::Assertion.new("foo",
|
46
|
+
Riot::Assertion.new("foo", fake_situation) { "a" }.nil
|
37
47
|
end.kind_of(Riot::Failure)
|
38
48
|
end # nil assertion
|
39
49
|
|
@@ -41,25 +51,25 @@ context "raises assertion:" do
|
|
41
51
|
should("raise an Exception") { raise Exception }.raises(Exception)
|
42
52
|
|
43
53
|
should "pass if provided message equals expectation" do
|
44
|
-
Riot::Assertion.new("foo",
|
54
|
+
Riot::Assertion.new("foo", fake_situation) do
|
45
55
|
raise Exception, "I'm a nerd"
|
46
56
|
end.raises(Exception, "I'm a nerd")
|
47
57
|
end.equals(true)
|
48
58
|
|
49
59
|
should "fail if provided message does not equal expectation" do
|
50
|
-
Riot::Assertion.new("foo",
|
60
|
+
Riot::Assertion.new("foo", fake_situation) do
|
51
61
|
raise(Exception, "I'm a nerd")
|
52
62
|
end.raises(Exception, "But I'm not")
|
53
63
|
end.kind_of(Riot::Failure)
|
54
64
|
|
55
65
|
should "pass if provided message matches expectation" do
|
56
|
-
Riot::Assertion.new("foo",
|
66
|
+
Riot::Assertion.new("foo", fake_situation) do
|
57
67
|
raise(Exception, "I'm a nerd")
|
58
68
|
end.raises(Exception, %r[nerd])
|
59
69
|
end.equals(true)
|
60
70
|
|
61
71
|
should "fail if provided message does not match expectation" do
|
62
|
-
Riot::Assertion.new("foo",
|
72
|
+
Riot::Assertion.new("foo", fake_situation) do
|
63
73
|
raise(Exception, "I'm a nerd")
|
64
74
|
end.raises(Exception, %r[foo])
|
65
75
|
end.kind_of(Riot::Failure)
|
@@ -67,24 +77,62 @@ end # raises assertion
|
|
67
77
|
|
68
78
|
context "matching assertion:" do
|
69
79
|
asserts "result matches expression" do
|
70
|
-
Riot::Assertion.new("foo",
|
80
|
+
Riot::Assertion.new("foo", fake_situation) { "a" }.matches(%r[.])
|
71
81
|
end.equals(0)
|
72
82
|
|
73
83
|
should "raise a Failure if result does not match" do
|
74
|
-
Riot::Assertion.new("foo",
|
84
|
+
Riot::Assertion.new("foo", fake_situation) { "" }.matches(%r[.])
|
75
85
|
end.kind_of(Riot::Failure)
|
76
86
|
|
77
87
|
should "return the result of a matching operation" do
|
78
|
-
Riot::Assertion.new("foo",
|
88
|
+
Riot::Assertion.new("foo", fake_situation) { "a" }.matches("a")
|
79
89
|
end.equals(0)
|
80
90
|
end # maching assertion
|
81
91
|
|
82
92
|
context "kind_of assertion:" do
|
83
93
|
asserts "specific result is a kind of String" do
|
84
|
-
Riot::Assertion.new("foo",
|
94
|
+
Riot::Assertion.new("foo", fake_situation) { "a" }.kind_of(String)
|
85
95
|
end
|
86
96
|
|
87
97
|
should "raise a Failure if not a kind of String" do
|
88
|
-
Riot::Assertion.new("foo",
|
98
|
+
Riot::Assertion.new("foo", fake_situation) { 0 }.kind_of(String)
|
89
99
|
end.kind_of(Riot::Failure)
|
90
100
|
end # kind_of assertion
|
101
|
+
|
102
|
+
context "assigns assertion:" do
|
103
|
+
setup do
|
104
|
+
situation = Riot::Situation.new
|
105
|
+
situation.instance_eval { @foo = "bar"; @bar = nil}
|
106
|
+
situation
|
107
|
+
end
|
108
|
+
|
109
|
+
asserts("an instance variable was assigned") do
|
110
|
+
test_object = topic
|
111
|
+
Riot::Assertion.new("duh", fake_situation) { test_object }.assigns(:foo)
|
112
|
+
end
|
113
|
+
|
114
|
+
asserts("an instance variable was never assigned") do
|
115
|
+
test_object = topic
|
116
|
+
Riot::Assertion.new("foo", fake_situation) { test_object }.assigns(:baz)
|
117
|
+
end.kind_of(Riot::Failure)
|
118
|
+
|
119
|
+
asserts "an instance variable was defined with nil value" do
|
120
|
+
test_object = topic
|
121
|
+
Riot::Assertion.new("foo", fake_situation) { test_object }.assigns(:bar).message
|
122
|
+
end.equals("expected @bar to be assigned a value")
|
123
|
+
|
124
|
+
asserts("an instance variable was assigned a specific value") do
|
125
|
+
test_object = topic
|
126
|
+
Riot::Assertion.new("duh", fake_situation) { test_object }.assigns(:foo, "bar")
|
127
|
+
end
|
128
|
+
|
129
|
+
asserts("failure when instance never assigned even when a value is expected") do
|
130
|
+
test_object = topic
|
131
|
+
Riot::Assertion.new("duh", fake_situation) { test_object }.assigns(:bar, "bar").message
|
132
|
+
end.equals("expected @bar to be assigned a value")
|
133
|
+
|
134
|
+
asserts("failure when expected value is not assigned to variable with a value") do
|
135
|
+
test_object = topic
|
136
|
+
Riot::Assertion.new("duh", fake_situation) { test_object }.assigns(:foo, "baz").message
|
137
|
+
end.equals("expected @foo to be equal to 'baz', not 'bar'")
|
138
|
+
end # assigns assertion
|
data/test/context_test.rb
CHANGED
data/test/teststrap.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: riot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Knowlden
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-05 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -65,3 +65,4 @@ test_files:
|
|
65
65
|
- test/assertion_test.rb
|
66
66
|
- test/benchmark/simple_context_and_assertions.rb
|
67
67
|
- test/context_test.rb
|
68
|
+
- test/teststrap.rb
|