ccp 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -265,3 +265,13 @@ Static options(hard code) are also available.
265
265
  Cmd.execute(:a=>1)
266
266
 
267
267
  This generates "tmp/stub.json", "tmp/mock.json".
268
+
269
+ === Test
270
+
271
+ Once you got fixtures, call "test" method to test it.
272
+
273
+ Cmd.test
274
+
275
+ This automatically searchs fixtures when no fixture files are explicitly given.
276
+ The search algorithm is as same as 'fixture_save'.
277
+
data/lib/ccp.rb CHANGED
@@ -5,6 +5,8 @@ require "active_support/core_ext"
5
5
  require "ccp/version"
6
6
 
7
7
  module Ccp
8
+ Failed = Class.new(RuntimeError)
9
+
8
10
  autoload :Utils, 'ccp/utils'
9
11
  autoload :Fixtures, 'ccp/fixtures'
10
12
  autoload :Commands, 'ccp/commands'
@@ -5,6 +5,18 @@ module Ccp
5
5
  base.class_eval do
6
6
  include Ccp::Utils::Options
7
7
  dsl_accessor :fixture, options(:stub, :mock, :fail, :save, :keys, :dir, :kvs, :ext)
8
+
9
+ extend Testable
10
+ end
11
+ end
12
+
13
+ module Testable
14
+ def test(options = {})
15
+ c = new
16
+ c.receiver = options.delete(:receiver)
17
+ c.receiver.parse!(options)
18
+ c.receiver.test(c)
19
+ return c
8
20
  end
9
21
  end
10
22
  end
@@ -1,5 +1,6 @@
1
1
  module Ccp
2
2
  module Fixtures
3
+ NotFound = Class.new(RuntimeError)
3
4
  autoload :Observer , 'ccp/fixtures/observer'
4
5
  autoload :Writers , 'ccp/fixtures/writers'
5
6
  end
@@ -48,11 +48,14 @@ class Ccp::Persistent::Versioned
48
48
  extend self
49
49
  end
50
50
 
51
+ attr_reader :kvs
52
+ attr_reader :ext
53
+
51
54
  def initialize(dir, options = {})
52
- @path = Pathname(dir)
53
- @default_kvs = options[:kvs] || :dir
54
- @default_ext = options[:ext] || :json
55
- @storages = {}
55
+ @path = Pathname(dir)
56
+ @kvs = options[:kvs] || :dir
57
+ @ext = options[:ext] || :json
58
+ @storages = {}
56
59
 
57
60
  @path.mkpath
58
61
  end
@@ -80,11 +83,11 @@ class Ccp::Persistent::Versioned
80
83
 
81
84
  # 指定したストレージを返す。存在しなければ作成して返す
82
85
  def [](key)
83
- storage = Storage.complete(key, path, @default_kvs, @default_ext)
86
+ storage = Storage.complete(key, path, @kvs, @ext)
84
87
  @storages[storage.to_s] ||= storage.create
85
88
  end
86
89
 
87
90
  def inspect
88
- "<Kvs::Versioned dir=#{path} kvs=#{@default_kvs} ext=#{@default_ext}>"
91
+ "<Kvs::Versioned dir=#{path} kvs=#{@kvs} ext=#{@ext}>"
89
92
  end
90
93
  end
@@ -39,19 +39,34 @@ module Ccp
39
39
  super
40
40
  end
41
41
 
42
+ def test(cmd)
43
+ # set stub if exist
44
+ stub = fixture_versioned_for(cmd)["stub"].path
45
+ runtime_stubs[cmd] = stub if stub.exist?
46
+
47
+ # set mock
48
+ runtime_mocks[cmd] = fixture_versioned_for(cmd)["mock"].path
49
+
50
+ execute(cmd)
51
+ end
52
+
42
53
  def fixture_stub(cmd)
43
- path = cmd.class.stub or return
54
+ path = cmd.class.stub || runtime_stubs[cmd] or return
44
55
  hash = Ccp::Persistent.load(path).read!
45
56
  data.merge!(hash)
57
+ rescue Ccp::Persistent::NotFound => e
58
+ raise Ccp::Fixtures::NotFound, e.to_s
46
59
  end
47
60
 
48
61
  def fixture_mock(cmd)
49
- path = cmd.class.mock or return
62
+ path = cmd.class.mock || runtime_mocks[cmd] or return
50
63
  hash = Ccp::Persistent.load(path).read!
51
64
 
52
65
  hash.keys.each do |key|
53
66
  fixture_validate(cmd, key, data, hash)
54
67
  end
68
+ rescue Ccp::Persistent::NotFound => e
69
+ raise Ccp::Fixtures::NotFound, e.to_s
55
70
  end
