ruby-cdk 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/CHANGELOG.md +87 -0
- data/LICENSE +21 -0
- data/README.md +303 -0
- data/THIRD_PARTY_NOTICES.md +327 -0
- data/data/spec/META.json +33 -0
- data/data/spec/model.json.gz +0 -0
- data/exe/ruby-cdk +9 -0
- data/lib/ruby-cdk.rb +4 -0
- data/lib/ruby_cdk/assets/builder.rb +122 -0
- data/lib/ruby_cdk/assets/hasher.rb +175 -0
- data/lib/ruby_cdk/assets/record.rb +49 -0
- data/lib/ruby_cdk/assets/uploader.rb +139 -0
- data/lib/ruby_cdk/assets/zipper.rb +108 -0
- data/lib/ruby_cdk/assets.rb +116 -0
- data/lib/ruby_cdk/bootstrap.rb +289 -0
- data/lib/ruby_cdk/cfn/resource.rb +93 -0
- data/lib/ruby_cdk/cfn.rb +10 -0
- data/lib/ruby_cdk/cli/arguments.rb +41 -0
- data/lib/ruby_cdk/cli/aws.rb +35 -0
- data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
- data/lib/ruby_cdk/cli/diff.rb +98 -0
- data/lib/ruby_cdk/cli/failure.rb +80 -0
- data/lib/ruby_cdk/cli/io.rb +70 -0
- data/lib/ruby_cdk/cli/list.rb +46 -0
- data/lib/ruby_cdk/cli/new.rb +313 -0
- data/lib/ruby_cdk/cli/operation.rb +124 -0
- data/lib/ruby_cdk/cli/pairs.rb +32 -0
- data/lib/ruby_cdk/cli/report.rb +118 -0
- data/lib/ruby_cdk/cli/selection.rb +134 -0
- data/lib/ruby_cdk/cli/session.rb +290 -0
- data/lib/ruby_cdk/cli/shell.rb +30 -0
- data/lib/ruby_cdk/cli/synth.rb +58 -0
- data/lib/ruby_cdk/cli/validate.rb +101 -0
- data/lib/ruby_cdk/cli.rb +242 -0
- data/lib/ruby_cdk/constructs/bucket.rb +191 -0
- data/lib/ruby_cdk/constructs/function.rb +163 -0
- data/lib/ruby_cdk/constructs/grant.rb +90 -0
- data/lib/ruby_cdk/constructs/log_group.rb +115 -0
- data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
- data/lib/ruby_cdk/constructs/queue.rb +118 -0
- data/lib/ruby_cdk/constructs/role.rb +282 -0
- data/lib/ruby_cdk/constructs/table.rb +160 -0
- data/lib/ruby_cdk/constructs/topic.rb +88 -0
- data/lib/ruby_cdk/constructs.rb +56 -0
- data/lib/ruby_cdk/core/app.rb +146 -0
- data/lib/ruby_cdk/core/construct.rb +105 -0
- data/lib/ruby_cdk/core/elements.rb +107 -0
- data/lib/ruby_cdk/core/ids.rb +117 -0
- data/lib/ruby_cdk/core/stack.rb +291 -0
- data/lib/ruby_cdk/core/stateful.rb +156 -0
- data/lib/ruby_cdk/core.rb +15 -0
- data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
- data/lib/ruby_cdk/deploy/deployer.rb +805 -0
- data/lib/ruby_cdk/deploy/errors.rb +72 -0
- data/lib/ruby_cdk/deploy/fakes.rb +546 -0
- data/lib/ruby_cdk/deploy/output/base.rb +167 -0
- data/lib/ruby_cdk/deploy/output/human.rb +37 -0
- data/lib/ruby_cdk/deploy/output/json.rb +80 -0
- data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
- data/lib/ruby_cdk/deploy/parameters.rb +86 -0
- data/lib/ruby_cdk/deploy/poller.rb +72 -0
- data/lib/ruby_cdk/deploy/ports.rb +205 -0
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
- data/lib/ruby_cdk/deploy/states.rb +62 -0
- data/lib/ruby_cdk/deploy.rb +25 -0
- data/lib/ruby_cdk/diff/compare.rb +141 -0
- data/lib/ruby_cdk/diff/normalizer.rb +83 -0
- data/lib/ruby_cdk/diff/render.rb +242 -0
- data/lib/ruby_cdk/diff/security.rb +322 -0
- data/lib/ruby_cdk/diff/statement.rb +256 -0
- data/lib/ruby_cdk/diff/template.rb +524 -0
- data/lib/ruby_cdk/diff.rb +62 -0
- data/lib/ruby_cdk/env.rb +42 -0
- data/lib/ruby_cdk/errors.rb +38 -0
- data/lib/ruby_cdk/fn.rb +409 -0
- data/lib/ruby_cdk/lazy.rb +55 -0
- data/lib/ruby_cdk/nag/config.rb +303 -0
- data/lib/ruby_cdk/nag/engine.rb +152 -0
- data/lib/ruby_cdk/nag/report.rb +86 -0
- data/lib/ruby_cdk/nag/rule.rb +171 -0
- data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
- data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
- data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
- data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
- data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
- data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
- data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
- data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
- data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
- data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
- data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
- data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
- data/lib/ruby_cdk/nag/rules.rb +51 -0
- data/lib/ruby_cdk/nag/values.rb +305 -0
- data/lib/ruby_cdk/nag.rb +60 -0
- data/lib/ruby_cdk/schema/normalizer.rb +164 -0
- data/lib/ruby_cdk/schema/property_schema.rb +22 -0
- data/lib/ruby_cdk/schema/schema.rb +195 -0
- data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
- data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
- data/lib/ruby_cdk/schema/sources/download.rb +86 -0
- data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
- data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
- data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
- data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
- data/lib/ruby_cdk/schema/sources/update.rb +178 -0
- data/lib/ruby_cdk/schema/sources.rb +20 -0
- data/lib/ruby_cdk/schema/type_schema.rb +109 -0
- data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
- data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
- data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
- data/lib/ruby_cdk/schema/validator/report.rb +92 -0
- data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
- data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
- data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
- data/lib/ruby_cdk/schema/validator/template.rb +203 -0
- data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
- data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
- data/lib/ruby_cdk/schema/validator.rb +159 -0
- data/lib/ruby_cdk/schema.rb +19 -0
- data/lib/ruby_cdk/synth/manifest.rb +128 -0
- data/lib/ruby_cdk/synth/resolver.rb +326 -0
- data/lib/ruby_cdk/synth/rules.rb +83 -0
- data/lib/ruby_cdk/synth/serializer.rb +120 -0
- data/lib/ruby_cdk/synth/writer.rb +31 -0
- data/lib/ruby_cdk/synth.rb +255 -0
- data/lib/ruby_cdk/test/capture.rb +133 -0
- data/lib/ruby_cdk/test/match.rb +426 -0
- data/lib/ruby_cdk/test/matcher.rb +290 -0
- data/lib/ruby_cdk/test/private/absent.rb +34 -0
- data/lib/ruby_cdk/test/private/conditions.rb +39 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
- data/lib/ruby_cdk/test/private/error.rb +48 -0
- data/lib/ruby_cdk/test/private/js.rb +326 -0
- data/lib/ruby_cdk/test/private/mappings.rb +39 -0
- data/lib/ruby_cdk/test/private/outputs.rb +39 -0
- data/lib/ruby_cdk/test/private/parameters.rb +39 -0
- data/lib/ruby_cdk/test/private/resources.rb +116 -0
- data/lib/ruby_cdk/test/private/section.rb +98 -0
- data/lib/ruby_cdk/test/private/sorting.rb +40 -0
- data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
- data/lib/ruby_cdk/test/private/type.rb +23 -0
- data/lib/ruby_cdk/test/template.rb +364 -0
- data/lib/ruby_cdk/test.rb +116 -0
- data/lib/ruby_cdk/values.rb +8 -0
- data/lib/ruby_cdk/version.rb +5 -0
- data/lib/ruby_cdk.rb +47 -0
- metadata +256 -0
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/matching/lib/match.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: camelCase to snake_case, the names of the matchers included, so that
|
|
9
|
+
# a message names the method the user called: arrayWith() is array_with().
|
|
10
|
+
# `not` is `not_`, because `not` is a Ruby keyword (as Fn.not_). Match is a
|
|
11
|
+
# module and its factories are module functions. The classes LiteralMatch to
|
|
12
|
+
# StringLikeRegexpMatch, which that file keeps to itself, are under Private.
|
|
13
|
+
# Before matching, the Symbols of a pattern become strings (Js.normalize).
|
|
14
|
+
# Where JavaScript throws one of its own TypeErrors (a null where an object was
|
|
15
|
+
# needed), this port throws a Ruby TypeError with the same message.
|
|
16
|
+
#
|
|
17
|
+
# Known difference, stated for the record: a Ruby Integer is compared exactly,
|
|
18
|
+
# where V8 collapses both sides to a double. A Ruby literal and the JSON bytes
|
|
19
|
+
# of a template that disagree past 2^53 (exact(9007199254740993) against bytes
|
|
20
|
+
# saying ...992) fail here and pass there. JSON.parse of the same bytes gives
|
|
21
|
+
# the same Integer on both sides, so nothing else is affected.
|
|
22
|
+
|
|
23
|
+
require_relative "matcher"
|
|
24
|
+
require_relative "private/absent"
|
|
25
|
+
require_relative "private/error"
|
|
26
|
+
require_relative "private/js"
|
|
27
|
+
require_relative "private/sorting"
|
|
28
|
+
require_relative "private/sparse_matrix"
|
|
29
|
+
require_relative "private/type"
|
|
30
|
+
|
|
31
|
+
module RubyCdk
|
|
32
|
+
module Test
|
|
33
|
+
# Partial and special matching during template assertions.
|
|
34
|
+
#
|
|
35
|
+
# In the CDK these are the static methods of `Match`. Here they are also on
|
|
36
|
+
# RubyCdk::Test itself, which is where the documentation of this tool puts them.
|
|
37
|
+
module Match
|
|
38
|
+
class << self
|
|
39
|
+
# Use this matcher in the place of a field's value, if the field must not be present.
|
|
40
|
+
def absent
|
|
41
|
+
Private::AbsentMatch.new("absent")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Matches the specified pattern with the array found in the same relative
|
|
45
|
+
# path of the target. The set of elements (or matchers) must be in the same
|
|
46
|
+
# order as would be found.
|
|
47
|
+
#
|
|
48
|
+
# pattern: the pattern to match
|
|
49
|
+
def array_with(pattern)
|
|
50
|
+
Private::ArrayMatch.new("array_with", Private::Js.normalize(pattern))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Matches the specified pattern with the array found in the same relative
|
|
54
|
+
# path of the target. The set of elements (or matchers) must match exactly
|
|
55
|
+
# and in order.
|
|
56
|
+
#
|
|
57
|
+
# pattern: the pattern to match
|
|
58
|
+
def array_equals(pattern)
|
|
59
|
+
Private::ArrayMatch.new("array_equals", Private::Js.normalize(pattern), subsequence: false)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Deep exact matching of the specified pattern to the target.
|
|
63
|
+
#
|
|
64
|
+
# pattern: the pattern to match
|
|
65
|
+
def exact(pattern)
|
|
66
|
+
Private::LiteralMatch.new("exact", Private::Js.normalize(pattern))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Matches the specified pattern to an object found in the same relative path
|
|
70
|
+
# of the target. The keys and their values (or matchers) must be present in
|
|
71
|
+
# the target but the target can be a superset.
|
|
72
|
+
#
|
|
73
|
+
# pattern: the pattern to match
|
|
74
|
+
def object_like(pattern)
|
|
75
|
+
Private::ObjectMatch.new("object_like", Private::Js.normalize(pattern))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Matches the specified pattern to an object found in the same relative path
|
|
79
|
+
# of the target. The keys and their values (or matchers) must match exactly
|
|
80
|
+
# with the target.
|
|
81
|
+
#
|
|
82
|
+
# pattern: the pattern to match
|
|
83
|
+
def object_equals(pattern)
|
|
84
|
+
Private::ObjectMatch.new("object_equals", Private::Js.normalize(pattern), partial: false)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Matches any target which does NOT follow the specified pattern.
|
|
88
|
+
#
|
|
89
|
+
# pattern: the pattern to NOT match
|
|
90
|
+
def not_(pattern)
|
|
91
|
+
Private::NotMatch.new("not", Private::Js.normalize(pattern))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Matches any string-encoded JSON and applies the specified pattern after parsing it.
|
|
95
|
+
#
|
|
96
|
+
# pattern: the pattern to match after parsing the encoded JSON.
|
|
97
|
+
def serialized_json(pattern)
|
|
98
|
+
Private::SerializedJson.new("serialized_json", Private::Js.normalize(pattern))
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Matches any non-null value at the target.
|
|
102
|
+
def any_value
|
|
103
|
+
Private::AnyMatch.new("any_value")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Matches targets according to a regular expression.
|
|
107
|
+
def string_like_regexp(pattern)
|
|
108
|
+
Private::StringLikeRegexpMatch.new("string_like_regexp", pattern)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
module Private
|
|
114
|
+
# A Match class that expects the target to match with the pattern exactly.
|
|
115
|
+
# The pattern may be nested with other matchers that are then delegated to.
|
|
116
|
+
class LiteralMatch < Matcher
|
|
117
|
+
attr_reader :name
|
|
118
|
+
|
|
119
|
+
# partial_objects: whether objects nested at any level should be matched
|
|
120
|
+
# partially. False unless given.
|
|
121
|
+
def initialize(name, pattern, partial_objects: false)
|
|
122
|
+
@name = name
|
|
123
|
+
@pattern = pattern
|
|
124
|
+
@partial_objects = partial_objects
|
|
125
|
+
|
|
126
|
+
if Matcher.matcher?(@pattern)
|
|
127
|
+
raise AssertionError, "LiteralMatch cannot directly contain another matcher. Remove the top-level matcher or nest it more deeply."
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def test(actual)
|
|
132
|
+
if @pattern.is_a?(Array)
|
|
133
|
+
return ArrayMatch.new(@name, @pattern, subsequence: false, partial_objects: @partial_objects).test(actual)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
if Js.typeof(@pattern) == "object"
|
|
137
|
+
return ObjectMatch.new(@name, @pattern, partial: @partial_objects).test(actual)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
result = MatchResult.new(actual)
|
|
141
|
+
if Js.typeof(@pattern) != Js.typeof(actual)
|
|
142
|
+
return result.record_failure(matcher: self, path: [], message: "Expected type #{Js.typeof(@pattern)} but received #{Private.get_type(actual)}")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
unless Js.strict_equal?(actual, @pattern)
|
|
146
|
+
result.record_failure(matcher: self, path: [], message: "Expected #{Js.string(@pattern)} but received #{Js.string(actual)}")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
result
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Match class that matches arrays.
|
|
154
|
+
class ArrayMatch < Matcher
|
|
155
|
+
attr_reader :name
|
|
156
|
+
|
|
157
|
+
# subsequence: whether the pattern is a subsequence of the target: a
|
|
158
|
+
# sequence that can be derived from another by deleting
|
|
159
|
+
# some or no elements without changing the order of the
|
|
160
|
+
# rest. True unless given.
|
|
161
|
+
# partial_objects: whether to continue matching objects inside the array
|
|
162
|
+
# partially. False unless given.
|
|
163
|
+
def initialize(name, pattern, subsequence: true, partial_objects: false)
|
|
164
|
+
@name = name
|
|
165
|
+
@pattern = pattern
|
|
166
|
+
@subsequence = subsequence
|
|
167
|
+
@partial_objects = partial_objects
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def test(actual)
|
|
171
|
+
unless actual.is_a?(Array)
|
|
172
|
+
return MatchResult.new(actual).record_failure(matcher: self, path: [], message: "Expected type array but received #{Private.get_type(actual)}")
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
@subsequence ? test_subsequence(actual) : test_full_array(actual)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
private
|
|
179
|
+
|
|
180
|
+
def matcher_for(pattern_element)
|
|
181
|
+
Matcher.matcher?(pattern_element) ? pattern_element : LiteralMatch.new(@name, pattern_element, partial_objects: @partial_objects)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def test_full_array(actual)
|
|
185
|
+
result = MatchResult.new(actual)
|
|
186
|
+
|
|
187
|
+
index = 0
|
|
188
|
+
while index < @pattern.length && index < actual.length
|
|
189
|
+
result.compose(index.to_s, matcher_for(@pattern[index]).test(actual[index]))
|
|
190
|
+
index += 1
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
if index < @pattern.length
|
|
194
|
+
result.record_failure(
|
|
195
|
+
matcher: self,
|
|
196
|
+
message: "Not enough elements in array (expecting #{@pattern.length}, got #{actual.length})",
|
|
197
|
+
path: [ index.to_s ]
|
|
198
|
+
)
|
|
199
|
+
end
|
|
200
|
+
if index < actual.length
|
|
201
|
+
result.record_failure(
|
|
202
|
+
matcher: self,
|
|
203
|
+
message: "Too many elements in array (expecting #{@pattern.length}, got #{actual.length})",
|
|
204
|
+
path: [ index.to_s ]
|
|
205
|
+
)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
result
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def test_subsequence(actual)
|
|
212
|
+
result = MatchResult.new(actual)
|
|
213
|
+
|
|
214
|
+
# For subsequences, there is a lot of testing and backtracking that
|
|
215
|
+
# happens here; keep track of it all so we can report in a sensible
|
|
216
|
+
# amount of detail on what we did if the match happens to fail.
|
|
217
|
+
pattern_index = 0
|
|
218
|
+
actual_index = 0
|
|
219
|
+
matches = SparseMatrix.new
|
|
220
|
+
|
|
221
|
+
while pattern_index < @pattern.length && actual_index < actual.length
|
|
222
|
+
matcher = matcher_for(@pattern[pattern_index])
|
|
223
|
+
|
|
224
|
+
matcher_name = matcher.name
|
|
225
|
+
if matcher_name == "absent" || matcher_name == "any_value"
|
|
226
|
+
# array subsequence matcher is not compatible with anyValue() or absent() matcher. They don't make sense to be used together.
|
|
227
|
+
raise AssertionError, "The Matcher #{matcher_name}() cannot be nested within array_with()"
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
inner_result = matcher.test(actual[actual_index])
|
|
231
|
+
matches.set(pattern_index, actual_index, inner_result)
|
|
232
|
+
|
|
233
|
+
actual_index += 1
|
|
234
|
+
if inner_result.is_success?
|
|
235
|
+
result.compose(actual_index.to_s, inner_result) # Record any captures
|
|
236
|
+
pattern_index += 1
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# If we haven't matched all patterns:
|
|
241
|
+
# - Report on each one that did match on where it matched (perhaps it was wrong)
|
|
242
|
+
# - Report the closest match for the failing one
|
|
243
|
+
if pattern_index < @pattern.length
|
|
244
|
+
# Succeeded Pattern Index
|
|
245
|
+
(0...pattern_index).each do |succeeded|
|
|
246
|
+
found_match = matches.row(succeeded).find { |_index, inner| inner.is_success? }
|
|
247
|
+
next unless found_match # Should never fail but let's be defensive
|
|
248
|
+
|
|
249
|
+
index = found_match[0]
|
|
250
|
+
|
|
251
|
+
result.compose(
|
|
252
|
+
index.to_s,
|
|
253
|
+
MatchResult.new(actual[index]).record_failure(
|
|
254
|
+
matcher: self,
|
|
255
|
+
message: "array_with pattern #{succeeded} matched here",
|
|
256
|
+
path: [],
|
|
257
|
+
cost: 0 # This is an informational message so it would be unfair to assign it cost
|
|
258
|
+
)
|
|
259
|
+
)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
failed_matches = matches.row(pattern_index)
|
|
263
|
+
failed_matches = failed_matches.sort(&Private.sort_key_comparator { |index, inner| [ inner.fail_cost, index ] })
|
|
264
|
+
if failed_matches.length > 0
|
|
265
|
+
index, closest = failed_matches[0]
|
|
266
|
+
result.record_failure(
|
|
267
|
+
matcher: self,
|
|
268
|
+
message: "Could not match array_with pattern #{pattern_index}. This is the closest match",
|
|
269
|
+
path: [ index.to_s ],
|
|
270
|
+
cost: 0 # Informational message
|
|
271
|
+
)
|
|
272
|
+
result.compose(index.to_s, closest)
|
|
273
|
+
else
|
|
274
|
+
# The previous matcher matched at the end of the pattern and we didn't even get to try anything
|
|
275
|
+
result.record_failure(
|
|
276
|
+
matcher: self,
|
|
277
|
+
message: "Could not match array_with pattern #{pattern_index}. No more elements to try",
|
|
278
|
+
path: [ actual.length.to_s ]
|
|
279
|
+
)
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
result
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Match class that matches objects.
|
|
288
|
+
class ObjectMatch < Matcher
|
|
289
|
+
attr_reader :name
|
|
290
|
+
|
|
291
|
+
# partial: whether the pattern should partially match with the target
|
|
292
|
+
# object, which can then contain more keys than the pattern expects. True
|
|
293
|
+
# unless given.
|
|
294
|
+
def initialize(name, pattern, partial: true)
|
|
295
|
+
@name = name
|
|
296
|
+
@pattern = pattern
|
|
297
|
+
@partial = partial
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def test(actual)
|
|
301
|
+
unless Js.typeof(actual) == "object" && !actual.is_a?(Array)
|
|
302
|
+
return MatchResult.new(actual).record_failure(matcher: self, path: [], message: "Expected type object but received #{Private.get_type(actual)}")
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
result = MatchResult.new(actual)
|
|
306
|
+
unless @partial
|
|
307
|
+
Js.object_keys(actual).each do |key|
|
|
308
|
+
result.record_failure(matcher: self, path: [ key ], message: "Unexpected key #{key}") unless Js.in?(key, @pattern)
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
Js.entries(@pattern).each do |pattern_key, pattern_value|
|
|
313
|
+
if !Js.in?(pattern_key, actual) && !pattern_value.is_a?(AbsentMatch)
|
|
314
|
+
result.record_failure(matcher: self, path: [ pattern_key ], message: "Missing key '#{pattern_key}'")
|
|
315
|
+
next
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
result.compose(pattern_key, matcher_for(pattern_value).test(Js.get(actual, pattern_key)))
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
result
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
private
|
|
325
|
+
|
|
326
|
+
def matcher_for(pattern_value)
|
|
327
|
+
Matcher.matcher?(pattern_value) ? pattern_value : LiteralMatch.new(@name, pattern_value, partial_objects: @partial)
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
class SerializedJson < Matcher
|
|
332
|
+
attr_reader :name
|
|
333
|
+
|
|
334
|
+
def initialize(name, pattern)
|
|
335
|
+
@name = name
|
|
336
|
+
@pattern = pattern
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def test(actual)
|
|
340
|
+
unless Private.get_type(actual) == "string"
|
|
341
|
+
return MatchResult.new(actual).record_failure(matcher: self, path: [], message: "Expected JSON as a string but found #{Private.get_type(actual)}")
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
begin
|
|
345
|
+
parsed = JSON.parse(actual, max_nesting: false)
|
|
346
|
+
rescue JSON::ParserError
|
|
347
|
+
return MatchResult.new(actual).record_failure(matcher: self, path: [], message: "Invalid JSON string: #{actual}")
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
matcher = Matcher.matcher?(@pattern) ? @pattern : LiteralMatch.new(@name, @pattern)
|
|
351
|
+
inner_result = matcher.test(parsed)
|
|
352
|
+
if inner_result.has_failed?
|
|
353
|
+
inner_result.record_failure(matcher: self, path: [], message: "Encoded JSON value does not match")
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
inner_result
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
class NotMatch < Matcher
|
|
361
|
+
attr_reader :name
|
|
362
|
+
|
|
363
|
+
def initialize(name, pattern)
|
|
364
|
+
@name = name
|
|
365
|
+
@pattern = pattern
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def test(actual)
|
|
369
|
+
matcher = Matcher.matcher?(@pattern) ? @pattern : LiteralMatch.new(@name, @pattern)
|
|
370
|
+
|
|
371
|
+
inner_result = matcher.test(actual)
|
|
372
|
+
result = MatchResult.new(actual)
|
|
373
|
+
if inner_result.fail_count.zero?
|
|
374
|
+
result.record_failure(matcher: self, path: [], message: "Found unexpected match: #{Js.json(actual, 2)}")
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
result
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
class AnyMatch < Matcher
|
|
382
|
+
attr_reader :name
|
|
383
|
+
|
|
384
|
+
def initialize(name)
|
|
385
|
+
@name = name
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
def test(actual)
|
|
389
|
+
result = MatchResult.new(actual)
|
|
390
|
+
if Js.nullish?(actual)
|
|
391
|
+
result.record_failure(matcher: self, path: [], message: "Expected a value but found none")
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
result
|
|
395
|
+
end
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
class StringLikeRegexpMatch < Matcher
|
|
399
|
+
attr_reader :name
|
|
400
|
+
|
|
401
|
+
def initialize(name, pattern)
|
|
402
|
+
@name = name
|
|
403
|
+
@pattern = pattern
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
def test(actual)
|
|
407
|
+
result = MatchResult.new(actual)
|
|
408
|
+
|
|
409
|
+
# JavaScript compiles it with the flags 'gm'. Ruby needs none: ^ and $
|
|
410
|
+
# match at line ends there too, and `.` does not cross a newline.
|
|
411
|
+
regex = Regexp.new(@pattern)
|
|
412
|
+
|
|
413
|
+
unless actual.is_a?(String)
|
|
414
|
+
result.record_failure(matcher: self, path: [], message: "Expected a string, but got '#{Js.typeof(actual)}'")
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
unless regex.match?(Js.string(actual))
|
|
418
|
+
result.record_failure(matcher: self, path: [], message: "String '#{Js.string(actual)}' did not match pattern '#{@pattern}'")
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
result
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
end
|