argspec 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e489a3f3ed71350a6e16435cbf19efda2a9bac84
4
- data.tar.gz: 2af97d57c3ea415a720d0db32c3ba22d0d7ae7c8
3
+ metadata.gz: 78bed1162720f96bec34e01c4b6f0dd1fd7d7a75
4
+ data.tar.gz: 204b8dc5273e335cf2da9f5bd6e30c86a43adc95
5
5
  SHA512:
6
- metadata.gz: 3dda7ebf91f61ad7c310950f0788f0941f175019e46e470417c286756c27f1f932db0cda8443817af0bb7b93dc81a371562c83b1fa9e320c7ec1766374b3f47f
7
- data.tar.gz: 44e82ea8b0b64566e51540615024e20b38b1f7225ed6eee4d57de71c642f8f6f9d2dfe0a1d0e2cbcef9fae5ee4979c0d0fd90690df4351969c97a1b88fcf37c2
6
+ metadata.gz: 4cbdb4f44be0effc0cefa45d89e51273af695b169d8c41e3b868e4fc6502fe4ef5d87b7fb01fa327b9d3f487d6855fc28ef68c5b7c635d7b56a2c1e6c0819d4a
7
+ data.tar.gz: c2fa42bb7e822850b1f2aafb03497888947e1f37904edff56dcf52a972e6e8e8d7c711ed75947f45ce2b87e76449d7c33b049ee3b8d0ab79df54da0fc01f59f6
data/README.md CHANGED
@@ -41,9 +41,9 @@ class Person
41
41
  attr_reader :name, :gender, :birthdate
42
42
 
43
43
  def initialize(name, gender, birthdate)
44
- raise ArgumentError, "The name must be a string and must not be nil." if !name.is_a?(String) || name.empty?
45
- raise ArgumentError, "The gender must be a symbol." unless gender.is_a?(Symbol)
46
- raise ArgumentError, "The birthdate must be a date." unless birthdate.is_a?(Date)
44
+ raise ArgumentError, 'The name must be a string and must not be nil.' if !name.is_a?(String) || name.empty?
45
+ raise ArgumentError, 'The gender must be a symbol.' unless gender.is_a?(Symbol)
46
+ raise ArgumentError, 'The birthdate must be a date.' unless birthdate.is_a?(Date)
47
47
 
48
48
  @name = name
49
49
  @gender = gender
data/lib/argspec.rb CHANGED
@@ -8,9 +8,9 @@ module ArgumentSpecification
8
8
  #
9
9
  def require!
10
10
  require 'argspec/constants'
11
+ require 'argspec/dsl'
11
12
  require 'argspec/matchers'
12
13
  require 'argspec/argument'
13
- require 'argspec/dsl'
14
14
 
15
15
  nil
16
16
  end
@@ -1,130 +1,63 @@
1
1
  module ArgumentSpecification
2
2
  class Argument
3
- attr_reader :object
3
+ attr_reader :actual
4
4
 
5
5
  # Create a new argument
6
6
  #
7
7
  # Arguments:
8
- # object: (?)
8
+ # actual: (Object)
9
9
  #
10
10
  # Example:
11
11
  # >> test = :test
12
12
  # >> ArgumentSpecification::Argument.new(test)
13
- # => #<Argument:0x00000000000000 @object=:test>
13
+ # => #<Argument:0x00000000000000 @actual=:test>
14
14
  #
15
- def initialize(object)
16
- @object = object
15
+ def initialize(actual)
16
+ @actual = actual
17
17
  end
18
18
 
19
- # Should
19
+ # Ensure the argument matches
20
20
  #
21
21
  # Arguments:
22
- # matcher: (Symbol) A method name as a symbol (see the matchers.rb file)
23
- # args: (Splat)
22
+ # matcher: (Matchers::BaseMatcher)
24
23
  #
25
24
  # Example:
26
- # >> argument.should(:be, true)
25
+ # >> argument.should be_a(Symbol)
27
26
  # => nil
