sinclair 1.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +24 -0
- data/.rubocop_todo.yml +1 -1
- data/README.md +126 -103
- data/lib/sinclair.rb +40 -10
- data/lib/sinclair/method_definition.rb +96 -15
- data/lib/sinclair/version.rb +1 -1
- data/spec/integration/readme/my_class_spec.rb +13 -10
- data/spec/integration/readme/my_model_spec.rb +29 -0
- data/spec/integration/readme/sinclair/matchers_spec.rb +24 -0
- data/spec/integration/readme/sinclair_spec.rb +25 -0
- data/spec/integration/{matcher_spec.rb → sinclair/matchers_spec.rb} +7 -7
- data/spec/integration/yard/my_builder_spec.rb +27 -0
- data/spec/integration/yard/{matchers → sinclair/matchers}/add_method_spec.rb +2 -2
- data/spec/integration/yard/{matchers → sinclair/matchers}/add_method_to_spec.rb +2 -2
- data/spec/integration/yard/sinclair/method_definition_spec.rb +54 -0
- data/spec/integration/yard/{options_parser_spec.rb → sinclair/options_parser_spec.rb} +5 -4
- data/spec/integration/yard/sinclair_spec.rb +17 -19
- data/spec/lib/sinclair/matchers/add_method_spec.rb +7 -8
- data/spec/lib/sinclair/matchers/add_method_to_spec.rb +28 -26
- data/spec/lib/sinclair/method_definition_spec.rb +75 -20
- data/spec/lib/sinclair/options_parser_spec.rb +14 -15
- data/spec/lib/sinclair_spec.rb +43 -43
- data/spec/spec_helper.rb +0 -1
- data/spec/support/models/default_value.rb +20 -0
- data/spec/support/models/my_builder.rb +14 -13
- metadata +12 -8
- data/spec/integration/readme/matcher_spec.rb +0 -27
- data/spec/integration/readme_spec.rb +0 -23
@@ -2,14 +2,15 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe
|
6
|
-
describe
|
5
|
+
describe Sinclair::OptionsParser do
|
6
|
+
describe 'yard' do
|
7
|
+
subject(:model) { described_class::Dummy.new(options) }
|
8
|
+
|
7
9
|
let(:options) { { switch: false, option_1: 10, option_2: 20 } }
|
8
|
-
subject { described_class::Dummy.new(options) }
|
9
10
|
|
10
11
|
describe '#the_method' do
|
11
12
|
it 'returns the value for option given' do
|
12
|
-
expect(
|
13
|
+
expect(model.the_method).to eq('The value is not 10 but 20')
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -2,27 +2,27 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe
|
6
|
-
describe
|
7
|
-
let(:klass)
|
8
|
-
let(:instance)
|
9
|
-
let(:builder)
|
5
|
+
describe Sinclair do
|
6
|
+
describe 'yarn' do
|
7
|
+
let(:klass) { Class.new(MyModel) }
|
8
|
+
let(:instance) { klass.new }
|
9
|
+
let(:builder) { described_class.new(klass) }
|
10
10
|
let(:default_value) { 10 }
|
11
11
|
|
12
12
|
describe '#initialize' do
|
13
13
|
describe '#total_price' do
|
14
14
|
before do
|
15
|
-
|
15
|
+
builder.eval_and_add_method(:total_price) do
|
16
16
|
code = 'self.value * self.quantity'
|
17
17
|
code.concat ' rescue 0' if options_object.rescue_error
|
18
18
|
code
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
builder.build
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'without options' do
|
25
|
-
subject {
|
25
|
+
subject(:builder) { described_class.new(klass, rescue_error: true) }
|
26
26
|
|
27
27
|
let(:klass) { Class.new(Purchase) }
|
28
28
|
let(:instance) { klass.new(2.3, 5) }
|
@@ -33,7 +33,7 @@ describe 'yarn' do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'with options' do
|
36
|
-
subject {
|
36
|
+
subject(:model) { described_class.new(klass) }
|
37
37
|
|
38
38
|
let(:klass) { Class.new(Purchase) }
|
39
39
|
let(:instance) { klass.new(2.3, 5) }
|
@@ -63,8 +63,9 @@ describe 'yarn' do
|
|
63
63
|
builder.add_method(:value=) { |val| @value = val }
|
64
64
|
end
|
65
65
|
|
66
|
-
|
66
|
+
describe 'after the build' do
|
67
67
|
before { builder.build }
|
68
|
+
|
68
69
|
it 'creates the expected methods' do
|
69
70
|
expect(instance.value).to eq(10)
|
70
71
|
end
|
@@ -80,7 +81,7 @@ describe 'yarn' do
|
|
80
81
|
end
|
81
82
|
end
|
82
83
|
|
83
|
-
context 'calling the build' do
|
84
|
+
context 'when calling the build' do
|
84
85
|
it do
|
85
86
|
expect do
|
86
87
|
builder.build
|
@@ -112,13 +113,10 @@ describe 'yarn' do
|
|
112
113
|
expect(instance.bond_name).to eq('wick, john wick')
|
113
114
|
end
|
114
115
|
end
|
115
|
-
|
116
|
-
describe '#bond_style' do
|
117
|
-
end
|
118
116
|
end
|
119
117
|
|
120
118
|
describe '#eval_and_add_method' do
|
121
|
-
subject { klass.new }
|
119
|
+
subject(:builder) { klass.new }
|
122
120
|
|
123
121
|
let(:klass) do
|
124
122
|
Class.new do
|
@@ -131,18 +129,18 @@ describe 'yarn' do
|
|
131
129
|
describe '#age' do
|
132
130
|
context 'when it has not been initialized' do
|
133
131
|
it do
|
134
|
-
expect(
|
132
|
+
expect(builder.age).to eq(20)
|
135
133
|
end
|
136
134
|
end
|
137
135
|
|
138
136
|
context 'when it has been initialized' do
|
139
137
|
before do
|
140
|
-
|
141
|
-
|
138
|
+
builder.age
|
139
|
+
builder.age = 30
|
142
140
|
end
|
143
141
|
|
144
142
|
it do
|
145
|
-
expect(
|
143
|
+
expect(builder.age).to eq(30)
|
146
144
|
end
|
147
145
|
end
|
148
146
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Sinclair::Matchers::AddMethod do
|
6
|
-
subject { described_class.new(method) }
|
6
|
+
subject(:matcher) { described_class.new(method) }
|
7
7
|
|
8
8
|
let(:method) { :the_method }
|
9
9
|
let(:klass) { Class.new }
|
@@ -11,22 +11,21 @@ describe Sinclair::Matchers::AddMethod do
|
|
11
11
|
|
12
12
|
describe '#to' do
|
13
13
|
it do
|
14
|
-
expect(
|
14
|
+
expect(matcher.to(klass.new)).to be_a(Sinclair::Matchers::AddMethodTo)
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'returns an add_method_to' do
|
18
|
-
expect(
|
18
|
+
expect(matcher.to(instance)).to eq(Sinclair::Matchers::AddMethodTo.new(instance, method))
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe '#matches?' do
|
23
23
|
it do
|
24
|
-
expect
|
25
|
-
|
26
|
-
|
27
|
-
SyntaxError, 'You should specify which instance the method is being added to' \
|
24
|
+
expect { matcher.matches?(proc {}) }
|
25
|
+
.to raise_error(
|
26
|
+
SyntaxError, 'You should specify which instance the method is being added to' \
|
28
27
|
"add_method(:#{method}).to(instance)"
|
29
|
-
|
28
|
+
)
|
30
29
|
end
|
31
30
|
end
|
32
31
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Sinclair::Matchers::AddMethodTo do
|
6
|
-
subject { described_class.new(instance, method) }
|
6
|
+
subject(:matcher) { described_class.new(instance, method) }
|
7
7
|
|
8
8
|
let(:method) { :the_method }
|
9
9
|
let(:klass) { Class.new }
|
@@ -15,13 +15,13 @@ describe Sinclair::Matchers::AddMethodTo do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
context 'when a method is added' do
|
18
|
-
it { expect(
|
18
|
+
it { expect(matcher).to be_matches(event_proc) }
|
19
19
|
end
|
20
20
|
|
21
21
|
context 'when a method is not added' do
|
22
22
|
let(:event_proc) { proc {} }
|
23
23
|
|
24
|
-
it { expect(
|
24
|
+
it { expect(matcher).not_to be_matches(event_proc) }
|
25
25
|
end
|
26
26
|
|
27
27
|
context 'when the wrong method is added' do
|
@@ -29,69 +29,71 @@ describe Sinclair::Matchers::AddMethodTo do
|
|
29
29
|
proc { klass.send(:define_method, :another_method) {} }
|
30
30
|
end
|
31
31
|
|
32
|
-
it { expect(
|
32
|
+
it { expect(matcher).not_to be_matches(event_proc) }
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'when initializing with class' do
|
36
|
-
subject { described_class.new(klass, method) }
|
36
|
+
subject(:matcher) { described_class.new(klass, method) }
|
37
37
|
|
38
38
|
context 'when a method is added' do
|
39
|
-
it { expect(
|
39
|
+
it { expect(matcher).to be_matches(event_proc) }
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
44
|
describe '#failure_message_for_should' do
|
45
45
|
it 'returns information on the instance class and method' do
|
46
|
-
expect(
|
46
|
+
expect(matcher.failure_message_for_should)
|
47
47
|
.to eq("expected '#{method}' to be added to #{klass} but it didn't")
|
48
48
|
end
|
49
49
|
|
50
50
|
context 'when method already exited' do
|
51
51
|
before do
|
52
52
|
klass.send(:define_method, method) {}
|
53
|
-
|
53
|
+
matcher.matches?(proc {})
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'returns information on the instance class and method' do
|
57
|
-
expect(
|
57
|
+
expect(matcher.failure_message_for_should)
|
58
58
|
.to eq("expected '#{method}' to be added to #{klass} but it already existed")
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
context 'when initializing with class' do
|
63
|
-
subject { described_class.new(klass, method) }
|
63
|
+
subject(:matcher) { described_class.new(klass, method) }
|
64
64
|
|
65
65
|
it 'returns information on the instance class and method' do
|
66
|
-
expect(
|
66
|
+
expect(matcher.failure_message_for_should)
|
67
67
|
.to eq("expected '#{method}' to be added to #{klass} but it didn't")
|
68
68
|
end
|
69
|
+
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
klass.send(:define_method, method) {}
|
73
|
-
subject.matches?(proc {})
|
74
|
-
end
|
71
|
+
context 'when initializing with class and method already exited' do
|
72
|
+
subject(:matcher) { described_class.new(klass, method) }
|
75
73
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
before do
|
75
|
+
klass.send(:define_method, method) {}
|
76
|
+
matcher.matches?(proc {})
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'returns information on the instance class and method' do
|
80
|
+
expect(matcher.failure_message_for_should)
|
81
|
+
.to eq("expected '#{method}' to be added to #{klass} but it already existed")
|
80
82
|
end
|
81
83
|
end
|
82
84
|
end
|
83
85
|
|
84
86
|
describe '#failure_message_for_should_not' do
|
85
87
|
it 'returns information on the instance class and method' do
|
86
|
-
expect(
|
88
|
+
expect(matcher.failure_message_for_should_not)
|
87
89
|
.to eq("expected '#{method}' not to be added to #{klass} but it was")
|
88
90
|
end
|
89
91
|
|
90
92
|
context 'when initializing with class' do
|
91
|
-
subject { described_class.new(klass, method) }
|
93
|
+
subject(:matcher) { described_class.new(klass, method) }
|
92
94
|
|
93
95
|
it 'returns information on the instance class and method' do
|
94
|
-
expect(
|
96
|
+
expect(matcher.failure_message_for_should_not)
|
95
97
|
.to eq("expected '#{method}' not to be added to #{klass} but it was")
|
96
98
|
end
|
97
99
|
end
|
@@ -99,15 +101,15 @@ describe Sinclair::Matchers::AddMethodTo do
|
|
99
101
|
|
100
102
|
describe 'description' do
|
101
103
|
it 'returns information on the instance class and method' do
|
102
|
-
expect(
|
104
|
+
expect(matcher.description)
|
103
105
|
.to eq("add method '#{method}' to #{klass} instances")
|
104
106
|
end
|
105
107
|
|
106
108
|
context 'when initializing with class' do
|
107
|
-
subject { described_class.new(klass, method) }
|
109
|
+
subject(:matcher) { described_class.new(klass, method) }
|
108
110
|
|
109
111
|
it 'returns information on the instance class and method' do
|
110
|
-
expect(
|
112
|
+
expect(matcher.description)
|
111
113
|
.to eq("add method '#{method}' to #{klass} instances")
|
112
114
|
end
|
113
115
|
end
|
@@ -2,48 +2,103 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
+
shared_examples 'MethodDefinition#build' do
|
6
|
+
it do
|
7
|
+
expect { method_definition.build(klass) }.to add_method(method_name).to(klass)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'after build' do
|
11
|
+
before do
|
12
|
+
method_definition.build(klass)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'adds the method to the klass instance' do
|
16
|
+
expect(instance).to respond_to(method_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'evaluates return of the method within the instance context' do
|
20
|
+
expect(instance.the_method).to eq(1)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'evaluates in the context of the instance' do
|
24
|
+
expect { instance.the_method }
|
25
|
+
.to change { instance.instance_variable_get(:@x) }
|
26
|
+
.from(nil).to(1)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
5
31
|
describe Sinclair::MethodDefinition do
|
6
|
-
let(:klass)
|
32
|
+
let(:klass) { Class.new }
|
7
33
|
let(:instance) { klass.new }
|
8
34
|
|
9
35
|
describe '#build' do
|
10
36
|
let(:method_name) { :the_method }
|
11
37
|
|
12
|
-
context 'when method was defined with
|
13
|
-
|
38
|
+
context 'when method was defined with a string' do
|
39
|
+
subject(:method_definition) do
|
40
|
+
described_class.new(method_name, code)
|
41
|
+
end
|
14
42
|
|
15
|
-
|
43
|
+
let(:code) { '@x = @x.to_i + 1' }
|
16
44
|
|
17
|
-
|
18
|
-
subject.build(klass)
|
19
|
-
end
|
45
|
+
it_behaves_like 'MethodDefinition#build'
|
20
46
|
|
21
|
-
it '
|
22
|
-
|
47
|
+
it 'creates a dynamic method' do
|
48
|
+
method_definition.build(klass)
|
49
|
+
expect { instance.the_method }.to change(instance, :the_method)
|
50
|
+
.from(1).to(3)
|
23
51
|
end
|
24
52
|
|
25
|
-
|
26
|
-
|
53
|
+
context 'with cached options' do
|
54
|
+
subject(:method_definition) do
|
55
|
+
described_class.new(method_name, code, cached: true)
|
56
|
+
end
|
57
|
+
|
58
|
+
it_behaves_like 'MethodDefinition#build'
|
59
|
+
|
60
|
+
it 'creates a semi-dynamic method' do
|
61
|
+
method_definition.build(klass)
|
62
|
+
expect { instance.the_method }.not_to change(instance, :the_method)
|
63
|
+
end
|
27
64
|
end
|
28
65
|
end
|
29
66
|
|
30
67
|
context 'when method was defined with a block' do
|
31
|
-
subject do
|
68
|
+
subject(:method_definition) do
|
32
69
|
described_class.new(method_name) do
|
33
|
-
|
70
|
+
@x = @x.to_i + 1
|
34
71
|
end
|
35
72
|
end
|
36
73
|
|
37
|
-
|
38
|
-
subject.build(klass)
|
39
|
-
end
|
74
|
+
it_behaves_like 'MethodDefinition#build'
|
40
75
|
|
41
|
-
it '
|
42
|
-
|
76
|
+
it 'creates a dynamic method' do
|
77
|
+
method_definition.build(klass)
|
78
|
+
expect { instance.the_method }.to change(instance, :the_method)
|
79
|
+
.from(1).to(3)
|
43
80
|
end
|
44
81
|
|
45
|
-
|
46
|
-
|
82
|
+
context 'with cached options' do
|
83
|
+
subject(:method_definition) do
|
84
|
+
described_class.new(method_name, cached: true) do
|
85
|
+
@x = @x.to_i + 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it_behaves_like 'MethodDefinition#build'
|
90
|
+
|
91
|
+
it 'creates a dynamic method' do
|
92
|
+
method_definition.build(klass)
|
93
|
+
expect { instance.the_method }.not_to change(instance, :the_method)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'sets the instance variable' do
|
97
|
+
method_definition.build(klass)
|
98
|
+
expect { instance.the_method }
|
99
|
+
.to change { instance.instance_variable_get("@#{method_name}") }
|
100
|
+
.from(nil).to(1)
|
101
|
+
end
|
47
102
|
end
|
48
103
|
end
|
49
104
|
end
|
@@ -3,24 +3,22 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Sinclair::OptionsParser do
|
6
|
-
|
7
|
-
let(:switched) { true }
|
8
|
-
let(:value_1) { 'value1' }
|
9
|
-
let(:options) { { switch: switched, option_1: value_1, option_2: 2 } }
|
6
|
+
subject(:model) { klass.new(options) }
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
let(:klass) { described_class::Dummy }
|
9
|
+
let(:switched) { true }
|
10
|
+
let(:value_1) { 'value1' }
|
11
|
+
let(:options) { { switch: switched, option_1: value_1, option_2: 2 } }
|
14
12
|
|
15
13
|
it 'enables the given options to be acced' do
|
16
|
-
expect(
|
14
|
+
expect(model.the_method).to eq('The value is value1')
|
17
15
|
end
|
18
16
|
|
19
17
|
context 'when changing the options' do
|
20
18
|
let(:switched) { false }
|
21
19
|
|
22
20
|
it 'enables the given options to be acced' do
|
23
|
-
expect(
|
21
|
+
expect(model.the_method).to eq('The value is not value1 but 2')
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
@@ -28,33 +26,34 @@ describe Sinclair::OptionsParser do
|
|
28
26
|
let(:options) { { option_1: 'value1', option_2: 2 } }
|
29
27
|
|
30
28
|
it do
|
31
|
-
expect {
|
29
|
+
expect { model.the_method }.not_to raise_error
|
32
30
|
end
|
33
31
|
|
34
32
|
it 'considers is to be nil' do
|
35
|
-
expect(
|
33
|
+
expect(model.the_method).to eq('missing option')
|
36
34
|
end
|
37
35
|
end
|
38
36
|
|
39
37
|
context 'when changing the options before the option call' do
|
40
38
|
before do
|
41
|
-
|
39
|
+
model
|
42
40
|
options[:switch] = false
|
43
41
|
end
|
44
42
|
|
45
43
|
it 'does not reevaluate the options' do
|
46
|
-
expect(
|
44
|
+
expect(model.the_method).to eq('The value is value1')
|
47
45
|
end
|
48
46
|
|
49
47
|
context 'when the option value is another object on its own' do
|
50
48
|
let(:value_1) { { key: 'value' } }
|
49
|
+
|
51
50
|
before do
|
52
|
-
|
51
|
+
model
|
53
52
|
options[:option_1][:key] = 100
|
54
53
|
end
|
55
54
|
|
56
55
|
it 'does not reevaluate the options' do
|
57
|
-
expect(
|
56
|
+
expect(model.the_method).to eq('The value is {:key=>"value"}')
|
58
57
|
end
|
59
58
|
end
|
60
59
|
end
|