smart_rspec 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +8 -2
- data/Rakefile +5 -1
- data/lib/smart_rspec/macros.rb +1 -1
- data/lib/smart_rspec/matchers.rb +6 -6
- data/lib/smart_rspec/support/assertions.rb +20 -30
- data/lib/smart_rspec/support/expectations.rb +29 -6
- data/lib/smart_rspec/support/regexes.rb +16 -23
- data/lib/smart_rspec/version.rb +1 -1
- data/smart_rspec.gemspec +1 -1
- data/spec/factories/user.rb +2 -2
- data/spec/smart_rspec/macros_spec.rb +1 -1
- data/spec/smart_rspec/matchers_spec.rb +2 -2
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48b874a311311e784f28124875c47496d24341a4
|
4
|
+
data.tar.gz: 58bcf16c460903b3c77fb796bd62e95ce37ef177
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28623faa3ad5c10172b668672598cfaf0da6725bb4532fb3e7ad40043ddeb390ccbbf5252115b3874edadd374023d96acd30ff4cf2a59fdd479dc84846630f17
|
7
|
+
data.tar.gz: 65f2a44c1f27a69ac5ac9ead3b7e88d97ef1e471b5dba36c0121a6f229a6d2157383f740bb7a111bf0f8962c8ae7d46578e38b5da735114a1c1e94b18e519ccf
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# SmartRspec
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/tiagopog/smart_rspec.svg)](https://travis-ci.org/tiagopog/smart_rspec)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/tiagopog/smart_rspec/badges/gpa.svg)](https://codeclimate.com/github/tiagopog/smart_rspec)
|
5
|
+
[![Dependency Status](https://gemnasium.com/tiagopog/smart_rspec.svg)](https://gemnasium.com/tiagopog/smart_rspec)
|
6
|
+
[![Gem Version](https://badge.fury.io/rb/smart_rspec.svg)](http://badge.fury.io/rb/smart_rspec)
|
7
|
+
|
8
|
+
It's time to make your specs even more awesome! SmartRspec adds useful macros and matchers into the RSpec's test suite, so you can quickly define specs for your Rails app and get focused on making things turn into green.
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
@@ -192,7 +197,7 @@ it { expect(%w(foo bar foobar)).to include_items(%w(foo bar foobar)) }
|
|
192
197
|
# TODO
|
193
198
|
|
194
199
|
- Create macros for model scopes;
|
195
|
-
- Create macros for controllers
|
200
|
+
- Create macros for controllers;
|
196
201
|
- Add more matchers.
|
197
202
|
|
198
203
|
## Contributing
|
@@ -204,3 +209,4 @@ it { expect(%w(foo bar foobar)).to include_items(%w(foo bar foobar)) }
|
|
204
209
|
4. Commit your changes (`git commit -am 'Add some feature'`);
|
205
210
|
5. Push to the branch (`git push origin my-new-feature`);
|
206
211
|
6. Create new Pull Request.
|
212
|
+
|
data/Rakefile
CHANGED
data/lib/smart_rspec/macros.rb
CHANGED
@@ -8,7 +8,7 @@ module SmartRspec::Macros
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def has_attributes(*attrs)
|
11
|
-
options = attrs.last.is_a?(Hash) && attrs.last.
|
11
|
+
options = attrs.last.is_a?(Hash) && attrs.last.key?(:type) ? attrs.pop : nil
|
12
12
|
assert_has_attributes(attrs, options)
|
13
13
|
end
|
14
14
|
|
data/lib/smart_rspec/matchers.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
require 'rspec/matchers'
|
2
|
-
require 'rspec/expectations'
|
3
2
|
require 'rspec/collection_matchers'
|
4
3
|
require 'smart_rspec/support/regexes'
|
5
4
|
|
6
|
-
include SmartRspec::Support::Regexes
|
7
|
-
|
8
5
|
module SmartRspec
|
9
6
|
module Matchers
|
10
7
|
extend RSpec::Matchers::DSL
|
8
|
+
include SmartRspec::Support::Regexes
|
11
9
|
|
12
10
|
matcher :be_boolean do
|
13
11
|
match { |actual| [true, false].include?(actual) }
|
@@ -29,8 +27,8 @@ module SmartRspec
|
|
29
27
|
match { |actual| actual.errors.keys.include?(attr) }
|
30
28
|
end
|
31
29
|
|
32
|
-
matcher :include_items do
|
33
|
-
match { |actual| (items
|
30
|
+
matcher :include_items do |items|
|
31
|
+
match { |actual| (items - actual).empty? }
|
34
32
|
end
|
35
33
|
|
36
34
|
matcher :be_ascending do
|
@@ -38,7 +36,9 @@ module SmartRspec
|
|
38
36
|
end
|
39
37
|
|
40
38
|
matcher :be_descending do
|
41
|
-
match
|
39
|
+
match do |actual|
|
40
|
+
actual.each_cons(2).all? { |i, j| i >= j }
|
41
|
+
end
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
@@ -6,14 +6,14 @@ module SmartRspec
|
|
6
6
|
def validates_email_of(attr, validation)
|
7
7
|
it 'has an invalid format' do
|
8
8
|
%w(foobar foobar@ @foobar foo@bar).each do |e|
|
9
|
-
|
9
|
+
validation_expectation(attr, e)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def validates_exclusion_of(attr, validation)
|
15
15
|
it 'has a reserved value' do
|
16
|
-
|
16
|
+
validation_expectation(attr, validation[:in].sample)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -24,7 +24,7 @@ module SmartRspec
|
|
24
24
|
validation.values_at(:with).first
|
25
25
|
|
26
26
|
if mock && with && with !~ mock
|
27
|
-
|
27
|
+
validation_expectation(attr, mock)
|
28
28
|
else
|
29
29
|
raise ArgumentError, ':with and :mock are required when using the :format validation'
|
30
30
|
end
|
@@ -36,64 +36,54 @@ module SmartRspec
|
|
36
36
|
begin
|
37
37
|
value = SecureRandom.hex
|
38
38
|
end while validation[:in].include?(value)
|
39
|
-
|
39
|
+
validation_expectation(attr, value)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
def validates_length_of(attr, validation)
|
44
44
|
validation.each do |key, value|
|
45
|
-
next unless
|
45
|
+
next unless [:in, :is, :maximum, :minimum, :within].include?(key)
|
46
46
|
txt, n = build_length_validation(key, value)
|
47
47
|
it txt do
|
48
|
-
|
48
|
+
validation_expectation(attr, 'x' * n)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
def validates_presence_of(attr, validation)
|
54
54
|
it 'is blank' do
|
55
|
-
|
55
|
+
validation_expectation(attr, nil)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
def validates_uniqueness_of(attr, validation)
|
60
|
-
scoped =
|
61
|
-
|
60
|
+
scoped, scope =
|
61
|
+
scoped_validation?(validation),
|
62
|
+
validation.values_at(:scope).first
|
62
63
|
|
63
64
|
it "is already in use#{" (scope: #{scope})" if scoped}" do
|
64
|
-
mock = (
|
65
|
-
scoped && mock.send("#{scope}=", subject.send(scope))
|
66
|
-
|
67
|
-
assert_validation(attr, subject.send(attr), mock)
|
65
|
+
mock = validation.values_at(:mock).first || subject.dup
|
66
|
+
scoped && mock.send("#{validation[:scope]}=", subject.send(validation[:scope]))
|
67
|
+
validation_expectation(attr, subject.send(attr), mock)
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
71
|
def assert_has_attributes(attrs, options)
|
72
72
|
type_str = build_type_str(options)
|
73
|
-
default, enum, type = options.values_at(:default, :enum, :type)
|
74
73
|
|
75
74
|
attrs.each do |attr|
|
76
75
|
it %Q(has an attribute named "#{attr}"#{type_str}) do
|
77
76
|
expect(subject).to respond_to(attr)
|
78
|
-
|
79
|
-
enum && (expect(enum).to include(subject.send(attr).to_sym))
|
80
|
-
type && assert_attr_type(attr, type, options)
|
77
|
+
has_attributes_expectation(attr, options)
|
81
78
|
end
|
82
79
|
end
|
83
80
|
end
|
84
81
|
|
85
|
-
def assert_association(
|
86
|
-
associations.each do |
|
87
|
-
it "#{
|
88
|
-
expect(subject).to respond_to(
|
89
|
-
|
90
|
-
when :belongs_to
|
91
|
-
%W(#{association}= #{association}_id #{association}_id=).each do |method|
|
92
|
-
expect(subject).to respond_to(method)
|
93
|
-
end
|
94
|
-
when :has_many
|
95
|
-
expect(subject).to respond_to("#{association.to_s.singularize}_ids")
|
96
|
-
end
|
82
|
+
def assert_association(type, associations)
|
83
|
+
associations.each do |model|
|
84
|
+
it "#{type.to_s.gsub('_', ' ')} #{model}" do
|
85
|
+
expect(subject).to respond_to(model)
|
86
|
+
association_expectation(type, model)
|
97
87
|
end
|
98
88
|
end
|
99
89
|
end
|
@@ -117,7 +107,7 @@ module SmartRspec
|
|
117
107
|
end
|
118
108
|
|
119
109
|
def scoped_validation?(validation)
|
120
|
-
validation.is_a?(Hash) && (
|
110
|
+
validation.is_a?(Hash) && ([:scope, :mock] - validation.keys).empty?
|
121
111
|
end
|
122
112
|
end
|
123
113
|
end
|
@@ -1,18 +1,41 @@
|
|
1
1
|
module SmartRspec
|
2
2
|
module Support
|
3
3
|
module Expectations
|
4
|
-
def
|
5
|
-
assert_type = type == :Boolean ? be_boolean : be_kind_of(Kernel.const_get(type))
|
6
|
-
expect(subject.send(attr)).to assert_type
|
7
|
-
end
|
8
|
-
|
9
|
-
def assert_validation(attr, value = nil, mock = nil)
|
4
|
+
def validation_expectation(attr, value = nil, mock = nil)
|
10
5
|
mock ||= subject
|
11
6
|
mock.send("#{attr}=", value)
|
12
7
|
|
13
8
|
expect(mock).not_to be_valid
|
14
9
|
expect(mock).to have_error_on(attr)
|
15
10
|
end
|
11
|
+
|
12
|
+
def default_expectation(attr, value)
|
13
|
+
expect(subject.send(attr)).to eq(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def enum_expectation(attr, value)
|
17
|
+
expect(value).to include(subject.send(attr).to_sym)
|
18
|
+
end
|
19
|
+
|
20
|
+
def type_expectation(attr, value)
|
21
|
+
assert_type = value != :Boolean ? be_kind_of(Kernel.const_get(value)) : be_boolean
|
22
|
+
expect(subject.send(attr)).to assert_type
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_attributes_expectation(attr, options) options.each do |key, value|
|
26
|
+
send("#{key}_expectation", attr, value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def association_expectation(type, model)
|
31
|
+
if type == :has_many
|
32
|
+
expect(subject).to respond_to("#{model.to_s.singularize}_ids")
|
33
|
+
elsif type == :belongs_to
|
34
|
+
%W(#{model}= #{model}_id #{model}_id=).each do |method|
|
35
|
+
expect(subject).to respond_to(method)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
16
39
|
end
|
17
40
|
end
|
18
41
|
end
|
@@ -1,22 +1,29 @@
|
|
1
1
|
module SmartRspec
|
2
2
|
module Support
|
3
3
|
module Regexes
|
4
|
+
@@uri_regexes =
|
5
|
+
{ protocol: /((ht|f)tp[s]?)/i,
|
6
|
+
uri: %r{^(
|
7
|
+
(((ht|f)tp[s]?://)|([a-z0-9]+\.))+
|
8
|
+
(?<!@)
|
9
|
+
([a-z0-9\_\-]+)
|
10
|
+
(\.[a-z]+)+
|
11
|
+
([\?/\:][a-z0-9_=%&@\?\./\-\:\#\(\)]+)?
|
12
|
+
/?
|
13
|
+
)$}ix }
|
14
|
+
|
4
15
|
def build_regex(type, *args)
|
5
|
-
|
6
|
-
|
7
|
-
when :
|
8
|
-
|
9
|
-
when :image
|
10
|
-
build_img_regex(args.flatten)
|
11
|
-
else
|
12
|
-
build_uri_regex[type]
|
16
|
+
case type.to_sym
|
17
|
+
when :email then /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
18
|
+
when :image then build_img_regex(args)
|
19
|
+
else @@uri_regexes[type]
|
13
20
|
end
|
14
21
|
end
|
15
22
|
|
16
23
|
private_class_method
|
17
24
|
|
18
25
|
def build_img_regex(exts = [])
|
19
|
-
exts = [exts].flatten
|
26
|
+
exts = [exts].flatten
|
20
27
|
if exts.nil? || exts.empty?
|
21
28
|
exts = %w(jpg jpeg png gif)
|
22
29
|
elsif exts.include?(:jpg) && !exts.include?(:jpeg)
|
@@ -24,20 +31,6 @@ module SmartRspec
|
|
24
31
|
end
|
25
32
|
%r{(^http{1}[s]?://([w]{3}\.)?.+\.(#{exts.join('|')})(\?.+)?$)}i
|
26
33
|
end
|
27
|
-
|
28
|
-
def build_uri_regex
|
29
|
-
{
|
30
|
-
uri: %r{^(
|
31
|
-
(((ht|f)tp[s]?://)|([a-z0-9]+\.))+
|
32
|
-
(?<!@)
|
33
|
-
([a-z0-9\_\-]+)
|
34
|
-
(\.[a-z]+)+
|
35
|
-
([\?/\:][a-z0-9_=%&@\?\./\-\:\#\(\)]+)?
|
36
|
-
/?
|
37
|
-
)$}ix,
|
38
|
-
protocol: /((ht|f)tp[s]?)/i
|
39
|
-
}
|
40
|
-
end
|
41
34
|
end
|
42
35
|
end
|
43
36
|
end
|
data/lib/smart_rspec/version.rb
CHANGED
data/smart_rspec.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_runtime_dependency 'activesupport', '~> 4.1'
|
22
22
|
spec.add_runtime_dependency 'rspec-collection_matchers', '~> 1.1', '>= 1.1.2'
|
23
23
|
|
24
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
25
25
|
spec.add_development_dependency 'faker', '~> 1.4'
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
27
27
|
spec.add_development_dependency 'rspec', '~> 3.2'
|
data/spec/factories/user.rb
CHANGED
@@ -36,7 +36,7 @@ module Factories
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def locale=(locale)
|
39
|
-
|
39
|
+
[:en, :pt].include?(locale) && @locale = locale
|
40
40
|
end
|
41
41
|
|
42
42
|
def valid?
|
@@ -59,7 +59,7 @@ module Factories
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def check_locale
|
62
|
-
unless
|
62
|
+
unless [:en, :pt].include?(locale)
|
63
63
|
@errors.merge!({ locale: @@error_message[:inclusion] })
|
64
64
|
end
|
65
65
|
end
|
@@ -29,7 +29,7 @@ describe SmartRspec::Macros do
|
|
29
29
|
has_attributes :email, :name, :username, type: :String
|
30
30
|
has_attributes :is_admin, type: :Boolean
|
31
31
|
has_attributes :score, type: :Integer, default: 0
|
32
|
-
has_attributes :locale, type: :String, enum:
|
32
|
+
has_attributes :locale, type: :String, enum: [:en, :pt], default: 'en'
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'when it receives multiple args' do
|
@@ -73,7 +73,7 @@ describe 'SmartRspec Matchers' do
|
|
73
73
|
it { expect('http://foobar.com/foo.jpg').to be_image_url(:jpg) }
|
74
74
|
it { expect('http://foobar.com/foo.gif').to be_image_url(:gif) }
|
75
75
|
it { expect('http://foobar.com/foo.png').to be_image_url(:png) }
|
76
|
-
it { expect('http://foobar.com/foo.png').to be_image_url(
|
76
|
+
it { expect('http://foobar.com/foo.png').to be_image_url([:jpg, :png]) }
|
77
77
|
it { expect('http://foobar.com/foo/bar?image=foo.jpg').to be_image_url }
|
78
78
|
end
|
79
79
|
|
@@ -82,7 +82,7 @@ describe 'SmartRspec Matchers' do
|
|
82
82
|
it { expect('http://foobar.com/foo.jpg').not_to be_image_url(:gif) }
|
83
83
|
it { expect('http://foobar.com/foo.gif').not_to be_image_url(:png) }
|
84
84
|
it { expect('http://foobar.com/foo.png').not_to be_image_url(:jpg) }
|
85
|
-
it { expect('http://foobar.com/foo.gif').not_to be_image_url(
|
85
|
+
it { expect('http://foobar.com/foo.gif').not_to be_image_url([:jpg, :png]) }
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -50,14 +50,14 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '1.
|
53
|
+
version: '1.6'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '1.
|
60
|
+
version: '1.6'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: faker
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -110,6 +110,7 @@ extra_rdoc_files: []
|
|
110
110
|
files:
|
111
111
|
- ".gitignore"
|
112
112
|
- ".rspec"
|
113
|
+
- ".travis.yml"
|
113
114
|
- Gemfile
|
114
115
|
- LICENSE.txt
|
115
116
|
- README.md
|