28
27
  #
29
28
  # Raises:
30
- # ArgumentError: If the matcher determines the object does not match
29
+ # ArgumentError: When the argument does not match
31
30
  #
32
- def should(matcher, *args)
33
- ensure_matcher(matcher)
31
+ def should(matcher)
32
+ return nil unless matcher.is_a?(Matchers::BaseMatcher)
34
33
 
35
- return if Matchers.send(matcher, self, *args)
34
+ matcher.send(:actual=, @actual)
36
35
 
37
- raise ArgumentError, "'#{prettify_object}' should #{prettify_matcher(matcher)} '#{prettify_args(args)}'"
38
- end
36
+ return nil if matcher.matches?
39
37
 
40
- # Should not
41
- #
42
- # Arguments:
43
- # matcher: (Symbol) A method name as a symbol (see the matchers.rb file)
44
- # args: (Splat)
45
- #
46
- # Example:
47
- # >> argument.should_not(:be, true)
48
- # => nil
49
- #
50
- # Raises:
51
- # ArgumentError: If the matcher determines the object does match
52
- #
53
- def should_not(matcher, *args)
54
- ensure_matcher(matcher)
55
-
56
- return if Matchers.send(matcher, self, *args) == false
57
-
58
- raise ArgumentError, "'#{prettify_object}' should not #{prettify_matcher(matcher)} '#{prettify_args(args)}'"
38
+ raise ArgumentError, matcher.failure_message
59
39
  end
60
40
 
61
- private
62
- # Ensure a matcher exists
41
+ # Ensure the argument does not match
63
42
  #
64
43
  # Arguments:
65
- # matcher: (Symbol)
44
+ # matcher: (Matchers::BaseMatcher)
66
45
  #
67
46
  # Example:
68
- # >> ensure_matcher(:be)
47
+ # >> argument.should_not be_a(Symbol)
69
48
  # => nil
70
49
  #
71
50
  # Raises:
72
- # ArgumentError: If the specified matcher does not exist
73
- #
74
- def ensure_matcher(matcher)
75
- return if Matchers.all.include?(matcher)
76
-
77
- raise ArgumentError, "The matcher '#{matcher}' does not exist."
78
- end
79
-
80
- # Prettify a matcher
51
+ # ArgumentError: When the argument matches
81
52
  #
82
- # Arguments:
83
- # matcher: (String)
84
- #
85
- # Example:
86
- # >> prettify_matcher(:be_a)
87
- # => "be a"
88
- #
89
- def prettify_matcher(matcher)
90
- matcher.to_s.gsub('_', ' ')
91
- end
53
+ def should_not(matcher)
54
+ return nil unless matcher.is_a?(Matchers::BaseMatcher)
92
55
 
93
- # Prettify arguments
94
- #
95
- # Arguments:
96
- # args: (Array)
97
- #
98
- # Example:
99
- # >> prettify_args([:a])
100
- # => ":a"
101
- #
102
- def prettify_args(args)
103
- pretty_args = args.map do |argument|
104
- if argument == nil
105
- next 'nil'
106
- elsif argument.is_a?(Symbol)
107
- next ":#{argument}"
108
- else
109
- next argument
110
- end
111
- end
56
+ matcher.send(:actual=, @actual)
112
57
 
113
- return 'nil' if pretty_args.empty?
114
- return pretty_args.first if pretty_args.count == 1
115
-
116
- pretty_args
117
- end
58
+ return nil unless matcher.matches?
118
59
 
119
- # Prettify the object
120
- #
121
- # Example:
122
- # >> # argument.object = :a
123
- # >> prettify_object
124
- # => ":a"
125
- #
126
- def prettify_object
127
- prettify_args([@object])
60
+ raise ArgumentError, matcher.failure_message_when_negated
128
61
  end
129
62
  end
130
63
  end
