spectus 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ require_relative 'base'
2
+
3
+ module Spectus
4
+ module RequirementLevel
5
+ # Medium requirement level's class.
6
+ #
7
+ # @api private
8
+ #
9
+ class Medium < Base
10
+ # Evaluate the expectation.
11
+ #
12
+ # @return [Result::Fail, Result::Pass] report if the medium expectation
13
+ # pass or fail.
14
+ def result
15
+ state = sandbox
16
+
17
+ if state.valid? || state.exception.nil?
18
+ pass!(state)
19
+ else
20
+ fail!(state)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,83 @@
1
+ module Spectus
2
+ module Result
3
+ # Result base's module.
4
+ #
5
+ # @api public
6
+ #
7
+ module Base
8
+ # Initialize the result class.
9
+ #
10
+ # @param message [String] It is describing the actual/error value.
11
+ # @param subject [#object_id] The untrusted object to be tested.
12
+ # @param challenge [Symbol] The method to call on the subject.
13
+ # @param context [Array] Parameters of the challenge.
14
+ # @param actual [#object_id] The value that the subject return through
15
+ # its challenge.
16
+ # @param expected [Array, Hash, Symbol] The definition of the expected
17
+ # value.
18
+ # @param got [#object_id] The result of the boolean comparison
19
+ # between the actual value and the expected value.
20
+ # @param error [#exception, nil] Any possible raised exception.
21
+ # @param level [:High, :Medium, :Low] The level of the expectation.
22
+ # @param negate [Boolean] Evaluate to a negative assertion.
23
+ # @param valid [Boolean] Report if the test was true or false.
24
+ def initialize(message, subject, challenge, context, actual, expected,
25
+ got, error, level, negate, valid)
26
+
27
+ if respond_to?(:exception)
28
+ super(message)
29
+ else
30
+ @message = message
31
+ end
32
+
33
+ @subject = subject
34
+ @challenge = challenge
35
+ @context = context
36
+ @actual = actual
37
+ @expected = expected
38
+ @got = got
39
+ @error = error
40
+ @level = level
41
+ @negate = negate
42
+ @valid = valid
43
+ end
44
+
45
+ attr_reader :subject, :challenge, :context, :actual, :expected, :got,
46
+ :error, :level
47
+
48
+ # The value of the negate instance variable.
49
+ #
50
+ # @return [Boolean] evaluated to a negative assertion or not.
51
+ def negate?
52
+ @negate
53
+ end
54
+
55
+ # The value of the boolean comparison between the actual value and the
56
+ # expected value.
57
+ #
58
+ # @return [Boolean] the test was true or false.
59
+ def valid?
60
+ @valid
61
+ end
62
+
63
+ # Report the result.
64
+ #
65
+ # @return [Symbol] the properties of the result.
66
+ def to_h
67
+ {
68
+ subject: subject,
69
+ challenge: challenge,
70
+ context: context,
71
+ actual: actual,
72
+ expected: expected,
73
+ got: got,
74
+ error: error,
75
+ level: level,
76
+ negate: negate?,
77
+ valid: valid?,
78
+ result: result?
79
+ }
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'base'
2
+
3
+ module Spectus
4
+ module Result
5
+ # The class that is responsible for reporting that the expectation is false.
6
+ class Fail < StandardError
7
+ include Base
8
+
9
+ # The value of the expectation of the spec.
10
+ #
11
+ # @return [Boolean] the spec was false.
12
+ def result?
13
+ false
14
+ end
15
+
16
+ # Identify the state of the result.
17
+ #
18
+ # @return [String] the char that identify the state of the result.
19
+ def to_char
20
+ if error.nil?
21
+ 'F'
22
+ else
23
+ 'E'
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'base'
2
+
3
+ module Spectus
4
+ module Result
5
+ # The class that is responsible for reporting that the expectation is true.
6
+ class Pass
7
+ include Base
8
+
9
+ attr_reader :message
10
+
11
+ # The value of the expectation of the spec.
12
+ #
13
+ # @return [Boolean] the spec was true.
14
+ def result?
15
+ true
16
+ end
17
+
18
+ # Identify the state of the result.
19
+ #
20
+ # @return [String] the char that identify the state of the result.
21
+ def to_char
22
+ if got
23
+ '.'
24
+ else
25
+ 'I'
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,4 +1,4 @@
1
- require_relative 'matcher'
1
+ require 'matchi'
2
2
 
3
3
  module Spectus
4
4
  # This class evaluate the expectation with the passed block.
@@ -6,28 +6,44 @@ module Spectus
6
6
  # @api private
7
7
  #
8
8
  class Sandbox
9
- attr_reader :exception, :got
9
+ attr_reader :actual, :exception, :got
10
10
 
11
11
  # Execute the untested code from the passed block against the definition.
12
12
  #
