mutations 0.5.12 → 0.6.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.
- data/{.travis → .travis.yml} +3 -0
- data/CHANGELOG.md +10 -2
- data/Gemfile +5 -2
- data/README.md +1 -0
- data/TODO +1 -3
- data/lib/mutations.rb +14 -1
- data/lib/mutations/additional_filter.rb +13 -0
- data/lib/mutations/array_filter.rb +12 -30
- data/lib/mutations/boolean_filter.rb +3 -3
- data/lib/mutations/command.rb +5 -4
- data/lib/mutations/date_filter.rb +52 -0
- data/lib/mutations/duck_filter.rb +4 -4
- data/lib/mutations/errors.rb +24 -20
- data/lib/mutations/exception.rb +1 -1
- data/lib/mutations/file_filter.rb +7 -7
- data/lib/mutations/float_filter.rb +5 -5
- data/lib/mutations/hash_filter.rb +12 -29
- data/lib/mutations/integer_filter.rb +5 -5
- data/lib/mutations/model_filter.rb +13 -8
- data/lib/mutations/outcome.rb +1 -1
- data/lib/mutations/string_filter.rb +10 -10
- data/lib/mutations/version.rb +2 -2
- data/mutations.gemspec +0 -2
- data/spec/additional_filter_spec.rb +76 -0
- data/spec/array_filter_spec.rb +25 -17
- data/spec/boolean_filter_spec.rb +5 -5
- data/spec/command_spec.rb +30 -30
- data/spec/date_filter_spec.rb +146 -0
- data/spec/default_spec.rb +10 -10
- data/spec/duck_filter_spec.rb +6 -6
- data/spec/errors_spec.rb +8 -9
- data/spec/file_filter_spec.rb +26 -22
- data/spec/float_filter_spec.rb +8 -8
- data/spec/hash_filter_spec.rb +31 -23
- data/spec/inheritance_spec.rb +9 -9
- data/spec/integer_filter_spec.rb +9 -9
- data/spec/model_filter_spec.rb +26 -6
- data/spec/mutations_spec.rb +1 -1
- data/spec/simple_command.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/string_filter_spec.rb +17 -17
- metadata +8 -4
@@ -1,7 +1,16 @@
|
|
1
1
|
module Mutations
|
2
2
|
class HashFilter < InputFilter
|
3
|
+
def self.register_additional_filter(type_class, type_name)
|
4
|
+
define_method(type_name) do | *args |
|
5
|
+
name = args[0]
|
6
|
+
options = args[1] || {}
|
7
|
+
|
8
|
+
@current_inputs[name.to_sym] = type_class.new(options)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
3
12
|
@default_options = {
|
4
|
-
nils
|
13
|
+
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
|
5
14
|
}
|
6
15
|
|
7
16
|
attr_accessor :optional_inputs
|
@@ -48,38 +57,12 @@ module Mutations
|
|
48
57
|
@optional_inputs.keys
|
49
58
|
end
|
50
59
|
|
51
|
-
# Basic types:
|
52
|
-
def string(name, options = {})
|
53
|
-
@current_inputs[name.to_sym] = StringFilter.new(options)
|
54
|
-
end
|
55
|
-
|
56
|
-
def integer(name, options = {})
|
57
|
-
@current_inputs[name.to_sym] = IntegerFilter.new(options)
|
58
|
-
end
|
59
|
-
|
60
|
-
def float(name, options = {})
|
61
|
-
@current_inputs[name.to_sym] = FloatFilter.new(options)
|
62
|
-
end
|
63
|
-
|
64
|
-
def boolean(name, options = {})
|
65
|
-
@current_inputs[name.to_sym] = BooleanFilter.new(options)
|
66
|
-
end
|
67
|
-
|
68
|
-
def duck(name, options = {})
|
69
|
-
@current_inputs[name.to_sym] = DuckFilter.new(options)
|
70
|
-
end
|
71
|
-
|
72
|
-
def file(name, options = {})
|
73
|
-
@current_inputs[name.to_sym] = FileFilter.new(options)
|
74
|
-
end
|
75
|
-
|
76
60
|
def hash(name, options = {}, &block)
|
77
61
|
@current_inputs[name.to_sym] = HashFilter.new(options, &block)
|
78
62
|
end
|
79
63
|
|
80
64
|
def model(name, options = {})
|
81
|
-
|
82
|
-
@current_inputs[name_sym] = ModelFilter.new(name_sym, options)
|
65
|
+
@current_inputs[name.to_sym] = ModelFilter.new(name.to_sym, options)
|
83
66
|
end
|
84
67
|
|
85
68
|
def array(name, options = {}, &block)
|
@@ -170,4 +153,4 @@ module Mutations
|
|
170
153
|
end
|
171
154
|
end
|
172
155
|
end
|
173
|
-
end
|
156
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Mutations
|
2
|
-
class IntegerFilter <
|
2
|
+
class IntegerFilter < AdditionalFilter
|
3
3
|
@default_options = {
|
4
|
-
nils
|
5
|
-
min
|
6
|
-
max
|
7
|
-
in
|
4
|
+
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
|
5
|
+
:min => nil, # lowest value, inclusive
|
6
|
+
:max => nil, # highest value, inclusive
|
7
|
+
:in => nil # Can be an array like %w(3 4 5)
|
8
8
|
}
|
9
9
|
|
10
10
|
def filter(data)
|
@@ -1,35 +1,40 @@
|
|
1
1
|
module Mutations
|
2
2
|
class ModelFilter < InputFilter
|
3
3
|
@default_options = {
|
4
|
-
nils
|
5
|
-
class
|
6
|
-
builder
|
7
|
-
new_records
|
4
|
+
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
|
5
|
+
:class => nil, # default is the attribute name.to_s.camelize.constantize. This overrides it with class or class.constantize
|
6
|
+
:builder => nil, # Could be a class or a string which will be constantized. If present, and a hash is passed, then we use that to construct a model
|
7
|
+
:new_records => false, # If false, unsaved AR records are not valid. Things that don't respond to new_record? are valid. true: anything is valid
|
8
8
|
}
|
9
9
|
|
10
10
|
def initialize(name, opts = {})
|
11
11
|
super(opts)
|
12
12
|
@name = name
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
# Initialize the model class and builder
|
16
16
|
def initialize_constants!
|
17
17
|
@initialize_constants ||= begin
|
18
18
|
class_const = options[:class] || @name.to_s.camelize
|
19
19
|
class_const = class_const.constantize if class_const.is_a?(String)
|
20
|
-
|
20
|
+
options[:class] = class_const
|
21
21
|
|
22
22
|
if options[:builder]
|
23
23
|
options[:builder] = options[:builder].constantize if options[:builder].is_a?(String)
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
true
|
27
27
|
end
|
28
|
+
|
29
|
+
unless Mutations.cache_constants?
|
30
|
+
options[:class] = options[:class].to_s.constantize if options[:class]
|
31
|
+
options[:builder] = options[:builder].to_s.constantize if options[:builder]
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
35
|
def filter(data)
|
31
36
|
initialize_constants!
|
32
|
-
|
37
|
+
|
33
38
|
# Handle nil case
|
34
39
|
if data.nil?
|
35
40
|
return [nil, nil] if options[:nils]
|
data/lib/mutations/outcome.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
module Mutations
|
2
|
-
class StringFilter <
|
2
|
+
class StringFilter < AdditionalFilter
|
3
3
|
@default_options = {
|
4
|
-
strip
|
5
|
-
strict
|
6
|
-
nils
|
7
|
-
empty
|
8
|
-
min_length
|
9
|
-
max_length
|
10
|
-
matches
|
11
|
-
in
|
12
|
-
discard_empty
|
4
|
+
:strip => true, # true calls data.strip if data is a string
|
5
|
+
:strict => false, # If false, then symbols, numbers, and booleans are converted to a string with to_s. # TODO: TEST
|
6
|
+
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
|
7
|
+
:empty => false, # false disallows "". true allows "" and overrides any other validations (b/c they couldn't be true if it's empty)
|
8
|
+
:min_length => nil, # Can be a number like 5, meaning that 5 codepoints are required
|
9
|
+
:max_length => nil, # Can be a number like 10, meaning that at most 10 codepoints are permitted
|
10
|
+
:matches => nil, # Can be a regexp
|
11
|
+
:in => nil, # Can be an array like %w(red blue green)
|
12
|
+
:discard_empty => false # If the param is optional, discard_empty: true drops empty fields.
|
13
13
|
}
|
14
14
|
|
15
15
|
def filter(data)
|
data/lib/mutations/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module Mutations
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
2
|
+
VERSION = "0.6.0"
|
3
|
+
end
|
data/mutations.gemspec
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Mutations::AdditionalFilter" do
|
4
|
+
|
5
|
+
describe "Additional Filter" do
|
6
|
+
module Mutations
|
7
|
+
class SometestFilter < Mutations::AdditionalFilter
|
8
|
+
@default_options = {
|
9
|
+
:nils => false
|
10
|
+
}
|
11
|
+
|
12
|
+
def filter(data)
|
13
|
+
return [data, nil]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class TestCommandUsingAdditionalFilters < Mutations::Command
|
19
|
+
required do
|
20
|
+
sometest :first_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def execute
|
24
|
+
{ :first_name => first_name }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should recognize additional filters" do
|
29
|
+
outcome = TestCommandUsingAdditionalFilters.run(:first_name => "John")
|
30
|
+
assert outcome.success?
|
31
|
+
assert_equal nil, outcome.errors
|
32
|
+
end
|
33
|
+
|
34
|
+
class TestCommandUsingAdditionalFiltersInHashes < Mutations::Command
|
35
|
+
required do
|
36
|
+
hash :a_hash do
|
37
|
+
sometest :first_name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def execute
|
42
|
+
{ :a_hash => a_hash }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be useable in hashes" do
|
47
|
+
outcome = TestCommandUsingAdditionalFiltersInHashes.run(
|
48
|
+
:a_hash => { :first_name => "John" }
|
49
|
+
)
|
50
|
+
|
51
|
+
assert outcome.success?
|
52
|
+
assert_equal nil, outcome.errors
|
53
|
+
end
|
54
|
+
|
55
|
+
class TestCommandUsingAdditionalFiltersInArrays < Mutations::Command
|
56
|
+
required do
|
57
|
+
array :an_array do
|
58
|
+
sometest
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def execute
|
63
|
+
{ :an_array => an_array }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be useable in arrays" do
|
68
|
+
outcome = TestCommandUsingAdditionalFiltersInArrays.run(
|
69
|
+
:an_array => [ "John", "Bill" ]
|
70
|
+
)
|
71
|
+
|
72
|
+
assert outcome.success?
|
73
|
+
assert_equal nil, outcome.errors
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/array_filter_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'stringio'
|
3
3
|
|
4
4
|
describe "Mutations::ArrayFilter" do
|
@@ -12,42 +12,42 @@ describe "Mutations::ArrayFilter" do
|
|
12
12
|
|
13
13
|
it "considers non-arrays to be invalid" do
|
14
14
|
f = Mutations::ArrayFilter.new(:arr)
|
15
|
-
['hi', true, 1, {a
|
15
|
+
['hi', true, 1, {:a => "1"}, Object.new].each do |thing|
|
16
16
|
filtered, errors = f.filter(thing)
|
17
17
|
assert_equal :array, errors
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
it "considers nil to be invalid" do
|
22
|
-
f = Mutations::ArrayFilter.new(:arr, nils
|
22
|
+
f = Mutations::ArrayFilter.new(:arr, :nils => false)
|
23
23
|
filtered, errors = f.filter(nil)
|
24
24
|
assert_equal nil, filtered
|
25
25
|
assert_equal :nils, errors
|
26
26
|
end
|
27
27
|
|
28
28
|
it "considers nil to be valid" do
|
29
|
-
f = Mutations::ArrayFilter.new(:arr, nils
|
29
|
+
f = Mutations::ArrayFilter.new(:arr, :nils => true)
|
30
30
|
filtered, errors = f.filter(nil)
|
31
31
|
filtered, errors = f.filter(nil)
|
32
32
|
assert_equal nil, errors
|
33
33
|
end
|
34
34
|
|
35
35
|
it "lets you specify a class, and has valid elements" do
|
36
|
-
f = Mutations::ArrayFilter.new(:arr, class
|
36
|
+
f = Mutations::ArrayFilter.new(:arr, :class => Fixnum)
|
37
37
|
filtered, errors = f.filter([1,2,3])
|
38
38
|
assert_equal nil, errors
|
39
39
|
assert_equal [1,2,3], filtered
|
40
40
|
end
|
41
41
|
|
42
42
|
it "lets you specify a class as a string, and has valid elements" do
|
43
|
-
f = Mutations::ArrayFilter.new(:arr, class
|
43
|
+
f = Mutations::ArrayFilter.new(:arr, :class => 'Fixnum')
|
44
44
|
filtered, errors = f.filter([1,2,3])
|
45
45
|
assert_equal nil, errors
|
46
46
|
assert_equal [1,2,3], filtered
|
47
47
|
end
|
48
48
|
|
49
49
|
it "lets you specify a class, and has invalid elements" do
|
50
|
-
f = Mutations::ArrayFilter.new(:arr, class
|
50
|
+
f = Mutations::ArrayFilter.new(:arr, :class => Fixnum)
|
51
51
|
filtered, errors = f.filter([1, "bob"])
|
52
52
|
assert_equal [nil, :class], errors.symbolic
|
53
53
|
assert_equal [1,"bob"], filtered
|
@@ -56,13 +56,13 @@ describe "Mutations::ArrayFilter" do
|
|
56
56
|
it "lets you use a block to supply an element filter" do
|
57
57
|
f = Mutations::ArrayFilter.new(:arr) { string }
|
58
58
|
|
59
|
-
filtered, errors = f.filter(["hi", {stuff
|
59
|
+
filtered, errors = f.filter(["hi", {:stuff => "ok"}])
|
60
60
|
assert_nil errors[0]
|
61
61
|
assert_equal :string, errors[1].symbolic
|
62
62
|
end
|
63
63
|
|
64
64
|
it "lets you array-ize everything" do
|
65
|
-
f = Mutations::ArrayFilter.new(:arr, arrayize
|
65
|
+
f = Mutations::ArrayFilter.new(:arr, :arrayize => true) { string }
|
66
66
|
|
67
67
|
filtered, errors = f.filter("foo")
|
68
68
|
assert_equal ["foo"], filtered
|
@@ -70,7 +70,7 @@ describe "Mutations::ArrayFilter" do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
it "lets you array-ize an empty string" do
|
73
|
-
f = Mutations::ArrayFilter.new(:arr, arrayize
|
73
|
+
f = Mutations::ArrayFilter.new(:arr, :arrayize => true) { string }
|
74
74
|
|
75
75
|
filtered, errors = f.filter("")
|
76
76
|
assert_equal [], filtered
|
@@ -78,7 +78,7 @@ describe "Mutations::ArrayFilter" do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
it "lets you pass integers in arrays" do
|
81
|
-
f = Mutations::ArrayFilter.new(:arr) { integer min
|
81
|
+
f = Mutations::ArrayFilter.new(:arr) { integer :min => 4 }
|
82
82
|
|
83
83
|
filtered, errors = f.filter([5,6,1,"bob"])
|
84
84
|
assert_equal [5,6,1,"bob"], filtered
|
@@ -86,21 +86,29 @@ describe "Mutations::ArrayFilter" do
|
|
86
86
|
end
|
87
87
|
|
88
88
|
it "lets you pass floats in arrays" do
|
89
|
-
f = Mutations::ArrayFilter.new(:float) { float min
|
89
|
+
f = Mutations::ArrayFilter.new(:float) { float :min => 4.0 }
|
90
90
|
|
91
91
|
filtered, errors = f.filter([5.0,6.0,1.0,"bob"])
|
92
92
|
assert_equal [5.0,6.0,1.0,"bob"], filtered
|
93
93
|
assert_equal [nil, nil, :min, :float], errors.symbolic
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
it "lets you pass ducks in arrays" do
|
97
|
-
f = Mutations::ArrayFilter.new(:arr) { duck(methods
|
97
|
+
f = Mutations::ArrayFilter.new(:arr) { duck(:methods => :length) }
|
98
98
|
|
99
99
|
filtered, errors = f.filter(["hi", [1], true])
|
100
100
|
assert_equal ["hi", [1], true], filtered
|
101
101
|
assert_equal [nil, nil, :duck], errors.symbolic
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
|
+
it "lets you pass dates in arrays" do
|
105
|
+
f = Mutations::ArrayFilter.new(:arr) {date(:format => "%Y-%m-%d")}
|
106
|
+
|
107
|
+
filtered, errors = f.filter(["2000-1-1", Date.new(2000, 1, 1), "2000-20-1"])
|
108
|
+
assert_equal ["2000-1-1", Date.new(2000, 1, 1), "2000-20-1"], filtered
|
109
|
+
assert_equal [nil, nil, :date], errors.symbolic
|
110
|
+
end
|
111
|
+
|
104
112
|
it "lets you pass files in arrays" do
|
105
113
|
sio = StringIO.new("bob")
|
106
114
|
f = Mutations::ArrayFilter.new(:arr) { file }
|
@@ -140,7 +148,7 @@ describe "Mutations::ArrayFilter" do
|
|
140
148
|
end
|
141
149
|
end
|
142
150
|
|
143
|
-
filtered, errors = f.filter([{foo
|
151
|
+
filtered, errors = f.filter([{:foo => "f", :bar => 3, :baz => true}, {:foo => "f", :bar => 3}, {:foo => "f"}])
|
144
152
|
assert_equal [{:foo=>"f", :bar=>3, :baz=>true}, {:foo=>"f", :bar=>3}, {:foo=>"f"}], filtered
|
145
153
|
|
146
154
|
assert_equal nil, errors[0]
|
@@ -173,4 +181,4 @@ describe "Mutations::ArrayFilter" do
|
|
173
181
|
assert_equal ["Array[2] isn't a string", "Array[0] can't be blank"], errors.message_list
|
174
182
|
end
|
175
183
|
|
176
|
-
end
|
184
|
+
end
|
data/spec/boolean_filter_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Mutations::BooleanFilter" do
|
4
4
|
|
@@ -15,21 +15,21 @@ describe "Mutations::BooleanFilter" do
|
|
15
15
|
|
16
16
|
it "considers non-booleans to be invalid" do
|
17
17
|
f = Mutations::BooleanFilter.new
|
18
|
-
[[true], {a
|
18
|
+
[[true], {:a => "1"}, Object.new].each do |thing|
|
19
19
|
filtered, errors = f.filter(thing)
|
20
20
|
assert_equal :boolean, errors
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
it "considers nil to be invalid" do
|
25
|
-
f = Mutations::BooleanFilter.new(nils
|
25
|
+
f = Mutations::BooleanFilter.new(:nils => false)
|
26
26
|
filtered, errors = f.filter(nil)
|
27
27
|
assert_equal nil, filtered
|
28
28
|
assert_equal :nils, errors
|
29
29
|
end
|
30
30
|
|
31
31
|
it "considers nil to be valid" do
|
32
|
-
f = Mutations::BooleanFilter.new(nils
|
32
|
+
f = Mutations::BooleanFilter.new(:nils => true)
|
33
33
|
filtered, errors = f.filter(nil)
|
34
34
|
assert_equal nil, filtered
|
35
35
|
assert_equal nil, errors
|
@@ -52,4 +52,4 @@ describe "Mutations::BooleanFilter" do
|
|
52
52
|
assert_equal :boolean, errors
|
53
53
|
end
|
54
54
|
end
|
55
|
-
end
|
55
|
+
end
|
data/spec/command_spec.rb
CHANGED
@@ -1,67 +1,67 @@
|
|
1
|
-
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'simple_command'
|
3
3
|
|
4
4
|
describe "Command" do
|
5
5
|
|
6
6
|
describe "SimpleCommand" do
|
7
7
|
it "should allow valid in put in" do
|
8
|
-
outcome = SimpleCommand.run(name
|
8
|
+
outcome = SimpleCommand.run(:name => "John", :email => "john@gmail.com", :amount => 5)
|
9
9
|
|
10
10
|
assert outcome.success?
|
11
|
-
assert_equal ({name
|
11
|
+
assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, outcome.result
|
12
12
|
assert_equal nil, outcome.errors
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should filter out spurious params" do
|
16
|
-
outcome = SimpleCommand.run(name
|
16
|
+
outcome = SimpleCommand.run(:name => "John", :email => "john@gmail.com", :amount => 5, :buggers => true)
|
17
17
|
|
18
18
|
assert outcome.success?
|
19
|
-
assert_equal ({name
|
19
|
+
assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, outcome.result
|
20
20
|
assert_equal nil, outcome.errors
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should discover errors in inputs" do
|
24
|
-
outcome = SimpleCommand.run(name
|
24
|
+
outcome = SimpleCommand.run(:name => "JohnTooLong", :email => "john@gmail.com")
|
25
25
|
|
26
26
|
assert !outcome.success?
|
27
27
|
assert_equal :max_length, outcome.errors.symbolic[:name]
|
28
28
|
end
|
29
29
|
|
30
30
|
it "shouldn't throw an exception with run!" do
|
31
|
-
result = SimpleCommand.run!(name
|
32
|
-
assert_equal ({name
|
31
|
+
result = SimpleCommand.run!(:name => "John", :email => "john@gmail.com", :amount => 5)
|
32
|
+
assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, result
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should throw an exception with run!" do
|
36
36
|
assert_raises Mutations::ValidationException do
|
37
|
-
result = SimpleCommand.run!(name
|
37
|
+
result = SimpleCommand.run!(:name => "John", :email => "john@gmail.com", :amount => "bob")
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should do standalone validation" do
|
42
|
-
outcome = SimpleCommand.validate(name
|
42
|
+
outcome = SimpleCommand.validate(:name => "JohnLong", :email => "john@gmail.com")
|
43
43
|
assert outcome.success?
|
44
44
|
assert_nil outcome.result
|
45
45
|
assert_nil outcome.errors
|
46
46
|
|
47
|
-
outcome = SimpleCommand.validate(name
|
47
|
+
outcome = SimpleCommand.validate(:name => "JohnTooLong", :email => "john@gmail.com")
|
48
48
|
assert !outcome.success?
|
49
49
|
assert_nil outcome.result
|
50
50
|
assert_equal :max_length, outcome.errors.symbolic[:name]
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should merge multiple hashes" do
|
54
|
-
outcome = SimpleCommand.run({name
|
54
|
+
outcome = SimpleCommand.run({:name => "John", :email => "john@gmail.com"}, {:email => "bob@jones.com", :amount => 5})
|
55
55
|
|
56
56
|
assert outcome.success?
|
57
|
-
assert_equal ({name
|
57
|
+
assert_equal ({:name => "John", :email => "bob@jones.com", :amount => 5}).stringify_keys, outcome.result
|
58
58
|
end
|
59
59
|
|
60
60
|
it "should merge hashes indifferently" do
|
61
|
-
outcome = SimpleCommand.run({name
|
61
|
+
outcome = SimpleCommand.run({:name => "John", :email => "john@gmail.com"}, {"email" => "bob@jones.com", "amount" => 5})
|
62
62
|
|
63
63
|
assert outcome.success?
|
64
|
-
assert_equal ({name
|
64
|
+
assert_equal ({:name => "John", :email => "bob@jones.com", :amount => 5}).stringify_keys, outcome.result
|
65
65
|
end
|
66
66
|
|
67
67
|
it "shouldn't accept non-hashes" do
|
@@ -74,17 +74,17 @@ describe "Command" do
|
|
74
74
|
end
|
75
75
|
|
76
76
|
assert_raises ArgumentError do
|
77
|
-
outcome = SimpleCommand.run({name
|
77
|
+
outcome = SimpleCommand.run({:name => "John"}, 1)
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should accept nothing at all" do
|
82
82
|
SimpleCommand.run # make sure nothing is raised
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
it "should return the filtered inputs in the outcome" do
|
86
|
-
outcome = SimpleCommand.run(name
|
87
|
-
assert_equal ({name
|
86
|
+
outcome = SimpleCommand.run(:name => " John ", :email => "john@gmail.com", :amount => "5")
|
87
|
+
assert_equal ({:name => "John", :email => "john@gmail.com", :amount => 5}).stringify_keys, outcome.inputs
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
@@ -95,13 +95,13 @@ describe "Command" do
|
|
95
95
|
optional { string :email }
|
96
96
|
|
97
97
|
def execute
|
98
|
-
{name
|
98
|
+
{:name => name, :email => email}
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
102
102
|
it "should define getter methods on params" do
|
103
|
-
mutation = EigenCommand.run(name
|
104
|
-
assert_equal ({name
|
103
|
+
mutation = EigenCommand.run(:name => "John", :email => "john@gmail.com")
|
104
|
+
assert_equal ({:name => "John", :email => "john@gmail.com"}), mutation.result
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -113,13 +113,13 @@ describe "Command" do
|
|
113
113
|
|
114
114
|
def execute
|
115
115
|
self.name, self.email = "bob", "bob@jones.com"
|
116
|
-
{name
|
116
|
+
{:name => inputs[:name], :email => inputs[:email]}
|
117
117
|
end
|
118
118
|
end
|
119
119
|
|
120
120
|
it "should define setter methods on params" do
|
121
|
-
mutation = MutatatedCommand.run(name
|
122
|
-
assert_equal ({name
|
121
|
+
mutation = MutatatedCommand.run(:name => "John", :email => "john@gmail.com")
|
122
|
+
assert_equal ({:name => "bob", :email => "bob@jones.com"}), mutation.result
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
@@ -137,7 +137,7 @@ describe "Command" do
|
|
137
137
|
end
|
138
138
|
|
139
139
|
it "should let you add errors" do
|
140
|
-
outcome = ErrorfulCommand.run(name
|
140
|
+
outcome = ErrorfulCommand.run(:name => "John", :email => "john@gmail.com")
|
141
141
|
|
142
142
|
assert !outcome.success?
|
143
143
|
assert_nil outcome.result
|
@@ -163,7 +163,7 @@ describe "Command" do
|
|
163
163
|
end
|
164
164
|
|
165
165
|
it "should let you merge errors" do
|
166
|
-
outcome = ErrorfulCommand.run(name
|
166
|
+
outcome = ErrorfulCommand.run(:name => "John", :email => "john@gmail.com")
|
167
167
|
|
168
168
|
assert !outcome.success?
|
169
169
|
assert_nil outcome.result
|
@@ -194,9 +194,9 @@ describe "Command" do
|
|
194
194
|
end
|
195
195
|
|
196
196
|
it "should handle *_present? methods" do
|
197
|
-
assert_equal 1, PresentCommand.run!(name
|
198
|
-
assert_equal 2, PresentCommand.run!(email
|
199
|
-
assert_equal 3, PresentCommand.run!(name
|
197
|
+
assert_equal 1, PresentCommand.run!(:name => "John", :email => "john@gmail.com")
|
198
|
+
assert_equal 2, PresentCommand.run!(:email => "john@gmail.com")
|
199
|
+
assert_equal 3, PresentCommand.run!(:name => "John")
|
200
200
|
assert_equal 4, PresentCommand.run!
|
201
201
|
end
|
202
202
|
end
|