deep_double 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/deep_double.rb +6 -6
- data/lib/deep_double/fake_method.rb +40 -0
- data/lib/deep_double/fake_method/validate_stubbed_values.rb +44 -0
- data/lib/deep_double/{recursive_function.rb → recursive_fake_method.rb} +5 -5
- data/lib/deep_double/version.rb +1 -1
- metadata +5 -5
- data/lib/deep_double/function.rb +0 -46
- data/lib/deep_double/function/validate_result_table.rb +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2d86400433bcf2186dbab75a25cc58151a8934d1c672d2d3d1692d895ddafd3
|
4
|
+
data.tar.gz: 8493e72d2bc1444d2eb3b4185b9068ae8ad33373738f0333e52b41e4963187e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb4a27a46a3520bf82b1efb7e120ecd0dc7d416beef4be394539a3e0ceeba0ab6360e97986f59ce5b0732ee8d45f9ad4e1c770183309447c8847b0379585140b
|
7
|
+
data.tar.gz: 3b4f0744335a8a49045a2f7cb8d92a019079dad84a8e73519a12b8b384474020fd0b37c12035651771b52d8a0872f1717bb4a69ba30522a3cbf6fbdf9b84218c
|
data/lib/deep_double.rb
CHANGED
@@ -4,9 +4,9 @@
|
|
4
4
|
# It drastically reduces the amount of boilerplate compared with rspec.
|
5
5
|
#
|
6
6
|
|
7
|
-
require 'deep_double/
|
7
|
+
require 'deep_double/fake_method'
|
8
8
|
require 'deep_double/literal'
|
9
|
-
require 'deep_double/
|
9
|
+
require 'deep_double/recursive_fake_method'
|
10
10
|
require "deep_double/version"
|
11
11
|
|
12
12
|
module DeepDouble
|
@@ -45,12 +45,12 @@ module DeepDouble
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def create_method(meth)
|
48
|
-
define_singleton_method(meth.to_sym, &
|
48
|
+
define_singleton_method(meth.to_sym, &fake_method(meth))
|
49
49
|
end
|
50
50
|
|
51
|
-
def
|
52
|
-
raw_fn =
|
53
|
-
|
51
|
+
def fake_method(meth)
|
52
|
+
raw_fn = FakeMethod.new(@definition[meth])
|
53
|
+
RecursiveFakeMethod.new(raw_fn)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'deep_double/fake_method/validate_stubbed_values'
|
2
|
+
|
3
|
+
# Takes a Hash of stubbed values to create a fake method. The stubbed values map
|
4
|
+
# lists of arguments (represented as arrays, with the empty array representing a
|
5
|
+
# method that takes no args) to return values.
|
6
|
+
#
|
7
|
+
|
8
|
+
module DeepDouble
|
9
|
+
class FakeMethod
|
10
|
+
|
11
|
+
def initialize(stubbed_values)
|
12
|
+
@stubbed_values = stubbed_values
|
13
|
+
validate_stubbed_values
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(*args)
|
17
|
+
result(args)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def validate_stubbed_values
|
23
|
+
ValidateStubbedValues.new(@stubbed_values).call
|
24
|
+
end
|
25
|
+
|
26
|
+
def result(args)
|
27
|
+
validate_result_exists(args)
|
28
|
+
@stubbed_values.key?(args) ? @stubbed_values[args] : @stubbed_values[:default]
|
29
|
+
end
|
30
|
+
|
31
|
+
def validate_result_exists(args)
|
32
|
+
return if result_exists?(args)
|
33
|
+
raise ArgumentError, "FakeMethod not defined for args: #{args}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def result_exists?(args)
|
37
|
+
@stubbed_values.key?(args) || @stubbed_values.key?(:default)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# Validates that the structure of a stubbed values Hash is valid:
|
2
|
+
# - It must be a Hash
|
3
|
+
# - It's keys must be Arrays or the special value ":default"
|
4
|
+
#
|
5
|
+
module DeepDouble
|
6
|
+
class FakeMethod
|
7
|
+
|
8
|
+
class ValidateStubbedValues
|
9
|
+
def initialize(stubbed_values)
|
10
|
+
@stubbed_values = stubbed_values
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
validate_type
|
15
|
+
validate_keys
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def validate_type
|
21
|
+
return if @stubbed_values.is_a?(Hash)
|
22
|
+
raise ArgumentError,
|
23
|
+
"The stubbed values defining a FakeMethod must be a Hash"
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_keys
|
27
|
+
@stubbed_values.keys.each { |key| validate_key(key) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_key(key)
|
31
|
+
return if valid_key?(key)
|
32
|
+
raise ArgumentError,
|
33
|
+
"Keys in a stubbed values Hash must by Arrays representing " +
|
34
|
+
"argument lists (the empty array represents 0 arguments). The " +
|
35
|
+
"following key was invalid: #{key.inspect}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def valid_key?(key)
|
39
|
+
key.is_a?(Array) || key == :default
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
# Makes a `DeepDouble::
|
1
|
+
# Makes a `DeepDouble::FakeMethod` recursive, so that `Hash` results are
|
2
2
|
# automatically converted to `DeepDouble` instances in their own right.
|
3
3
|
#
|
4
4
|
module DeepDouble
|
5
|
-
class
|
5
|
+
class RecursiveFakeMethod
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@
|
7
|
+
def initialize(fake_method)
|
8
|
+
@fake_method = fake_method
|
9
9
|
end
|
10
10
|
|
11
11
|
def call(*args)
|
12
|
-
result = @
|
12
|
+
result = @fake_method.call(*args)
|
13
13
|
special_case_transforms(result)
|
14
14
|
end
|
15
15
|
|
data/lib/deep_double/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_double
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,10 +73,10 @@ files:
|
|
73
73
|
- bin/setup
|
74
74
|
- deep_double.gemspec
|
75
75
|
- lib/deep_double.rb
|
76
|
-
- lib/deep_double/
|
77
|
-
- lib/deep_double/
|
76
|
+
- lib/deep_double/fake_method.rb
|
77
|
+
- lib/deep_double/fake_method/validate_stubbed_values.rb
|
78
78
|
- lib/deep_double/literal.rb
|
79
|
-
- lib/deep_double/
|
79
|
+
- lib/deep_double/recursive_fake_method.rb
|
80
80
|
- lib/deep_double/version.rb
|
81
81
|
- spec/deep_double_spec.rb
|
82
82
|
- spec/spec_helper.rb
|
data/lib/deep_double/function.rb
DELETED
@@ -1,46 +0,0 @@
|
|
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
|
@@ -1,47 +0,0 @@
|
|
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
|