13
- # @param [Hash] definition
13
+ # @param [Array, Hash, Symbol] definition
14
14
  # @param [Boolean] negate
15
- # @yieldparam actual the value which is compared with the expected value.
16
- def initialize(definition, negate, &actual)
17
- @got = negate ^ Matcher.pass?(definition, &actual)
15
+ # @param [#object_id] object the front object which is challenged.
16
+ # @param [Symbol] meth the name of the method.
17
+ # @param [Array] args the arguments of the method.
18
+ def initialize(definition, negate, object, meth, *args)
19
+ @got = negate ^ matcher(definition).matches? do
20
+ @actual = object.public_send(meth, *args)
21
+ end
18
22
  rescue => e
19
23
  @exception = e
20
24
  end
21
25
 
22
- # Return the result.
26
+ # Report to the spec's requirement level if the test is true or false.
23
27
  #
24
28
  # @return [Boolean] Report if the test was true or false.
25
- def pass?
29
+ def valid?
26
30
  if defined?(@exception)
27
31
  false
28
32
  else
29
33
  @got
30
34
  end
31
35
  end
36
+
37
+ private
38
+
39
+ # Load the matcher.
40
+ #
41
+ # @param definition [Array, Hash, Symbol]
42
+ #
43
+ # @return [#matches?] the matcher
44
+ def matcher(definition)
45
+ params = Array(definition).flatten(1)
46
+ Matchi.fetch(params.first, *params[1..-1])
47
+ end
32
48
  end
33
49
  end
@@ -4,23 +4,23 @@ Gem::Specification.new do |spec|
4
4
  spec.authors = ['Cyril Wack']
5
5
  spec.email = ['contact@cyril.email']
6
6
 
7
- spec.summary = 'Expectation library with requirement levels.'
8
- spec.description = 'Expectation library with RFC 2119\'s requirement ' \
9
- 'levels, and some matchers for Ruby.'
7
+ spec.summary = 'Expectation library with RFC 2119 keywords.'
8
+ spec.description = "Expectation library with RFC 2119's requirement levels."
10
9
  spec.homepage = 'https://github.com/fixrb/spectus'
11
10
  spec.license = 'MIT'
12
11
 
13
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^test\//) }
14
- spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
12
+ spec.files =
13
+ `git ls-files -z`.split("\x0").reject { |f| f.match(/%r{^test}/) }
14
+ spec.executables = spec.files.grep(/%r{^exe}/) { |f| File.basename(f) }
15
15
  spec.require_paths = ['lib']
16
16
 
17
- spec.add_dependency 'matchi', '~> 0.0.4'
17
+ spec.add_dependency 'matchi', '~> 0.0.6'
18
18
 
19
- spec.add_development_dependency 'bundler', '~> 1.8'
20
- spec.add_development_dependency 'rake', '~> 10.0'
19
+ spec.add_development_dependency 'bundler', '~> 1.9'
20
+ spec.add_development_dependency 'rake', '~> 10.4'
21
21
  spec.add_development_dependency 'yard', '~> 0.8'
22
- spec.add_development_dependency 'simplecov', '~> 0.9.1'
23
- spec.add_development_dependency 'rubocop', '~> 0.29'
22
+ spec.add_development_dependency 'simplecov', '~> 0.10'
23
+ spec.add_development_dependency 'rubocop', '~> 0.31'
24
24
 
25
25
  private_key = File.expand_path '~/.gem/spectus-gem-private_key.pem'
26
26
  if File.exist? private_key
