ccp 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -271,7 +271,9 @@ This generates "tmp/stub.json", "tmp/mock.json".
271
271
  Once you got fixtures, call "test" method to test it.
272
272
 
273
273
  Cmd.test
274
+ # or
275
+ # Cmd.execute(:fixture_test=>true)
274
276
 
275
277
  This automatically searchs fixtures when no fixture files are explicitly given.
276
- The search algorithm is as same as 'fixture_save'.
278
+ This mechanism is as same as 'fixture_save'.
277
279
 
@@ -4,9 +4,8 @@ module Ccp
4
4
  def self.included(base)
5
5
  super
6
6
  base.class_eval do
7
- extend ClassMethods
8
- extend Executable::ClassMethods
9
- include Fixturable
7
+ include Core
8
+ extend CommandManager
10
9
  end
11
10
  end
12
11
 
@@ -25,7 +24,7 @@ module Ccp
25
24
  ######################################################################
26
25
  ### Class Methods
27
26
 
28
- module ClassMethods
27
+ module CommandManager
29
28
  include Resolvable
30
29
 
31
30
  def prepend_command(klass, *args, &block)
@@ -1,14 +1,12 @@
1
1
  module Ccp
2
2
  module Commands
3
3
  module Core
4
- include Receivable
5
- include Commentable
6
- include Executable
7
-
8
4
  def self.included(base)
9
5
  super
10
6
  base.class_eval do
11
- extend ClassMethods
7
+ include Receivable
8
+ include Commentable
9
+ include Executable
12
10
  include Fixturable
13
11
  end
14
12
  end
@@ -3,17 +3,13 @@ module Ccp
3
3
  module Executable
4
4
  def self.included(base)
5
5
  base.class_eval do
6
- extend ClassMethods
7
- end
8
- end
9
-
10
- module ClassMethods
11
- def execute(options = {})
12
- c = new
13
- c.receiver = options.delete(:receiver)
14
- c.receiver.parse!(options)
15
- c.receiver.execute(c)
16
- return c
6
+ def self.execute(options = {})
7
+ c = new
8
+ c.receiver = options.delete(:receiver)
9
+ c.receiver.parse!(options)
10
+ c.receiver.execute(c)
11
+ return c
12
+ end
17
13
  end
18
14
  end
19
15
 
@@ -6,17 +6,9 @@ module Ccp
6
6
  include Ccp::Utils::Options
7
7
  dsl_accessor :fixture, options(:stub, :mock, :fail, :save, :keys, :dir, :kvs, :ext)
8
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
9
+ def self.test(options = {})
10
+ execute({:fixture_test=>true}.merge(options))
11
+ end
20
12
  end
21
13
  end
22
14
  end
@@ -9,6 +9,7 @@ module Ccp::Persistent::Loadable
9
9
 
10
10
  private
11
11
  def load_kvs_lookup_by_filename(file)
12
+ return Ccp::Persistent::Dir if File.directory?(file.to_s)
12
13
  case file.to_s
13
14
  when %r{/$} ; Ccp::Persistent::Dir
14
15
  when %r{\.tsv$}; Ccp::Persistent::Tsv
@@ -8,10 +8,18 @@ module Ccp
8
8
  super
9
9
  observer.stop
10
10
  fixture_save(cmd, observer.read, observer.write)
11
+
12
+ elsif fixture_test?(cmd)
13
+ stub = fixture_versioned_for(cmd)["stub"].path
14
+ fixture_stub(cmd.class.stub || (stub.exist? ? stub : nil))
15
+ super
16
+ mock = fixture_versioned_for(cmd)["mock"].path
17
+ fixture_mock(cmd, cmd.class.mock || mock)
18
+
11
19
  else
12
- fixture_stub(cmd)
20
+ fixture_stub(cmd.class.stub)
13
21
  super
14
- fixture_mock(cmd)
22
+ fixture_mock(cmd, cmd.class.mock)
15
23
  end
16
24
  end
17
25
 
@@ -20,6 +28,7 @@ module Ccp
20
28
 
21
29
  # Schema
22
30
  self[:fixture_save] = Object # Define schema explicitly to accept true|false|Proc
31
+ self[:fixture_test] = Object # Define schema explicitly to accept true|false|Proc
23
32
  self[:fixture_keys] = Object # Define schema explicitly to accept true|[String]
24
33
 
25
34
  # Values
@@ -27,6 +36,7 @@ module Ccp
27
36
  self[:fixture_kvs] = :file
28
37
  self[:fixture_ext] = :json
29
38
  self[:fixture_save] = false
39
+ self[:fixture_test] = false
30
40
  self[:fixture_keys] = true
31
41
  self[:fixture_path_for] = default_fixture_path_for
32
42
  end
@@ -39,27 +49,16 @@ module Ccp
39
49
  super
40
50
  end
41
51
 
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
-
53
- def fixture_stub(cmd)
54
- path = cmd.class.stub || runtime_stubs[cmd] or return
52
+ def fixture_stub(path)
53
+ return unless path
55
54
  hash = Ccp::Persistent.load(path).read!
56
55
  data.merge!(hash)
