sinclair 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +1 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +216 -0
- data/README.md +76 -3
- data/lib/sinclair.rb +1 -0
- data/lib/sinclair/matchers.rb +10 -0
- data/lib/sinclair/matchers/add_method.rb +29 -0
- data/lib/sinclair/matchers/add_method_to.rb +71 -0
- data/lib/sinclair/version.rb +1 -1
- data/sinclair.gemspec +1 -0
- data/sinclair.jpg +0 -0
- data/spec/integration/matcher_spec.rb +96 -0
- data/spec/integration/readme/matcher_spec.rb +25 -0
- data/spec/lib/sinclair/matchers/add_method_spec.rb +30 -0
- data/spec/lib/sinclair/matchers/add_method_to_spec.rb +113 -0
- data/spec/lib/sinclair/matchers_spec.rb +17 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/models/my_builder.rb +18 -0
- metadata +28 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b59fc038965ca4d716620bcdba5e7cb3eb250b6d
|
4
|
+
data.tar.gz: 060a5b3eaf6b829a4b3cd97acdf6465cab863e06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1846ce94184b85e892a2c3ec24c5420f159bcf330b377d0f4694cb4e5c06779fd4d03cc35bed5967a7799761408416c76d6b82eae270e5b105c3e941634f9c95
|
7
|
+
data.tar.gz: 0e348b3209b78b692d9a98f6badf4f77a4e52f27e230f24b7d6257dd326be20ce64f0c9a6aa33cfcdc8ff32007e86047bbe8354dbc09e37cf4bb73c163a0d76e
|
data/.circleci/config.yml
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2018-06-29 18:23:09 +0000 using RuboCop version 0.57.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
# Cop supports --auto-correct.
|
11
|
+
# Configuration parameters: Include, TreatCommentsAsGroupSeparators.
|
12
|
+
# Include: **/*.gemspec
|
13
|
+
Gemspec/OrderedDependencies:
|
14
|
+
Exclude:
|
15
|
+
- 'sinclair.gemspec'
|
16
|
+
|
17
|
+
# Offense count: 1
|
18
|
+
# Cop supports --auto-correct.
|
19
|
+
# Configuration parameters: EnforcedStyleAlignWith.
|
20
|
+
# SupportedStylesAlignWith: either, start_of_block, start_of_line
|
21
|
+
Layout/BlockAlignment:
|
22
|
+
Exclude:
|
23
|
+
- 'spec/lib/sinclair/method_definition_spec.rb'
|
24
|
+
|
25
|
+
# Offense count: 1
|
26
|
+
# Cop supports --auto-correct.
|
27
|
+
Layout/EmptyLineAfterMagicComment:
|
28
|
+
Exclude:
|
29
|
+
- 'sinclair.gemspec'
|
30
|
+
|
31
|
+
# Offense count: 3
|
32
|
+
# Cop supports --auto-correct.
|
33
|
+
# Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
|
34
|
+
Layout/ExtraSpacing:
|
35
|
+
Exclude:
|
36
|
+
- 'sinclair.gemspec'
|
37
|
+
- 'spec/lib/sinclair_spec.rb'
|
38
|
+
- 'spec/spec_helper.rb'
|
39
|
+
|
40
|
+
# Offense count: 3
|
41
|
+
# Cop supports --auto-correct.
|
42
|
+
# Configuration parameters: Width, IgnoredPatterns.
|
43
|
+
Layout/IndentationWidth:
|
44
|
+
Exclude:
|
45
|
+
- 'spec/lib/sinclair/method_definition_spec.rb'
|
46
|
+
- 'spec/support/fixture_helpers.rb'
|
47
|
+
|
48
|
+
# Offense count: 1
|
49
|
+
# Cop supports --auto-correct.
|
50
|
+
# Configuration parameters: EnforcedStyle.
|
51
|
+
# SupportedStyles: space, no_space
|
52
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
53
|
+
Exclude:
|
54
|
+
- 'spec/support/models/validator_builder.rb'
|
55
|
+
|
56
|
+
# Offense count: 1
|
57
|
+
# Cop supports --auto-correct.
|
58
|
+
# Configuration parameters: EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
|
59
|
+
# SupportedStyles: space, no_space
|
60
|
+
# SupportedStylesForEmptyBraces: space, no_space
|
61
|
+
Layout/SpaceInsideBlockBraces:
|
62
|
+
EnforcedStyle: space
|
63
|
+
|
64
|
+
# Offense count: 2
|
65
|
+
# Cop supports --auto-correct.
|
66
|
+
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
|
67
|
+
# SupportedStyles: space, no_space, compact
|
68
|
+
# SupportedStylesForEmptyBraces: space, no_space
|
69
|
+
Layout/SpaceInsideHashLiteralBraces:
|
70
|
+
Exclude:
|
71
|
+
- 'spec/lib/sinclair/options_parser_spec.rb'
|
72
|
+
|
73
|
+
# Offense count: 4
|
74
|
+
# Cop supports --auto-correct.
|
75
|
+
# Configuration parameters: EnforcedStyle.
|
76
|
+
# SupportedStyles: space, no_space
|
77
|
+
Layout/SpaceInsideStringInterpolation:
|
78
|
+
Exclude:
|
79
|
+
- 'spec/lib/sinclair_spec.rb'
|
80
|
+
- 'spec/support/models/dummy_builder.rb'
|
81
|
+
|
82
|
+
# Offense count: 2
|
83
|
+
# Cop supports --auto-correct.
|
84
|
+
# Configuration parameters: EnforcedStyle.
|
85
|
+
# SupportedStyles: final_newline, final_blank_line
|
86
|
+
Layout/TrailingBlankLines:
|
87
|
+
Exclude:
|
88
|
+
- 'lib/sinclair/version.rb'
|
89
|
+
- 'sinclair.gemspec'
|
90
|
+
|
91
|
+
# Offense count: 1
|
92
|
+
# Cop supports --auto-correct.
|
93
|
+
# Configuration parameters: AllowInHeredoc.
|
94
|
+
Layout/TrailingWhitespace:
|
95
|
+
Exclude:
|
96
|
+
- 'spec/integration/matcher_spec.rb'
|
97
|
+
|
98
|
+
# Offense count: 1
|
99
|
+
Lint/ParenthesesAsGroupedExpression:
|
100
|
+
Exclude:
|
101
|
+
- 'spec/support/fixture_helpers.rb'
|
102
|
+
|
103
|
+
# Offense count: 9
|
104
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
105
|
+
Metrics/BlockLength:
|
106
|
+
Max: 119
|
107
|
+
|
108
|
+
# Offense count: 4
|
109
|
+
# Cop supports --auto-correct.
|
110
|
+
# Configuration parameters: AutoCorrect, EnforcedStyle.
|
111
|
+
# SupportedStyles: nested, compact
|
112
|
+
Style/ClassAndModuleChildren:
|
113
|
+
Exclude:
|
114
|
+
- 'lib/sinclair/method_definition.rb'
|
115
|
+
- 'lib/sinclair/options_parser.rb'
|
116
|
+
- 'spec/support/models/dummy_builder.rb'
|
117
|
+
- 'spec/support/models/dummy_options_parser.rb'
|
118
|
+
|
119
|
+
# Offense count: 6
|
120
|
+
Style/Documentation:
|
121
|
+
Exclude:
|
122
|
+
- 'spec/**/*'
|
123
|
+
- 'test/**/*'
|
124
|
+
- 'lib/sinclair.rb'
|
125
|
+
- 'lib/sinclair/matchers.rb'
|
126
|
+
- 'lib/sinclair/matchers/add_method.rb'
|
127
|
+
- 'lib/sinclair/matchers/add_method_to.rb'
|
128
|
+
- 'lib/sinclair/method_definition.rb'
|
129
|
+
- 'lib/sinclair/options_parser.rb'
|
130
|
+
|
131
|
+
# Offense count: 1
|
132
|
+
# Cop supports --auto-correct.
|
133
|
+
Style/Encoding:
|
134
|
+
Exclude:
|
135
|
+
- 'sinclair.gemspec'
|
136
|
+
|
137
|
+
# Offense count: 1
|
138
|
+
# Cop supports --auto-correct.
|
139
|
+
Style/ExpandPathArguments:
|
140
|
+
Exclude:
|
141
|
+
- 'sinclair.gemspec'
|
142
|
+
|
143
|
+
# Offense count: 1
|
144
|
+
# Cop supports --auto-correct.
|
145
|
+
# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
|
146
|
+
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
|
147
|
+
Style/HashSyntax:
|
148
|
+
Exclude:
|
149
|
+
- 'Rakefile'
|
150
|
+
|
151
|
+
# Offense count: 1
|
152
|
+
# Cop supports --auto-correct.
|
153
|
+
Style/MutableConstant:
|
154
|
+
Exclude:
|
155
|
+
- 'lib/sinclair/version.rb'
|
156
|
+
|
157
|
+
# Offense count: 3
|
158
|
+
# Cop supports --auto-correct.
|
159
|
+
# Configuration parameters: Whitelist.
|
160
|
+
# Whitelist: be, be_a, be_an, be_between, be_falsey, be_kind_of, be_instance_of, be_truthy, be_within, eq, eql, end_with, include, match, raise_error, respond_to, start_with
|
161
|
+
Style/NestedParenthesizedCalls:
|
162
|
+
Exclude:
|
163
|
+
- 'spec/lib/sinclair/matchers/add_method_to_spec.rb'
|
164
|
+
|
165
|
+
# Offense count: 1
|
166
|
+
# Cop supports --auto-correct.
|
167
|
+
# Configuration parameters: PreferredDelimiters.
|
168
|
+
Style/PercentLiteralDelimiters:
|
169
|
+
Exclude:
|
170
|
+
- 'spec/integration/readme/my_class_spec.rb'
|
171
|
+
|
172
|
+
# Offense count: 3
|
173
|
+
# Cop supports --auto-correct.
|
174
|
+
Style/Proc:
|
175
|
+
Exclude:
|
176
|
+
- 'spec/lib/sinclair/matchers/add_method_to_spec.rb'
|
177
|
+
|
178
|
+
# Offense count: 1
|
179
|
+
# Cop supports --auto-correct.
|
180
|
+
Style/RedundantParentheses:
|
181
|
+
Exclude:
|
182
|
+
- 'spec/support/fixture_helpers.rb'
|
183
|
+
|
184
|
+
# Offense count: 1
|
185
|
+
# Cop supports --auto-correct.
|
186
|
+
Style/RedundantSelf:
|
187
|
+
Exclude:
|
188
|
+
- 'spec/lib/sinclair/method_definition_spec.rb'
|
189
|
+
|
190
|
+
# Offense count: 1
|
191
|
+
# Cop supports --auto-correct.
|
192
|
+
# Configuration parameters: EnforcedStyle.
|
193
|
+
# SupportedStyles: use_perl_names, use_english_names
|
194
|
+
Style/SpecialGlobalVars:
|
195
|
+
Exclude:
|
196
|
+
- 'sinclair.gemspec'
|
197
|
+
|
198
|
+
# Offense count: 14
|
199
|
+
# Cop supports --auto-correct.
|
200
|
+
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
|
201
|
+
# SupportedStyles: single_quotes, double_quotes
|
202
|
+
Style/StringLiterals:
|
203
|
+
Exclude:
|
204
|
+
- 'Gemfile'
|
205
|
+
- 'Rakefile'
|
206
|
+
- 'sinclair.gemspec'
|
207
|
+
- 'spec/lib/sinclair/method_definition_spec.rb'
|
208
|
+
- 'spec/lib/sinclair_spec.rb'
|
209
|
+
- 'spec/spec_helper.rb'
|
210
|
+
- 'spec/support/models/dummy_builder.rb'
|
211
|
+
|
212
|
+
# Offense count: 8
|
213
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
214
|
+
# URISchemes: http, https
|
215
|
+
Metrics/LineLength:
|
216
|
+
Max: 103
|
data/README.md
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
Sinclair
|
2
2
|
========
|
3
|
+
[![Code Climate](https://codeclimate.com/github/darthjee/sinclair/badges/gpa.svg)](https://codeclimate.com/github/darthjee/sinclair)
|
4
|
+
[![Test Coverage](https://codeclimate.com/github/darthjee/sinclair/badges/coverage.svg)](https://codeclimate.com/github/darthjee/sinclair/coverage)
|
5
|
+
[![Issue Count](https://codeclimate.com/github/darthjee/sinclair/badges/issue_count.svg)](https://codeclimate.com/github/darthjee/sinclair)
|
6
|
+
|
7
|
+
|
8
|
+
![sinclair](https://raw.githubusercontent.com/darthjee/sinclair/master/sinclair.jpg)
|
3
9
|
|
4
10
|
This gem helps the creation of complex concern with class methods
|
5
11
|
|
6
|
-
|
12
|
+
Installation
|
7
13
|
---------------
|
8
|
-
1. Installation
|
9
14
|
- Install it
|
10
15
|
|
11
16
|
```ruby
|
@@ -22,7 +27,8 @@ Getting started
|
|
22
27
|
bundle install sinclair
|
23
28
|
```
|
24
29
|
|
25
|
-
|
30
|
+
Usage
|
31
|
+
---------------
|
26
32
|
The concern builder can actully be used in two ways, as an stand alone object capable of
|
27
33
|
adding methods to your class or by extending it for more complex logics
|
28
34
|
|
@@ -150,3 +156,70 @@ adding methods to your class or by extending it for more complex logics
|
|
150
156
|
```
|
151
157
|
false
|
152
158
|
```
|
159
|
+
|
160
|
+
RSspec matcher
|
161
|
+
---------------
|
162
|
+
|
163
|
+
You can use the provided matcher to check that your builder is adding a method correctly
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class DefaultValue
|
167
|
+
delegate :build, to: :builder
|
168
|
+
attr_reader :klass, :method, :value
|
169
|
+
|
170
|
+
def initialize(klass, method, value)
|
171
|
+
@klass = klass
|
172
|
+
@method = method
|
173
|
+
@value = value
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
def builder
|
179
|
+
@builder ||= Sinclair.new(klass).tap do |b|
|
180
|
+
b.add_method(method) { value }
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
RSpec.configure do |config|
|
186
|
+
config.include Sinclair::Matchers
|
187
|
+
end
|
188
|
+
|
189
|
+
RSpec.describe DefaultValue do
|
190
|
+
let(:klass) { Class.new }
|
191
|
+
let(:method) { :the_method }
|
192
|
+
let(:value) { Random.rand(100) }
|
193
|
+
let(:builder) { described_class.new(klass, method, value) }
|
194
|
+
let(:instance) { klass.new }
|
195
|
+
|
196
|
+
context 'when the builder runs' do
|
197
|
+
it do
|
198
|
+
expect do
|
199
|
+
described_class.new(klass, method, value).build
|
200
|
+
end.to add_method(method).to(instance)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context 'when the builder runs' do
|
205
|
+
it do
|
206
|
+
expect do
|
207
|
+
described_class.new(klass, method, value).build
|
208
|
+
end.to add_method(method).to(klass)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
```
|
213
|
+
|
214
|
+
```bash
|
215
|
+
> bundle exec rspec
|
216
|
+
```
|
217
|
+
|
218
|
+
```string
|
219
|
+
DefaultValue
|
220
|
+
when the builder runs
|
221
|
+
should add method 'the_method' to #<Class:0x0000000146c160> instances
|
222
|
+
when the builder runs
|
223
|
+
should add method 'the_method' to #<Class:0x0000000143a1b0> instances
|
224
|
+
|
225
|
+
```
|
data/lib/sinclair.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Sinclair
|
2
|
+
module Matchers
|
3
|
+
class AddMethod < RSpec::Matchers::BuiltIn::BaseMatcher
|
4
|
+
attr_reader :method
|
5
|
+
|
6
|
+
def matches?(_actual)
|
7
|
+
raise SyntaxError, 'You should specify which instance the method is being added to' \
|
8
|
+
"add_method(:#{method}).to(instance)"
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(method = nil)
|
12
|
+
@method = method
|
13
|
+
end
|
14
|
+
|
15
|
+
def to(instance = nil)
|
16
|
+
AddMethodTo.new(instance, method)
|
17
|
+
end
|
18
|
+
|
19
|
+
def equal?(other)
|
20
|
+
return unless other.class == self.class
|
21
|
+
other.method == method
|
22
|
+
end
|
23
|
+
|
24
|
+
def supports_block_expectations?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
class Sinclair
|
2
|
+
module Matchers
|
3
|
+
class AddMethodTo < RSpec::Matchers::BuiltIn::BaseMatcher
|
4
|
+
attr_reader :method, :instance
|
5
|
+
|
6
|
+
def initialize(instance, method)
|
7
|
+
if instance.is_a?(Class)
|
8
|
+
@instance_class = instance
|
9
|
+
else
|
10
|
+
@instance = instance
|
11
|
+
end
|
12
|
+
@method = method
|
13
|
+
end
|
14
|
+
|
15
|
+
def description
|
16
|
+
"add method '#{method}' to #{instance_class} instances"
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure_message_for_should
|
20
|
+
"expected '#{method}' to be added to #{instance_class} but " \
|
21
|
+
"#{@initial_state ? 'it already existed' : "it didn't"}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def failure_message_for_should_not
|
25
|
+
"expected '#{method}' not to be added to #{instance_class} but it was"
|
26
|
+
end
|
27
|
+
|
28
|
+
def matches?(event_proc)
|
29
|
+
return false unless event_proc.is_a?(Proc)
|
30
|
+
raise_block_syntax_error if block_given?
|
31
|
+
perform_change(event_proc)
|
32
|
+
changed?
|
33
|
+
end
|
34
|
+
|
35
|
+
def supports_block_expectations?
|
36
|
+
true
|
37
|
+
end
|
38
|
+
|
39
|
+
def equal?(other)
|
40
|
+
return unless other.class == self.class
|
41
|
+
other.method == method &&
|
42
|
+
other.instance == instance
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
def changed?
|
48
|
+
!@initial_state && @final_state
|
49
|
+
end
|
50
|
+
|
51
|
+
def perform_change(event_proc)
|
52
|
+
@initial_state = method_defined?
|
53
|
+
event_proc.call
|
54
|
+
@final_state = method_defined?
|
55
|
+
end
|
56
|
+
|
57
|
+
def method_defined?
|
58
|
+
instance_class.method_defined?(method)
|
59
|
+
end
|
60
|
+
|
61
|
+
def instance_class
|
62
|
+
@instance_class ||= instance.class
|
63
|
+
end
|
64
|
+
|
65
|
+
def raise_block_syntax_error
|
66
|
+
raise SyntaxError, 'Block not received by the `add_method_to` matcher. ' \
|
67
|
+
'Perhaps you want to use `{ ... }` instead of do/end?'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/sinclair/version.rb
CHANGED
data/sinclair.gemspec
CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |gem|
|
|
23
23
|
gem.add_development_dependency 'rake', '~> 12.0.0'
|
24
24
|
gem.add_development_dependency 'rspec', '~> 2.14'
|
25
25
|
gem.add_development_dependency 'simplecov', '~> 0.16.1'
|
26
|
+
gem.add_development_dependency 'rubocop'
|
26
27
|
gem.add_development_dependency 'pry-nav'
|
27
28
|
end
|
28
29
|
|
data/sinclair.jpg
ADDED
Binary file
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'matchers' do
|
4
|
+
describe 'add_method_to' do
|
5
|
+
let(:method) { :the_method }
|
6
|
+
let(:klass) { Class.new }
|
7
|
+
let(:instance) { klass.new }
|
8
|
+
let(:expectation) do
|
9
|
+
expect { block.call }.to add_method(method).to(instance)
|
10
|
+
end
|
11
|
+
let(:block) { proc { klass.send(:define_method, method) {} } }
|
12
|
+
|
13
|
+
context 'when method is added' do
|
14
|
+
it 'returns a succes' do
|
15
|
+
expect { expectation }.not_to raise_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when method is not added' do
|
20
|
+
let(:block) { proc {} }
|
21
|
+
|
22
|
+
it 'raises expectation error' do
|
23
|
+
expect { expectation }.to raise_error(
|
24
|
+
RSpec::Expectations::ExpectationNotMetError,
|
25
|
+
"expected 'the_method' to be added to #{klass} but it didn't"
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when method existed before' do
|
31
|
+
before do
|
32
|
+
block.call
|
33
|
+
end
|
34
|
+
it 'raises expectation error' do
|
35
|
+
expect { expectation }.to raise_error(
|
36
|
+
RSpec::Expectations::ExpectationNotMetError,
|
37
|
+
"expected 'the_method' to be added to #{klass} but it already existed"
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when negating' do
|
43
|
+
let(:expectation) do
|
44
|
+
expect { block.call }.not_to add_method(method).to(instance)
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when method is not added' do
|
48
|
+
let(:block) { proc {} }
|
49
|
+
|
50
|
+
it 'returns a succes' do
|
51
|
+
expect { expectation }.not_to raise_error
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when method is added' do
|
56
|
+
it 'raises expectation error' do
|
57
|
+
expect { expectation }.to raise_error(
|
58
|
+
RSpec::Expectations::ExpectationNotMetError,
|
59
|
+
"expected 'the_method' not to be added to #{klass} but it was"
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when method existed before' do
|
65
|
+
before do
|
66
|
+
block.call
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'returns a succes' do
|
70
|
+
expect { expectation }.not_to raise_error
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'add method' do
|
77
|
+
let(:method) { :the_method }
|
78
|
+
let(:klass) { Class.new }
|
79
|
+
let(:expectation) do
|
80
|
+
expect { block.call }.to add_method(method)
|
81
|
+
end
|
82
|
+
let(:block) { proc { klass.send(:define_method, method) {} } }
|
83
|
+
|
84
|
+
context 'when not calling to' do
|
85
|
+
it 'raises error' do
|
86
|
+
expect do
|
87
|
+
expectation
|
88
|
+
end.to raise_error(
|
89
|
+
SyntaxError,
|
90
|
+
'You should specify which instance the method is being added to' \
|
91
|
+
"add_method(:#{method}).to(instance)"
|
92
|
+
)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe DefaultValue do
|
4
|
+
let(:klass) { Class.new }
|
5
|
+
let(:method) { :the_method }
|
6
|
+
let(:value) { Random.rand(100) }
|
7
|
+
let(:builder) { described_class.new(klass, method, value) }
|
8
|
+
let(:instance) { klass.new }
|
9
|
+
|
10
|
+
context 'when the builder runs' do
|
11
|
+
it do
|
12
|
+
expect do
|
13
|
+
described_class.new(klass, method, value).build
|
14
|
+
end.to add_method(method).to(instance)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when the builder runs' do
|
19
|
+
it do
|
20
|
+
expect do
|
21
|
+
described_class.new(klass, method, value).build
|
22
|
+
end.to add_method(method).to(klass)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sinclair::Matchers::AddMethod do
|
4
|
+
subject { described_class.new(method) }
|
5
|
+
|
6
|
+
let(:method) { :the_method }
|
7
|
+
let(:klass) { Class.new }
|
8
|
+
let(:instance) { klass.new }
|
9
|
+
|
10
|
+
describe '#to' do
|
11
|
+
it do
|
12
|
+
expect(subject.to(klass.new)).to be_a(Sinclair::Matchers::AddMethodTo)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns an add_method_to' do
|
16
|
+
expect(subject.to(instance)).to eq(Sinclair::Matchers::AddMethodTo.new(instance, method))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#matches?' do
|
21
|
+
it do
|
22
|
+
expect do
|
23
|
+
subject.matches?(proc {})
|
24
|
+
end.to raise_error(
|
25
|
+
SyntaxError, 'You should specify which instance the method is being added to' \
|
26
|
+
"add_method(:#{method}).to(instance)"
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sinclair::Matchers::AddMethodTo do
|
4
|
+
subject { described_class.new(instance, method) }
|
5
|
+
|
6
|
+
let(:method) { :the_method }
|
7
|
+
let(:klass) { Class.new }
|
8
|
+
let(:instance) { klass.new }
|
9
|
+
|
10
|
+
describe '#matches?' do
|
11
|
+
let(:event_proc) do
|
12
|
+
Proc.new { klass.send(:define_method, method) {} }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when a method is added' do
|
16
|
+
it { expect(subject.matches? event_proc).to be_truthy }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when a method is not added' do
|
20
|
+
let(:event_proc) { Proc.new {} }
|
21
|
+
|
22
|
+
it { expect(subject.matches? event_proc).to be_falsey }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when the wrong method is added' do
|
26
|
+
let(:event_proc) do
|
27
|
+
Proc.new { klass.send(:define_method, :another_method) {} }
|
28
|
+
end
|
29
|
+
|
30
|
+
it { expect(subject.matches? event_proc).to be_falsey }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when initializing with class' do
|
34
|
+
subject { described_class.new(klass, method) }
|
35
|
+
|
36
|
+
context 'when a method is added' do
|
37
|
+
it { expect(subject.matches? event_proc).to be_truthy }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#failure_message_for_should' do
|
43
|
+
it 'returns information on the instance class and method' do
|
44
|
+
expect(subject.failure_message_for_should)
|
45
|
+
.to eq("expected '#{method}' to be added to #{klass} but it didn't")
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when method already exited' do
|
49
|
+
before do
|
50
|
+
klass.send(:define_method, method) {}
|
51
|
+
subject.matches?(Proc.new {})
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns information on the instance class and method' do
|
55
|
+
expect(subject.failure_message_for_should)
|
56
|
+
.to eq("expected '#{method}' to be added to #{klass} but it already existed")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when initializing with class' do
|
61
|
+
subject { described_class.new(klass, method) }
|
62
|
+
|
63
|
+
it 'returns information on the instance class and method' do
|
64
|
+
expect(subject.failure_message_for_should)
|
65
|
+
.to eq("expected '#{method}' to be added to #{klass} but it didn't")
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when method already exited' do
|
69
|
+
before do
|
70
|
+
klass.send(:define_method, method) {}
|
71
|
+
subject.matches?(Proc.new {})
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'returns information on the instance class and method' do
|
75
|
+
expect(subject.failure_message_for_should)
|
76
|
+
.to eq("expected '#{method}' to be added to #{klass} but it already existed")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#failure_message_for_should_not' do
|
83
|
+
it 'returns information on the instance class and method' do
|
84
|
+
expect(subject.failure_message_for_should_not)
|
85
|
+
.to eq("expected '#{method}' not to be added to #{klass} but it was")
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when initializing with class' do
|
89
|
+
subject { described_class.new(klass, method) }
|
90
|
+
|
91
|
+
it 'returns information on the instance class and method' do
|
92
|
+
expect(subject.failure_message_for_should_not)
|
93
|
+
.to eq("expected '#{method}' not to be added to #{klass} but it was")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'description' do
|
99
|
+
it 'returns information on the instance class and method' do
|
100
|
+
expect(subject.description)
|
101
|
+
.to eq("add method '#{method}' to #{klass} instances")
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when initializing with class' do
|
105
|
+
subject { described_class.new(klass, method) }
|
106
|
+
|
107
|
+
it 'returns information on the instance class and method' do
|
108
|
+
expect(subject.description)
|
109
|
+
.to eq("add method '#{method}' to #{klass} instances")
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sinclair::Matchers do
|
4
|
+
describe '#add_method' do
|
5
|
+
it 'has been added to DSL' do
|
6
|
+
expect(respond_to?(:add_method)).to be_truthy
|
7
|
+
end
|
8
|
+
|
9
|
+
it do
|
10
|
+
expect(add_method(:method_name)).to be_a(described_class::AddMethod)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns the matcher with correct argument' do
|
14
|
+
expect(add_method(:method_name)).to eq(described_class::AddMethod.new(:method_name))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
class DefaultValue
|
2
|
+
delegate :build, to: :builder
|
3
|
+
attr_reader :klass, :method, :value
|
4
|
+
|
5
|
+
def initialize(klass, method, value)
|
6
|
+
@klass = klass
|
7
|
+
@method = method
|
8
|
+
@value = value
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def builder
|
14
|
+
@builder ||= Sinclair.new(klass).tap do |b|
|
15
|
+
b.add_method(method) { value }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinclair
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DarthJee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.16.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: pry-nav
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,18 +118,29 @@ files:
|
|
104
118
|
- ".circleci/config.yml"
|
105
119
|
- ".gitignore"
|
106
120
|
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- ".rubocop_todo.yml"
|
107
123
|
- Gemfile
|
108
124
|
- LICENSE
|
109
125
|
- README.md
|
110
126
|
- Rakefile
|
111
127
|
- docker-compose.yml
|
112
128
|
- lib/sinclair.rb
|
129
|
+
- lib/sinclair/matchers.rb
|
130
|
+
- lib/sinclair/matchers/add_method.rb
|
131
|
+
- lib/sinclair/matchers/add_method_to.rb
|
113
132
|
- lib/sinclair/method_definition.rb
|
114
133
|
- lib/sinclair/options_parser.rb
|
115
134
|
- lib/sinclair/version.rb
|
116
135
|
- sinclair.gemspec
|
136
|
+
- sinclair.jpg
|
137
|
+
- spec/integration/matcher_spec.rb
|
138
|
+
- spec/integration/readme/matcher_spec.rb
|
117
139
|
- spec/integration/readme/my_class_spec.rb
|
118
140
|
- spec/integration/readme_spec.rb
|
141
|
+
- spec/lib/sinclair/matchers/add_method_spec.rb
|
142
|
+
- spec/lib/sinclair/matchers/add_method_to_spec.rb
|
143
|
+
- spec/lib/sinclair/matchers_spec.rb
|
119
144
|
- spec/lib/sinclair/method_definition_spec.rb
|
120
145
|
- spec/lib/sinclair/options_parser_spec.rb
|
121
146
|
- spec/lib/sinclair_spec.rb
|
@@ -123,6 +148,7 @@ files:
|
|
123
148
|
- spec/support/fixture_helpers.rb
|
124
149
|
- spec/support/models/dummy_builder.rb
|
125
150
|
- spec/support/models/dummy_options_parser.rb
|
151
|
+
- spec/support/models/my_builder.rb
|
126
152
|
- spec/support/models/my_class.rb
|
127
153
|
- spec/support/models/my_concern.rb
|
128
154
|
- spec/support/models/validator_builder.rb
|