mutant 0.8.24 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +3 -3
- data/Changelog.md +14 -654
- data/Gemfile +13 -0
- data/Gemfile.lock +59 -64
- data/LICENSE +271 -20
- data/README.md +73 -140
- data/Rakefile +0 -21
- data/bin/mutant +7 -2
- data/config/reek.yml +2 -1
- data/config/rubocop.yml +5 -9
- data/docs/incremental.md +76 -0
- data/docs/known-problems.md +0 -14
- data/docs/mutant-minitest.md +1 -1
- data/docs/mutant-rspec.md +2 -24
- data/lib/mutant.rb +45 -53
- data/lib/mutant/ast/nodes.rb +0 -2
- data/lib/mutant/ast/types.rb +1 -117
- data/lib/mutant/base.rb +192 -0
- data/lib/mutant/bootstrap.rb +145 -0
- data/lib/mutant/cli.rb +68 -54
- data/lib/mutant/config.rb +119 -6
- data/lib/mutant/env.rb +94 -8
- data/lib/mutant/expression.rb +6 -1
- data/lib/mutant/expression/parser.rb +9 -31
- data/lib/mutant/integration.rb +64 -36
- data/lib/mutant/isolation.rb +16 -1
- data/lib/mutant/isolation/fork.rb +105 -40
- data/lib/mutant/license.rb +34 -0
- data/lib/mutant/license/subscription.rb +47 -0
- data/lib/mutant/license/subscription/commercial.rb +57 -0
- data/lib/mutant/license/subscription/opensource.rb +77 -0
- data/lib/mutant/loader.rb +27 -4
- data/lib/mutant/matcher.rb +48 -1
- data/lib/mutant/matcher/chain.rb +1 -1
- data/lib/mutant/matcher/config.rb +0 -2
- data/lib/mutant/matcher/filter.rb +1 -1
- data/lib/mutant/matcher/method.rb +11 -7
- data/lib/mutant/matcher/methods.rb +1 -1
- data/lib/mutant/matcher/namespace.rb +1 -1
- data/lib/mutant/matcher/null.rb +1 -1
- data/lib/mutant/matcher/scope.rb +1 -1
- data/lib/mutant/meta/example/dsl.rb +0 -8
- data/lib/mutant/mutation.rb +1 -2
- data/lib/mutant/mutator/node.rb +2 -9
- data/lib/mutant/mutator/node/arguments.rb +1 -1
- data/lib/mutant/mutator/node/class.rb +0 -8
- data/lib/mutant/mutator/node/define.rb +0 -12
- data/lib/mutant/mutator/node/generic.rb +30 -44
- data/lib/mutant/mutator/node/index.rb +4 -4
- data/lib/mutant/mutator/node/literal/regex.rb +0 -39
- data/lib/mutant/mutator/node/send.rb +13 -12
- data/lib/mutant/parallel.rb +61 -40
- data/lib/mutant/parallel/driver.rb +59 -0
- data/lib/mutant/parallel/source.rb +6 -2
- data/lib/mutant/parallel/worker.rb +63 -45
- data/lib/mutant/range.rb +15 -0
- data/lib/mutant/reporter/cli.rb +5 -11
- data/lib/mutant/reporter/cli/format.rb +3 -46
- data/lib/mutant/reporter/cli/printer/config.rb +5 -6
- data/lib/mutant/reporter/cli/printer/env.rb +40 -0
- data/lib/mutant/reporter/cli/printer/env_progress.rb +13 -17
- data/lib/mutant/reporter/cli/printer/isolation_result.rb +17 -3
- data/lib/mutant/reporter/cli/printer/mutation_result.rb +2 -3
- data/lib/mutant/reporter/cli/printer/status_progressive.rb +19 -10
- data/lib/mutant/repository.rb +0 -65
- data/lib/mutant/repository/diff.rb +104 -0
- data/lib/mutant/repository/diff/ranges.rb +52 -0
- data/lib/mutant/result.rb +16 -7
- data/lib/mutant/runner.rb +38 -47
- data/lib/mutant/runner/sink.rb +1 -1
- data/lib/mutant/selector/null.rb +19 -0
- data/lib/mutant/subject.rb +3 -1
- data/lib/mutant/subject/method/instance.rb +3 -1
- data/lib/mutant/transform.rb +511 -0
- data/lib/mutant/variable.rb +282 -0
- data/lib/mutant/version.rb +1 -1
- data/lib/mutant/warnings.rb +113 -0
- data/meta/case.rb +1 -0
- data/meta/class.rb +0 -9
- data/meta/def.rb +1 -26
- data/meta/regexp.rb +10 -20
- data/meta/send.rb +14 -46
- data/mutant-minitest.gemspec +1 -1
- data/mutant-rspec.gemspec +2 -2
- data/mutant.gemspec +15 -16
- data/mutant.yml +6 -0
- data/spec/integration/mutant/isolation/fork_spec.rb +22 -5
- data/spec/integration/mutant/minitest_spec.rb +3 -2
- data/spec/integration/mutant/rspec_spec.rb +4 -3
- data/spec/integrations.yml +16 -13
- data/spec/shared/base_behavior.rb +45 -0
- data/spec/shared/framework_integration_behavior.rb +43 -14
- data/spec/spec_helper.rb +21 -17
- data/spec/support/corpus.rb +56 -95
- data/spec/support/shared_context.rb +37 -14
- data/spec/support/xspec.rb +7 -3
- data/spec/unit/mutant/bootstrap_spec.rb +216 -0
- data/spec/unit/mutant/cli_spec.rb +173 -117
- data/spec/unit/mutant/config_spec.rb +126 -0
- data/spec/unit/mutant/either_spec.rb +247 -0
- data/spec/unit/mutant/env_spec.rb +162 -40
- data/spec/unit/mutant/expression/method_spec.rb +16 -0
- data/spec/unit/mutant/expression/parser_spec.rb +29 -33
- data/spec/unit/mutant/expression_spec.rb +5 -7
- data/spec/unit/mutant/integration_spec.rb +100 -9
- data/spec/unit/mutant/isolation/fork_spec.rb +125 -67
- data/spec/unit/mutant/isolation/result_spec.rb +33 -1
- data/spec/unit/mutant/license_spec.rb +257 -0
- data/spec/unit/mutant/loader_spec.rb +50 -11
- data/spec/unit/mutant/matcher/compiler_spec.rb +0 -78
- data/spec/unit/mutant/matcher/method/instance_spec.rb +55 -11
- data/spec/unit/mutant/matcher/method/singleton_spec.rb +12 -2
- data/spec/unit/mutant/matcher_spec.rb +102 -0
- data/spec/unit/mutant/maybe_spec.rb +60 -0
- data/spec/unit/mutant/meta/example/dsl_spec.rb +1 -17
- data/spec/unit/mutant/mutation_spec.rb +13 -6
- data/spec/unit/mutant/parallel/driver_spec.rb +112 -14
- data/spec/unit/mutant/parallel/source/array_spec.rb +25 -17
- data/spec/unit/mutant/parallel/worker_spec.rb +182 -44
- data/spec/unit/mutant/parallel_spec.rb +105 -8
- data/spec/unit/mutant/range_spec.rb +141 -0
- data/spec/unit/mutant/reporter/cli/printer/config_spec.rb +7 -21
- data/spec/unit/mutant/reporter/cli/printer/env_progress_spec.rb +15 -6
- data/spec/unit/mutant/reporter/cli/printer/env_result_spec.rb +10 -2
- data/spec/unit/mutant/reporter/cli/printer/isolation_result_spec.rb +12 -4
- data/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb +31 -2
- data/spec/unit/mutant/reporter/cli/printer/status_progressive_spec.rb +4 -4
- data/spec/unit/mutant/reporter/cli/printer/subject_result_spec.rb +5 -0
- data/spec/unit/mutant/reporter/cli_spec.rb +46 -123
- data/spec/unit/mutant/repository/diff/ranges_spec.rb +180 -0
- data/spec/unit/mutant/repository/diff_spec.rb +84 -71
- data/spec/unit/mutant/require_highjack_spec.rb +1 -1
- data/spec/unit/mutant/result/env_spec.rb +39 -9
- data/spec/unit/mutant/result/test_spec.rb +14 -0
- data/spec/unit/mutant/runner_spec.rb +88 -41
- data/spec/unit/mutant/selector/expression_spec.rb +11 -10
- data/spec/unit/mutant/selector/null_spec.rb +17 -0
- data/spec/unit/mutant/subject/method/instance_spec.rb +44 -5
- data/spec/unit/mutant/subject/method/singleton_spec.rb +9 -2
- data/spec/unit/mutant/subject_spec.rb +9 -1
- data/spec/unit/mutant/transform/array_spec.rb +92 -0
- data/spec/unit/mutant/transform/bool_spec.rb +63 -0
- data/spec/unit/mutant/transform/error_spec.rb +132 -0
- data/spec/unit/mutant/transform/exception_spec.rb +44 -0
- data/spec/unit/mutant/transform/hash_spec.rb +236 -0
- data/spec/unit/mutant/transform/index_spec.rb +92 -0
- data/spec/unit/mutant/transform/named_spec.rb +49 -0
- data/spec/unit/mutant/transform/primitive_spec.rb +56 -0
- data/spec/unit/mutant/transform/sequence_spec.rb +98 -0
- data/spec/unit/mutant/variable_spec.rb +618 -0
- data/spec/unit/mutant/warnings_spec.rb +89 -0
- data/spec/unit/mutant/world_spec.rb +63 -0
- data/test_app/Gemfile.minitest +0 -2
- metadata +79 -113
- data/.gitattributes +0 -1
- data/.ruby-gemset +0 -1
- data/config/triage.yml +0 -2
- data/lib/mutant/actor.rb +0 -57
- data/lib/mutant/actor/env.rb +0 -31
- data/lib/mutant/actor/mailbox.rb +0 -34
- data/lib/mutant/actor/receiver.rb +0 -42
- data/lib/mutant/actor/sender.rb +0 -26
- data/lib/mutant/ast/meta/restarg.rb +0 -19
- data/lib/mutant/ast/regexp.rb +0 -42
- data/lib/mutant/ast/regexp/transformer.rb +0 -187
- data/lib/mutant/ast/regexp/transformer/direct.rb +0 -123
- data/lib/mutant/ast/regexp/transformer/named_group.rb +0 -59
- data/lib/mutant/ast/regexp/transformer/options_group.rb +0 -83
- data/lib/mutant/ast/regexp/transformer/quantifier.rb +0 -114
- data/lib/mutant/ast/regexp/transformer/recursive.rb +0 -58
- data/lib/mutant/ast/regexp/transformer/root.rb +0 -31
- data/lib/mutant/ast/regexp/transformer/text.rb +0 -60
- data/lib/mutant/env/bootstrap.rb +0 -160
- data/lib/mutant/matcher/compiler.rb +0 -60
- data/lib/mutant/mutator/node/regexp.rb +0 -35
- data/lib/mutant/mutator/node/regexp/alternation_meta.rb +0 -23
- data/lib/mutant/mutator/node/regexp/capture_group.rb +0 -28
- data/lib/mutant/mutator/node/regexp/character_type.rb +0 -32
- data/lib/mutant/mutator/node/regexp/end_of_line_anchor.rb +0 -23
- data/lib/mutant/mutator/node/regexp/end_of_string_or_before_end_of_line_anchor.rb +0 -23
- data/lib/mutant/mutator/node/regexp/greedy_zero_or_more.rb +0 -27
- data/lib/mutant/parallel/master.rb +0 -181
- data/lib/mutant/reporter/cli/printer/status.rb +0 -53
- data/lib/mutant/reporter/cli/tput.rb +0 -46
- data/lib/mutant/warning_filter.rb +0 -61
- data/meta/regexp/character_types.rb +0 -23
- data/meta/regexp/regexp_alternation_meta.rb +0 -13
- data/meta/regexp/regexp_bol_anchor.rb +0 -10
- data/meta/regexp/regexp_bos_anchor.rb +0 -18
- data/meta/regexp/regexp_capture_group.rb +0 -19
- data/meta/regexp/regexp_eol_anchor.rb +0 -10
- data/meta/regexp/regexp_eos_anchor.rb +0 -8
- data/meta/regexp/regexp_eos_ob_eol_anchor.rb +0 -10
- data/meta/regexp/regexp_greedy_zero_or_more.rb +0 -12
- data/meta/regexp/regexp_root_expression.rb +0 -10
- data/meta/restarg.rb +0 -10
- data/spec/support/fake_actor.rb +0 -111
- data/spec/support/warning.rb +0 -66
- data/spec/unit/mutant/actor/binding_spec.rb +0 -34
- data/spec/unit/mutant/actor/env_spec.rb +0 -31
- data/spec/unit/mutant/actor/mailbox_spec.rb +0 -28
- data/spec/unit/mutant/actor/message_spec.rb +0 -25
- data/spec/unit/mutant/actor/receiver_spec.rb +0 -58
- data/spec/unit/mutant/actor/sender_spec.rb +0 -24
- data/spec/unit/mutant/ast/regexp/parse_spec.rb +0 -19
- data/spec/unit/mutant/ast/regexp/transformer/lookup_table/table_spec.rb +0 -21
- data/spec/unit/mutant/ast/regexp/transformer/lookup_table_spec.rb +0 -35
- data/spec/unit/mutant/ast/regexp/transformer_spec.rb +0 -21
- data/spec/unit/mutant/ast/regexp_spec.rb +0 -704
- data/spec/unit/mutant/env/bootstrap_spec.rb +0 -188
- data/spec/unit/mutant/matcher/compiler/subject_prefix_spec.rb +0 -26
- data/spec/unit/mutant/parallel/master_spec.rb +0 -338
- data/spec/unit/mutant/reporter/cli/printer/status_spec.rb +0 -121
- data/spec/unit/mutant/reporter/cli/tput_spec.rb +0 -50
- data/spec/unit/mutant/warning_filter_spec.rb +0 -106
- data/spec/unit/mutant_spec.rb +0 -17
- data/test_app/Gemfile.rspec3.7 +0 -7
data/lib/mutant/runner/sink.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mutant
|
4
|
+
class Selector
|
5
|
+
# Selector that never returns tests
|
6
|
+
class Null < self
|
7
|
+
include Equalizer.new
|
8
|
+
|
9
|
+
# Tests for subject
|
10
|
+
#
|
11
|
+
# @param [Subject] subject
|
12
|
+
#
|
13
|
+
# @return [Enumerable<Test>]
|
14
|
+
def call(_subject)
|
15
|
+
EMPTY_ARRAY
|
16
|
+
end
|
17
|
+
end # Null
|
18
|
+
end # Selector
|
19
|
+
end # Mutant
|
data/lib/mutant/subject.rb
CHANGED
@@ -4,7 +4,9 @@ module Mutant
|
|
4
4
|
# Subject of a mutation
|
5
5
|
class Subject
|
6
6
|
include AbstractType, Adamantium::Flat, Enumerable
|
7
|
-
include
|
7
|
+
include Anima.new(:context, :node, :warnings)
|
8
|
+
|
9
|
+
private :warnings
|
8
10
|
|
9
11
|
# Mutations for this subject
|
10
12
|
#
|
@@ -0,0 +1,511 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mutant
|
4
|
+
class Transform
|
5
|
+
include Adamantium, AbstractType
|
6
|
+
|
7
|
+
# Default slug
|
8
|
+
#
|
9
|
+
# @return [String]
|
10
|
+
def slug
|
11
|
+
self.class.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
# Apply transformation to input
|
15
|
+
#
|
16
|
+
# @param [Object] input
|
17
|
+
#
|
18
|
+
# @return [Either<Error, Object>]
|
19
|
+
abstract_method :apply
|
20
|
+
|
21
|
+
# Deep error data structure
|
22
|
+
class Error
|
23
|
+
include Adamantium, Anima.new(
|
24
|
+
:cause,
|
25
|
+
:input,
|
26
|
+
:message,
|
27
|
+
:transform
|
28
|
+
)
|
29
|
+
|
30
|
+
COMPACT = '%<path>s: %<message>s'
|
31
|
+
|
32
|
+
private_constant(*constants(false))
|
33
|
+
|
34
|
+
# Compact error message
|
35
|
+
#
|
36
|
+
# @return [String]
|
37
|
+
def compact_message
|
38
|
+
COMPACT % { path: path, message: trace.last.message }
|
39
|
+
end
|
40
|
+
memoize :compact_message
|
41
|
+
|
42
|
+
# Error path trace
|
43
|
+
#
|
44
|
+
# @return [Array<Error>]
|
45
|
+
def trace
|
46
|
+
[self, *cause&.trace]
|
47
|
+
end
|
48
|
+
memoize :trace
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# Path representation to error
|
53
|
+
#
|
54
|
+
# @return [String]
|
55
|
+
def path
|
56
|
+
trace.map { |error| error.transform.slug }.reject(&:empty?).join('/')
|
57
|
+
end
|
58
|
+
end # Error
|
59
|
+
|
60
|
+
# Wrapper adding a name to a transformation
|
61
|
+
class Named < self
|
62
|
+
include Concord.new(:name, :transformer)
|
63
|
+
|
64
|
+
# Apply transformation to input
|
65
|
+
#
|
66
|
+
# @return [Either<Error, Object>]
|
67
|
+
def apply(input)
|
68
|
+
transformer.apply(input).lmap(&method(:wrap_error))
|
69
|
+
end
|
70
|
+
|
71
|
+
# Named slug
|
72
|
+
#
|
73
|
+
# @return [String]
|
74
|
+
def slug
|
75
|
+
name
|
76
|
+
end
|
77
|
+
end # Named
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
# Make error from curent context
|
82
|
+
#
|
83
|
+
# @param [Error, nil] cause
|
84
|
+
# original error, if any
|
85
|
+
#
|
86
|
+
# @param [Object] input
|
87
|
+
# input that ran into the error
|
88
|
+
#
|
89
|
+
# @param [String] message
|
90
|
+
# human readable error message
|
91
|
+
#
|
92
|
+
# @param [Transform, nil]
|
93
|
+
# transform that generated the error from input
|
94
|
+
#
|
95
|
+
# @return [Error]
|
96
|
+
#
|
97
|
+
# ignore :reek:LongParameterList
|
98
|
+
def error(cause: nil, input:, message: nil)
|
99
|
+
Error.new(
|
100
|
+
cause: cause,
|
101
|
+
input: input,
|
102
|
+
message: message,
|
103
|
+
transform: self
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Lift error
|
108
|
+
#
|
109
|
+
# @param [Error]
|
110
|
+
#
|
111
|
+
# @return [Error]
|
112
|
+
def lift_error(error)
|
113
|
+
error.with(transform: self)
|
114
|
+
end
|
115
|
+
|
116
|
+
# Wrap error
|
117
|
+
#
|
118
|
+
# @param [Error]
|
119
|
+
#
|
120
|
+
# @return [Error]
|
121
|
+
def wrap_error(error)
|
122
|
+
error(cause: error, input: error.input)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Create failure
|
126
|
+
#
|
127
|
+
# @param [Object] value
|
128
|
+
#
|
129
|
+
# @return [Either::Left]
|
130
|
+
#
|
131
|
+
# ignore :reek:UtilityFunction
|
132
|
+
def failure(value)
|
133
|
+
Either::Left.new(value)
|
134
|
+
end
|
135
|
+
|
136
|
+
# Create success
|
137
|
+
#
|
138
|
+
# @param [Object] value
|
139
|
+
#
|
140
|
+
# @return [Either::Right]
|
141
|
+
#
|
142
|
+
# ignore :reek:UtilityFunction
|
143
|
+
def success(value)
|
144
|
+
Either::Right.new(value)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Index attached to a transform
|
148
|
+
class Index < self
|
149
|
+
include Anima.new(:index, :transform)
|
150
|
+
|
151
|
+
private(*anima.attribute_names)
|
152
|
+
|
153
|
+
# Create error at specified index
|
154
|
+
#
|
155
|
+
# @param [Error] cause
|
156
|
+
# @param [Integer] index
|
157
|
+
#
|
158
|
+
# @return [Error]
|
159
|
+
def self.wrap(cause, index)
|
160
|
+
Error.new(
|
161
|
+
cause: cause,
|
162
|
+
input: cause.input,
|
163
|
+
message: nil,
|
164
|
+
transform: new(index: index, transform: cause.transform)
|
165
|
+
)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Apply transformation to input
|
169
|
+
#
|
170
|
+
# @param [Object] input
|
171
|
+
#
|
172
|
+
# @return [Either<Error, Object>]
|
173
|
+
def apply(input)
|
174
|
+
transform.apply(input).lmap(&method(:wrap_error))
|
175
|
+
end
|
176
|
+
|
177
|
+
# Rendering slug
|
178
|
+
#
|
179
|
+
# @return [Array<String>]
|
180
|
+
def slug
|
181
|
+
'%<index>d' % { index: index }
|
182
|
+
end
|
183
|
+
memoize :slug
|
184
|
+
end # Index
|
185
|
+
|
186
|
+
# Transform guarding a specific primitive
|
187
|
+
class Primitive < self
|
188
|
+
include Concord.new(:primitive)
|
189
|
+
|
190
|
+
MESSAGE = 'Expected: %<expected>s but got: %<actual>s'
|
191
|
+
|
192
|
+
private_constant(*constants(false))
|
193
|
+
|
194
|
+
# Apply transformation to input
|
195
|
+
#
|
196
|
+
# @param [Object] input
|
197
|
+
#
|
198
|
+
# @return [Either<Error, Object>]
|
199
|
+
def apply(input)
|
200
|
+
if input.instance_of?(primitive)
|
201
|
+
success(input)
|
202
|
+
else
|
203
|
+
failure(
|
204
|
+
error(
|
205
|
+
input: input,
|
206
|
+
message: MESSAGE % { actual: input.class, expected: primitive }
|
207
|
+
)
|
208
|
+
)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# Rendering slug
|
213
|
+
#
|
214
|
+
# @return [String]
|
215
|
+
def slug
|
216
|
+
primitive.to_s
|
217
|
+
end
|
218
|
+
memoize :slug
|
219
|
+
end # Primitive
|
220
|
+
|
221
|
+
# Transform guarding boolean primitives
|
222
|
+
class Boolean < self
|
223
|
+
include Concord.new
|
224
|
+
|
225
|
+
MESSAGE = 'Expected: boolean but got: %<actual>s'
|
226
|
+
|
227
|
+
private_constant(*constants(false))
|
228
|
+
|
229
|
+
# Apply transformation to input
|
230
|
+
#
|
231
|
+
# @param [Object] input
|
232
|
+
#
|
233
|
+
# @return [Either<Error, Object>]
|
234
|
+
def apply(input)
|
235
|
+
if input.equal?(true) || input.equal?(false)
|
236
|
+
success(input)
|
237
|
+
else
|
238
|
+
failure(
|
239
|
+
error(
|
240
|
+
message: MESSAGE % { actual: input.inspect },
|
241
|
+
input: input
|
242
|
+
)
|
243
|
+
)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end # Boolean
|
247
|
+
|
248
|
+
# Transform an array via mapping it over transform
|
249
|
+
class Array < self
|
250
|
+
include Concord.new(:transform)
|
251
|
+
|
252
|
+
MESSAGE = 'Failed to coerce array at index: %<index>d'
|
253
|
+
PRIMITIVE = Primitive.new(::Array)
|
254
|
+
|
255
|
+
private_constant(*constants(false))
|
256
|
+
|
257
|
+
# Apply transformation to input
|
258
|
+
#
|
259
|
+
# @param [Object] input
|
260
|
+
#
|
261
|
+
# @return [Either<Error, Array<Object>>]
|
262
|
+
def apply(input)
|
263
|
+
PRIMITIVE
|
264
|
+
.apply(input)
|
265
|
+
.lmap(&method(:lift_error))
|
266
|
+
.apply(&method(:run))
|
267
|
+
end
|
268
|
+
|
269
|
+
private
|
270
|
+
|
271
|
+
# Transform array
|
272
|
+
#
|
273
|
+
# @param [Array<Object>] input
|
274
|
+
#
|
275
|
+
# @return [Either<Error, Array<Object>]
|
276
|
+
#
|
277
|
+
# ignore :reek:NestedIterators
|
278
|
+
#
|
279
|
+
# rubocop:disable Metrics/MethodLength
|
280
|
+
def run(input)
|
281
|
+
output = []
|
282
|
+
|
283
|
+
input.each_with_index do |value, index|
|
284
|
+
output << transform.apply(value).lmap do |error|
|
285
|
+
return failure(
|
286
|
+
error(
|
287
|
+
cause: Index.wrap(error, index),
|
288
|
+
message: MESSAGE % { index: index },
|
289
|
+
input: input
|
290
|
+
)
|
291
|
+
)
|
292
|
+
end.from_right
|
293
|
+
end
|
294
|
+
|
295
|
+
success(output)
|
296
|
+
end
|
297
|
+
# rubocop:enable Metrics/MethodLength
|
298
|
+
end # Array
|
299
|
+
|
300
|
+
# Transform a hash via mapping it over key specific transforms
|
301
|
+
class Hash < self
|
302
|
+
include Anima.new(:optional, :required)
|
303
|
+
|
304
|
+
KEY_MESSAGE = 'Missing keys: %<missing>s, Unexpected keys: %<unexpected>s'
|
305
|
+
PRIMITIVE = Primitive.new(::Hash)
|
306
|
+
|
307
|
+
private_constant(*constants(false))
|
308
|
+
|
309
|
+
# Transform to symbolize array keys
|
310
|
+
class Symbolize < Transform
|
311
|
+
# Apply transformation to input
|
312
|
+
#
|
313
|
+
# @param [Hash{String => Object}]
|
314
|
+
#
|
315
|
+
# @return [Hash{Symbol => Object}]
|
316
|
+
def apply(input)
|
317
|
+
success(input.transform_keys(&:to_sym))
|
318
|
+
end
|
319
|
+
end # Symbolize
|
320
|
+
|
321
|
+
# Key specific transformation
|
322
|
+
class Key < Transform
|
323
|
+
include Concord::Public.new(:value, :transform)
|
324
|
+
|
325
|
+
# Rendering slug
|
326
|
+
#
|
327
|
+
# @return [String]
|
328
|
+
def slug
|
329
|
+
'[%<key>s]' % { key: value.inspect }
|
330
|
+
end
|
331
|
+
memoize :slug
|
332
|
+
|
333
|
+
# Apply transformation to input
|
334
|
+
#
|
335
|
+
# @param [Object]
|
336
|
+
#
|
337
|
+
# @return [Either<Error, Object>]
|
338
|
+
def apply(input)
|
339
|
+
transform.apply(input).lmap do |error|
|
340
|
+
error(cause: error, input: input)
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end # Key
|
344
|
+
|
345
|
+
# Apply transformation to input
|
346
|
+
#
|
347
|
+
# @param [Object] input
|
348
|
+
#
|
349
|
+
# @return [Either<Error, Object>]
|
350
|
+
def apply(input)
|
351
|
+
PRIMITIVE
|
352
|
+
.apply(input)
|
353
|
+
.lmap(&method(:lift_error))
|
354
|
+
.apply(&method(:reject_keys))
|
355
|
+
.apply(&method(:transform))
|
356
|
+
end
|
357
|
+
|
358
|
+
private
|
359
|
+
|
360
|
+
# Transform hash
|
361
|
+
#
|
362
|
+
# @param [Hash] input
|
363
|
+
#
|
364
|
+
# @return [Either<Error, Hash>]
|
365
|
+
def transform(input)
|
366
|
+
transform_required(input).apply do |required|
|
367
|
+
transform_optional(input).fmap(&required.method(:merge))
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
# Transform required keys
|
372
|
+
#
|
373
|
+
# @param [Hash] input
|
374
|
+
#
|
375
|
+
# @return [Either<Error, Hash>]
|
376
|
+
def transform_required(input)
|
377
|
+
transform_keys(required, input)
|
378
|
+
end
|
379
|
+
|
380
|
+
# Transform optional keys
|
381
|
+
#
|
382
|
+
# @param [Hash] input
|
383
|
+
#
|
384
|
+
# @return [Either<Error, Hash>]
|
385
|
+
def transform_optional(input)
|
386
|
+
transform_keys(
|
387
|
+
optional.select { |key| input.key?(key.value) },
|
388
|
+
input
|
389
|
+
)
|
390
|
+
end
|
391
|
+
|
392
|
+
# Transform keys
|
393
|
+
#
|
394
|
+
# @param [Array<Transform::Hash::Key>] keys
|
395
|
+
# @param [Hash] input
|
396
|
+
#
|
397
|
+
# @return [Either<Error, Hash>]
|
398
|
+
#
|
399
|
+
# ignore :reek:NestedIterators
|
400
|
+
#
|
401
|
+
# rubocop:disable Metrics/MethodLength
|
402
|
+
def transform_keys(keys, input)
|
403
|
+
success(
|
404
|
+
keys
|
405
|
+
.map do |key|
|
406
|
+
[
|
407
|
+
key.value,
|
408
|
+
coerce_key(key, input).from_right do |error|
|
409
|
+
return failure(error)
|
410
|
+
end
|
411
|
+
]
|
412
|
+
end
|
413
|
+
.to_h
|
414
|
+
)
|
415
|
+
end
|
416
|
+
# rubocop:enable Metrics/MethodLength
|
417
|
+
|
418
|
+
# Coerce key value under key specific transformer
|
419
|
+
#
|
420
|
+
# @param [Hash::Key] key
|
421
|
+
# @param [Hash] input
|
422
|
+
#
|
423
|
+
# @return [Either<Error, Object>]
|
424
|
+
def coerce_key(key, input)
|
425
|
+
key.apply(input.fetch(key.value)).lmap do |error|
|
426
|
+
error(input: input, cause: error)
|
427
|
+
end
|
428
|
+
end
|
429
|
+
|
430
|
+
# Reject unexpected keys
|
431
|
+
#
|
432
|
+
# @param [Hash]
|
433
|
+
#
|
434
|
+
# @return [Either<Error, Object>]
|
435
|
+
#
|
436
|
+
# rubocop:disable Metrics/MethodLength
|
437
|
+
def reject_keys(input)
|
438
|
+
keys = input.keys
|
439
|
+
unexpected = keys - allowed_keys
|
440
|
+
missing = required_keys - keys
|
441
|
+
|
442
|
+
if unexpected.empty? && missing.empty?
|
443
|
+
success(input)
|
444
|
+
else
|
445
|
+
failure(
|
446
|
+
error(
|
447
|
+
input: input,
|
448
|
+
message: KEY_MESSAGE % { missing: missing, unexpected: unexpected }
|
449
|
+
)
|
450
|
+
)
|
451
|
+
end
|
452
|
+
end
|
453
|
+
# rubocop:enable Metrics/MethodLength
|
454
|
+
|
455
|
+
# Key values allowed to be present
|
456
|
+
#
|
457
|
+
# @return [Array<Object>]
|
458
|
+
def allowed_keys
|
459
|
+
required_keys + optional.map(&:value)
|
460
|
+
end
|
461
|
+
memoize :allowed_keys
|
462
|
+
|
463
|
+
# Key values required to be present
|
464
|
+
#
|
465
|
+
# @return [Array<Object>]
|
466
|
+
def required_keys
|
467
|
+
required.map(&:value)
|
468
|
+
end
|
469
|
+
memoize :required_keys
|
470
|
+
end # Hash
|
471
|
+
|
472
|
+
# Sequence of transformations
|
473
|
+
class Sequence < self
|
474
|
+
include Concord.new(:steps)
|
475
|
+
|
476
|
+
# Apply transformation to input
|
477
|
+
#
|
478
|
+
# @param [Object]
|
479
|
+
#
|
480
|
+
# ignore :reek:NestedIterators
|
481
|
+
#
|
482
|
+
# @return [Either<Error, Object>]
|
483
|
+
def apply(input)
|
484
|
+
current = input
|
485
|
+
|
486
|
+
steps.each_with_index do |step, index|
|
487
|
+
current = step.apply(current).from_right do |error|
|
488
|
+
return failure(error(cause: Index.wrap(error, index), input: input))
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
success(current)
|
493
|
+
end
|
494
|
+
end # Sequence
|
495
|
+
|
496
|
+
# Generic exception transformer
|
497
|
+
class Exception < self
|
498
|
+
include Concord.new(:error_class, :block)
|
499
|
+
|
500
|
+
# Apply transformation to input
|
501
|
+
#
|
502
|
+
# @param [Object]
|
503
|
+
#
|
504
|
+
# @return [Either<Error, Object>]
|
505
|
+
def apply(input)
|
506
|
+
Either.wrap_error(error_class) { block.call(input) }
|
507
|
+
.lmap { |exception| error(input: input, message: exception.to_s) }
|
508
|
+
end
|
509
|
+
end # Exception
|
510
|
+
end # Transform
|
511
|
+
end # Mutant
|