57
56
  rescue Ccp::Persistent::NotFound => e
58
57
  raise Ccp::Fixtures::NotFound, e.to_s
59
58
  end
60
59
 
61
- def fixture_mock(cmd)
62
- path = cmd.class.mock || runtime_mocks[cmd] or return
60
+ def fixture_mock(cmd, path)
61
+ return unless path
63
62
  hash = Ccp::Persistent.load(path).read!
64
63
 
65
64
  hash.keys.each do |key|
@@ -126,6 +125,22 @@ module Ccp
126
125
  storage.save(mock, fixture_keys_filter(keys, mock.keys))
127
126
  end
128
127
 
128
+ def fixture_test?(cmd)
129
+ case (obj = self[:fixture_test])
130
+ when true ; true
131
+ when false ; false
132
+ when String; cmd.class.name == obj
133
+ when Array ; ary = obj.map(&:to_s); name = cmd.class.name
134
+ return false if ary.blank?
135
+ return true if ary.include?(name)
136
+ return false if ary.include?("!#{name}")
137
+ return true if ary.size == ary.grep(/^!/).size
138
+ return false
139
+ when Proc ; instance_exec(cmd, &obj).must(true,false) {raise ":fixture_test should return true|false"}
140
+ else; raise ":fixture_test is invalid: #{obj.class}"
141
+ end
142
+ end
143
+
129
144
  def fixture_keys_filter(acl, keys)
130
145
  case acl
131
146
  when true ; keys
data/lib/ccp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Ccp::Commands::Composite do
4
4
  describe "(instance)" do
5
- subject { Object.new.extend Ccp::Commands::Composite }
5
+ subject { Program.new }
6
6
 
7
7
  # data container
8
8
  it { should respond_to(:data?) }
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Ccp::Commands::Core do
4
4
  describe "(accessor)" do
5
- subject { Object.new.extend Ccp::Commands::Core }
5
+ subject { Cmd1.new }
6
6
 
7
7
  # data container
8
8
  it { should respond_to(:data?) }