56
71
 
57
72
  def fixture_validate(cmd, key, data, hash)
@@ -71,12 +86,12 @@ module Ccp
71
86
 
72
87
  def default_fixture_fail(cmd, key, exp, got)
73
88
  if exp == nil and got == nil
74
- raise "#{cmd.class} should write #{key} but not found"
89
+ raise Failed, "#{cmd.class} should write #{key} but not found"
75
90
  end
76
91
 
77
92
  exp_info = "%s(%s)" % [exp.inspect.truncate(200), Must::StructInfo.new(exp).compact.inspect]
78
93
  got_info = "%s(%s)" % [got.inspect.truncate(200), Must::StructInfo.new(got).compact.inspect]
79
- raise "%s should create %s for %s, but got %s" % [cmd.class, exp_info, key, got_info]
94
+ raise Failed, "%s should create %s for %s, but got %s" % [cmd.class, exp_info, key, got_info]
80
95
  end
81
96
 
82
97
  def fixture_save?(cmd)
@@ -98,21 +113,16 @@ module Ccp
98
113
  end
99
114
 
100
115
  def fixture_save(cmd, stub, mock)
101
- path = self[:fixture_path_for].call(cmd)
102
- path = Pathname(cmd.class.dir) + cmd.class.name.underscore if cmd.class.dir
103
-
116
+ versioned = fixture_versioned_for(cmd)
104
117
  keys = cmd.class.keys || self[:fixture_keys]
105
- kvs = cmd.class.kvs || self[:fixture_kvs]
106
- ext = cmd.class.ext || self[:fixture_ext]
107
-
108
- versioned = Ccp::Persistent::Versioned.new(path, :kvs=>kvs, :ext=>ext)
118
+ kvs = Ccp::Persistent.lookup(versioned.kvs)
109
119
 
110
120
  # stub
111
- storage = cmd.class.stub ? Ccp::Persistent.lookup(kvs).new(cmd.class.stub, ext) : versioned["stub"]
121
+ storage = cmd.class.stub ? kvs.new(cmd.class.stub, versioned.ext) : versioned["stub"]
112
122
  storage.save(stub, fixture_keys_filter(keys, stub.keys))
113
123
 
114
124
  # mock
115
- storage = cmd.class.mock ? Ccp::Persistent.lookup(kvs).new(cmd.class.mock, ext) : versioned["mock"]
125
+ storage = cmd.class.mock ? kvs.new(cmd.class.mock, versioned.ext) : versioned["mock"]
116
126
  storage.save(mock, fixture_keys_filter(keys, mock.keys))
117
127
  end
118
128
 
@@ -136,6 +146,26 @@ module Ccp
136
146
  def default_fixture_path_for
137
147
  proc{|cmd| settings.path(:fixture_dir) + cmd.class.name.underscore}
138
148
  end
149
+
150
+ private
151
+ def runtime_stubs
152
+ @runtime_stubs ||= {} # key:cmd object, val:filename
153
+ end
154
+
155
+ def runtime_mocks
156
+ @runtime_mocks ||= {} # key:cmd object, val:filename
157
+ end
158
+
159
+ def fixture_versioned_for(cmd)
160
+ dir = cmd.class.dir
161
+ path = dir ? (Pathname(dir) + cmd.class.name.underscore) : self[:fixture_path_for].call(cmd)
162
+
163
+ kvs = cmd.class.kvs || self[:fixture_kvs]
164
+ ext = cmd.class.ext || self[:fixture_ext]
165
+
166
+ versioned = Ccp::Persistent::Versioned.new(path, :kvs=>kvs, :ext=>ext)
167
+ return versioned
168
+ end
139
169
  end
140
170
  end
141
171
  end
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -19,6 +19,8 @@ describe Ccp::Commands::Fixturable do
19
19
  it { should respond_to("ext") }
20
20
  it { should respond_to("fixture") }
21
21
  its(:fixture) { should respond_to("options") }
22
+
23
+ it { should respond_to("test") }
22
24
  end
23
25
 
24
26
  context "Ccp::Commands::Composite" do
@@ -33,6 +35,8 @@ describe Ccp::Commands::Fixturable do
33
35
  it { should respond_to("ext") }
34
36
  it { should respond_to("fixture") }
35
37
  its(:fixture) { should respond_to("options") }
38
+
39
+ it { should respond_to("test") }
36
40
  end
37
41
 
38
42
  context "Ccp::Invokers::Base" do
@@ -47,6 +51,8 @@ describe Ccp::Commands::Fixturable do
47
51
  it { should respond_to("ext") }
