deep_double 0.1.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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/deep_double.gemspec +26 -0
- data/lib/deep_double/function/validate_result_table.rb +47 -0
- data/lib/deep_double/function.rb +46 -0
- data/lib/deep_double/literal.rb +21 -0
- data/lib/deep_double/recursive_function.rb +36 -0
- data/lib/deep_double/version.rb +3 -0
- data/lib/deep_double.rb +56 -0
- data/spec/deep_double_spec.rb +178 -0
- data/spec/spec_helper.rb +15 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 10da248b3fdf8a64407a89a83c6f77e6df110f55c44bf3c5be5a2ae1c1924066
|
4
|
+
data.tar.gz: 73bb4881d11356d2ec95f810b7446e51e6b0c516c1066258eee7b04e74a0c158
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 95ec83eeaec878133e679e327bc16f7c18623a5822df6eaf48a99634a30df7e6f9f643c372ca006502be02002ad733191d3b8ce6fb59ab95f5a9ffceeb07d9ca
|
7
|
+
data.tar.gz: dc383f873d38b97b9be99c107efb7ea0331f2021c5795d3b3a89b1b17f92c745349613b6895f4f7afd5e9ec628104d5124a854f12ccb28f29b2b9f2fa164812f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
deep_double (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
deep_double!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonah
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# DeepDouble
|
2
|
+
|
3
|
+
Proper README Coming Soon...
|
4
|
+
|
5
|
+
For now, please see the spec file for how to use.
|
6
|
+
|
7
|
+
TODO: Motivation
|
8
|
+
TODO: Compare with RSpec doubles
|
9
|
+
TODO: Add example use in README
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'deep_double'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install deep_double
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
See the spec file for more examples. Here's a basic double:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'deep_double'
|
33
|
+
|
34
|
+
DeepDouble::Double.new(
|
35
|
+
methA: {
|
36
|
+
[] => "methA()",
|
37
|
+
[1] => "methA(1)",
|
38
|
+
[2] => "methA(2)",
|
39
|
+
[1, 2] => "methA(1, 2)",
|
40
|
+
[2, 1] => "methA(2, 1)"
|
41
|
+
},
|
42
|
+
methB: {[] => "methB()"},
|
43
|
+
methC: {[{a: 1, b: 2}] => "methC(a: 1, b: 2)"},
|
44
|
+
methD: {[] => proc { raise ZeroDivisionError }},
|
45
|
+
methE: {[] => -> { raise TypeError }},
|
46
|
+
)
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/deep_double.
|
54
|
+
|
55
|
+
## License
|
56
|
+
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "deep_double"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/deep_double.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "deep_double/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "deep_double"
|
8
|
+
spec.version = DeepDouble::VERSION
|
9
|
+
spec.authors = ["Jonah"]
|
10
|
+
spec.email = ["jonahx@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Fast, declarative test doubles that support nesting.}
|
13
|
+
spec.homepage = "https://github.com/jonahx/deep_double"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "bin"
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# A "function" in the mathematical sense: a mapping of input values to output
|
2
|
+
# values. This (argument -> value) mapping is specified by a "result_table" --
|
3
|
+
# just an ordinary ruby hash -- passed to the constructor.
|
4
|
+
#
|
5
|
+
# If the return value is itself a hash, it's interpreted as the definition of
|
6
|
+
# another double, and so another `DeepDouble` instance is returned.
|
7
|
+
#
|
8
|
+
module DeepDouble
|
9
|
+
class Function
|
10
|
+
|
11
|
+
class ValidateResultTable
|
12
|
+
def initialize(result_table)
|
13
|
+
@result_table = result_table
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
validate_result_table_type
|
18
|
+
validate_result_table_keys
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def validate_result_table_type
|
24
|
+
return if @result_table.is_a?(Hash)
|
25
|
+
raise ArgumentError,
|
26
|
+
"The result table defining a Function must be a Hash"
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_result_table_keys
|
30
|
+
@result_table.keys.each { |key| validate_key(key) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate_key(key)
|
34
|
+
return if valid_key?(key)
|
35
|
+
raise ArgumentError,
|
36
|
+
"Keys in a result table must by Arrays representing argument " +
|
37
|
+
"lists (the empty array represents 0 arguments). The following " +
|
38
|
+
"key was invalid: #{key.inspect}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid_key?(key)
|
42
|
+
key.is_a?(Array) || key == :default
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'deep_double/function/validate_result_table'
|
2
|
+
|
3
|
+
# A "function" in the mathematical sense: a mapping of input values to output
|
4
|
+
# values. This (argument -> value) mapping is specified by a "result_table" --
|
5
|
+
# just an ordinary ruby hash -- passed to the constructor.
|
6
|
+
#
|
7
|
+
# If the return value is itself a hash, it's interpreted as the definition of
|
8
|
+
# another double, and so another `DeepDouble` instance is returned.
|
9
|
+
#
|
10
|
+
module DeepDouble
|
11
|
+
class Function
|
12
|
+
|
13
|
+
def initialize(result_table)
|
14
|
+
@result_table = result_table
|
15
|
+
validate_result_table
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(*args)
|
19
|
+
validate_result_exists(args)
|
20
|
+
result(args)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def validate_result_table
|
26
|
+
ValidateResultTable.new(@result_table).call
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_result_exists(args)
|
30
|
+
return if value_defined_for?(args)
|
31
|
+
raise ArgumentError, "Function not defined for args: #{args}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def result(args)
|
35
|
+
if @result_table.key?(args)
|
36
|
+
@result_table[args]
|
37
|
+
else
|
38
|
+
@result_table[:default]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def value_defined_for?(args)
|
43
|
+
@result_table.key?(args) || @result_table.key?(:default)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Because a Hash in the context of a DeepDouble definition is considered to be
|
2
|
+
# the definition of a *nested* DeepDouble, we need a way to "escape" a Hash for
|
3
|
+
# methods that actually return Hash values.
|
4
|
+
#
|
5
|
+
# Likewise, a Proc in the context of a DeepDouble definition is invoked
|
6
|
+
# automatically. (This lets us, eg, create methods that raise errors.) Again,
|
7
|
+
# we need a way to "escape" a Proc for methods that actually return literal
|
8
|
+
# Proc values.
|
9
|
+
#
|
10
|
+
# This class provides a general mechanism to "escape" special values in
|
11
|
+
# Function definitions.
|
12
|
+
#
|
13
|
+
module DeepDouble
|
14
|
+
class Literal
|
15
|
+
attr_reader :value
|
16
|
+
|
17
|
+
def initialize(value)
|
18
|
+
@value = value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# Makes a `DeepDouble::Function` recursive, so that `Hash` results are
|
2
|
+
# automatically converted to `DeepDouble` instances in their own right.
|
3
|
+
#
|
4
|
+
module DeepDouble
|
5
|
+
class RecursiveFunction
|
6
|
+
|
7
|
+
def initialize(function)
|
8
|
+
@function = function
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(*args)
|
12
|
+
result = @function.call(*args)
|
13
|
+
special_case_transforms(result)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_proc
|
17
|
+
method(:call).to_proc
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def special_case_transforms(result)
|
23
|
+
class_name = result.class.name.split('::').last
|
24
|
+
case class_name
|
25
|
+
when 'Hash'
|
26
|
+
Double.new(result) # recursive case
|
27
|
+
when 'Proc'
|
28
|
+
result.call
|
29
|
+
when 'Literal'
|
30
|
+
result.value
|
31
|
+
else
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/deep_double.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# This is a utility class for quickly and declaratively creating doubles of
|
2
|
+
# arbitrarily nested depth.
|
3
|
+
#
|
4
|
+
# It drastically reduces the amount of boilerplate compared with rspec.
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'deep_double/function'
|
8
|
+
require 'deep_double/literal'
|
9
|
+
require 'deep_double/recursive_function'
|
10
|
+
require "deep_double/version"
|
11
|
+
|
12
|
+
module DeepDouble
|
13
|
+
class Double
|
14
|
+
|
15
|
+
# In this case, having an optional name first makes for a cleaner API
|
16
|
+
#
|
17
|
+
# rubocop:disable OptionalArguments
|
18
|
+
def initialize(name = 'Anonymous', definition)
|
19
|
+
@name = name
|
20
|
+
@definition = definition
|
21
|
+
validate_definition
|
22
|
+
create_methods_in_definition
|
23
|
+
end
|
24
|
+
# rubocop:enable OptionalArguments
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def validate_definition
|
29
|
+
@definition.keys.each { |key| validate_method_name(key) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate_method_name(name)
|
33
|
+
return if valid_method_name?(name)
|
34
|
+
raise ArgumentError,
|
35
|
+
"Method names in DeepDouble definition must be Symbols or Strings. " +
|
36
|
+
"The following name is invalid: #{name.inspect}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid_method_name?(name)
|
40
|
+
name.is_a?(Symbol) || name.is_a?(String)
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_methods_in_definition
|
44
|
+
@definition.keys.each { |meth| create_method(meth) }
|
45
|
+
end
|
46
|
+
|
47
|
+
def create_method(meth)
|
48
|
+
define_singleton_method(meth.to_sym, &function(meth))
|
49
|
+
end
|
50
|
+
|
51
|
+
def function(meth)
|
52
|
+
raw_fn = Function.new(@definition[meth])
|
53
|
+
RecursiveFunction.new(raw_fn)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'DeepDouble' do
|
4
|
+
|
5
|
+
def deep_double(definition)
|
6
|
+
DeepDouble::Double.new(definition)
|
7
|
+
end
|
8
|
+
|
9
|
+
def literal(value)
|
10
|
+
DeepDouble::Literal.new(value)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with valid unnested definition' do
|
14
|
+
|
15
|
+
subject(:valid) do
|
16
|
+
deep_double(
|
17
|
+
methA: {
|
18
|
+
[] => "methA()",
|
19
|
+
[1] => "methA(1)",
|
20
|
+
[2] => "methA(2)",
|
21
|
+
[1, 2] => "methA(1, 2)",
|
22
|
+
[2, 1] => "methA(2, 1)"
|
23
|
+
},
|
24
|
+
methB: {[] => "methB()"},
|
25
|
+
methC: {[{a: 1, b: 2}] => "methC(a: 1, b: 2)"},
|
26
|
+
methD: {[] => proc { raise ZeroDivisionError }},
|
27
|
+
methE: {[] => -> { raise TypeError }},
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns correct value for zero args' do
|
32
|
+
expect(valid.methA).to eq('methA()')
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'handles multiple 0 argument methods' do
|
36
|
+
expect(valid.methB).to eq('methB()')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns correct value for 1 argument methods' do
|
40
|
+
expect(valid.methA(1)).to eq('methA(1)')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'uses the argument to find the correct value' do
|
44
|
+
expect(valid.methA(2)).to eq('methA(2)')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns correct value for many-argument methods' do
|
48
|
+
expect(valid.methA(1, 2)).to eq('methA(1, 2)')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'uses both arguments to find the correct value' do
|
52
|
+
expect(valid.methA(2, 1)).to eq('methA(2, 1)')
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'works with named arguments' do
|
56
|
+
expect(valid.methC(a: 1, b: 2)).to eq('methC(a: 1, b: 2)')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'requires all named arguments to match' do
|
60
|
+
expect{ valid.methC(a: 1, b: 3) }.to raise_error(ArgumentError)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'invokes procs when they are specified as the return value' do
|
64
|
+
expect{ valid.methD }.to raise_error(ZeroDivisionError)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'invokes lambdas when they are specified as the return value' do
|
68
|
+
expect{ valid.methE }.to raise_error(TypeError)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'raises NoMethodError when you call a non-existent method' do
|
72
|
+
expect { valid.blah }.to raise_error(NoMethodError)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "raises ArgumentError when you haven't configured args" do
|
76
|
+
expect { valid.methA(3) }.to raise_error(ArgumentError)
|
77
|
+
expect { valid.methA(1, 2, 3) }.to raise_error(ArgumentError)
|
78
|
+
expect { valid.methA(1, 3) }.to raise_error(ArgumentError)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'with valid definition and default value' do
|
83
|
+
|
84
|
+
subject(:valid) do
|
85
|
+
deep_double(
|
86
|
+
methA: {
|
87
|
+
[] => "methA()",
|
88
|
+
[1] => "methA(1)",
|
89
|
+
:default => "Default Value"
|
90
|
+
}
|
91
|
+
)
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'lets you set a default value for unspecified args' do
|
95
|
+
expect(valid.methA('blah')).to eq('Default Value')
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'gives precedence to specific args' do
|
99
|
+
expect(valid.methA).to eq('methA()')
|
100
|
+
expect(valid.methA(1)).to eq('methA(1)')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'with valid nested definition' do
|
105
|
+
|
106
|
+
subject(:valid) do
|
107
|
+
deep_double(
|
108
|
+
methA: {
|
109
|
+
[] => {
|
110
|
+
methAA: {[] => 'methAA()'},
|
111
|
+
methAB: {[] => {
|
112
|
+
methABA: {[] => 'methABA()'}}
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'automatically creates nested DeepDouble instances' do
|
120
|
+
expect(valid.methA.methAA).to eq('methAA()')
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'handles deep nesting too' do
|
124
|
+
expect(valid.methA.methAB.methABA).to eq('methABA()')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'with an invalid definition' do
|
129
|
+
|
130
|
+
let(:invalid_method_name) do
|
131
|
+
deep_double(
|
132
|
+
methA: {[] => 'methA()'},
|
133
|
+
[] => {[] => 'will not work'},
|
134
|
+
)
|
135
|
+
end
|
136
|
+
|
137
|
+
let(:invalid_result_type) do
|
138
|
+
deep_double(methA: 'methA()')
|
139
|
+
end
|
140
|
+
|
141
|
+
let(:invalid_result_key) do
|
142
|
+
deep_double(methA: {non_array_key: 'methA()'})
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'raises when the method name is not a string or symbol' do
|
146
|
+
expect{ invalid_method_name }.to raise_error(ArgumentError)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'raises when the result is not a Hash' do
|
150
|
+
expect{ invalid_result_type }.to raise_error(ArgumentError)
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'raises if a result key is not an array (a list of args)' do
|
154
|
+
expect{ invalid_result_key }.to raise_error(ArgumentError)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'when returning literal values' do
|
159
|
+
|
160
|
+
literal_hash = {a: 1}
|
161
|
+
literal_proc = -> { raise ArgumentError }
|
162
|
+
|
163
|
+
let(:literal_results) do
|
164
|
+
deep_double(
|
165
|
+
methA: {[] => literal(literal_hash)},
|
166
|
+
methB: {[] => literal(literal_proc)},
|
167
|
+
)
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'provides a way to return a literal Hash' do
|
171
|
+
expect(literal_results.methA).to eq(literal_hash)
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'provides a way to return a literal Proc' do
|
175
|
+
expect(literal_results.methB).to eq(literal_proc)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "rspec"
|
3
|
+
require "deep_double"
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
# Enable flags like --only-failures and --next-failure
|
7
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
8
|
+
|
9
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
10
|
+
config.disable_monkey_patching!
|
11
|
+
|
12
|
+
config.expect_with :rspec do |c|
|
13
|
+
c.syntax = :expect
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deep_double
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- jonahx@gmail.com
|
58
|
+
executables:
|
59
|
+
- console
|
60
|
+
- setup
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- deep_double.gemspec
|
75
|
+
- lib/deep_double.rb
|
76
|
+
- lib/deep_double/function.rb
|
77
|
+
- lib/deep_double/function/validate_result_table.rb
|
78
|
+
- lib/deep_double/literal.rb
|
79
|
+
- lib/deep_double/recursive_function.rb
|
80
|
+
- lib/deep_double/version.rb
|
81
|
+
- spec/deep_double_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: https://github.com/jonahx/deep_double
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.7.6
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Fast, declarative test doubles that support nesting.
|
107
|
+
test_files: []
|