@@ -1,3 +1,3 @@
1
1
  module ArgumentSpecification
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/argspec/dsl.rb CHANGED
@@ -1,9 +1,34 @@
1
1
  module ArgumentSpecification
2
2
  module DSL
3
+ class << self
4
+ # Register a matcher
5
+ #
6
+ # Arguments:
7
+ # klass: (Matchers::BaseMatcher)
8
+ # name: (Symbol)
9
+ #
10
+ # Example:
11
+ # >> register_matcher(TestMatcher, :test_matcher)
12
+ # => true
13
+ #
14
+ def register_matcher(klass, name)
15
+ return unless klass.ancestors.include?(Matchers::BaseMatcher)
16
+ return unless name.is_a?(Symbol)
17
+
18
+ module_eval <<-EOS
19
+ def #{name}(*args, &block)
20
+ instance = #{klass}.new(*args, &block)
21
+ instance.send(:setup, '#{name}'.to_sym, args)
22
+ instance
23
+ end
24
+ EOS
25
+ end
26
+ end
27
+
3
28
  # Get an argument object
4
29
  #
5
30
  # Arguments:
6
- # object: (?)
31
+ # object: (Object)
7
32
  #
8
33
  # Example:
9
34
  # >> test = :test
@@ -1,60 +1,2 @@
1
- module ArgumentSpecification
2
- module Matchers
3
- class << self
4
- # Get an array of all available matchers
5
- #
6
- # Example:
7
- # >> all
8
- # => [:be, ...]
9
- #
10
- def all
11
- [:be, :be_a, :include]
12
- end
13
-
14
- # The be matcher
15
- #
16
- # Arguments:
17
- # argument: (ArgumentSpecification::Argument)
18
- # object: (?)
19
- #
20
- # Example:
21
- # >> test = 'Test'
22
- # >> argument(test).should_not(:be, nil)
23
- # => nil
24
- #
25
- def be(argument, object)
26
- argument.object == object
27
- end
28
-
29
- # The be_a matcher
30
- #
31
- # Arguments:
32
- # argument: (ArgumentSpecification::Argument)
33
- # object: (?)
34
- #
35
- # Example:
36
- # >> test = :test
37
- # >> argument(test).should(:be_a, Symbol)
38
- # => nil
39
- #
40
- def be_a(argument, klass)
41
- argument.object.is_a?(klass)
42
- end
43
-
44
- # The include matcher
45
- #
46
- # Arguments:
47
- # argument: (ArgumentSpecification::Argument)
48
- # object: (?)
49
- #
50
- # Example:
51
- # >> test = [:test]
52
- # >> argument(test).should(:include, :test)
53
- # => nil
54
- #
55
- def include(argument, object)
56
- argument.object.include?(object)
57
- end
58
- end
59
- end
60
- end
1
+ require 'argspec/matchers/base_matcher'
2
+ Dir.glob(File.expand_path('../matchers/**/*.rb', __FILE__)).each { |f| require(f) }
@@ -0,0 +1,82 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class All < BaseMatcher
4
+ matcher_name :all
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (BaseMatcher)
12
+ #
13
+ # Example:
14
+ # >> symbol_matcher = ArgumentSpecification::Matchers::BeA.new(Symbol)
15
+ # >> ArgumentSpecification::Matchers::All.new(symbol_matcher)
16
+ # => #<ArgumentSpecification::Matchers::All:0x00000000000000 @expected=#<ArgumentSpecification::Matchers::BeA:0x00000000000000 @expected=Symbol>>
17
+ #
18
+ def initialize(expected)
19
+ if expected.is_a?(BaseMatcher) == false || expected.is_a?(All)
20
+ raise ArgumentError, 'You must provide a matcher as an argument (excluding the all matcher).'
21
+ end
22
+
23
+ @expected = expected
24
+ end
25
+
26
+ # The failure message when using 'should'
27
+ #
28
+ # Example:
29
+ # >> matcher.failure_message
30
+ # => "'test' should be a 'Symbol'"
31
+ #
32
+ def failure_message
33
+ actual = prettify_args(@actual)
34
+ matcher = prettify_matcher(@expected.metadata[:name])
35
+
36
+ if @expected.metadata[:args].count > 0
37
+ args = prettify_args(*@expected.metadata[:args])
38
+
39
+ "'#{actual}' should all #{matcher} '#{args}'"
40
+ else
41
+ "'#{actual}' should all #{matcher}"
42
+ end
43
+ end
44
+
45
+ # The failure message when using 'should not'
46
+ #
47
+ # Example:
48
+ # >> matcher.failure_message_when_negated
49
+ # => "':test' should not be a 'Symbol'"
50
+ #
51
+ def failure_message_when_negated
52
+ actual = prettify_args(@actual)
53
+ matcher = prettify_matcher(@expected.metadata[:name])
54
+
55
+ if @expected.metadata[:args].count > 0
56
+ args = prettify_args(*@expected.metadata[:args])
57
+
58
+ "'#{actual}' should not all #{matcher} '#{args}'"
59
+ else
60
+ "'#{actual}' should not all #{matcher}"
61
+ end
62
+ end
63
+
64
+ # Check if the actual object matches
65
+ #
66
+ # Example:
67
+ # >> matcher.matches?
68
+ # => true
69
+ #
70
+ def matches?
71
+ actual = @actual.is_a?(Array) ? @actual : [@actual]
72
+ actual.each do |value|
73
+ @expected.send(:actual=, value)
74
+
75
+ return false unless @expected.matches?
76
+ end
77
+
78
+ true
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,145 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BaseMatcher
4
+ class << self
5
+ # Add matcher names
6
+ #
7
+ # Arguments:
8
+ # names: (Splat)
9
+ #
10
+ # Example:
11
+ # >> class TestMatcher < BaseMatcher
12
+ # >> matcher_name :test_matcher
13
+ # >> end
14
+ # => TestMatcher
15
+ #
16
+ def matcher_name(*names)
17
+ names.each do |name|
18
+ next unless name.is_a?(Symbol)
19
+
20
+ DSL.register_matcher(self, name)
21
+ end
22
+ end
23
+ end
24
+
25
+ attr_reader :actual, :metadata
26
+
27
+ # The failure message when using 'should'
28
+ #
29
+ # Example:
30
+ # >> matcher.failure_message
31
+ # => "'test' should be a 'Symbol'"
32
+ #
33
+ def failure_message
34
+ actual = prettify_args(@actual)
35
+ matcher = prettify_matcher(@metadata[:name])
36
+
37
+ if @metadata[:args].count > 0
38
+ args = prettify_args(*@metadata[:args])
39
+
40
+ "'#{actual}' should #{matcher} '#{args}'"
41
+ else
42
+ "'#{actual}' should #{matcher}"
43
+ end
44
+ end
45
+
46
+ # The failure message when using 'should not'
47
+ #
48
+ # Example:
49
+ # >> matcher.failure_message_when_negated
50
+ # => "':test' should not be a 'Symbol'"
51
+ #
52
+ def failure_message_when_negated
53
+ actual = prettify_args(@actual)
54
+ matcher = prettify_matcher(@metadata[:name])
55
+
56
+ if @metadata[:args].count > 0
57
+ args = prettify_args(*@metadata[:args])
58
+
59
+ "'#{actual}' should not #{matcher} '#{args}'"
60
+ else
61
+ "'#{actual}' should not #{matcher}"
62
+ end
63
+ end
64
+
65
+ # Check if the actual object matches
66
+ #
67
+ # You must override this method!
68
+ #
69
+ # Example:
70
+ # >> matcher.matches?
71
+ # => true
72
+ #
73
+ def matches?
74
+ raise NotImplementedError, 'You must override the matches? method in your matcher.'
75
+ end
76
+
77
+ private
78
+ # Setup the matcher
79
+ #
80
+ # Arguments:
81
+ # name: (Symbol)
82
+ # args: (Array)
83
+ #
84
+ # Example:
85
+ # >> matcher.setup(:test_matcher, [])
86
+ # => { name: :test_matcher, args: [] }
87
+ #
88
+ def setup(name, args)
89
+ @metadata = { name: name, args: args }
90
+ end
91
+
92
+ # Set the actual object
93
+ #
94
+ # Arguments:
95
+ # actual: (Object)
96
+ #
97
+ # Example:
98
+ # >> matcher.actual = :test
99
+ # => :test
100
+ #
101
+ def actual=(actual)
102
+ @actual = actual
103
+ end
104
+
105
+ # Prettify a matcher
106
+ #
107
+ # Arguments:
108
+ # matcher: (String)
109
+ #
110
+ # Example:
111
+ # >> prettify_matcher(:be_a)
112
+ # => "be a"
113
+ #
114
+ def prettify_matcher(matcher)
115
+ matcher.to_s.gsub('_', ' ')
116
+ end
117
+
118
+ # Prettify arguments
119
+ #
120
+ # Arguments:
121
+ # args: (Splat)
122
+ #
123
+ # Example:
124
+ # >> prettify_args(:a)
125
+ # => ":a"
126
+ #
127
+ def prettify_args(*args)
128
+ pretty_args = args.map do |argument|
129
+ if argument == nil
130
+ next 'nil'
131
+ elsif argument.is_a?(Symbol)
132
+ next ":#{argument}"
133
+ else
134
+ next argument
135
+ end
136
+ end
137
+
138
+ return 'nil' if pretty_args.empty?
139
+ return pretty_args.first if pretty_args.count == 1
140
+
141
+ pretty_args
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,33 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class Be < BaseMatcher
4
+ matcher_name :be
5
+
6
+ OPERATORS = {
7
+ :== => :equal,
8
+ :=== => :case_equal,
9
+ :=~ => :match,
10
+ :< => :be_less_than,
11
+ :> => :be_greater_than,
12
+ :<= => :be_less_than_or_equal_to,
13
+ :>= => :be_greater_than_or_equal_to
14
+ }
15
+
16
+ OPERATORS.keys.each do |operator|
17
+ define_method(operator) do |expected|
18
+ send(OPERATORS[operator], expected)
19
+ end
20
+ end
21
+
22
+ # Check if the actual object matches
23
+ #
24
+ # Example:
25
+ # >> matcher.matches?
26
+ # => true
27
+ #
28
+ def matches?
29
+ !!@actual
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeA < BaseMatcher
4
+ matcher_name :be_a, :be_an
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Class)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::BeA.new(Symbol)
15
+ # => #<ArgumentSpecification::Matchers::BeA:0x00000000000000 @expected=Symbol>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual.is_a?(@expected)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeEmpty < BaseMatcher
4
+ matcher_name :be_empty
5
+
6
+ # Check if the actual object matches
7
+ #
8
+ # Example:
9
+ # >> matcher.matches?
10
+ # => true
11
+ #
12
+ def matches?
13
+ @actual.empty?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeFalsey < BaseMatcher
4
+ matcher_name :be_falsey
5
+
6
+ # Check if the actual object matches
7
+ #
8
+ # Example:
9
+ # >> matcher.matches?
10
+ # => true
11
+ #
12
+ def matches?
13
+ !@actual
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeGreaterThan < BaseMatcher
4
+ matcher_name :be_greater_than
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::BeGreaterThan.new(10)
15
+ # => #<ArgumentSpecification::Matchers::BeGreaterThan:0x00000000000000 @expected=10>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual > @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeGreaterThanOrEqualTo < BaseMatcher
4
+ matcher_name :be_greater_than_or_equal_to
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::BeGreaterThanOrEqualTo.new(10)
15
+ # => #<ArgumentSpecification::Matchers::BeGreaterThanOrEqualTo:0x00000000000000 @expected=10>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual >= @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeLessThan < BaseMatcher
4
+ matcher_name :be_less_than
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::BeLessThan.new(10)
15
+ # => #<ArgumentSpecification::Matchers::BeLessThan:0x00000000000000 @expected=10>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual < @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeLessThanOrEqualTo < BaseMatcher
4
+ matcher_name :be_less_than_or_equal_to
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::BeLessThanOrEqualTo.new(10)
15
+ # => #<ArgumentSpecification::Matchers::BeLessThanOrEqualTo:0x00000000000000 @expected=10>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual <= @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeNil < BaseMatcher
4
+ matcher_name :be_nil
5
+
6
+ # Check if the actual object matches
7
+ #
8
+ # Example:
9
+ # >> matcher.matches?
10
+ # => true
11
+ #
12
+ def matches?
13
+ @actual.nil?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeTruthy < BaseMatcher
4
+ matcher_name :be_truthy
5
+
6
+ # Check if the actual object matches
7
+ #
8
+ # Example:
9
+ # >> matcher.matches?
10
+ # => true
11
+ #
12
+ def matches?
13
+ !!@actual
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class BeWithinRange < BaseMatcher
4
+ matcher_name :be_within_range
5
+
6
+ attr_reader :range
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # range: (Range)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::BeWithinRange.new(1..10)
15
+ # => #<ArgumentSpecification::Matchers::BeWithinRange:0x00000000000000 @range=1..10>
16
+ #
17
+ def initialize(range)
18
+ @range = range
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @range.include?(@actual)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class CaseEqual < BaseMatcher
4
+ matcher_name :case_equal
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::CaseEqual.new(:test)
15
+ # => #<ArgumentSpecification::Matchers::CaseEqual:0x00000000000000 @expected=:test>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual === @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class Contain < BaseMatcher
4
+ matcher_name :contain
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::Contain.new(Symbol)
15
+ # => #<ArgumentSpecification::Matchers::Contain:0x00000000000000 @expected=Symbol>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual.include?(@expected)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class Cover < BaseMatcher
4
+ matcher_name :cover
5
+
6
+ attr_reader :values
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # values: (Splat)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::Cover.new(1, 2)
15
+ # => #<ArgumentSpecification::Matchers::Cover:0x00000000000000 @values=[1, 2]>
16
+ #
17
+ def initialize(*values)
18
+ @values = values
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @values.each do |value|
29
+ return false unless @actual.include?(value)
30
+ end
31
+
32
+ true
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class EndWith < BaseMatcher
4
+ matcher_name :end_with
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (String)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::EndWith.new("test")
15
+ # => #<ArgumentSpecification::Matchers::EndWith:0x00000000000000 @expected="test">
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual.end_with?(@expected)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class Equal < BaseMatcher
4
+ matcher_name :equal
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::Equal.new(:test)
15
+ # => #<ArgumentSpecification::Matchers::Equal:0x00000000000000 @expected=:test>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual == @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class Match < BaseMatcher
4
+ matcher_name :match
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Object)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::Match.new(Symbol)
15
+ # => #<ArgumentSpecification::Matchers::Match:0x00000000000000 @expected=Symbol>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual =~ @expected
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class RespondTo < BaseMatcher
4
+ matcher_name :respond_to
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (Symbol)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::RespondTo.new(:hello)
15
+ # => #<ArgumentSpecification::Matchers::RespondTo:0x00000000000000 @expected=:hello>
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual.respond_to?(@expected)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,66 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class Satisfy < BaseMatcher
4
+ matcher_name :satisfy
5
+
6
+ attr_reader :description, :block
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # description: (String)
12
+ # block: (Block)
13
+ #
14
+ # Example:
15
+ # >> ArgumentSpecification::Matchers::Satisfy.new('always pass') { true }
16
+ # => #<ArgumentSpecification::Matchers::Satisfy:0x00000000000000 @block=#<Proc:0x00000000000000>>
17
+ #
18
+ def initialize(description, &block)
19
+ @description = description
20
+ @block = block
21
+ end
22
+
23
+ # The failure message when using 'should'
24
+ #
25
+ # Example:
26
+ # >> matcher.failure_message
27
+ # => "'test' should always pass"
28
+ #
29
+ def failure_message
30
+ actual = prettify_args(@actual)
31
+
32
+ if @description
33
+ "'#{actual}' should #{@description}"
34
+ else
35
+ "'#{actual}' should satisfy the requirements"
36
+ end
37
+ end
38
+
39
+ # The failure message when using 'should not'
40
+ #
41
+ # Example:
42
+ # >> matcher.failure_message_when_negated
43
+ # => "':test' should not always pass"
44
+ #
45
+ def failure_message_when_negated
46
+ actual = prettify_args(@actual)
47
+
48
+ if @description
49
+ "'#{actual}' should not #{@description}"
50
+ else
51
+ "'#{actual}' should not satisfy the requirements"
52
+ end
53
+ end
54
+
55
+ # Check if the actual object matches
56
+ #
57
+ # Example:
58
+ # >> matcher.matches?
59
+ # => true
60
+ #
61
+ def matches?
62
+ @block.call(@actual)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,32 @@
1
+ module ArgumentSpecification
2
+ module Matchers
3
+ class StartWith < BaseMatcher
4
+ matcher_name :start_with, :begin_with
5
+
6
+ attr_reader :expected
7
+
8
+ # Create a new matcher instance
9
+ #
10
+ # Arguments:
11
+ # expected: (String)
12
+ #
13
+ # Example:
14
+ # >> ArgumentSpecification::Matchers::StartWith.new("test")
15
+ # => #<ArgumentSpecification::Matchers::StartWith:0x00000000000000 @expected="test">
16
+ #
17
+ def initialize(expected)
18
+ @expected = expected
19
+ end
20
+
21
+ # Check if the actual object matches
22
+ #
23
+ # Example:
24
+ # >> matcher.matches?
25
+ # => true
26
+ #
27
+ def matches?
28
+ @actual.start_with?(@expected)
29
+ end
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: argspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nialto Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,28 @@ files:
73
73
  - lib/argspec/constants.rb
