minitest-matcher-library 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 164c5bb35584e50351745a9047042e576472ccbd
4
+ data.tar.gz: f8c37c940fb905ba3633f2ecd8f0185e75ef5f9b
5
+ !binary "U0hBNTEy":
6
+ metadata.gz: c20f940be38f981aaaba0e0d49bb6631d1e0662c55588468de449c9d994318b49f737c5b05cd8e029dbeb2263a81edaf2f9c9eb257205f800b482cb469752113
7
+ data.tar.gz: f371d66ac65dc902a51ae1d481a2dd0a1a623cd31e1919ee84854de46abe4f4a7d42a34f8af51815e0537b80644b45a1cd12b9c02cdbd391b07cbc12e77f3b7c
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :development, :test do
4
+ gem 'mocha', :require => false
5
+ gem 'rake'
6
+ gem 'bundler'
7
+ gem 'minitest'
8
+ gem 'rails', '4.0.0.beta1'
9
+ gem 'minitest'
10
+ gem 'minitest-matchers'
11
+ gem 'sqlite3'
12
+ end
13
+
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ Minitest Matcher Library
2
+ ------------------------
3
+
4
+ The goal is to be a series of matchers, that bring minitest-matchers into a state where it is as useful as shoulda-matchers, instead of having to build own matchers custom.
5
+
6
+ For simple validations see [valid_attribute](https://github.com/bcardarella/valid_attribute).
7
+
8
+ Installation
9
+ -----------
10
+
11
+ * Add to your Gemfile
12
+ * Include the helpers in your TestCase class, like below:
13
+
14
+ ```
15
+ class ActiveSupport::TestCase
16
+ include MinitestMatcherLibrary::TestHelpers
17
+ end
18
+ ```
19
+
20
+ Usage
21
+ -----
22
+
23
+
24
+ Date Validations:
25
+
26
+ Assert that only valid dates passed are accepted.
27
+
28
+ ```
29
+ object = Object.new
30
+ assert_must validate_datetime(:start_date), object
31
+ ```
32
+
33
+
34
+ Relationships
35
+
36
+ Assert that the relationship for the given class is setup.
37
+
38
+ ```
39
+ assert_must belong_to(:yolo), Swag
40
+ assert_must have_many(:fomos), Swag
41
+ assert_must have_one(:yolo), Swag
42
+ assert_must have_and_belong_to_many(:yolos), Swag
43
+ ```
44
+
45
+
46
+
47
+ This gem is very much a work in progress. Needs major cleanup, and tests. Also feel free to add additional useful matchers into this library that help you in your project.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'rake/testtask'
13
+ Rake::TestTask.new(:test) do |test|
14
+ test.test_files = FileList['test/**/*_test.rb']
15
+ test.libs = ['lib', 'test']
16
+ end
17
+
18
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ require 'minitest-matcher-library/test_helpers.rb'
2
+ require 'minitest-matcher-library/relationship_matcher.rb'
3
+ require 'minitest-matcher-library/date_validation_matcher.rb'
4
+ require 'minitest-matcher-library/version.rb'
@@ -0,0 +1,87 @@
1
+ module MinitestMatcherLibrary
2
+
3
+ class DateValidationMatcher
4
+ attr_accessor :attr, :or_nil, :subject, :unallowed_values, :allowed_values
5
+
6
+ def initialize(attr)
7
+ self.attr = attr
8
+ end
9
+
10
+ def or_nil
11
+ self.or_nil = true
12
+ end
13
+
14
+ def failure_message
15
+ message(unallowed_values)
16
+ end
17
+
18
+ def description
19
+ msg = "only be valid when #{attr} is a date"
20
+ msg = [msg, "or nil"].join(" ") if self.or_nil
21
+ end
22
+
23
+ def matches?(subject)
24
+ check_values(subject)
25
+ unallowed_values.empty?
26
+ end
27
+
28
+ def clone?
29
+ !!@clone
30
+ end
31
+
32
+ # Force the matcher to clone the subject in between
33
+ # testing each value.
34
+ #
35
+ # *Warning* This could lead to unintended results. The clone
36
+ # is not a deep copy
37
+ def clone
38
+ @clone = true
39
+ end
40
+
41
+ private
42
+
43
+ DATETIME_VALUES = ['2013-02-13T18:18:34-0500', Time.now]
44
+ NON_DATETIME_VALUES = ['invalid date string', 12345678]
45
+
46
+ def check_values(subject)
47
+ self.subject = subject
48
+ self.allowed_values = []
49
+ self.unallowed_values = []
50
+
51
+
52
+ DATETIME_VALUES.each do |value|
53
+ check_value(value, true)
54
+ check_value(nil, false) unless self.or_nil
55
+ end
56
+
57
+ NON_DATETIME_VALUES.each do |value|
58
+ check_value(value, false)
59
+ end
60
+ end
61
+
62
+ def check_value(value, expects_valid)
63
+ _subject = clone? ? subject.clone : subject
64
+ _subject.send(:"#{attr}=", value)
65
+ _subject.valid?
66
+
67
+ if valid_attribute?(_subject, attr) == expects_valid
68
+ self.allowed_values << value
69
+ else
70
+ self.unallowed_values << value
71
+ end
72
+ end
73
+
74
+ def valid_attribute?(_subject, attr)
75
+ errors = _subject.errors[attr]
76
+ errors.respond_to?(:empty?) ? !!errors.empty? : !errors
77
+ end
78
+
79
+ def quote_values(values)
80
+ values.map { |value| value.inspect }.join(', ')
81
+ end
82
+
83
+ def message(values)
84
+ " date validation for #{subject.class}##{attr} did not perform as expected for: #{quote_values(unallowed_values)}"
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,28 @@
1
+ module MinitestMatcherLibrary
2
+ class RelationshipMatcher
3
+ attr_accessor :attr, :macro, :subject, :failure_message
4
+
5
+ def initialize(attr, macro)
6
+ self.attr = attr
7
+ self.macro = macro
8
+ end
9
+
10
+ def matches?(subject)
11
+ if reflection(subject).nil?
12
+ self.failure_message = "has no reflection for #{subject}##{attr.to_s}"
13
+ return false
14
+ elsif reflection(subject).macro.to_sym != self.macro.to_sym
15
+ self.failure_message = "macro expected #{self.macro.to_sym.inspect}, macro observed #{reflection(subject).macro.to_sym.inspect}"
16
+ return false
17
+ else
18
+ return true
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def reflection(subject)
25
+ @reflection ||= subject.reflect_on_association(self.attr)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ module MinitestMatcherLibrary
2
+ module TestHelpers
3
+
4
+ def validate_datetime(attr)
5
+ MinitestMatcherLibrary::DateValidationMatcher.new(attr)
6
+ end
7
+
8
+ def belong_to(attr)
9
+ MinitestMatcherLibrary::RelationshipMatcher.new(attr, :belongs_to)
10
+ end
11
+
12
+ def have_many(attr)
13
+ MinitestMatcherLibrary::RelationshipMatcher.new(attr, :has_many)
14
+ end
15
+
16
+ def have_one(attr)
17
+ MinitestMatcherLibrary::RelationshipMatcher.new(attr, :has_one)
18
+ end
19
+
20
+ def have_and_belong_to_many(attr)
21
+ MinitestMatcherLibrary::RelationshipMatcher.new(attr, :has_and_belongs_to_many)
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module MinitestMatcherLibrary
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,32 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ require 'minitest-matcher-library/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "minitest-matcher-library"
6
+ s.version = MinitestMatcherLibrary::VERSION
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["ken mazaika"]
10
+ s.date = "2013-03-29"
11
+ s.description = "Add a library of useful matchers that work with zenspider/minitest-matchers"
12
+ s.summary = "Add a library of useful matchers that work with zenspider/minitest-matchers. Should be used with minitest / minitest-matchers / valid_attribute"
13
+ s.email = "kenmazaika@gmail.com"
14
+ s.extra_rdoc_files = [
15
+ "README.md"
16
+ ]
17
+ s.files = [
18
+ "Gemfile",
19
+ "README.md",
20
+ "Rakefile",
21
+ "lib/minitest-matcher-library.rb",
22
+ "lib/minitest-matcher-library/test_helpers.rb",
23
+ "lib/minitest-matcher-library/date_validation_matcher.rb",
24
+ "lib/minitest-matcher-library/relationship_matcher.rb",
25
+ "lib/minitest-matcher-library/version.rb",
26
+ "minitest-matcher-library.gemspec",
27
+ ]
28
+ s.homepage = "http://github.com/where/minitest-matcher-library"
29
+ s.require_paths = ["lib"]
30
+
31
+ end
32
+
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-matcher-library
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ken mazaika
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Add a library of useful matchers that work with zenspider/minitest-matchers
14
+ email: kenmazaika@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ files:
20
+ - Gemfile
21
+ - README.md
22
+ - Rakefile
23
+ - lib/minitest-matcher-library.rb
24
+ - lib/minitest-matcher-library/test_helpers.rb
25
+ - lib/minitest-matcher-library/date_validation_matcher.rb
26
+ - lib/minitest-matcher-library/relationship_matcher.rb
27
+ - lib/minitest-matcher-library/version.rb
28
+ - minitest-matcher-library.gemspec
29
+ homepage: http://github.com/where/minitest-matcher-library
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.0.0
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Add a library of useful matchers that work with zenspider/minitest-matchers. Should
52
+ be used with minitest / minitest-matchers / valid_attribute
53
+ test_files: []
54
+ has_rdoc: