remarkable 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +11 -11
- data/LICENSE +1 -1
- data/README +189 -189
- data/lib/remarkable.rb +6 -6
- data/lib/remarkable/base.rb +2 -2
- data/lib/remarkable/dsl.rb +15 -15
- data/lib/remarkable/dsl/assertions.rb +8 -8
- data/lib/remarkable/dsl/callbacks.rb +39 -39
- data/lib/remarkable/dsl/matches.rb +52 -52
- data/lib/remarkable/dsl/optionals.rb +46 -46
- data/lib/remarkable/macros.rb +33 -33
- data/lib/remarkable/matchers.rb +13 -13
- data/lib/remarkable/messages.rb +10 -10
- data/lib/remarkable/pending.rb +27 -27
- data/lib/remarkable/version.rb +1 -1
- data/spec/base_spec.rb +8 -8
- data/spec/dsl/assertions_spec.rb +3 -3
- data/spec/dsl/optionals_spec.rb +2 -2
- data/spec/locale/en.yml +1 -1
- data/spec/locale/pt-BR.yml +1 -1
- data/spec/macros_spec.rb +7 -7
- data/spec/matchers/single_contain_matcher.rb +6 -6
- data/spec/messages_spec.rb +5 -5
- data/spec/pending_spec.rb +7 -7
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# v3.0.0
|
2
|
-
|
3
|
-
* Added Remarkable::Matchers. Now you can include your Remarkable matchers and
|
4
|
-
macros in test unit as well.
|
5
|
-
|
6
|
-
class Test::Unit::TestCase
|
7
|
-
include Spec::Matchers
|
8
|
-
include Remarkable::Matchers
|
9
|
-
extend Remarkable::Macros
|
10
|
-
end
|
11
|
-
|
2
|
+
|
3
|
+
* Added Remarkable::Matchers. Now you can include your Remarkable matchers and
|
4
|
+
macros in test unit as well.
|
5
|
+
|
6
|
+
class Test::Unit::TestCase
|
7
|
+
include Spec::Matchers
|
8
|
+
include Remarkable::Matchers
|
9
|
+
extend Remarkable::Macros
|
10
|
+
end
|
11
|
+
|
12
12
|
* Added pending and disabled macros
|
13
13
|
* Added I18n
|
14
14
|
* Added DSL core structure
|
15
15
|
* Added macros core structure
|
16
|
-
* Added matchers core structure
|
16
|
+
* Added matchers core structure
|
data/LICENSE
CHANGED
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
CHANGED
@@ -1,199 +1,199 @@
|
|
1
1
|
= Remarkable
|
2
|
-
|
3
|
-
This is the core package of Remarkable. It provides a DSL for creating matchers
|
4
|
-
with I18n support, decoupling messages from matcher's logic and adding rspec
|
5
|
-
extra features.
|
6
|
-
|
7
|
-
== Macros
|
8
|
-
|
9
|
-
Each matcher in Remarkable is also available as a macro. So this matcher:
|
10
|
-
|
11
|
-
it { should validate_presence_of(:name) }
|
12
|
-
|
13
|
-
Can also be written as:
|
14
|
-
|
15
|
-
should_validate_presence_of :name
|
16
|
-
|
17
|
-
Remarkable adds the possibility to disable macros. So as you could do:
|
18
|
-
|
19
|
-
xit { should validate_presence_of(:name) }
|
20
|
-
|
21
|
-
You can also do:
|
22
|
-
|
23
|
-
xshould_validate_presence_of :name
|
24
|
-
|
25
|
-
And it will show in your specs output:
|
26
|
-
|
27
|
-
"Example disabled: require name to be set"
|
28
|
-
|
29
|
-
== Pending macros
|
30
|
-
|
31
|
-
In Rspec you can mark some examples as pending:
|
32
|
-
|
33
|
-
it "should have one manager" do
|
34
|
-
pending("create managers resource")
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should validate associated manager" do
|
38
|
-
pending("create managers resource")
|
39
|
-
end
|
40
|
-
|
41
|
-
To allow this to work with macros, we created the pending group:
|
42
|
-
|
43
|
-
pending "create managers resource" do
|
44
|
-
should_have_one :manager
|
45
|
-
should_validate_associated :manager
|
46
|
-
end
|
47
|
-
|
48
|
-
This outputs the same as above.
|
49
|
-
|
50
|
-
== I18n
|
51
|
-
|
52
|
-
All matchers come with I18n support. You can find an example locale file under
|
53
|
-
the locale folder of each project.
|
54
|
-
|
55
|
-
To change the locale, you have first to add your locale file:
|
56
|
-
|
57
|
-
Remarkable.add_locale 'path/to/my_locale.yml'
|
58
|
-
|
59
|
-
And then:
|
60
|
-
|
61
|
-
Remarkable.locale = :my_locale
|
62
|
-
|
63
|
-
Internationalization is powered by the I18n gem. If you are using it with Rails,
|
64
|
-
it will use the built in gem, otherwise you will have to install the gem by hand:
|
65
|
-
|
2
|
+
|
3
|
+
This is the core package of Remarkable. It provides a DSL for creating matchers
|
4
|
+
with I18n support, decoupling messages from matcher's logic and adding rspec
|
5
|
+
extra features.
|
6
|
+
|
7
|
+
== Macros
|
8
|
+
|
9
|
+
Each matcher in Remarkable is also available as a macro. So this matcher:
|
10
|
+
|
11
|
+
it { should validate_presence_of(:name) }
|
12
|
+
|
13
|
+
Can also be written as:
|
14
|
+
|
15
|
+
should_validate_presence_of :name
|
16
|
+
|
17
|
+
Remarkable adds the possibility to disable macros. So as you could do:
|
18
|
+
|
19
|
+
xit { should validate_presence_of(:name) }
|
20
|
+
|
21
|
+
You can also do:
|
22
|
+
|
23
|
+
xshould_validate_presence_of :name
|
24
|
+
|
25
|
+
And it will show in your specs output:
|
26
|
+
|
27
|
+
"Example disabled: require name to be set"
|
28
|
+
|
29
|
+
== Pending macros
|
30
|
+
|
31
|
+
In Rspec you can mark some examples as pending:
|
32
|
+
|
33
|
+
it "should have one manager" do
|
34
|
+
pending("create managers resource")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should validate associated manager" do
|
38
|
+
pending("create managers resource")
|
39
|
+
end
|
40
|
+
|
41
|
+
To allow this to work with macros, we created the pending group:
|
42
|
+
|
43
|
+
pending "create managers resource" do
|
44
|
+
should_have_one :manager
|
45
|
+
should_validate_associated :manager
|
46
|
+
end
|
47
|
+
|
48
|
+
This outputs the same as above.
|
49
|
+
|
50
|
+
== I18n
|
51
|
+
|
52
|
+
All matchers come with I18n support. You can find an example locale file under
|
53
|
+
the locale folder of each project.
|
54
|
+
|
55
|
+
To change the locale, you have first to add your locale file:
|
56
|
+
|
57
|
+
Remarkable.add_locale 'path/to/my_locale.yml'
|
58
|
+
|
59
|
+
And then:
|
60
|
+
|
61
|
+
Remarkable.locale = :my_locale
|
62
|
+
|
63
|
+
Internationalization is powered by the I18n gem. If you are using it with Rails,
|
64
|
+
it will use the built in gem, otherwise you will have to install the gem by hand:
|
65
|
+
|
66
66
|
gem sources -a http://gems.github.com
|
67
|
-
sudo gem install svenfuchs-i18n
|
68
|
-
|
69
|
-
== Creating you own matcher
|
70
|
-
|
71
|
-
Create a new matcher is easy. Let's create validate_inclusion_of matcher for
|
72
|
-
ActiveRecord as an example. A first matcher version would be:
|
73
|
-
|
67
|
+
sudo gem install svenfuchs-i18n
|
68
|
+
|
69
|
+
== Creating you own matcher
|
70
|
+
|
71
|
+
Create a new matcher is easy. Let's create validate_inclusion_of matcher for
|
72
|
+
ActiveRecord as an example. A first matcher version would be:
|
73
|
+
|
74
74
|
module Remarkable
|
75
75
|
module ActiveRecord
|
76
76
|
module Matchers
|
77
|
-
class ValidateInclusionOfMatcher < Remarkable::ActiveRecord::Base
|
78
|
-
arguments :attribute
|
79
|
-
assertion :is_valid?
|
80
|
-
|
81
|
-
optional :in
|
82
|
-
optional :allow_blank, :allow_nil, :default => true
|
83
|
-
|
84
|
-
protected
|
85
|
-
|
86
|
-
def is_valid?
|
87
|
-
@options[:in].each do |value|
|
88
|
-
@subject.send(:"#{@attribute}=", value)
|
89
|
-
return false, :value => value unless @subject.valid?
|
90
|
-
end
|
91
|
-
true
|
92
|
-
end
|
93
|
-
end
|
77
|
+
class ValidateInclusionOfMatcher < Remarkable::ActiveRecord::Base
|
78
|
+
arguments :attribute
|
79
|
+
assertion :is_valid?
|
80
|
+
|
81
|
+
optional :in
|
82
|
+
optional :allow_blank, :allow_nil, :default => true
|
83
|
+
|
84
|
+
protected
|
85
|
+
|
86
|
+
def is_valid?
|
87
|
+
@options[:in].each do |value|
|
88
|
+
@subject.send(:"#{@attribute}=", value)
|
89
|
+
return false, :value => value unless @subject.valid?
|
90
|
+
end
|
91
|
+
true
|
92
|
+
end
|
93
|
+
end
|
94
94
|
|
95
95
|
def validate_inclusion_of(*args)
|
96
96
|
ValidateInclusionOfMatcher.new(*args).spec(self)
|
97
|
-
end
|
97
|
+
end
|
98
98
|
end
|
99
99
|
end
|
100
|
-
end
|
101
|
-
|
102
|
-
This creates a matcher which requires one attribute and has :in, :allow_blank
|
103
|
-
and :allow_nil as options. So you can call the matcher in the following way:
|
104
|
-
|
105
|
-
should_validate_inclusion_of :size, :in => %w(S M L XL)
|
106
|
-
should_validate_inclusion_of :size, :in => %w(S M L XL), :allow_blank => true
|
107
|
-
|
108
|
-
it { should validate_inclusion_of(:size, :in => %w(S M L XL)).allow_nil(true) }
|
109
|
-
it { should validate_inclusion_of(:size, :in => %w(S M L XL)).allow_nil }
|
110
|
-
|
111
|
-
it { should validate_inclusion_of(:size, :in => %w(S M L XL)) }
|
112
|
-
it { should validate_inclusion_of(:size, :in => %w(S M L XL), :allow_nil => true) }
|
113
|
-
|
114
|
-
The assertions methods (in this case, :is_valid?) makes the matcher pass when
|
115
|
-
it returns true and fail when returns false.
|
116
|
-
|
117
|
-
As you noticed, the matcher doesn't have any message on it. You add them on I18n
|
118
|
-
file. A file for this example would be:
|
119
|
-
|
120
|
-
remarkable:
|
121
|
-
active_record:
|
122
|
-
validate_inclusion_of:
|
123
|
-
description: "validate inclusion of {{attribute}}"
|
124
|
-
expectations:
|
125
|
-
is_valid: "to be valid when {{attribute}} is {{value}}"
|
126
|
-
optionals:
|
127
|
-
in:
|
128
|
-
positive: "in {{inspect}}"
|
129
|
-
allow_nil:
|
130
|
-
positive: "allowing nil values"
|
131
|
-
negative: "not allowing nil values"
|
132
|
-
allow_blank:
|
133
|
-
positive: "allowing blank values"
|
134
|
-
negative: "allowing blank values"
|
135
|
-
|
136
|
-
The optionals are just added to the description message when they are supplied.
|
137
|
-
Look some description messages examples:
|
138
|
-
|
139
|
-
should_validate_inclusion_of :size, :in => %w(S M L XL)
|
140
|
-
#=> should validate inclusion of size in ["S", "M", "L", "XL"]
|
141
|
-
|
142
|
-
should_validate_inclusion_of :size, :in => %w(S M L XL), :allow_nil => true
|
143
|
-
#=> should validate inclusion of size in ["S", "M", "L", "XL"] and allowing nil values
|
144
|
-
|
145
|
-
should_validate_inclusion_of :size, :in => %w(S M L XL), :allow_nil => false
|
146
|
-
#=> should validate inclusion of size in ["S", "M", "L", "XL"] and not allowing nil values
|
147
|
-
|
148
|
-
Please notice that the arguments are available as interpolation option, as well
|
149
|
-
as the optionals.
|
150
|
-
|
151
|
-
The expectations message are set whenever one of the assertions returns false.
|
152
|
-
In this case, whenever the assertion fails, we are also returning a hash, with
|
153
|
-
the value that failed:
|
154
|
-
|
155
|
-
return false, :value => value
|
156
|
-
|
157
|
-
This will tell remarkable to make value as interpolation option too.
|
158
|
-
|
159
|
-
Whenever you create all your matchers, you tell remarkable to add them to the
|
160
|
-
desired rspec example group:
|
161
|
-
|
162
|
-
Remarkable.include_matchers!(Remarkable::ActiveRecord, Spec::Example::ExampleGroup)
|
163
|
-
|
164
|
-
== Working with collections
|
165
|
-
|
166
|
-
Finally, Remarkable also makes easy to deal with collections. The same matcher
|
167
|
-
could be easily extended to accept a collection of attributes instead of just one:
|
168
|
-
|
169
|
-
should_validate_inclusion_of :first_size, :second_size, :in => %w(S M L XL)
|
170
|
-
|
171
|
-
For this we have just those two lines:
|
172
|
-
|
173
|
-
arguments :attribute
|
174
|
-
assertion :is_valid?
|
175
|
-
|
176
|
-
For:
|
177
|
-
|
178
|
-
arguments :collection => :attributes, :as => :attribute
|
179
|
-
collection_assertion :is_valid?
|
180
|
-
|
181
|
-
This means that the collection will be kept in the @attributes instance variable
|
182
|
-
and for each value in the collection, it will run the :is_valid? assertion.
|
183
|
-
|
184
|
-
Whenever running the assertion, it will also set the @attribute (in singular)
|
185
|
-
variable. In your I18n files, you just need to change your description:
|
186
|
-
|
187
|
-
validate_inclusion_of:
|
188
|
-
description: "validate inclusion of {{attributes}}"
|
189
|
-
|
190
|
-
And this will output:
|
191
|
-
|
192
|
-
should_validate_inclusion_of :first_size, :second_size, :in => %w(S M L XL)
|
193
|
-
#=> should validate inclusion of first size and second size in ["S", "M", "L", "XL"]
|
194
|
-
|
195
|
-
== More
|
196
|
-
|
197
|
-
This is just an overview of the API. You can add extra options to interpolation
|
198
|
-
by overwriting the interpolation_options methods, you can add callbacks after
|
199
|
-
initialize your matcher or before asserting and much more!
|
100
|
+
end
|
101
|
+
|
102
|
+
This creates a matcher which requires one attribute and has :in, :allow_blank
|
103
|
+
and :allow_nil as options. So you can call the matcher in the following way:
|
104
|
+
|
105
|
+
should_validate_inclusion_of :size, :in => %w(S M L XL)
|
106
|
+
should_validate_inclusion_of :size, :in => %w(S M L XL), :allow_blank => true
|
107
|
+
|
108
|
+
it { should validate_inclusion_of(:size, :in => %w(S M L XL)).allow_nil(true) }
|
109
|
+
it { should validate_inclusion_of(:size, :in => %w(S M L XL)).allow_nil }
|
110
|
+
|
111
|
+
it { should validate_inclusion_of(:size, :in => %w(S M L XL)) }
|
112
|
+
it { should validate_inclusion_of(:size, :in => %w(S M L XL), :allow_nil => true) }
|
113
|
+
|
114
|
+
The assertions methods (in this case, :is_valid?) makes the matcher pass when
|
115
|
+
it returns true and fail when returns false.
|
116
|
+
|
117
|
+
As you noticed, the matcher doesn't have any message on it. You add them on I18n
|
118
|
+
file. A file for this example would be:
|
119
|
+
|
120
|
+
remarkable:
|
121
|
+
active_record:
|
122
|
+
validate_inclusion_of:
|
123
|
+
description: "validate inclusion of {{attribute}}"
|
124
|
+
expectations:
|
125
|
+
is_valid: "to be valid when {{attribute}} is {{value}}"
|
126
|
+
optionals:
|
127
|
+
in:
|
128
|
+
positive: "in {{inspect}}"
|
129
|
+
allow_nil:
|
130
|
+
positive: "allowing nil values"
|
131
|
+
negative: "not allowing nil values"
|
132
|
+
allow_blank:
|
133
|
+
positive: "allowing blank values"
|
134
|
+
negative: "allowing blank values"
|
135
|
+
|
136
|
+
The optionals are just added to the description message when they are supplied.
|
137
|
+
Look some description messages examples:
|
138
|
+
|
139
|
+
should_validate_inclusion_of :size, :in => %w(S M L XL)
|
140
|
+
#=> should validate inclusion of size in ["S", "M", "L", "XL"]
|
141
|
+
|
142
|
+
should_validate_inclusion_of :size, :in => %w(S M L XL), :allow_nil => true
|
143
|
+
#=> should validate inclusion of size in ["S", "M", "L", "XL"] and allowing nil values
|
144
|
+
|
145
|
+
should_validate_inclusion_of :size, :in => %w(S M L XL), :allow_nil => false
|
146
|
+
#=> should validate inclusion of size in ["S", "M", "L", "XL"] and not allowing nil values
|
147
|
+
|
148
|
+
Please notice that the arguments are available as interpolation option, as well
|
149
|
+
as the optionals.
|
150
|
+
|
151
|
+
The expectations message are set whenever one of the assertions returns false.
|
152
|
+
In this case, whenever the assertion fails, we are also returning a hash, with
|
153
|
+
the value that failed:
|
154
|
+
|
155
|
+
return false, :value => value
|
156
|
+
|
157
|
+
This will tell remarkable to make value as interpolation option too.
|
158
|
+
|
159
|
+
Whenever you create all your matchers, you tell remarkable to add them to the
|
160
|
+
desired rspec example group:
|
161
|
+
|
162
|
+
Remarkable.include_matchers!(Remarkable::ActiveRecord, Spec::Example::ExampleGroup)
|
163
|
+
|
164
|
+
== Working with collections
|
165
|
+
|
166
|
+
Finally, Remarkable also makes easy to deal with collections. The same matcher
|
167
|
+
could be easily extended to accept a collection of attributes instead of just one:
|
168
|
+
|
169
|
+
should_validate_inclusion_of :first_size, :second_size, :in => %w(S M L XL)
|
170
|
+
|
171
|
+
For this we have just those two lines:
|
172
|
+
|
173
|
+
arguments :attribute
|
174
|
+
assertion :is_valid?
|
175
|
+
|
176
|
+
For:
|
177
|
+
|
178
|
+
arguments :collection => :attributes, :as => :attribute
|
179
|
+
collection_assertion :is_valid?
|
180
|
+
|
181
|
+
This means that the collection will be kept in the @attributes instance variable
|
182
|
+
and for each value in the collection, it will run the :is_valid? assertion.
|
183
|
+
|
184
|
+
Whenever running the assertion, it will also set the @attribute (in singular)
|
185
|
+
variable. In your I18n files, you just need to change your description:
|
186
|
+
|
187
|
+
validate_inclusion_of:
|
188
|
+
description: "validate inclusion of {{attributes}}"
|
189
|
+
|
190
|
+
And this will output:
|
191
|
+
|
192
|
+
should_validate_inclusion_of :first_size, :second_size, :in => %w(S M L XL)
|
193
|
+
#=> should validate inclusion of first size and second size in ["S", "M", "L", "XL"]
|
194
|
+
|
195
|
+
== More
|
196
|
+
|
197
|
+
This is just an overview of the API. You can add extra options to interpolation
|
198
|
+
by overwriting the interpolation_options methods, you can add callbacks after
|
199
|
+
initialize your matcher or before asserting and much more!
|
data/lib/remarkable.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
# Load core files
|
2
2
|
dir = File.dirname(__FILE__)
|
3
|
-
require File.join(dir, 'remarkable', 'version')
|
3
|
+
require File.join(dir, 'remarkable', 'version')
|
4
4
|
require File.join(dir, 'remarkable', 'matchers')
|
5
5
|
require File.join(dir, 'remarkable', 'i18n')
|
6
6
|
require File.join(dir, 'remarkable', 'dsl')
|
7
7
|
require File.join(dir, 'remarkable', 'messages')
|
8
8
|
|
9
9
|
require File.join(dir, 'remarkable', 'base')
|
10
|
-
require File.join(dir, 'remarkable', 'macros')
|
11
|
-
require File.join(dir, 'remarkable', 'pending')
|
10
|
+
require File.join(dir, 'remarkable', 'macros')
|
11
|
+
require File.join(dir, 'remarkable', 'pending')
|
12
12
|
require File.join(dir, 'remarkable', 'core_ext', 'array')
|
13
|
-
|
13
|
+
|
14
14
|
if defined?(Spec)
|
15
|
-
require File.join(dir, 'remarkable', 'rspec')
|
15
|
+
require File.join(dir, 'remarkable', 'rspec')
|
16
16
|
end
|
17
17
|
|
18
|
-
Remarkable.add_locale File.join(dir, '..', 'locale', 'en.yml')
|
18
|
+
Remarkable.add_locale File.join(dir, '..', 'locale', 'en.yml')
|
data/lib/remarkable/base.rb
CHANGED
@@ -2,7 +2,7 @@ module Remarkable
|
|
2
2
|
class Base
|
3
3
|
include Remarkable::Messages
|
4
4
|
extend Remarkable::DSL
|
5
|
-
|
5
|
+
|
6
6
|
# Optional to provide spec binding to matchers.
|
7
7
|
def spec(binding)
|
8
8
|
@spec = binding
|
@@ -24,7 +24,7 @@ module Remarkable
|
|
24
24
|
subject_class.respond_to?(:human_name) ? subject_class.human_name : subject_class.name
|
25
25
|
end
|
26
26
|
|
27
|
-
# Iterates over the collection given yielding the block and return false
|
27
|
+
# Iterates over the collection given yielding the block and return false
|
28
28
|
# if any of them also returns false.
|
29
29
|
def assert_matcher_for(collection) #:nodoc:
|
30
30
|
collection.each do |item|
|