on 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +34 -0
- data/lib/on/test_helper.rb +115 -0
- data/lib/on/version.rb +1 -1
- data/test/helper.rb +4 -16
- data/test/integration_test.rb +11 -34
- data/test/on_proc_test.rb +4 -15
- data/test/on_test.rb +11 -28
- metadata +5 -4
data/README.rdoc
CHANGED
@@ -53,6 +53,40 @@ Syntatic sugar for creating an +on+ callback from Proc.
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
+
== Testing with On::TestHelper
|
57
|
+
|
58
|
+
require 'minitest/autorun'
|
59
|
+
require 'on'
|
60
|
+
require 'on/test_helper'
|
61
|
+
|
62
|
+
class SomeTest < MiniTest::Spec
|
63
|
+
include On::TestHelper
|
64
|
+
|
65
|
+
let(:recorder) { On::TestHelper::Recorder.new }
|
66
|
+
|
67
|
+
it "records everything" do
|
68
|
+
on = On.new(:success, :failure, &recorder)
|
69
|
+
on.call :success, :some, :args
|
70
|
+
assert_callback recorder, :success, :some, :args
|
71
|
+
end
|
72
|
+
|
73
|
+
it "calls nothing" do
|
74
|
+
on = On.new(:success, :failure, &recorder)
|
75
|
+
# nothing called
|
76
|
+
refute_callbacks recorder
|
77
|
+
end
|
78
|
+
|
79
|
+
it "records everything manually" do
|
80
|
+
on = On.new(:success, :failure) do |result|
|
81
|
+
recorder.record_block
|
82
|
+
recorder.record_callback(result, :success, :failure)
|
83
|
+
end
|
84
|
+
on.call :success, :some, :args
|
85
|
+
assert_callback recorder, :success, :some, :args
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
56
90
|
== Installation
|
57
91
|
|
58
92
|
Add this line to your application's Gemfile:
|
@@ -0,0 +1,115 @@
|
|
1
|
+
class On
|
2
|
+
# Helper for testing On callbacks.
|
3
|
+
#
|
4
|
+
# == Example
|
5
|
+
#
|
6
|
+
# require 'minitest/autorun'
|
7
|
+
# require 'on'
|
8
|
+
# require 'on/test_helper'
|
9
|
+
#
|
10
|
+
# class SomeTest < MiniTest::Spec
|
11
|
+
# include On::TestHelper
|
12
|
+
#
|
13
|
+
# let(:recorder) { On::TestHelper::Recorder.new }
|
14
|
+
#
|
15
|
+
# it "records everything" do
|
16
|
+
# on = On.new(:success, :failure, &recorder)
|
17
|
+
# on.call :success, :some, :args
|
18
|
+
# assert_callback recorder, :success, :some, :args
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# it "calls nothing" do
|
22
|
+
# on = On.new(:success, :failure, &recorder)
|
23
|
+
# # nothing called
|
24
|
+
# refute_callbacks recorder
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# it "records everything manually" do
|
28
|
+
# on = On.new(:success, :failure) do |result|
|
29
|
+
# recorder.record_block
|
30
|
+
# recorder.record_callback(result, :success, :failure)
|
31
|
+
# end
|
32
|
+
# on.call :success, :some, :args
|
33
|
+
# assert_callback recorder, :success, :some, :args
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
module TestHelper
|
37
|
+
# Asserts that a certain callbacks has been recorded by +recorder+.
|
38
|
+
#
|
39
|
+
# == Example
|
40
|
+
#
|
41
|
+
# assert_callback recorder, :success, "some", "args"
|
42
|
+
#
|
43
|
+
def assert_callback(recorder, name, *args)
|
44
|
+
callback = recorder.last_record
|
45
|
+
assert callback, "No callbacks found"
|
46
|
+
assert_equal name, callback.name, "Callback was #{callback}"
|
47
|
+
assert_equal args, callback.args, "Callback was #{callback}"
|
48
|
+
end
|
49
|
+
|
50
|
+
# Asserts that *no* callbacks has been recorder by +recorder+.
|
51
|
+
def refute_callbacks(recorder)
|
52
|
+
assert recorder.empty?, "Something has been recorded #{recorder.inspect}"
|
53
|
+
end
|
54
|
+
|
55
|
+
# Record callbacks.
|
56
|
+
class Recorder
|
57
|
+
Callback = Struct.new(:name, :args)
|
58
|
+
|
59
|
+
def initialize
|
60
|
+
@block = false
|
61
|
+
@callbacks = []
|
62
|
+
end
|
63
|
+
|
64
|
+
def block_called?
|
65
|
+
@block
|
66
|
+
end
|
67
|
+
|
68
|
+
def empty?
|
69
|
+
@callbacks.empty?
|
70
|
+
end
|
71
|
+
|
72
|
+
# Pops the last recorded recorded.
|
73
|
+
def last_record
|
74
|
+
@callbacks.pop
|
75
|
+
end
|
76
|
+
|
77
|
+
# Records block and all defined callbacks.
|
78
|
+
def record_all
|
79
|
+
proc do |on|
|
80
|
+
record_block
|
81
|
+
record_callback(on, *on.callbacks)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Records a block.
|
86
|
+
#
|
87
|
+
# recorder = Recorder.new
|
88
|
+
# On.new(:success) do |callback|
|
89
|
+
# recorder.record_block
|
90
|
+
# callback.on :success do
|
91
|
+
# # assert...
|
92
|
+
# end
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# assert recorder.block_called?
|
96
|
+
def record_block
|
97
|
+
@block = true
|
98
|
+
end
|
99
|
+
|
100
|
+
# Records a single callback.
|
101
|
+
def record_callback(on, *names)
|
102
|
+
names.each do |name|
|
103
|
+
on.on name do |*args|
|
104
|
+
@callbacks << Callback.new(name, args)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# Short-hand for &+recorder.record_all+.
|
110
|
+
def to_proc
|
111
|
+
record_all
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/lib/on/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -1,22 +1,10 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require 'testem'
|
3
3
|
|
4
|
-
|
5
|
-
let(:called) { [] }
|
6
|
-
|
7
|
-
setup do
|
8
|
-
called.clear
|
9
|
-
end
|
4
|
+
require 'on/test_helper'
|
10
5
|
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
def assert_called(*args)
|
16
|
-
assert_equal called, args
|
17
|
-
end
|
6
|
+
class Testem
|
7
|
+
include On::TestHelper
|
18
8
|
|
19
|
-
|
20
|
-
assert_called
|
21
|
-
end
|
9
|
+
let(:recorder) { On::TestHelper::Recorder.new }
|
22
10
|
end
|
data/test/integration_test.rb
CHANGED
@@ -14,53 +14,30 @@ class IntegrationTest < Testem
|
|
14
14
|
end
|
15
15
|
|
16
16
|
test "it calls success" do
|
17
|
-
tweet "Sir, hello world"
|
18
|
-
|
19
|
-
callback.on :success do |message|
|
20
|
-
called! :success, message
|
21
|
-
end
|
22
|
-
callback.on :failure do |message|
|
23
|
-
called! :failure, message
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
assert_called [ :method ], [ :success, "Sir, hello world" ]
|
17
|
+
tweet "Sir, hello world", &recorder
|
18
|
+
assert_callback recorder, :success, "Sir, hello world"
|
28
19
|
end
|
29
20
|
|
30
21
|
test "it calls failure" do
|
31
|
-
tweet nil
|
32
|
-
|
33
|
-
callback.on :success do |message|
|
34
|
-
called! :success, message
|
35
|
-
end
|
36
|
-
callback.on :failure do |message|
|
37
|
-
called! :failure, message
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
assert_called [ :method ], [ :failure, "blank" ]
|
22
|
+
tweet nil, &recorder
|
23
|
+
assert_callback recorder, :failure, "blank"
|
42
24
|
end
|
43
25
|
|
44
26
|
test "no callback called" do
|
45
|
-
tweet "you're such a fool"
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
assert_called
|
27
|
+
tweet "you're such a fool", &recorder
|
28
|
+
refute_callbacks recorder
|
29
|
+
refute recorder.block_called?
|
50
30
|
end
|
51
31
|
|
52
32
|
test "invalid callback name" do
|
53
33
|
e = assert_raises On::InvalidCallback do
|
54
34
|
tweet "Sir, hi" do |callback|
|
55
|
-
|
56
|
-
|
57
|
-
callback.on :invalid do
|
58
|
-
called! :invalid
|
59
|
-
end
|
35
|
+
recorder.record_block
|
36
|
+
callback.on(:invalid) {}
|
60
37
|
end
|
61
38
|
end
|
62
39
|
assert_equal "Invalid callback :invalid", e.message
|
63
|
-
|
64
|
-
|
40
|
+
assert recorder.block_called?
|
41
|
+
refute_callbacks recorder
|
65
42
|
end
|
66
43
|
end
|
data/test/on_proc_test.rb
CHANGED
@@ -12,22 +12,11 @@ class OnProcTest < Testem
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def verify(number)
|
16
|
-
oddeven(number) do |callback|
|
17
|
-
called! :block, number
|
18
|
-
callback.on :odd do
|
19
|
-
called! :odd
|
20
|
-
end
|
21
|
-
callback.on :even do
|
22
|
-
called! :even
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
15
|
test "call proc" do
|
28
|
-
|
29
|
-
|
16
|
+
oddeven(1, &recorder)
|
17
|
+
assert_callback recorder, :odd
|
30
18
|
|
31
|
-
|
19
|
+
oddeven(2, &recorder)
|
20
|
+
assert_callback recorder, :even
|
32
21
|
end
|
33
22
|
end
|
data/test/on_test.rb
CHANGED
@@ -20,10 +20,6 @@ class OnTest < Testem
|
|
20
20
|
end
|
21
21
|
|
22
22
|
context "with an instance" do
|
23
|
-
before do
|
24
|
-
called.clear
|
25
|
-
end
|
26
|
-
|
27
23
|
test "returns a list of supported callback names" do
|
28
24
|
on = On.new(:success, :failure) {}
|
29
25
|
assert_equal 2, on.callbacks.size
|
@@ -32,27 +28,20 @@ class OnTest < Testem
|
|
32
28
|
end
|
33
29
|
|
34
30
|
test "calls callback w/o args" do
|
35
|
-
on = On.new(:success)
|
36
|
-
called! :block
|
37
|
-
callback.on :success do |*args|
|
38
|
-
called! :success, *args
|
39
|
-
end
|
40
|
-
end
|
31
|
+
on = On.new(:success, &recorder)
|
41
32
|
on.call :success
|
42
33
|
|
43
|
-
|
34
|
+
assert recorder.block_called?
|
35
|
+
assert_callback recorder, :success
|
44
36
|
end
|
45
37
|
|
46
38
|
test "calls callback with args" do
|
47
|
-
on = On.new(:success)
|
48
|
-
called! :block
|
49
|
-
callback.on :success do |*args|
|
50
|
-
called! :success, *args
|
51
|
-
end
|
52
|
-
end
|
39
|
+
on = On.new(:success, &recorder)
|
53
40
|
on.call :success, :foo, :bar
|
54
41
|
|
55
|
-
|
42
|
+
assert recorder.block_called?
|
43
|
+
assert_callback recorder, :success, :foo, :bar
|
44
|
+
|
56
45
|
assert on.callback
|
57
46
|
assert_equal :success, on.callback.name
|
58
47
|
end
|
@@ -68,26 +57,20 @@ class OnTest < Testem
|
|
68
57
|
|
69
58
|
test "handles invalid callback" do
|
70
59
|
on = On.new(:correct) do |callback|
|
71
|
-
called! :block
|
72
60
|
callback.on :wrong
|
73
61
|
end
|
74
62
|
e = assert_raises On::InvalidCallback do
|
75
63
|
on.call :correct
|
76
64
|
end
|
77
|
-
|
78
65
|
assert_equal "Invalid callback :wrong", e.message
|
79
|
-
assert_called [ :block ]
|
80
66
|
end
|
81
67
|
|
82
68
|
test "does not explode when nothing called" do
|
83
|
-
on = On.new(:something)
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
called! :something
|
88
|
-
end
|
69
|
+
on = On.new(:something, &recorder)
|
70
|
+
|
71
|
+
refute_callbacks recorder
|
72
|
+
refute recorder.block_called?
|
89
73
|
|
90
|
-
assert_nothing_called
|
91
74
|
assert_nil on.callback
|
92
75
|
end
|
93
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 'on'
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- Rakefile
|
90
90
|
- lib/on.rb
|
91
91
|
- lib/on/proc.rb
|
92
|
+
- lib/on/test_helper.rb
|
92
93
|
- lib/on/version.rb
|
93
94
|
- on.gemspec
|
94
95
|
- on.rb
|
@@ -110,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
111
|
version: '0'
|
111
112
|
segments:
|
112
113
|
- 0
|
113
|
-
hash: -
|
114
|
+
hash: -2276299258964583998
|
114
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
116
|
none: false
|
116
117
|
requirements:
|
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
120
|
version: '0'
|
120
121
|
segments:
|
121
122
|
- 0
|
122
|
-
hash: -
|
123
|
+
hash: -2276299258964583998
|
123
124
|
requirements: []
|
124
125
|
rubyforge_project:
|
125
126
|
rubygems_version: 1.8.24
|