better_matchers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +30 -0
- data/Rakefile +2 -0
- data/better_matchers.gemspec +20 -0
- data/database +0 -0
- data/lib/better_matchers/error_matcher.rb +37 -0
- data/lib/better_matchers/version.rb +5 -0
- data/lib/better_matchers.rb +7 -0
- data/spec/matchers/error.rb +30 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/helpers/validators_test_helper.rb +34 -0
- metadata +81 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 mrodrigues
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# BetterMatchers
|
2
|
+
|
3
|
+
Some matchers for making RSpec tests more semantic.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'better_matchers'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install better_matchers
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
user.should have_error(:blank).on :name
|
22
|
+
user.should_not have_error(:invalid).on :email
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/better_matchers/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["mrodrigues"]
|
6
|
+
gem.email = ["mrodrigues.uff@gmail.com"]
|
7
|
+
gem.description = %q{Some matchers for more semantic tests.}
|
8
|
+
gem.summary = %q{Some matchers for more semantic tests.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "better_matchers"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = BetterMatchers::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
gem.add_development_dependency "activerecord"
|
20
|
+
end
|
data/database
ADDED
File without changes
|
@@ -0,0 +1,37 @@
|
|
1
|
+
RSpec::Matchers.define :have_error do |error|
|
2
|
+
|
3
|
+
def error_message(record, attribute, error)
|
4
|
+
I18n.translate("activerecord.errors.models.#{record.class.to_s.underscore}.attributes.#{attribute}.#{error}")
|
5
|
+
end
|
6
|
+
|
7
|
+
chain :on do |attribute|
|
8
|
+
@attribute = attribute
|
9
|
+
end
|
10
|
+
|
11
|
+
match do |record|
|
12
|
+
unless @attribute
|
13
|
+
raise "It should specify an attribute"
|
14
|
+
end
|
15
|
+
|
16
|
+
record.valid?
|
17
|
+
message = error_message(record, @attribute, error.to_s)
|
18
|
+
record.errors[@attribute].to_a.detect { |e| e == message }
|
19
|
+
end
|
20
|
+
|
21
|
+
failure_message_for_should do |record|
|
22
|
+
if @attribute
|
23
|
+
%{It expected that [#{record.errors[@attribute].collect{|e| "\"#{e}\"" }.join(', ')}] included "#{error_message(record, @attribute, error.to_s)}"}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
failure_message_for_should_not do |record|
|
28
|
+
if @attribute
|
29
|
+
%{It expected that [#{record.errors[@attribute].collect{|e| "\"#{e}\"" }.join(', ')}] didn't include "#{error_message(record, @attribute, error.to_s)}"}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
description do
|
34
|
+
"it should include error '#{error.to_s}'"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "have_error" do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
@object = object_with_validations :name => {:presence => true}
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Testing should_not" do
|
10
|
+
it "should not raise error when object doesn't contain error" do
|
11
|
+
@object.name = "something"
|
12
|
+
lambda{ @object.should_not have_error(:blank).on :name }.should_not raise_error RSpec::Expectations::ExpectationNotMetError
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should raise error when object contain error" do
|
16
|
+
lambda{ @object.should_not have_error(:blank).on :name }.should raise_error RSpec::Expectations::ExpectationNotMetError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "Testing should" do
|
21
|
+
it "should raise error when object doesn't contain error" do
|
22
|
+
@object.name = "something"
|
23
|
+
lambda{ @object.should have_error(:blank).on :name }.should raise_error RSpec::Expectations::ExpectationNotMetError
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not raise error when object contain error" do
|
27
|
+
lambda{ @object.should have_error(:blank).on :name }.should_not raise_error RSpec::Expectations::ExpectationNotMetError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'better_matchers'
|
9
|
+
require 'active_record'
|
10
|
+
|
11
|
+
Dir["./spec/support/**/*.rb"].each {|f| require f}
|
12
|
+
Object.constants.grep(/TestHelper$/).each do |helper|
|
13
|
+
include Object.const_get(helper)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
19
|
+
config.run_all_when_everything_filtered = true
|
20
|
+
config.filter_run :focus
|
21
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'database')
|
22
|
+
I18n.locale = "pt-BR"
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ValidatorsTestHelper
|
2
|
+
|
3
|
+
@@class_name_sequence = 0
|
4
|
+
##
|
5
|
+
# Object for use in validators tests.
|
6
|
+
#
|
7
|
+
# It should be initialized with a hash of hashes, as follow:
|
8
|
+
# { :first_attribute_name => { :type => :string, :validator_name => true } }
|
9
|
+
# Usage:
|
10
|
+
# * object_with_validations :name => { :type => :string, :presence => true }
|
11
|
+
# * ObjectWithValidations.new :size => { :type => :integer, :presence => true, :inclusion => {
|
12
|
+
# :in => %w(small medium large) } }
|
13
|
+
##
|
14
|
+
def object_with_validations(columns = {})
|
15
|
+
klass = Class.new ActiveRecord::Base do
|
16
|
+
def self.columns() @columns ||= []; end
|
17
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
18
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
19
|
+
attr_accessor name
|
20
|
+
end
|
21
|
+
|
22
|
+
columns.each_pair do |column_name, opts|
|
23
|
+
column column_name, opts.delete(:type)
|
24
|
+
validates column_name, opts unless opts.empty?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
Object.const_set("ObjectWithValidation_#{@@class_name_sequence}", klass)
|
28
|
+
@@class_name_sequence += 1
|
29
|
+
|
30
|
+
klass.new
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- mrodrigues
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-05 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &71902080 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *71902080
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &71901870 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *71901870
|
36
|
+
description: Some matchers for more semantic tests.
|
37
|
+
email:
|
38
|
+
- mrodrigues.uff@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- better_matchers.gemspec
|
50
|
+
- database
|
51
|
+
- lib/better_matchers.rb
|
52
|
+
- lib/better_matchers/error_matcher.rb
|
53
|
+
- lib/better_matchers/version.rb
|
54
|
+
- spec/matchers/error.rb
|
55
|
+
- spec/spec_helper.rb
|
56
|
+
- spec/support/helpers/validators_test_helper.rb
|
57
|
+
homepage: ''
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.8.17
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Some matchers for more semantic tests.
|
81
|
+
test_files: []
|