punchout 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/punchout/fabricable.rb +9 -0
- data/lib/punchout/fabricator.rb +27 -0
- data/test/integration/punchout/fabricable_test.rb +69 -0
- data/test/unit/punchout/fabricator_test.rb +48 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d41e5e66093156e892f64ae4488e388d86788c7
|
4
|
+
data.tar.gz: bb7f6416d9b5f6f9328ee105064307825421de8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2f89a361ec05f13413aecece10fb3a100fa8d661d1f4874065e8943350b103b4982f6d49e3a6dac15e735f50d3d677fe49128dc73e5fcada8e53266cb98ae77
|
7
|
+
data.tar.gz: 791224925c8690f29ff5e498b252451458bec73964ea4ac7a47ce2c40202348c4b0929399cc7fdabe84d7eba7adc1ae60989283bd697923af9d74812b3c25b0d
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Punchout
|
2
|
+
class Fabricator
|
3
|
+
def initialize(factory)
|
4
|
+
@puncher = Puncher.new
|
5
|
+
@factory = factory
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(matchable)
|
9
|
+
@puncher.add(matchable)
|
10
|
+
end
|
11
|
+
|
12
|
+
def can_punch?(type)
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def punch(type)
|
17
|
+
|
18
|
+
if @puncher.can_punch?(type)
|
19
|
+
@puncher.punch(type)
|
20
|
+
else
|
21
|
+
matchable = @factory.build(type)
|
22
|
+
@puncher.add(matchable)
|
23
|
+
matchable.thing
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
require 'punchout/fabricable'
|
4
|
+
require 'punchout/matcher/class'
|
5
|
+
|
6
|
+
class Punchout::Fabricator::Test < ActiveSupport::TestCase
|
7
|
+
class Factory
|
8
|
+
def initialize(pairings)
|
9
|
+
@pairings = pairings
|
10
|
+
end
|
11
|
+
|
12
|
+
def build(thing)
|
13
|
+
matcher = Punchout::Matcher::Klass.new(thing)
|
14
|
+
punchable = @pairings.find {|p| p[:match] == thing.class}[:punchable]
|
15
|
+
matcher.punches(punchable)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
setup do
|
20
|
+
punchout_klass = Class.new do
|
21
|
+
include Punchout::Fabricable
|
22
|
+
|
23
|
+
attr_accessor :factory
|
24
|
+
end
|
25
|
+
|
26
|
+
@pairings = 3.times.map {
|
27
|
+
{match: Class.new, punchable: Class.new}
|
28
|
+
}
|
29
|
+
|
30
|
+
fabricator = punchout_klass.new
|
31
|
+
fabricator.factory = Factory.new(@pairings)
|
32
|
+
|
33
|
+
@puncher = fabricator.puncher
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
test "#punch exists" do
|
38
|
+
@pairings.each do |p|
|
39
|
+
matcher = Punchout::Matcher::Klass.new(p[:match])
|
40
|
+
|
41
|
+
matchable = matcher.punches(p[:punchable])
|
42
|
+
|
43
|
+
@puncher.add(matchable)
|
44
|
+
end
|
45
|
+
|
46
|
+
expected = @pairings.first
|
47
|
+
|
48
|
+
expected_matching_instance = expected[:match].new
|
49
|
+
|
50
|
+
result = @puncher.can_punch?(expected_matching_instance)
|
51
|
+
assert result, "#{@puncher.inspect} CAN NOT PUNCH #{expected[:match]}"
|
52
|
+
|
53
|
+
result = @puncher.punch(expected_matching_instance)
|
54
|
+
assert_equal expected[:punchable],result
|
55
|
+
end
|
56
|
+
|
57
|
+
test "#punch does not exists" do
|
58
|
+
expected = @pairings.first
|
59
|
+
|
60
|
+
expected_matching_instance = expected[:match].new
|
61
|
+
|
62
|
+
result = @puncher.can_punch?(expected_matching_instance)
|
63
|
+
assert result, "#{@puncher.inspect} CAN NOT PUNCH #{expected[:match]}"
|
64
|
+
|
65
|
+
result = @puncher.punch(expected_matching_instance)
|
66
|
+
assert_equal expected[:punchable],result
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
require 'punchout/fabricator'
|
4
|
+
require 'punchout/puncher'
|
5
|
+
|
6
|
+
class Punchout::FabricatorTest < ActiveSupport::TestCase
|
7
|
+
setup do
|
8
|
+
@mock_factory = mock
|
9
|
+
@mock_puncher = mock
|
10
|
+
|
11
|
+
Punchout::Puncher.expects(:new).returns(@mock_puncher)
|
12
|
+
|
13
|
+
@fabricator = Punchout::Fabricator.new(@mock_factory)
|
14
|
+
|
15
|
+
@mock_type = mock
|
16
|
+
end
|
17
|
+
|
18
|
+
test '#punch no match' do
|
19
|
+
@mock_puncher.expects(:can_punch?).with(@mock_type).returns(false)
|
20
|
+
|
21
|
+
mock_matchable = mock
|
22
|
+
@mock_factory.expects(:build).with(@mock_type).returns(mock_matchable)
|
23
|
+
|
24
|
+
@mock_puncher.expects(:add).with(mock_matchable)
|
25
|
+
|
26
|
+
mock_thing = mock
|
27
|
+
mock_matchable.expects(:thing).returns(mock_thing)
|
28
|
+
|
29
|
+
result = @fabricator.punch(@mock_type)
|
30
|
+
|
31
|
+
assert_equal mock_thing, result
|
32
|
+
end
|
33
|
+
|
34
|
+
test '#punch match located' do
|
35
|
+
mock_thing = mock
|
36
|
+
|
37
|
+
@mock_puncher.expects(:can_punch?).with(@mock_type).returns(true)
|
38
|
+
|
39
|
+
@mock_puncher.expects(:punch).with(@mock_type).returns(mock_thing)
|
40
|
+
|
41
|
+
@fabricator.instance_variable_set(:@matchables, @mock_matchables)
|
42
|
+
|
43
|
+
result = @fabricator.punch(@mock_type)
|
44
|
+
|
45
|
+
assert_equal mock_thing, result
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: punchout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ed Carrel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_support
|
@@ -61,6 +61,8 @@ executables: []
|
|
61
61
|
extensions: []
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
|
+
- lib/punchout/fabricable.rb
|
65
|
+
- lib/punchout/fabricator.rb
|
64
66
|
- lib/punchout/matcher/ancestry.rb
|
65
67
|
- lib/punchout/matcher/class.rb
|
66
68
|
- lib/punchout/matcher/equal.rb
|
@@ -71,8 +73,10 @@ files:
|
|
71
73
|
- lib/punchout/puncher/matchables.rb
|
72
74
|
- lib/punchout/puncher.rb
|
73
75
|
- lib/punchout.rb
|
76
|
+
- test/integration/punchout/fabricable_test.rb
|
74
77
|
- test/integration/punchout_test.rb
|
75
78
|
- test/integration/test_helper.rb
|
79
|
+
- test/unit/punchout/fabricator_test.rb
|
76
80
|
- test/unit/punchout/matcher/ancestry_test.rb
|
77
81
|
- test/unit/punchout/matcher/class_test.rb
|
78
82
|
- test/unit/punchout/matcher/equal_test.rb
|