expects 0.0.2 → 0.0.3
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 +4 -4
- data/.travis.yml +18 -0
- data/Gemfile +3 -1
- data/README.md +3 -1
- data/lib/expects.rb +22 -5
- data/lib/expects/error.rb +4 -7
- data/lib/expects/handler.rb +26 -0
- data/lib/expects/handlers/proc.rb +20 -0
- data/lib/expects/handlers/regex.rb +21 -0
- data/lib/expects/version.rb +1 -1
- data/spec/expects_error_spec.rb +1 -1
- data/spec/handlers/proc_handler_spec.rb +42 -0
- data/spec/handlers/regex_handler_spec.rb +36 -0
- data/spec/spec_helper.rb +3 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdfcda338868a65ed97a21eb3fa5515a10c9af39
|
4
|
+
data.tar.gz: 522ee4b68b5ff826512abb16a215de422dd58dd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ebf2013447a9e805ad9a00441e1704cfd61647003c6492abce25ec21d664e4428ed99b8b9ca0e01130b1417661a32f7602da94975b97d8162b0fe783dd9d69f
|
7
|
+
data.tar.gz: 8ba988153fcc2f9a25a5767d729bdc34144e685ff582412e865041682ec127ae103338ac09dec3cec603afe6a5b7a8ddd87e110f3bc365ddfb49113414e11fe7
|
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- rbx-18mode
|
7
|
+
- rbx-19mode
|
8
|
+
- ruby-head
|
9
|
+
env:
|
10
|
+
- "rake=0.9"
|
11
|
+
script: "bundle exec rspec -c"
|
12
|
+
notifications:
|
13
|
+
email: false
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: 1.8.7
|
17
|
+
- rvm: rbx-18mode
|
18
|
+
- rvm: ruby-head
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Expects
|
2
2
|
|
3
|
-
A DSL for
|
3
|
+
A DSL for validating input for methods
|
4
|
+
|
5
|
+
[](https://coveralls.io/r/Arcath/expects?branch=master) [](https://travis-ci.org/Arcath/expects) [](https://codeclimate.com/github/Arcath/expects)
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
data/lib/expects.rb
CHANGED
@@ -1,18 +1,31 @@
|
|
1
1
|
# Version Number
|
2
2
|
require 'expects/version'
|
3
|
-
#
|
3
|
+
# Classes
|
4
4
|
require 'expects/error'
|
5
|
+
require 'expects/handler'
|
5
6
|
|
6
7
|
module Expects
|
7
8
|
module ClassMethods
|
8
9
|
def expects(subject, objects)
|
9
|
-
|
10
|
-
|
10
|
+
if objects.is_a? Regexp
|
11
|
+
handler = Expects::Handlers::Regex.new(subject, objects)
|
12
|
+
elsif objects.is_a? Proc
|
13
|
+
handler = Expects::Handlers::Proc.new(subject, objects)
|
14
|
+
else
|
15
|
+
handler = Expects::Handler.new(subject, [*objects])
|
16
|
+
end
|
17
|
+
handler.accept!
|
11
18
|
end
|
12
19
|
|
13
20
|
def reject(subject, objects)
|
14
|
-
|
15
|
-
|
21
|
+
if objects.is_a? Regexp
|
22
|
+
handler = Expects::Handlers::Regex.new(subject, objects)
|
23
|
+
elsif objects.is_a? Proc
|
24
|
+
handler = Expects::Handlers::Proc.new(subject, objects)
|
25
|
+
else
|
26
|
+
handler = Expects::Handler.new(subject, [*objects])
|
27
|
+
end
|
28
|
+
handler.reject!
|
16
29
|
end
|
17
30
|
end
|
18
31
|
|
@@ -30,3 +43,7 @@ module Expects
|
|
30
43
|
|
31
44
|
private :expects, :reject
|
32
45
|
end
|
46
|
+
|
47
|
+
# Handlers
|
48
|
+
require 'expects/handlers/proc'
|
49
|
+
require 'expects/handlers/regex'
|
data/lib/expects/error.rb
CHANGED
@@ -1,15 +1,12 @@
|
|
1
1
|
class UnexpectedInput < StandardError
|
2
2
|
attr_reader :subject, :expected
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(handler)
|
5
|
+
@handler = handler
|
6
|
+
@subject, @expected = handler.subject, handler.objects
|
6
7
|
end
|
7
8
|
|
8
9
|
def message
|
9
|
-
@message ||= build_message
|
10
|
-
end
|
11
|
-
|
12
|
-
def build_message
|
13
|
-
"Expected #{@subject.inspect} to be #{expected.join(", ")}"
|
10
|
+
@message ||= @handler.build_message
|
14
11
|
end
|
15
12
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Expects
|
2
|
+
class Handler
|
3
|
+
attr_reader :subject, :objects
|
4
|
+
|
5
|
+
def initialize(subject, objects)
|
6
|
+
@subject = subject
|
7
|
+
@objects = objects
|
8
|
+
end
|
9
|
+
|
10
|
+
def valid?
|
11
|
+
@objects.include? @subject.class
|
12
|
+
end
|
13
|
+
|
14
|
+
def accept!
|
15
|
+
raise UnexpectedInput.new(self) unless valid?
|
16
|
+
end
|
17
|
+
|
18
|
+
def reject!
|
19
|
+
raise UnexpectedInput.new(self) if valid?
|
20
|
+
end
|
21
|
+
|
22
|
+
def build_message
|
23
|
+
"Expected #{@subject.inspect} to be #{@objects.join(", ")}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Expects
|
2
|
+
module Handlers
|
3
|
+
class Proc < Expects::Handler
|
4
|
+
include Expects
|
5
|
+
|
6
|
+
def initialize(subject, objects)
|
7
|
+
expects objects, ::Proc
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
@objects.call(@subject)
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_message
|
16
|
+
"Expected #{@subject.inspect} to get a true from the proc"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Expects
|
2
|
+
module Handlers
|
3
|
+
class Regex < Expects::Handler
|
4
|
+
include Expects
|
5
|
+
|
6
|
+
def initialize(subject, objects)
|
7
|
+
expects subject, String
|
8
|
+
expects objects, Regexp
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
@subject.match(@objects)
|
14
|
+
end
|
15
|
+
|
16
|
+
def build_message
|
17
|
+
"Expected #{@subject.inspect} match \"#{@objects.inspect}\""
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/expects/version.rb
CHANGED
data/spec/expects_error_spec.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Expects::Handlers::Proc do
|
4
|
+
|
5
|
+
let(:test_class) do
|
6
|
+
Class.new do
|
7
|
+
include Expects
|
8
|
+
|
9
|
+
def initialize(array)
|
10
|
+
expects array, Proc.new(){ |array| array.length >= 3 }
|
11
|
+
end
|
12
|
+
|
13
|
+
def not_good(array)
|
14
|
+
reject array, Proc.new(){ |array| array.length >= 3 }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:fail_array) do
|
20
|
+
[1,2]
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:pass_array) do
|
24
|
+
[1,2,3,4,5]
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:working_instance) do
|
28
|
+
test_class.new(pass_array)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should take a proc" do
|
32
|
+
lambda { test_class.new(pass_array) }.should_not raise_exception
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should raise an error if the proc returns false" do
|
36
|
+
lambda { test_class.new(fail_array) }.should raise_exception UnexpectedInput
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should support rejection" do
|
40
|
+
lambda { working_instance.not_good(pass_array) }.should raise_exception UnexpectedInput
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Expects::Handlers::Regex do
|
4
|
+
|
5
|
+
let(:user) do
|
6
|
+
Class.new do
|
7
|
+
include Expects
|
8
|
+
|
9
|
+
def initialize(name, email)
|
10
|
+
expects name, String
|
11
|
+
expects email, /^.*\@.*$/
|
12
|
+
end
|
13
|
+
|
14
|
+
def not_email(email)
|
15
|
+
reject email, /^.*\@.*$/
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should only take a string and a regex pattern" do
|
21
|
+
lambda { Expects::Handlers::Regex.new("foo", /foo/) }.should_not raise_exception
|
22
|
+
lambda { Expects::Handlers::Regex.new(/foo/, "foo") }.should raise_exception
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should let you pass regex to expects" do
|
26
|
+
lambda { user.new("foo", "foo@bar.com") }.should_not raise_exception
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise an exception if the subject doesn't match the supplied pattern" do
|
30
|
+
lambda { user.new("foo", "foobar.com") }.should raise_exception
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should support reject aswell" do
|
34
|
+
lambda { user.new("foo", "foo@bar.com").not_email("foo@bar.com") }.should raise_exception
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expects
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Laycock
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- .gitignore
|
49
|
+
- .travis.yml
|
49
50
|
- Gemfile
|
50
51
|
- LICENSE.txt
|
51
52
|
- README.md
|
@@ -53,9 +54,14 @@ files:
|
|
53
54
|
- expects.gemspec
|
54
55
|
- lib/expects.rb
|
55
56
|
- lib/expects/error.rb
|
57
|
+
- lib/expects/handler.rb
|
58
|
+
- lib/expects/handlers/proc.rb
|
59
|
+
- lib/expects/handlers/regex.rb
|
56
60
|
- lib/expects/version.rb
|
57
61
|
- spec/expects_error_spec.rb
|
58
62
|
- spec/expects_spec.rb
|
63
|
+
- spec/handlers/proc_handler_spec.rb
|
64
|
+
- spec/handlers/regex_handler_spec.rb
|
59
65
|
- spec/spec_helper.rb
|
60
66
|
homepage: ''
|
61
67
|
licenses:
|
@@ -84,5 +90,7 @@ summary: A DSL for method inpu expectations
|
|
84
90
|
test_files:
|
85
91
|
- spec/expects_error_spec.rb
|
86
92
|
- spec/expects_spec.rb
|
93
|
+
- spec/handlers/proc_handler_spec.rb
|
94
|
+
- spec/handlers/regex_handler_spec.rb
|
87
95
|
- spec/spec_helper.rb
|
88
96
|
has_rdoc:
|