@@ -0,0 +1,93 @@
1
+ require "spec_helper"
2
+
3
+ describe Ccp::Receivers::Fixtures do
4
+ before do
5
+ FileUtils.rm_rf("tmp/fixtures")
6
+ FileUtils.mkdir_p("tmp/fixtures")
7
+ end
8
+
9
+ class CRFC1
10
+ def execute
11
+ data[:a] = 1
12
+ end
13
+ end
14
+
15
+ class CRFC2
16
+ def execute
17
+ data[:b] = 2
18
+ end
19
+ end
20
+
21
+ class CRFCC
22
+ include Ccp::Commands::Composite
23
+ command CRFC1
24
+ command CRFC2
25
+ end
26
+
27
+ describe CRFCC do
28
+ describe ".test" do
29
+ it "should test CRFC1, CRFC2, CRFF in order" do
30
+ lambda {
31
+ CRFCC.test
32
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfc1/mock.json")
33
+
34
+ save_fixture("tmp/fixtures/crfc1/mock.json", "a"=>1)
35
+ lambda {
36
+ CRFCC.test
37
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfc2/mock.json")
38
+
39
+ save_fixture("tmp/fixtures/crfc2/mock.json", "b"=>2)
40
+ lambda {
41
+ CRFCC.test
42
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfcc/mock.json")
43
+
44
+ save_fixture("tmp/fixtures/crfcc/mock.json", {})
45
+ lambda {
46
+ CRFCC.test
47
+ }.should_not raise_error
48
+ end
49
+
50
+ context "(:fixture_test=>'CRFC2')" do
51
+ it "should test only CRFC2" do
52
+ lambda {
53
+ CRFCC.test(:fixture_test=>'CRFC2')
54
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfc2/mock.json")
55
+ end
56
+ end
57
+ end
58
+
59
+ describe ".execute" do
60
+ context "(:fixture_test=>true)" do
61
+ it "should test CRFC1, CRFC2, CRFF in order" do
62
+ lambda {
63
+ CRFCC.execute(:fixture_test=>true)
64
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfc1/mock.json")
65
+
66
+ save_fixture("tmp/fixtures/crfc1/mock.json", "a"=>1)
67
+ lambda {
68
+ CRFCC.execute(:fixture_test=>true)
69
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfc2/mock.json")
70
+
71
+ save_fixture("tmp/fixtures/crfc2/mock.json", "b"=>2)
72
+ lambda {
73
+ CRFCC.execute(:fixture_test=>true)
74
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfcc/mock.json")
75
+
76
+ save_fixture("tmp/fixtures/crfcc/mock.json", {})
77
+ lambda {
78
+ CRFCC.execute(:fixture_test=>true)
79
+ }.should_not raise_error
80
+ end
81
+ end
82
+
83
+ context "(:fixture_test=>'CRFC2')" do
84
+ it "should test only CRFC2" do
85
+ lambda {
86
+ CRFCC.execute(:fixture_test=>'CRFC2')
87
+ }.should raise_error(Ccp::Fixtures::NotFound, "tmp/fixtures/crfc2/mock.json")
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,79 @@
1
+ require "spec_helper"
2
+ require 'fileutils'
3
+
4
+ describe Ccp::Receivers::Fixtures do
5
+ before do
6
+ FileUtils.rm_rf("tmp")
7
+ end
8
+
9
+ module Example1
10
+ class LoadInts
11
+ include Ccp::Commands::Core
12
+ def execute
13
+ data[:ints] = [10,20,30]
14
+ end
15
+ end
16
+
17
+ class CalculateMean
18
+ include Ccp::Commands::Core
19
+ def before
20
+ data.check(:ints)
21
+ end
22
+
23
+ def execute
24
+ data[:mean] = data[:ints].sum / data[:ints].size
25
+ end
26
+ end
27
+
28
+ class Main < Ccp::Invokers::Base
29
+ command LoadInts
30
+ command CalculateMean
31
+ end
32
+ end
33
+
34
+ describe ".execute" do
35
+ context "(:fixture_save=>true, :fixture_kvs=>:dir, :fixture_ext=>:json)" do
36
+ before { @options = {:fixture_save=>true, :fixture_kvs=>:dir, :fixture_ext=>:json} }
37
+
38
+ it "should create stubs and mocks as dir" do
39
+ Example1::Main.execute(@options)
40
+
41
+ Pathname("tmp/fixtures/example1").should exist
42
+ Dir["tmp/**/*.json"].sort.should ==
43
+ ["tmp/fixtures/example1/calculate_mean/mock.json",
44
+ "tmp/fixtures/example1/calculate_mean/mock.json/mean.json",
45
+ "tmp/fixtures/example1/calculate_mean/stub.json",
46
+ "tmp/fixtures/example1/calculate_mean/stub.json/ints.json",
47
+ "tmp/fixtures/example1/load_ints/mock.json",
48
+ "tmp/fixtures/example1/load_ints/mock.json/ints.json",
49
+ "tmp/fixtures/example1/main/mock.json",
50
+ "tmp/fixtures/example1/main/mock.json/ints.json",
51
+ "tmp/fixtures/example1/main/mock.json/mean.json",
52
+ "tmp/fixtures/example1/main/stub.json",
53
+ "tmp/fixtures/example1/main/stub.json/ints.json"]
54
+ end
55
+
56
+ it "should pass all test" do
57
+ Example1::Main.execute(@options)
58
+ lambda {
59
+ Example1::Main.test
60
+ }.should_not raise_error
61
+ end
62
+
63
+ it "should pass partial test" do
64
+ Example1::Main.execute(@options)
65
+ lambda {
66
+ Example1::CalculateMean.test
67
+ }.should_not raise_error
68
+ end
69
+
70
+ it "should raise Typed::NotDefined when partial stub file is deleted" do
71
+ Example1::Main.execute(@options)
72
+ Pathname("tmp/fixtures/example1/calculate_mean/stub.json/ints.json").unlink
73
+ lambda {
74
+ Example1::CalculateMean.test
75
+ }.should raise_error(Typed::NotDefined, /'ints' is not initialized/)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -6,6 +6,8 @@ describe Ccp::Persistent do
6
6
  it { should respond_to("load") }
7
7
 
8
8
  describe ".load" do
9
+ before { FileUtils.rm_rf "tmp" }
10
+
9
11
  context "('tmp/foo.json')" do
10
12
  subject { Ccp::Persistent.load('tmp/foo.json') }
11
13
  its(:class) { should == Ccp::Persistent::File }
@@ -27,6 +29,27 @@ describe Ccp::Persistent do
27
29
  its(:source) { should == 'tmp/foo.json' }
28
30
  end
29
31
 
32
+ context "(Pathname('tmp/foo.json) with existed directory')" do
33
+ before { FileUtils.mkdir_p("tmp/foo.json") }
34
+ subject { Ccp::Persistent.load(Pathname('tmp/foo.json')) }
35
+ its(:class) { should == Ccp::Persistent::Dir }
36
+ its(:ext) { should == "json" }
37
+ its(:source) { should == Pathname('tmp/foo.json') }
38
+ end
39
+
40
+ context "('tmp/foo.json with existed directory')" do
41
+ before { FileUtils.mkdir_p("tmp/foo.json") }
42
+ subject { Ccp::Persistent.load('tmp/foo.json') }
43
+ its(:class) { should == Ccp::Persistent::Dir }
44
+ its(:ext) { should == "json" }
45
+ its(:source) { should == 'tmp/foo.json' }
46
+
47
+ it "can read!" do
48
+ save_fixture("tmp/foo.json/a.json", 1)
49
+ subject.read!.should == {"a"=>1}
50
+ end
51
+ end
52
+
30
53
  context "('tmp/foo')" do
31
54
  it "should raise Ccp::Serializers::NotFound" do
32
55
  lambda {
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -145,7 +145,9 @@ 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_composite_spec.rb
148
149
  - spec/commands/fixturable_test_spec.rb
150
+ - spec/examples/fixture_save_dir_and_test_spec.rb
149
151
  - spec/fixtures/cmd1stub_mock/mock.json
150
152
  - spec/fixtures/stub/breadcrumbs.json
151
153
  - spec/invokers/base_spec.rb