48
52
  it { should respond_to("fixture") }
49
53
  its(:fixture) { should respond_to("options") }
54
+
55
+ it { should respond_to("test") }
50
56
  end
51
57
 
52
58
  context "included class" do
@@ -61,6 +67,8 @@ describe Ccp::Commands::Fixturable do
61
67
  it { should respond_to("ext") }
62
68
  it { should respond_to("fixture") }
63
69
  its(:fixture) { should respond_to("options") }
70
+
71
+ it { should respond_to("test") }
64
72
  end
65
73
  end
66
74
 
@@ -0,0 +1,92 @@
1
+ require "spec_helper"
2
+
3
+ describe Ccp::Commands::Fixturable do
4
+ before do
5
+ @stub = Pathname("tmp/fixtures/square_cmd/stub.json")
6
+ @mock = Pathname("tmp/fixtures/square_cmd/mock.json")
7
+ @stub.parent.mkpath
8
+ save_fixture(@stub, "x"=>10)
9
+ save_fixture(@mock, "x"=>100)
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf("tmp/fixtures")
14
+ end
15
+
16
+ class SquareCmd
17
+ include Ccp::Commands::Core
18
+ def execute
19
+ data[:x] = data[:x] * data[:x]
20
+ end
21
+ end
22
+
23
+ class SquareCmdPass < SquareCmd
24
+ stub "tmp/fixtures/square_cmd/stub.json"
25
+ mock "tmp/fixtures/square_cmd/mock.json"
26
+ end
27
+
28
+ class SquareCmdFail < SquareCmd
29
+ stub "tmp/fixtures/square_cmd/stub.json"
30
+ mock "tmp/fixtures/square_cmd/mock.json"
31
+
32
+ def execute
33
+ data[:x] = data[:x] + 1
34
+ end
35
+ end
36
+
37
+ describe ".test" do
38
+ it "should use stub automatically if exists" do
39
+ cmd = SquareCmd.test
40
+ cmd.data[:x].should == 100
41
+ end
42
+
43
+ it "should use mock automatically" do
44
+ save_fixture(@mock, "x"=>1)
45
+
46
+ lambda {
47
+ SquareCmd.test
48
+ }.should raise_error(Ccp::Failed)
49
+ end
50
+
51
+ it "should raise Fixtures::NotFound if mock doesn't exist" do
52
+ @mock.unlink
53
+
54
+ lambda {
55
+ SquareCmd.test
56
+ }.should raise_error(Ccp::Fixtures::NotFound)
57
+ end
58
+
59
+ context "(pass case)" do
60
+ it "should raise nothing" do
61
+ cmd = SquareCmdPass.test
62
+ cmd.data[:x].should == 100
63
+ end
64
+ end
65
+
66
+ context "(fail case)" do
67
+ it "should raise RuntimeError" do
68
+ failed = nil
69
+ SquareCmdFail.stub @stub
70
+ SquareCmdFail.mock @mock
71
+
72
+ lambda {
73
+ SquareCmdFail.test
74
+ }.should raise_error(Ccp::Failed)
75
+ end
76
+
77
+ it "should call fail handler if given" do
78
+ failed = nil
79
+ SquareCmdFail.stub @stub
80
+ SquareCmdFail.mock @mock
81
+ SquareCmdFail.fail {|cmd, key, expected, got|
82
+ failed = key
83
+ }
84
+
85
+ lambda {
86
+ SquareCmdFail.test
87
+ }.should_not raise_error
88
+ failed.should == "x"
89
+ end
90
+ end
91
+ end
92
+ end
@@ -10,6 +10,12 @@ describe Ccp::Persistent::Versioned do
10
10
  root.mkpath
11
11
  end
12
12
 
13
+ describe "(accessors)" do
14
+ subject { Ccp::Persistent::Versioned.new(root, :kvs=>"dir", :ext=>"yaml") }
15
+ its(:kvs) { should == "dir" }
16
+ its(:ext) { should == "yaml" }
17
+ end
18
+
13
19
  describe "#latest" do
14
20
  it "should return a kvs with the latest dated file" do
15
21
  # create directories
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-11 00:00:00 +09:00
18
+ date: 2012-05-15 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -145,6 +145,7 @@ files:
145
145
  - spec/commands/core_spec.rb
146
146
  - spec/commands/executable_spec.rb
147
147
  - spec/commands/fixturable_spec.rb
148
+ - spec/commands/fixturable_test_spec.rb
148
149
  - spec/fixtures/cmd1stub_mock/mock.json
149
150
  - spec/fixtures/stub/breadcrumbs.json
150
151
  - spec/invokers/base_spec.rb