@@ -0,0 +1,3 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start
@@ -0,0 +1,140 @@
1
+ require_relative File.join 'support', 'coverage'
2
+ require_relative File.join '..', 'lib', 'spectus'
3
+
4
+ subject = -> { 'foo'.upcase }
5
+
6
+ begin
7
+ Spectus.this(&subject).MUST(Eql: 'foo')
8
+ rescue Spectus::Result::Fail => raised_result
9
+ raise 'failing test' unless raised_result.to_char == 'F'
10
+ raise 'failing test' unless raised_result.to_h == {
11
+ subject: subject,
12
+ challenge: :call,
13
+ context: [],
14
+ actual: 'FOO',
15
+ expected: { Eql: 'foo' },
16
+ got: false,
17
+ error: nil,
18
+ level: :High,
19
+ negate: false,
20
+ valid: false,
21
+ result: false
22
+ }
23
+
24
+ print '.'
25
+ end
26
+
27
+ begin
28
+ Spectus.this(&subject).MUST_NOT(Eql: 'FOO')
29
+ rescue Spectus::Result::Fail => raised_result
30
+ raise 'failing test' unless raised_result.to_char == 'F'
31
+ raise 'failing test' unless raised_result.to_h == {
32
+ subject: subject,
33
+ challenge: :call,
34
+ context: [],
35
+ actual: 'FOO',
36
+ expected: { Eql: 'FOO' },
37
+ got: false,
38
+ error: nil,
39
+ level: :High,
40
+ negate: true,
41
+ valid: false,
42
+ result: false
43
+ }
44
+
45
+ print '.'
46
+ end
47
+
48
+ subject = -> { 'foo'.bar }
49
+
50
+ begin
51
+ Spectus.this(&subject).MUST(Eql: 'foo')
52
+ rescue Spectus::Result::Fail => raised_result
53
+ raise 'failing test' unless raised_result.error.class == NoMethodError
54
+ raise 'failing test' unless raised_result.to_char == 'E'
55
+ raise 'failing test' unless raised_result.to_h == {
56
+ subject: subject,
57
+ challenge: :call,
58
+ context: [],
59
+ actual: nil,
60
+ expected: { Eql: 'foo' },
61
+ got: nil,
62
+ error: raised_result.error,
63
+ level: :High,
64
+ negate: false,
65
+ valid: false,
66
+ result: false
67
+ }
68
+
69
+ print '.'
70
+ end
71
+
72
+ begin
73
+ Spectus.this(&subject).MUST_NOT(Eql: 'foo')
74
+ rescue Spectus::Result::Fail => raised_result
75
+ raise 'failing test' unless raised_result.error.class == NoMethodError
76
+ raise 'failing test' unless raised_result.to_char == 'E'
77
+ raise 'failing test' unless raised_result.to_h == {
78
+ subject: subject,
79
+ challenge: :call,
80
+ context: [],
81
+ actual: nil,
82
+ expected: { Eql: 'foo' },
83
+ got: nil,
84
+ error: raised_result.error,
85
+ level: :High,
86
+ negate: true,
87
+ valid: false,
88
+ result: false
89
+ }
90
+
91
+ print '.'
92
+ end
93
+
94
+ subject = -> { 'foo'.upcase(:bar) }
95
+
96
+ begin
97
+ Spectus.this(&subject).MUST(Eql: 'foo')
98
+ rescue Spectus::Result::Fail => raised_result
99
+ raise 'failing test' unless raised_result.error.class == ArgumentError
100
+ raise 'failing test' unless raised_result.to_char == 'E'
101
+ raise 'failing test' unless raised_result.to_h == {
102
+ subject: subject,
103
+ challenge: :call,
104
+ context: [],
105
+ actual: nil,
106
+ expected: { Eql: 'foo' },
107
+ got: nil,
108
+ error: raised_result.error,
109
+ level: :High,
110
+ negate: false,
111
+ valid: false,
112
+ result: false
113
+ }
114
+
115
+ print '.'
116
+ end
117
+
118
+ begin
119
+ Spectus.this(&subject).MUST_NOT(Eql: 'foo')
120
+ rescue Spectus::Result::Fail => raised_result
121
+ raise 'failing test' unless raised_result.error.class == ArgumentError
122
+ raise 'failing test' unless raised_result.to_char == 'E'
123
+ raise 'failing test' unless raised_result.to_h == {
124
+ subject: subject,
125
+ challenge: :call,
126
+ context: [],
127
+ actual: nil,
128
+ expected: { Eql: 'foo' },
129
+ got: nil,
130
+ error: raised_result.error,
131
+ level: :High,
132
+ negate: true,
133
+ valid: false,
134
+ result: false
135
+ }
136
+
137
+ print '.'
138
+ end
139
+
140
+ puts
@@ -0,0 +1,44 @@
1
+ require_relative File.join 'support', 'coverage'
2
+ require_relative File.join '..', 'lib', 'spectus'
3
+
4
+ subject = -> { 'foo'.upcase }
5
+
6
+ result = Spectus.this(&subject).MUST(Eql: 'FOO')
7
+
8
+ fail 'failing test' unless result.to_char == '.'
9
+ fail 'failing test' unless result.to_h == {
10
+ subject: subject,
11
+ challenge: :call,
12
+ context: [],
13
+ actual: 'FOO',
14
+ expected: { Eql: 'FOO' },
15
+ got: true,
16
+ error: nil,
17
+ level: :High,
18
+ negate: false,
19
+ valid: true,
20
+ result: true
21
+ }
22
+
23
+ print '.'
24
+
25
+ result = Spectus.this(&subject).MUST_NOT(Eql: 'foo')
26
+
27
+ fail 'failing test' unless result.to_char == '.'
28
+ fail 'failing test' unless result.to_h == {
29
+ subject: subject,
30
+ challenge: :call,
31
+ context: [],
32
+ actual: 'FOO',
33
+ expected: { Eql: 'foo' },
34
+ got: true,
35
+ error: nil,
36
+ level: :High,
37
+ negate: true,
38
+ valid: true,
39
+ result: true
40
+ }
41
+
42
+ print '.'
43
+
44
+ puts