74
74
  - lib/argspec/dsl.rb
75
75
  - lib/argspec/matchers.rb
76
+ - lib/argspec/matchers/all.rb
77
+ - lib/argspec/matchers/base_matcher.rb
78
+ - lib/argspec/matchers/be.rb
79
+ - lib/argspec/matchers/be_a.rb
80
+ - lib/argspec/matchers/be_empty.rb
81
+ - lib/argspec/matchers/be_falsey.rb
82
+ - lib/argspec/matchers/be_greater_than.rb
83
+ - lib/argspec/matchers/be_greater_than_or_equal_to.rb
84
+ - lib/argspec/matchers/be_less_than.rb
85
+ - lib/argspec/matchers/be_less_than_or_equal_to.rb
86
+ - lib/argspec/matchers/be_nil.rb
87
+ - lib/argspec/matchers/be_truthy.rb
88
+ - lib/argspec/matchers/be_within_range.rb
89
+ - lib/argspec/matchers/case_equal.rb
90
+ - lib/argspec/matchers/contain.rb
91
+ - lib/argspec/matchers/cover.rb
92
+ - lib/argspec/matchers/end_with.rb
93
+ - lib/argspec/matchers/equal.rb
94
+ - lib/argspec/matchers/match.rb
95
+ - lib/argspec/matchers/respond_to.rb
96
+ - lib/argspec/matchers/satisfy.rb
97
+ - lib/argspec/matchers/start_with.rb
76
98
  - spec/argspec_spec.rb
77
99
  - spec/spec_helper.rb
78
100
  homepage: https://rubygems.org/gems/argspec