nukadoko 0.2.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +153 -0
- data/README.ja.md +6 -3
- data/README.md +6 -3
- data/lib/nukadoko/aria_snapshot.rb +4 -4
- data/lib/nukadoko/base.rb +49 -29
- data/lib/nukadoko/check.rb +62 -5
- data/lib/nukadoko/claim_check.rb +2 -2
- data/lib/nukadoko/claim_rules.rb +1 -1
- data/lib/nukadoko/claim_sides.rb +481 -0
- data/lib/nukadoko/claims.rb +182 -20
- data/lib/nukadoko/cli.rb +24 -7
- data/lib/nukadoko/codes.json +20 -15
- data/lib/nukadoko/criteria.rb +1 -1
- data/lib/nukadoko/env_check.rb +1 -3
- data/lib/nukadoko/fixtures.rb +53 -21
- data/lib/nukadoko/init.rb +0 -1
- data/lib/nukadoko/js_json.rb +274 -0
- data/lib/nukadoko/js_pattern.rb +531 -0
- data/lib/nukadoko/parameter_types.rb +54 -0
- data/lib/nukadoko/parts.rb +1 -1
- data/lib/nukadoko/pattern.rb +112 -2
- data/lib/nukadoko/poll.rb +5 -3
- data/lib/nukadoko/program.rb +5 -5
- data/lib/nukadoko/project_files.rb +17 -0
- data/lib/nukadoko/project_program.rb +119 -0
- data/lib/nukadoko/run.rb +62 -17
- data/lib/nukadoko/run_record.rb +25 -22
- data/lib/nukadoko/scaffold.rb +2 -2
- data/lib/nukadoko/schema.rb +60 -18
- data/lib/nukadoko/secrets.rb +20 -4
- data/lib/nukadoko/spaces.rb +28 -0
- data/lib/nukadoko/step.rb +7 -1
- data/lib/nukadoko/text_fit.rb +1 -1
- data/lib/nukadoko/trace_actions.rb +4 -2
- data/lib/nukadoko/tree_passes.rb +6 -3
- data/lib/nukadoko/version.rb +1 -1
- data/lib/nukadoko.rb +26 -2
- data/llms.txt +20 -18
- data/sig/generated/nukadoko/aria_snapshot.rbs +2 -2
- data/sig/generated/nukadoko/base.rbs +24 -18
- data/sig/generated/nukadoko/check.rbs +11 -1
- data/sig/generated/nukadoko/claim_sides.rbs +216 -0
- data/sig/generated/nukadoko/claims.rbs +46 -3
- data/sig/generated/nukadoko/cli.rbs +1 -1
- data/sig/generated/nukadoko/env_check.rbs +0 -4
- data/sig/generated/nukadoko/fixtures.rbs +25 -2
- data/sig/generated/nukadoko/init.rbs +0 -2
- data/sig/generated/nukadoko/js_json.rbs +109 -0
- data/sig/generated/nukadoko/js_pattern.rbs +194 -0
- data/sig/generated/nukadoko/parameter_types.rbs +27 -0
- data/sig/generated/nukadoko/pattern.rbs +59 -1
- data/sig/generated/nukadoko/poll.rbs +3 -2
- data/sig/generated/nukadoko/program.rbs +1 -1
- data/sig/generated/nukadoko/project_files.rbs +19 -0
- data/sig/generated/nukadoko/project_program.rbs +55 -0
- data/sig/generated/nukadoko/run.rbs +17 -3
- data/sig/generated/nukadoko/run_record.rbs +5 -4
- data/sig/generated/nukadoko/schema.rbs +22 -4
- data/sig/generated/nukadoko/secrets.rbs +17 -2
- data/sig/generated/nukadoko/spaces.rbs +31 -0
- data/sig/generated/nukadoko/trace_actions.rbs +3 -1
- data/sig/generated/nukadoko/tree_passes.rbs +2 -2
- data/sig/generated/nukadoko.rbs +11 -0
- data/skills/nukadoko/SKILL.md +2 -2
- data/skills/nukadoko/references/diagnosing.md +1 -1
- data/skills/nukadoko/references/examples/http-api.md +19 -16
- data/skills/nukadoko/references/fixtures.md +1 -1
- data/skills/nukadoko/references/perturbed-worlds.md +8 -6
- data/skills/nukadoko/references/program.md +20 -20
- data/skills/nukadoko/references/running.md +2 -2
- data/skills/nukadoko/references/scenario-rules.md +2 -2
- data/skills/nukadoko/references/verdicts.md +3 -3
- data/skills/nukadoko/references/writing-steps.md +1 -1
- data/skills/nukadoko-verification/SKILL.md +6 -6
- metadata +19 -5
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
# Responsibility: the two sides of a top-level comparison claim, read as the
|
|
5
|
+
# TypeScript engine reads them: where each value sits (its place, whose
|
|
6
|
+
# label a reason names), the plain JSON copy a claim judges, and what stops
|
|
7
|
+
# a claim before any compare runs (a value that is undefined, one that is
|
|
8
|
+
# not plain JSON, a view that cannot read it, a claim that compares a value
|
|
9
|
+
# with itself or with one observe could have returned). It reads; the
|
|
10
|
+
# compares stay in Claims.compare. A claim on each item of a list is read
|
|
11
|
+
# elsewhere.
|
|
12
|
+
module Nukadoko
|
|
13
|
+
module Engine
|
|
14
|
+
module ClaimSides
|
|
15
|
+
Place = Data.define(
|
|
16
|
+
:origin, #: String
|
|
17
|
+
:segments, #: Array[String]
|
|
18
|
+
:label, #: String
|
|
19
|
+
:open, #: bool
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# What one side read: a value, or why the claim breaks or is not judged.
|
|
23
|
+
Side = Data.define(
|
|
24
|
+
:place, #: Place
|
|
25
|
+
:found, #: Hash[Symbol, untyped]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
OBSERVE = Place.new(origin: "observe", segments: [], label: "observe:", open: false)
|
|
29
|
+
LITERAL = Place.new(origin: "literal", segments: [], label: "literal:", open: false)
|
|
30
|
+
SAFE = 2**53 - 1
|
|
31
|
+
MAX_DEPTH = 256
|
|
32
|
+
INDEX = /\A(0|[1-9][0-9]*)\z/
|
|
33
|
+
|
|
34
|
+
# Where a claim reads from: what observe returned with the from values
|
|
35
|
+
# merged in, the args, whether the step observes, its from keys, the
|
|
36
|
+
# label of each args key's origin, and the args keys observe received.
|
|
37
|
+
Inputs = Data.define(
|
|
38
|
+
:observed, #: untyped
|
|
39
|
+
:args, #: untyped
|
|
40
|
+
:observes, #: bool
|
|
41
|
+
:from_keys, #: Array[String]
|
|
42
|
+
:labels, #: ^(String) -> String
|
|
43
|
+
:received, #: Array[String]
|
|
44
|
+
# Why a claim cannot compare with an args key, for each key the
|
|
45
|
+
# matched pattern fills with a parameter type that has a transformer.
|
|
46
|
+
:transformed, #: Hash[String, String]
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
# A Data block's methods are its own, but rbs-inline reads only a class
|
|
50
|
+
# body, so they are written in one.
|
|
51
|
+
class Inputs
|
|
52
|
+
#: (?transformed: Hash[String, String], **untyped) -> void
|
|
53
|
+
def initialize(transformed: {}, **rest) = super
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
#: (Place, String?) -> Place
|
|
57
|
+
def self.place_at(place, path)
|
|
58
|
+
return place if path.nil? || path.empty?
|
|
59
|
+
|
|
60
|
+
separator = if place.open then " at "
|
|
61
|
+
elsif place.label.end_with?(":") then ""
|
|
62
|
+
else "."
|
|
63
|
+
end
|
|
64
|
+
Place.new(origin: place.origin, segments: place.segments + path.split("."), label: "#{place.label}#{separator}#{path}", open: false)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
#: (Place, String?) -> Place
|
|
68
|
+
def self.place_viewed(place, view)
|
|
69
|
+
return place unless view
|
|
70
|
+
|
|
71
|
+
Place.new(origin: place.origin, segments: [*place.segments, "##{view}"], label: "#{place.label} as #{view}", open: true)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
#: (Place, bool?) -> Place
|
|
75
|
+
def self.place_counted(place, count)
|
|
76
|
+
return place unless count == true
|
|
77
|
+
|
|
78
|
+
Place.new(origin: place.origin, segments: [*place.segments, "#count"], label: "count of #{place.label}", open: false)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
#: (Place, Place) -> bool
|
|
82
|
+
def self.same_place?(a, b) = a.origin == b.origin && a.segments == b.segments
|
|
83
|
+
|
|
84
|
+
#: (Place, Place) -> bool
|
|
85
|
+
def self.overlap?(a, b)
|
|
86
|
+
size = [a.segments.size, b.segments.size].min
|
|
87
|
+
a.origin == b.origin && a.segments.take(size) == b.segments.take(size)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
#: (String, Inputs) -> Place
|
|
91
|
+
def self.arg_place(key, inputs)
|
|
92
|
+
Place.new(origin: inputs.labels.(key), segments: [], label: inputs.labels.(key), open: false)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
#: (String) -> Array[String]
|
|
96
|
+
def self.segments(path)
|
|
97
|
+
parts = path.split(".", -1)
|
|
98
|
+
parts.all? { !_1.empty? } ? parts : [path]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# The value at `segments` inside `root`, or the index of the first
|
|
102
|
+
# segment that finds nothing. nil is JSON's null, which is a value.
|
|
103
|
+
#: (untyped, Array[String]) -> Hash[Symbol, untyped]
|
|
104
|
+
def self.value_at(root, segments)
|
|
105
|
+
current = root
|
|
106
|
+
segments.each_with_index do |segment, index|
|
|
107
|
+
case current
|
|
108
|
+
when Hash
|
|
109
|
+
key = if current.key?(segment) then segment
|
|
110
|
+
elsif current.key?(segment.to_sym) then segment.to_sym
|
|
111
|
+
end
|
|
112
|
+
return { missing: index } if key.nil?
|
|
113
|
+
|
|
114
|
+
current = current[key]
|
|
115
|
+
when Array
|
|
116
|
+
return { missing: index } unless segment.match?(INDEX) && segment.to_i < current.size
|
|
117
|
+
|
|
118
|
+
current = current[segment.to_i]
|
|
119
|
+
else return { missing: index }
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
{ found: current }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
#: (untyped, Array[String]) -> Hash[Symbol, untyped]
|
|
126
|
+
def self.read_at(root, segments)
|
|
127
|
+
found = value_at(root, segments)
|
|
128
|
+
return found unless found.key?(:found)
|
|
129
|
+
|
|
130
|
+
taken = snapshot(found[:found])
|
|
131
|
+
taken[:refused] ? { refused: taken[:refused].merge(at: segments + taken[:refused][:at]) } : taken
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
#: (String) -> String
|
|
135
|
+
def self.article(word) = word.match?(/\A[aeiou]/i) ? "an #{word}" : "a #{word}"
|
|
136
|
+
|
|
137
|
+
# The plain JSON copy a claim judges, as JavaScript holds it, or where
|
|
138
|
+
# the first part that is not plain JSON sits and what it is. A whole
|
|
139
|
+
# float is the integer JavaScript's one number type would hold, so
|
|
140
|
+
# 2500.0 is judged and shown as 2500; a symbol is its name.
|
|
141
|
+
#: (untyped, ?Integer) -> Hash[Symbol, untyped]
|
|
142
|
+
def self.snapshot(value, max_depth = MAX_DEPTH)
|
|
143
|
+
ancestors = {} #: Hash[Integer, true]
|
|
144
|
+
refused = nil #: Hash[Symbol, untyped]?
|
|
145
|
+
refuse = lambda do |at, problem, limit = nil|
|
|
146
|
+
refused = { at: at, problem: problem, **(limit ? { limit: limit } : {}) }
|
|
147
|
+
nil
|
|
148
|
+
end
|
|
149
|
+
copy = lambda do |current, at|
|
|
150
|
+
case current
|
|
151
|
+
when nil, true, false, String then current
|
|
152
|
+
when Symbol then current.to_s
|
|
153
|
+
# JavaScript holds such an integer only as the nearest double.
|
|
154
|
+
when Integer then current.abs <= SAFE ? current : refuse.(at, "is the integer #{AriaSnapshot.js_number(current.to_f)}, beyond Number.MAX_SAFE_INTEGER")
|
|
155
|
+
when Float
|
|
156
|
+
next refuse.(at, "is NaN") if current.nan?
|
|
157
|
+
next refuse.(at, "is #{current.positive? ? "" : "-"}Infinity") if current.infinite?
|
|
158
|
+
next current unless current == current.floor
|
|
159
|
+
next current.to_i if current.abs <= SAFE
|
|
160
|
+
|
|
161
|
+
refuse.(at, "is the integer #{AriaSnapshot.js_number(current)}, beyond Number.MAX_SAFE_INTEGER")
|
|
162
|
+
when Array, Hash
|
|
163
|
+
next refuse.(at, "refers back to a value that holds it") if ancestors[current.object_id]
|
|
164
|
+
next refuse.([], "nests deeper than #{max_depth} levels", max_depth) if at.size >= max_depth
|
|
165
|
+
|
|
166
|
+
ancestors[current.object_id] = true
|
|
167
|
+
out = if current.is_a?(Array)
|
|
168
|
+
current.each_with_index.map do |item, index|
|
|
169
|
+
item = copy.(item, [*at, index.to_s])
|
|
170
|
+
break if refused
|
|
171
|
+
|
|
172
|
+
item
|
|
173
|
+
end
|
|
174
|
+
else
|
|
175
|
+
pairs = current.map do |key, item|
|
|
176
|
+
next refuse.(at, "has a key that is #{article(key.class.name.to_s)}") unless key.is_a?(String) || key.is_a?(Symbol)
|
|
177
|
+
|
|
178
|
+
item = copy.(item, [*at, key.to_s])
|
|
179
|
+
break if refused
|
|
180
|
+
|
|
181
|
+
[key.to_s, item]
|
|
182
|
+
end
|
|
183
|
+
refused ? nil : js_order(pairs.to_h)
|
|
184
|
+
end
|
|
185
|
+
ancestors.delete(current.object_id)
|
|
186
|
+
out
|
|
187
|
+
else
|
|
188
|
+
name = current.class.name
|
|
189
|
+
refuse.(at, name.nil? || name.empty? ? "is an object of no class" : "is #{article(name.split("::").last.to_s)}")
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
result = copy.(value, [])
|
|
193
|
+
refused ? { refused: refused } : { value: result }
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# A JavaScript object lists its array-index keys first, in ascending
|
|
197
|
+
# order, then its other keys in the order they were added.
|
|
198
|
+
#: (Hash[String, untyped]) -> Hash[String, untyped]
|
|
199
|
+
def self.js_order(hash)
|
|
200
|
+
index = hash.keys.select { _1.match?(INDEX) && _1.to_i < 2**32 - 1 }
|
|
201
|
+
return hash if index.empty?
|
|
202
|
+
|
|
203
|
+
(index.sort_by(&:to_i) + (hash.keys - index)).to_h { [_1, hash[_1]] }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
#: (Place) -> String
|
|
207
|
+
def self.name_of(place) = place.label.end_with?(":") ? "the value observe returned" : place.label
|
|
208
|
+
|
|
209
|
+
#: (Place, Hash[Symbol, untyped]) -> String
|
|
210
|
+
def self.refusal_reason(place, refusal)
|
|
211
|
+
name = name_of(place_at(place, refusal[:at].join(".")))
|
|
212
|
+
return "#{name} #{refusal[:problem]}, so the claim was not judged: the record keeps no value nested that deep" if refusal[:limit]
|
|
213
|
+
|
|
214
|
+
"#{name} #{refusal[:problem]}, which is not plain JSON, so the claim was not judged: a claim judges a plain JSON copy of the value it reads"
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
#: (Integer, Place, Array[String]) -> String
|
|
218
|
+
def self.first_missing(index, base, rest)
|
|
219
|
+
return "" if index >= rest.size - 1
|
|
220
|
+
return ", since #{name_of(base)} is undefined" if index.negative?
|
|
221
|
+
|
|
222
|
+
", since #{name_of(place_at(base, rest.take(index).join(".")))} has nothing at #{rest[index]}"
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
#: (Hash[Symbol, untyped], Place, Array[String], Integer, ?^(Place) -> String) -> Side
|
|
226
|
+
def self.side_of(read, base, rest, skip, name = method(:name_of))
|
|
227
|
+
place = place_at(base, rest.join("."))
|
|
228
|
+
if read[:refused]
|
|
229
|
+
return Side.new(place: place, found: { unjudged: refusal_reason(base, read[:refused].merge(at: read[:refused][:at].drop(skip))) })
|
|
230
|
+
end
|
|
231
|
+
return Side.new(place: place, found: { broken: "#{name.(place)} is undefined#{first_missing(read[:missing] - skip, base, rest)}" }) if read.key?(:missing)
|
|
232
|
+
|
|
233
|
+
Side.new(place: place, found: { value: read[:value] })
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
#: (Array[String], Inputs, ?^(Place) -> String) -> Side
|
|
237
|
+
def self.arg_side(segments, inputs, name = method(:name_of))
|
|
238
|
+
head, *rest = segments
|
|
239
|
+
side_of(read_at(inputs.args, segments), arg_place(head.to_s, inputs), rest, 1, name)
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The side a claim's actual names, read at its path and, with no
|
|
243
|
+
# view, at its at too; `at_read` says the at was read here.
|
|
244
|
+
#: (Hash[Symbol, untyped], Inputs) -> [Side, bool]
|
|
245
|
+
def self.top_side(claim, inputs)
|
|
246
|
+
path = segments(claim[:actual].to_s)
|
|
247
|
+
read_segments = claim[:view].nil? && claim[:at] ? path + claim[:at].to_s.split(".") : path
|
|
248
|
+
at_read = read_segments.size > path.size
|
|
249
|
+
from = inputs.from_keys.include?(path.first) || !inputs.observes
|
|
250
|
+
read = from ? read_at(inputs.args, read_segments) : read_at(inputs.observed, read_segments)
|
|
251
|
+
base = from ? arg_place(path.first.to_s, inputs) : OBSERVE
|
|
252
|
+
skip = from ? 1 : 0
|
|
253
|
+
rest = path.drop(skip)
|
|
254
|
+
if at_read && read.key?(:missing) && read[:missing] >= path.size
|
|
255
|
+
place = place_at(base, rest.join("."))
|
|
256
|
+
return [Side.new(place: place, found: { broken: "#{place.label} has nothing at #{claim[:at]}" }), at_read]
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
[side_of(read, base, rest, skip), at_read]
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# The value after a view, or why the view could not read it.
|
|
263
|
+
#: (String?, untyped) -> Hash[Symbol, untyped]
|
|
264
|
+
def self.apply_view(view, value)
|
|
265
|
+
return { value: value } unless view
|
|
266
|
+
return { reason: "view #{view} reads a string, and the value is #{article(Claims.json_type(value))}" } unless value.is_a?(String)
|
|
267
|
+
return { value: Claims.view(view, value) } unless view == "json"
|
|
268
|
+
|
|
269
|
+
parsed = begin
|
|
270
|
+
JsJson.parse(value)
|
|
271
|
+
rescue JSON::ParserError => e
|
|
272
|
+
return { reason: "view json could not parse the value: #{e.message.lines.first.to_s.strip}" }
|
|
273
|
+
end
|
|
274
|
+
snapshot(parsed)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
#: (Side, String?, String?, ?bool) -> Side
|
|
278
|
+
def self.shaped(side, view, at, at_read = false)
|
|
279
|
+
viewed_place = place_viewed(side.place, view)
|
|
280
|
+
place = place_at(viewed_place, at)
|
|
281
|
+
return Side.new(place: place, found: side.found) unless side.found.key?(:value)
|
|
282
|
+
return Side.new(place: place, found: side.found) if at_read
|
|
283
|
+
|
|
284
|
+
viewed = apply_view(view, side.found[:value])
|
|
285
|
+
return Side.new(place: place, found: { broken: viewed[:reason] }) if viewed[:reason]
|
|
286
|
+
return Side.new(place: place, found: { unjudged: refusal_reason(viewed_place, viewed[:refused]) }) if viewed[:refused]
|
|
287
|
+
return Side.new(place: viewed_place, found: { value: viewed[:value] }) if at.nil?
|
|
288
|
+
|
|
289
|
+
read = read_at(viewed[:value], at.split("."))
|
|
290
|
+
return Side.new(place: place, found: { value: read[:value] }) if read.key?(:value)
|
|
291
|
+
|
|
292
|
+
Side.new(place: place, found: { broken: "#{viewed_place.label} has nothing at #{at}" })
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
#: (Side, bool?) -> Side
|
|
296
|
+
def self.counted(side, count)
|
|
297
|
+
return side unless count == true
|
|
298
|
+
|
|
299
|
+
place = place_counted(side.place, count)
|
|
300
|
+
return Side.new(place: place, found: side.found) unless side.found.key?(:value)
|
|
301
|
+
|
|
302
|
+
value = side.found[:value]
|
|
303
|
+
return Side.new(place: place, found: { broken: "count reads an array, and the value is #{article(Claims.json_type(value))}" }) unless value.is_a?(Array)
|
|
304
|
+
|
|
305
|
+
Side.new(place: place, found: { value: value.size })
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
# The expected side, and whether observe gave it.
|
|
309
|
+
#: (Hash[Symbol, untyped], Inputs) -> [Side, bool]
|
|
310
|
+
def self.expected_side(claim, inputs)
|
|
311
|
+
name = ->(place) { "the expected value #{place.label}" }
|
|
312
|
+
case claim[:expected]
|
|
313
|
+
in String => key if (reason = inputs.transformed[key])
|
|
314
|
+
[Side.new(place: arg_place(key, inputs), found: { unjudged: reason }), false]
|
|
315
|
+
in String => key then [shaped(arg_side([key], inputs, name), claim[:expected_view], nil), false]
|
|
316
|
+
in { from: String => key } then [arg_side(segments(key), inputs, name), false]
|
|
317
|
+
in { observe: String => path } then [side_of(read_at(inputs.observed, segments(path)), OBSERVE, segments(path), 0, name), true]
|
|
318
|
+
in { literal: literal } then [Side.new(place: LITERAL, found: { value: literal.is_a?(Array) ? [] : literal }), false]
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# What stops a claim before any compare runs, or nil.
|
|
323
|
+
#: (Side, Side, bool, Inputs) -> Hash[Symbol, String]?
|
|
324
|
+
def self.precondition(actual, expected, from_observe, inputs)
|
|
325
|
+
[actual, expected].each do |side|
|
|
326
|
+
return { broken: side.found[:broken] } if side.found[:broken]
|
|
327
|
+
return { unjudged: side.found[:unjudged] } if side.found[:unjudged]
|
|
328
|
+
end
|
|
329
|
+
return { broken: "both sides read #{actual.place.label}, so the claim compares a value with itself" } if same_place?(actual.place, expected.place)
|
|
330
|
+
|
|
331
|
+
received = observe_received(expected.place, inputs, "the expected value reads")
|
|
332
|
+
received ||= observe_received(actual.place, inputs, "the actual value reads") if from_observe
|
|
333
|
+
return received if received
|
|
334
|
+
if from_observe && [true, false].include?(expected.found[:value])
|
|
335
|
+
return { broken: "the expected value #{expected.place.label} is a boolean observe computed, which is a judgment made in code: " \
|
|
336
|
+
"return the thing measured and let the claim compare it" }
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
nil
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
#: (Place, Inputs, String) -> Hash[Symbol, String]?
|
|
343
|
+
def self.observe_received(place, inputs, reads)
|
|
344
|
+
inputs.received.each do |key|
|
|
345
|
+
input = arg_place(key, inputs)
|
|
346
|
+
next unless overlap?(input, place)
|
|
347
|
+
|
|
348
|
+
return { broken: "observe received args.#{key}, which is #{input.label}, and #{reads} #{place.label}, " \
|
|
349
|
+
"so observe could return the value the claim compares it with" }
|
|
350
|
+
end
|
|
351
|
+
nil
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# A top-level comparison claim, judged as Claims.judge_one reports one.
|
|
355
|
+
#: (Hash[Symbol, untyped], Inputs) -> Hash[Symbol, untyped]
|
|
356
|
+
def self.judge(claim, inputs)
|
|
357
|
+
raw, at_read = top_side(claim, inputs)
|
|
358
|
+
judge_comparison(claim, raw, inputs, at_read)
|
|
359
|
+
rescue StandardError => e
|
|
360
|
+
unjudgeable(claim, e)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# One comparison on a side already read: a top-level claim's, or one
|
|
364
|
+
# item's under an each.
|
|
365
|
+
#: (Hash[Symbol, untyped], Side, Inputs, ?bool) -> Hash[Symbol, untyped]
|
|
366
|
+
def self.judge_comparison(claim, raw, inputs, at_read = false)
|
|
367
|
+
expected, from_observe = expected_side(claim, inputs)
|
|
368
|
+
actual = counted(shaped(raw, claim[:view], claim[:at], at_read), claim[:count])
|
|
369
|
+
base = { **(actual.found.key?(:value) ? { actual: actual.found[:value] } : {}), compare: claim[:compare],
|
|
370
|
+
**(expected.found.key?(:value) ? { expected: expected.found[:value] } : {}) }
|
|
371
|
+
stop = precondition(actual, expected, from_observe, inputs)
|
|
372
|
+
return base.merge(holds: false, reason: stop[:broken] || stop[:unjudged], **(stop[:unjudged] ? { unjudged: true } : {})) if stop
|
|
373
|
+
return base.merge(holds: true, for_judgment: true) if claim[:compare] == "judged-as"
|
|
374
|
+
|
|
375
|
+
result = Claims.compare(claim[:compare], actual.found[:value], expected.found[:value])
|
|
376
|
+
base.merge(holds: result[:holds] == true, **(result[:reason] ? { reason: result[:reason] } : {}),
|
|
377
|
+
**(result[:stopped] ? { reason: result[:stopped], unjudged: true } : {}))
|
|
378
|
+
rescue StandardError => e
|
|
379
|
+
unjudgeable(claim, e)
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
#: (Hash[Symbol, untyped], StandardError) -> Hash[Symbol, untyped]
|
|
383
|
+
def self.unjudgeable(claim, error)
|
|
384
|
+
{ compare: claim[:compare], holds: false, reason: "nukadoko could not judge this claim: #{error.message}", unjudged: true }
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# The array an each reads, after its view and at.
|
|
388
|
+
#: (Hash[Symbol, untyped], Inputs) -> Side
|
|
389
|
+
def self.array_side(claim, inputs)
|
|
390
|
+
raw, at_read = top_side(claim, inputs)
|
|
391
|
+
shaped(raw, claim[:view], claim[:at], at_read)
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# The side an item claim reads: the item, or a path inside it. The item
|
|
395
|
+
# is part of a snapshot, so a read inside it finds a value or nothing.
|
|
396
|
+
#: (untyped, Place, Hash[Symbol, untyped]) -> Side
|
|
397
|
+
def self.item_side(item, item_place, entry)
|
|
398
|
+
path = entry[:actual] ? segments(entry[:actual].to_s) : []
|
|
399
|
+
side_of(read_at(item, path), item_place, path, 0)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
# What ends an each before any item: a declaration whose expected value
|
|
403
|
+
# observe received, or reads through what observe returned while observe
|
|
404
|
+
# received the array, does not depend on an item, and an empty array
|
|
405
|
+
# would hide it. Then a where clause that implies an item claim.
|
|
406
|
+
#: (Hash[Symbol, untyped], Place, Inputs) -> Hash[Symbol, String]?
|
|
407
|
+
def self.each_stop(claim, array, inputs)
|
|
408
|
+
entries = (claim[:each][:where] || []).each_with_index.map { ["each.where[#{_2}]", _1] } +
|
|
409
|
+
claim[:each][:claims].each_with_index.map { ["each.claims[#{_2}]", _1] }
|
|
410
|
+
entries.each do |label, entry|
|
|
411
|
+
expected, from_observe = expected_side(entry, inputs)
|
|
412
|
+
stop = observe_received(expected.place, inputs, "#{label}'s expected value reads")
|
|
413
|
+
stop ||= observe_received(array, inputs, "#{label} reads each item of") if from_observe
|
|
414
|
+
return stop if stop
|
|
415
|
+
end
|
|
416
|
+
implied = where_implication(claim, inputs)
|
|
417
|
+
implied ? { broken: implied } : nil
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# The first where clause and item claim that read one value where the
|
|
421
|
+
# clause implies the claim whatever the value is, as the reason that
|
|
422
|
+
# breaks the each: the claim holds on every item where selects, so
|
|
423
|
+
# "every item" measures nothing.
|
|
424
|
+
#: (Hash[Symbol, untyped], Inputs) -> String?
|
|
425
|
+
def self.where_implication(claim, inputs)
|
|
426
|
+
value_of = lambda do |entry|
|
|
427
|
+
side, = expected_side(entry, inputs)
|
|
428
|
+
side.found.key?(:value) ? side.found : nil
|
|
429
|
+
end
|
|
430
|
+
(claim[:each][:where] || []).each_with_index do |where, where_index|
|
|
431
|
+
next unless where[:compare] == "not-equal" || ClaimRules::ORDER_COMPARES.include?(where[:compare])
|
|
432
|
+
|
|
433
|
+
path = where[:actual] ? segments(where[:actual].to_s) : []
|
|
434
|
+
path += where[:at].to_s.split(".") if where[:view].nil? && where[:at]
|
|
435
|
+
claim[:each][:claims].each_with_index do |entry, claim_index|
|
|
436
|
+
next unless ClaimRules.one_item_value?(where, entry)
|
|
437
|
+
|
|
438
|
+
selects = value_of.(where)
|
|
439
|
+
holds = value_of.(entry)
|
|
440
|
+
next unless selects && holds && where_implies?(where[:compare], selects[:value], entry[:compare], holds[:value])
|
|
441
|
+
|
|
442
|
+
viewed = where[:view] ? " as #{where[:view]}#{" at #{where[:at]}" if where[:at]}" : ""
|
|
443
|
+
what = "#{"count of " if where[:count] == true}#{path.empty? ? "value" : path.join(".")}#{viewed}"
|
|
444
|
+
return "each.where[#{where_index}] selects the items whose #{what} is #{where[:compare]} #{JsJson.generate(selects[:value])}, " \
|
|
445
|
+
"and every such item holds each.claims[#{claim_index}], #{entry[:compare]} #{JsJson.generate(holds[:value])}, " \
|
|
446
|
+
"whatever the data, so the claim measures nothing"
|
|
447
|
+
end
|
|
448
|
+
end
|
|
449
|
+
nil
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
# Whether every value a where clause selects holds an item claim on the
|
|
453
|
+
# same value: the same order direction with a bound at least as strict,
|
|
454
|
+
# not-equal against the same value, or an is-a whose kind is the one JSON
|
|
455
|
+
# type the clause can compare.
|
|
456
|
+
#: (String, untyped, String, untyped) -> bool
|
|
457
|
+
def self.where_implies?(where, where_value, claim, claim_value)
|
|
458
|
+
if claim == "is-a"
|
|
459
|
+
kind = claim_value.is_a?(String) ? Spaces.trim(claim_value).downcase : nil
|
|
460
|
+
type = if where == "not-equal" then Claims.json_type(where_value)
|
|
461
|
+
elsif ClaimRules::ORDER_COMPARES.include?(where) && where_value.is_a?(Numeric) then "number"
|
|
462
|
+
end
|
|
463
|
+
return !kind.nil? && !type.nil? && ClaimRules::KIND_JSON_TYPES[kind] == type
|
|
464
|
+
end
|
|
465
|
+
if where == "not-equal"
|
|
466
|
+
return claim == "not-equal" && Claims.json_type(where_value) == Claims.json_type(claim_value) && Claims.deep_equal?(where_value, claim_value)
|
|
467
|
+
end
|
|
468
|
+
return false unless where_value.is_a?(Numeric) && claim_value.is_a?(Numeric)
|
|
469
|
+
|
|
470
|
+
up = %w[greater-than at-least]
|
|
471
|
+
down = %w[less-than at-most]
|
|
472
|
+
strict = %w[greater-than less-than]
|
|
473
|
+
loose_to_strict = !strict.include?(where) && strict.include?(claim)
|
|
474
|
+
return loose_to_strict ? where_value > claim_value : where_value >= claim_value if up.include?(where) && up.include?(claim)
|
|
475
|
+
return loose_to_strict ? where_value < claim_value : where_value <= claim_value if down.include?(where) && down.include?(claim)
|
|
476
|
+
|
|
477
|
+
false
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
end
|