rspec-when 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63b5e2cc54a288d461b1fbfecc5e6415e4d6cc39
4
+ data.tar.gz: 0980ba0c3c700c501d6f92e68fa13a7ea755809c
5
+ SHA512:
6
+ metadata.gz: 8869c96cc00b12d1d9b3fbd25003a7402dfba007499fea695af368d1027f5c9234279989fbe3569681aadea1d4e3841691891c8168b67d34bc5b1fee6066d7f7
7
+ data.tar.gz: af92ba4fa7a1c544a22062e94959cab594607eab294bf6eebf5982dd4ffd38436e925b74278dbedf95b8ff1a77bfe45d90cf0097fad5f8f0a4a0359fc8d2ab85
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # RSpec when syntax
2
+
3
+ RSpec::When lets you describe both optional expectations and expectations
4
+ with inverses. This helps DRY up otherwise complicated or repetitive tests.
5
+
6
+ ## Installation
7
+
8
+ ```sh
9
+ $ gem install rspec-when
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ #### Plain `when` syntax
15
+
16
+ ```rb
17
+ expect(user).when(an_admin).to have_all_access
18
+ ```
19
+
20
+ Provides syntactic sugar for
21
+
22
+ ```rb
23
+ if an_admin
24
+ expect(user).to have_all_access
25
+ end
26
+ ```
27
+
28
+ #### Compound `when.with_inverse` syntax
29
+
30
+ ```rb
31
+ expect(user).when(an_admin).with_inverse.to have_all_access
32
+ ```
33
+
34
+ Provides syntactic sugar for
35
+
36
+ ```rb
37
+ if an_admin
38
+ expect(user).to have_all_access
39
+ else
40
+ expect(user).not_to have_all_access
41
+ end
42
+ ```
43
+
44
+ Typical usage is when abstracting common expectations into a helper method to
45
+ DRY up tests where testing the inverse expectation is also required.
@@ -0,0 +1,11 @@
1
+ module RSpec
2
+ module Expectations
3
+ # DSL extension to expectation target allowing optional expectations
4
+ module WhenExpectationExtensions
5
+ def when(expected)
6
+ WhenExpectationTarget.new @target, expected
7
+ end
8
+ end
9
+ ExpectationTarget.include WhenExpectationExtensions
10
+ end
11
+ end
@@ -0,0 +1,92 @@
1
+ module RSpec
2
+ module Expectations
3
+ # Wraps the target of an expectation and allowing of optional expectations
4
+ # as well as testing expectation inverses
5
+ #
6
+ # @example
7
+ # expect(something).when(flag) # => WhenExpectationTarget testing when `flag`
8
+ #
9
+ # # `when` used with `to`
10
+ # expect(actual).when(flag).to eq(3)
11
+ # # is the equivalent of
12
+ # if flag
13
+ # expect(actual).to eq(3)
14
+ # end
15
+ #
16
+ # # `when` used with `with_inverse` and `to`
17
+ # expect(actual).when(flag).with_inverse.to eq(3)
18
+ # # is the equivalent of
19
+ # if flag
20
+ # expect(actual).to eq(3)
21
+ # else
22
+ # expect(actual).not_to eq(3)
23
+ # end
24
+ #
25
+ # @note `WhenExpectationTarget` is not intended to be instantiated
26
+ # directly. Use `expect(target).when(flag)` instead
27
+
28
+ class WhenExpectationTarget < ExpectationTarget
29
+ # @return [Object] the target of the expectation
30
+ attr_reader :target
31
+
32
+ # @return [Boolean] is the target expected?
33
+ attr_reader :when_expected
34
+
35
+ def initialize(value, when_expected)
36
+ @target = value
37
+ @when_expected = when_expected
38
+ @with_inverse = false
39
+ end
40
+
41
+ # Indicates whether the inverse of the expectation should be applied
42
+ # should the `when` flag be false
43
+ def with_inverse
44
+ @with_inverse = true
45
+ self
46
+ end
47
+
48
+ alias_method :expect_to, :to
49
+ alias_method :expect_not_to, :not_to
50
+
51
+ # When expected, it will run the given expectation, passing if `matcher` returns true.
52
+ # When not expected and we're also testing the inverse, it will run the given expectation,
53
+ # passing if `matcher` returns false.
54
+ # @example
55
+ # expect(value).when(flag).to eq(5)
56
+ # expect(value).when(flag).with_inverse.to eq(5)
57
+ # expect { perform }.when(flag).to raise_error
58
+ # @param [Matcher]
59
+ # matcher
60
+ # @param [String or Proc] message optional message to display when the expectation fails
61
+ # @return [Boolean] true if the expectation succeeds (else raises)
62
+ # @see RSpec::Matchers
63
+ def to(matcher = nil, message = nil, &block)
64
+ if @when_expected
65
+ super
66
+ elsif @with_inverse
67
+ expect_not_to matcher, message, &block
68
+ end
69
+ end
70
+
71
+ # When expected, it will run the given expectation, passing if `matcher` returns false.
72
+ # When not expected and we're also testing the inverse, it will run the given expectation,
73
+ # passing if `matcher` returns true.
74
+ # @example
75
+ # expect(value).when(flag).not_to eq(5)
76
+ # expect(value).when(flag).with_inverse.not_to eq(5)
77
+ # @param [Matcher]
78
+ # matcher
79
+ # @param [String or Proc] message optional message to display when the expectation fails
80
+ # @return [Boolean] false if the negative expectation succeeds (else raises)
81
+ # @see RSpec::Matchers
82
+ def not_to(matcher = nil, message = nil, &block)
83
+ if @when_expected
84
+ super
85
+ elsif @with_inverse
86
+ expect_to matcher, message, &block
87
+ end
88
+ end
89
+ alias to_not not_to
90
+ end
91
+ end
92
+ end
data/lib/rspec/when.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'rspec/expectations/expectation_target'
2
+ require_relative 'expectations/when_expectation_extensions'
3
+ require_relative 'expectations/when_expectation_target'
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-when
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Bromwich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-expectations
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ description:
34
+ email: a.bromwich@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files:
38
+ - README.md
39
+ files:
40
+ - README.md
41
+ - lib/rspec/expectations/when_expectation_extensions.rb
42
+ - lib/rspec/expectations/when_expectation_target.rb
43
+ - lib/rspec/when.rb
44
+ homepage: http://github.com/abrom/rspec-when
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options:
50
+ - "--main"
51
+ - README.md
52
+ - "--markup"
53
+ - markdown
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.8
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: RSpec extension for when syntax
72
+ test_files: []
73
+ has_rdoc: