nukadoko 0.1.0 → 0.2.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 +291 -0
- data/README.ja.md +58 -5
- data/README.md +15 -3
- data/exe/nuka +8 -0
- data/lib/nukadoko/aria_snapshot.rb +1008 -0
- data/lib/nukadoko/aria_tree.rb +150 -0
- data/lib/nukadoko/base.rb +274 -0
- data/lib/nukadoko/check.rb +669 -22
- data/lib/nukadoko/claim_check.rb +177 -0
- data/lib/nukadoko/claim_check_run.rb +293 -0
- data/lib/nukadoko/claim_line.rb +381 -0
- data/lib/nukadoko/claim_rules.rb +519 -0
- data/lib/nukadoko/claims.rb +407 -30
- data/lib/nukadoko/cli.rb +802 -19
- data/lib/nukadoko/codes.json +942 -0
- data/lib/nukadoko/codes.rb +19 -0
- data/lib/nukadoko/command.rb +214 -12
- data/lib/nukadoko/criteria.rb +172 -0
- data/lib/nukadoko/definition.rb +33 -41
- data/lib/nukadoko/env_check.rb +90 -0
- data/lib/nukadoko/features.rb +80 -10
- data/lib/nukadoko/fixtures.rb +296 -10
- data/lib/nukadoko/http_recording.rb +68 -0
- data/lib/nukadoko/init.rb +130 -0
- data/lib/nukadoko/judge.rb +221 -0
- data/lib/nukadoko/judgment_file.rb +78 -0
- data/lib/nukadoko/judgments.rb +256 -0
- data/lib/nukadoko/literal.rb +129 -0
- data/lib/nukadoko/measured.rb +105 -0
- data/lib/nukadoko/order.rb +84 -0
- data/lib/nukadoko/parts.rb +42 -21
- data/lib/nukadoko/pattern.rb +11 -1
- data/lib/nukadoko/poll.rb +17 -6
- data/lib/nukadoko/program.rb +105 -17
- data/lib/nukadoko/project_fixtures.rb +17 -2
- data/lib/nukadoko/report_allure.rb +751 -0
- data/lib/nukadoko/report_messages.rb +167 -0
- data/lib/nukadoko/report_run.rb +169 -0
- data/lib/nukadoko/run.rb +865 -88
- data/lib/nukadoko/run_record.rb +532 -0
- data/lib/nukadoko/scaffold.rb +1015 -0
- data/lib/nukadoko/schema.rb +61 -4
- data/lib/nukadoko/secrets.rb +359 -0
- data/lib/nukadoko/selection.rb +163 -0
- data/lib/nukadoko/signature.rb +4 -1
- data/lib/nukadoko/skill.rb +36 -0
- data/lib/nukadoko/step.rb +62 -21
- data/lib/nukadoko/summary.rb +662 -0
- data/lib/nukadoko/table.rb +34 -2
- data/lib/nukadoko/text_fit.rb +179 -0
- data/lib/nukadoko/trace.rb +244 -0
- data/lib/nukadoko/trace_actions.rb +116 -0
- data/lib/nukadoko/tree_passes.rb +119 -0
- data/lib/nukadoko/verdict.rb +128 -0
- data/lib/nukadoko/version.rb +2 -1
- data/lib/nukadoko/vocabulary.rb +6 -0
- data/lib/nukadoko/walk.rb +305 -0
- data/lib/nukadoko/walk_files.rb +473 -0
- data/lib/nukadoko/walk_step.rb +149 -0
- data/lib/nukadoko.rb +36 -0
- data/llms.txt +296 -0
- data/sig/generated/nukadoko/aria_snapshot.rbs +331 -0
- data/sig/generated/nukadoko/aria_tree.rbs +142 -0
- data/sig/generated/nukadoko/base.rbs +136 -0
- data/sig/generated/nukadoko/check.rbs +255 -0
- data/sig/generated/nukadoko/claim_check.rbs +118 -0
- data/sig/generated/nukadoko/claim_check_run.rbs +88 -0
- data/sig/generated/nukadoko/claim_line.rbs +158 -0
- data/sig/generated/nukadoko/claim_rules.rbs +172 -0
- data/sig/generated/nukadoko/claims.rbs +165 -0
- data/sig/generated/nukadoko/cli.rbs +182 -0
- data/sig/generated/nukadoko/codes.rbs +16 -0
- data/sig/generated/nukadoko/command.rbs +109 -0
- data/sig/generated/nukadoko/criteria.rbs +83 -0
- data/sig/generated/nukadoko/definition.rbs +30 -0
- data/sig/generated/nukadoko/env_check.rbs +38 -0
- data/sig/generated/nukadoko/features.rbs +101 -0
- data/sig/generated/nukadoko/fixtures.rbs +152 -0
- data/sig/generated/nukadoko/http_recording.rbs +29 -0
- data/sig/generated/nukadoko/init.rbs +66 -0
- data/sig/generated/nukadoko/judge.rbs +64 -0
- data/sig/generated/nukadoko/judgment_file.rbs +39 -0
- data/sig/generated/nukadoko/judgments.rbs +120 -0
- data/sig/generated/nukadoko/literal.rbs +71 -0
- data/sig/generated/nukadoko/measured.rbs +42 -0
- data/sig/generated/nukadoko/order.rbs +54 -0
- data/sig/generated/nukadoko/parts.rbs +35 -0
- data/sig/generated/nukadoko/pattern.rbs +28 -0
- data/sig/generated/nukadoko/poll.rbs +40 -0
- data/sig/generated/nukadoko/program.rbs +101 -0
- data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
- data/sig/generated/nukadoko/report_allure.rbs +268 -0
- data/sig/generated/nukadoko/report_messages.rbs +68 -0
- data/sig/generated/nukadoko/report_run.rbs +102 -0
- data/sig/generated/nukadoko/run.rbs +257 -0
- data/sig/generated/nukadoko/run_record.rbs +174 -0
- data/sig/generated/nukadoko/scaffold.rbs +328 -0
- data/sig/generated/nukadoko/schema.rbs +137 -0
- data/sig/generated/nukadoko/secrets.rbs +157 -0
- data/sig/generated/nukadoko/selection.rbs +73 -0
- data/sig/generated/nukadoko/signature.rbs +17 -0
- data/sig/generated/nukadoko/skill.rbs +20 -0
- data/sig/generated/nukadoko/step.rbs +115 -0
- data/sig/generated/nukadoko/summary.rbs +204 -0
- data/sig/generated/nukadoko/table.rbs +43 -0
- data/sig/generated/nukadoko/text_fit.rbs +70 -0
- data/sig/generated/nukadoko/trace.rbs +127 -0
- data/sig/generated/nukadoko/trace_actions.rbs +40 -0
- data/sig/generated/nukadoko/tree_passes.rbs +48 -0
- data/sig/generated/nukadoko/verdict.rbs +56 -0
- data/sig/generated/nukadoko/version.rbs +5 -0
- data/sig/generated/nukadoko/vocabulary.rbs +25 -0
- data/sig/generated/nukadoko/walk.rbs +139 -0
- data/sig/generated/nukadoko/walk_files.rbs +189 -0
- data/sig/generated/nukadoko/walk_step.rbs +50 -0
- data/sig/generated/nukadoko.rbs +15 -0
- data/sig/nukadoko.rbs +5 -48
- data/skills/nukadoko/SKILL.md +120 -0
- data/skills/nukadoko/references/checking-steps.md +200 -0
- data/skills/nukadoko/references/diagnosing.md +200 -0
- data/skills/nukadoko/references/done.md +99 -0
- data/skills/nukadoko/references/evidence.md +124 -0
- data/skills/nukadoko/references/examples/cli.md +230 -0
- data/skills/nukadoko/references/examples/http-api.md +339 -0
- data/skills/nukadoko/references/examples/workflow.md +173 -0
- data/skills/nukadoko/references/exploring.md +184 -0
- data/skills/nukadoko/references/fixtures.md +198 -0
- data/skills/nukadoko/references/from-prose.md +51 -0
- data/skills/nukadoko/references/judgments.md +95 -0
- data/skills/nukadoko/references/maintenance.md +97 -0
- data/skills/nukadoko/references/merge-risk.md +74 -0
- data/skills/nukadoko/references/parts.md +152 -0
- data/skills/nukadoko/references/perturbed-worlds.md +107 -0
- data/skills/nukadoko/references/program.md +200 -0
- data/skills/nukadoko/references/reviewing.md +200 -0
- data/skills/nukadoko/references/running.md +200 -0
- data/skills/nukadoko/references/scenario-rules.md +188 -0
- data/skills/nukadoko/references/states.md +66 -0
- data/skills/nukadoko/references/step-files.md +200 -0
- data/skills/nukadoko/references/verdicts.md +200 -0
- data/skills/nukadoko/references/writing-steps.md +200 -0
- data/skills/nukadoko/references/writing-the-feature.md +165 -0
- data/skills/nukadoko/scripts/claude-code-verdict-hook.mjs +99 -0
- data/skills/nukadoko/scripts/merge-risk.jq +56 -0
- data/skills/nukadoko-verification/SKILL.md +263 -0
- metadata +121 -1
data/lib/nukadoko/claims.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
2
3
|
|
|
3
4
|
# A Then step declares what it compares; the engine judges. A claim names the
|
|
4
5
|
# actual value (a key the step observed or was given, and an optional `at`
|
|
@@ -6,46 +7,176 @@
|
|
|
6
7
|
# key, `{ literal: v }`, or `{ from: key }` for an args key an earlier step
|
|
7
8
|
# filled. The judge returns every comparison it made, so a broken claim shows
|
|
8
9
|
# what was read.
|
|
10
|
+
require "uri"
|
|
11
|
+
require_relative "text_fit"
|
|
12
|
+
|
|
9
13
|
module Nukadoko
|
|
10
14
|
module Engine
|
|
11
15
|
module Claims
|
|
12
16
|
COMPARES = %w[equal not-equal contains-text starts-with-text ends-with-text has-item has-same-items has-fields
|
|
13
17
|
lacks-field fits-text fits-aria-snapshot is-a matches greater-than at-least less-than at-most judged-as].freeze
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
# The ARIA snapshots nukadoko took of what observe returned, by the
|
|
20
|
+
# value observe returned, so a claim judges the tree taken right
|
|
21
|
+
# after that reading and never reads a live page.
|
|
22
|
+
ARIA_TAKEN = ObjectSpace::WeakKeyMap.new #: ObjectSpace::WeakKeyMap[Hash[String, untyped], Hash[String, Hash[Symbol, String]]]
|
|
23
|
+
ARIA_TAKES = "fits-aria-snapshot takes the ARIA snapshot of a locator or page that observe returns: return the locator or page itself, and nukadoko takes its snapshot"
|
|
24
|
+
|
|
25
|
+
# Takes the ARIA snapshot of the locator or page at each path a
|
|
26
|
+
# fits-aria-snapshot claim reads, once per reading. One path's failure
|
|
27
|
+
# is that path's read, so taking them never raises.
|
|
28
|
+
#: (Array[Hash[Symbol, untyped]], untyped) -> untyped
|
|
29
|
+
def self.take_aria_snapshots(claims, observed)
|
|
30
|
+
paths = claims.filter_map { _1[:actual].to_s if !_1[:each] && _1[:compare] == "fits-aria-snapshot" }
|
|
31
|
+
judged = claims.filter_map { judged_path(_1) if !_1[:each] && _1[:compare] == "judged-as" }
|
|
32
|
+
return observed if (paths + judged).empty? || !observed.is_a?(Hash)
|
|
33
|
+
|
|
34
|
+
taken = paths.uniq.to_h { [_1, aria_snapshot_at(observed, _1.split("."))] }
|
|
35
|
+
# A judged value is read as the tree the browser printed when it is
|
|
36
|
+
# a locator or a page, and as its plain value when it is not, so a
|
|
37
|
+
# judged path that holds neither keeps no entry.
|
|
38
|
+
judged.uniq.each do |path|
|
|
39
|
+
read = aria_snapshot_at(observed, path.split("."))
|
|
40
|
+
taken["judged:#{path}"] = read unless read[:broken]
|
|
41
|
+
end
|
|
42
|
+
ARIA_TAKEN[observed] = taken
|
|
43
|
+
observed
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The path a judged-as claim reads: its actual and, with no view, its at.
|
|
47
|
+
#: (Hash[Symbol, untyped]) -> String
|
|
48
|
+
def self.judged_path(claim)
|
|
49
|
+
[claim[:actual], (claim[:at] unless claim[:view])].compact.join(".")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
#: (untyped, Array[String]) -> Hash[Symbol, String]
|
|
53
|
+
def self.aria_snapshot_at(root, segments)
|
|
54
|
+
found = segments.reduce(root) do |current, segment|
|
|
55
|
+
case current
|
|
56
|
+
when Hash then current.key?(segment) ? current[segment] : current.fetch(segment.to_sym, :missing)
|
|
57
|
+
when Array then segment.match?(/\A(0|[1-9][0-9]*)\z/) && segment.to_i < current.size ? current[segment.to_i] : :missing
|
|
58
|
+
else :missing
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
return { broken: "is undefined" } if found == :missing
|
|
62
|
+
return { broken: "is #{aria_value_kind(found)}, and #{ARIA_TAKES}" } unless aria_snapshot_taker?(found)
|
|
63
|
+
|
|
64
|
+
taker = found #: untyped
|
|
65
|
+
text = begin
|
|
66
|
+
taker.aria_snapshot
|
|
67
|
+
rescue StandardError => e
|
|
68
|
+
return { unjudged: "gave no ARIA snapshot: #{e.message.lines.first.to_s.chomp.delete_suffix(".")}, so the claim was not judged" }
|
|
69
|
+
end
|
|
70
|
+
text.is_a?(String) ? { text: text } : { unjudged: "gave #{aria_value_kind(text)} from ariaSnapshot() rather than a text, so the claim was not judged" }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Only Playwright's own locator and page: a value step code wrote with
|
|
74
|
+
# a method of that name would be a tree written in code.
|
|
75
|
+
#: (untyped) -> bool
|
|
76
|
+
def self.aria_snapshot_taker?(value)
|
|
77
|
+
value.class.ancestors.any? { %w[Playwright::Locator Playwright::Page].include?(_1.name) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The phrase for a value that is not a locator or a page, as the
|
|
81
|
+
# TypeScript engine names it.
|
|
82
|
+
#: (untyped) -> String
|
|
83
|
+
def self.aria_value_kind(value)
|
|
84
|
+
case value
|
|
85
|
+
when nil then "null"
|
|
86
|
+
when String then "a string"
|
|
87
|
+
when Numeric then "a number"
|
|
88
|
+
when true, false then "a boolean"
|
|
89
|
+
when Hash then "an Object"
|
|
90
|
+
when Array then "an Array"
|
|
91
|
+
else
|
|
92
|
+
name = value.class.name.to_s.split("::").last.to_s
|
|
93
|
+
name.empty? ? "an object of no class" : article_of(name)
|
|
22
94
|
end
|
|
23
95
|
end
|
|
24
96
|
|
|
97
|
+
#: (String) -> String
|
|
98
|
+
def self.article_of(word) = word.match?(/\A[aeiou]/i) ? "an #{word}" : "a #{word}"
|
|
99
|
+
|
|
100
|
+
# The side a fits-aria-snapshot claim reads: the text of the snapshot
|
|
101
|
+
# taken of the value at its actual path.
|
|
102
|
+
#: (Hash[Symbol, untyped], untyped) -> Hash[Symbol, String]
|
|
103
|
+
def self.aria_side(claim, observed)
|
|
104
|
+
path = claim[:actual].to_s
|
|
105
|
+
taken = (observed.is_a?(Hash) ? ARIA_TAKEN[observed] : nil)&.[](path) || { unjudged: "has no ARIA snapshot nukadoko took, so the claim was not judged" } #: Hash[Symbol, String]
|
|
106
|
+
taken[:text] ? taken : taken.transform_values { "observe:#{path} #{_1}" }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
#: (Array[Hash[Symbol, untyped]], untyped, untyped) -> Array[Hash[Symbol, untyped]]
|
|
25
110
|
def self.judge(claims, observed, args)
|
|
26
111
|
claims.map { judge_one(_1, observed, args) }
|
|
27
112
|
end
|
|
28
113
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
114
|
+
# `top` is what observe returned, which an expected { observe } reads
|
|
115
|
+
# from inside an each as well. A claim on an item with no actual reads
|
|
116
|
+
# the item itself.
|
|
117
|
+
#: (Hash[Symbol, untyped], untyped, untyped, ?untyped) -> Hash[Symbol, untyped]
|
|
118
|
+
def self.judge_one(claim, observed, args, top = observed)
|
|
119
|
+
actual = dig(view(claim[:view], claim[:actual].nil? ? observed : read(observed, claim[:actual])), claim[:at])
|
|
120
|
+
return judge_each(claim[:each], actual, args, top) if claim[:each]
|
|
121
|
+
return judge_aria(claim, observed, args, top) if claim[:compare] == "fits-aria-snapshot"
|
|
122
|
+
return judge_judged(claim, observed, args, top) if claim[:compare] == "judged-as" && aria_judged(claim, observed)
|
|
123
|
+
|
|
124
|
+
if claim[:count]
|
|
125
|
+
unless actual.is_a?(Array)
|
|
126
|
+
# A count of no array has no value, and the record says so by leaving the actual out.
|
|
127
|
+
return { compare: claim[:compare], expected: expected_value(claim, args, top), holds: false, reason: "count reads an array, and the value is #{article(json_type(actual))}" }
|
|
128
|
+
end
|
|
32
129
|
|
|
33
|
-
|
|
34
|
-
|
|
130
|
+
actual = actual.size
|
|
131
|
+
end
|
|
132
|
+
expected = expected_value(claim, args, top)
|
|
133
|
+
result = compare(claim[:compare], actual, expected)
|
|
134
|
+
{ actual: actual, compare: claim[:compare], expected: expected, holds: result[:holds] == true, **(result[:for_judgment] ? { for_judgment: true } : {}),
|
|
135
|
+
**(result[:reason] ? { reason: result[:reason] } : {}), **(result[:stopped] ? { reason: result[:stopped], unjudged: true } : {}) }
|
|
35
136
|
end
|
|
36
137
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
138
|
+
#: (Hash[Symbol, untyped], untyped, untyped, untyped) -> Hash[Symbol, untyped]
|
|
139
|
+
def self.judge_aria(claim, observed, args, top)
|
|
140
|
+
side = aria_side(claim, observed)
|
|
141
|
+
expected = expected_value(claim, args, top)
|
|
142
|
+
return { compare: claim[:compare], expected: expected, holds: false, reason: side[:broken] } if side[:broken]
|
|
143
|
+
return { compare: claim[:compare], expected: expected, holds: false, reason: side[:unjudged], unjudged: true } if side[:unjudged]
|
|
144
|
+
|
|
145
|
+
result = compare(claim[:compare], side[:text], expected)
|
|
146
|
+
{ actual: side[:text], compare: claim[:compare], expected: expected, holds: result[:holds] == true,
|
|
147
|
+
**(result[:reason] ? { reason: result[:reason] } : {}), **(result[:stopped] ? { reason: result[:stopped], unjudged: true } : {}) }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# The tree nukadoko took of a judged value that is a locator or a page.
|
|
151
|
+
#: (Hash[Symbol, untyped], untyped) -> Hash[Symbol, String]?
|
|
152
|
+
def self.aria_judged(claim, observed)
|
|
153
|
+
observed.is_a?(Hash) ? ARIA_TAKEN[observed]&.[]("judged:#{judged_path(claim)}") : nil
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
#: (Hash[Symbol, untyped], untyped, untyped, untyped) -> Hash[Symbol, untyped]
|
|
157
|
+
def self.judge_judged(claim, observed, args, top)
|
|
158
|
+
taken = aria_judged(claim, observed) || {}
|
|
159
|
+
expected = expected_value(claim, args, top)
|
|
160
|
+
return { compare: claim[:compare], expected: expected, holds: false, reason: "observe:#{judged_path(claim)} #{taken[:unjudged]}", unjudged: true, aria: true } if taken[:unjudged]
|
|
161
|
+
|
|
162
|
+
{ actual: taken[:text], compare: claim[:compare], expected: expected, holds: true, for_judgment: true, aria: true }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
#: (Hash[Symbol, untyped], untyped, untyped, untyped) -> Hash[Symbol, untyped]
|
|
166
|
+
def self.judge_each(each, items, args, top)
|
|
167
|
+
chosen = Array(items).select { |item| (each[:where] || []).all? { judge_one(_1, item, args, top)[:holds] } }
|
|
168
|
+
results = chosen.flat_map { |item| each[:claims].map { judge_one(_1, item, args, top) } }
|
|
40
169
|
{ each: results, chosen: chosen.size, holds: !chosen.empty? && results.all? { _1[:holds] } }
|
|
41
170
|
end
|
|
42
171
|
|
|
43
|
-
|
|
172
|
+
#: (Hash[Symbol, untyped], untyped, ?untyped) -> untyped
|
|
173
|
+
def self.expected_value(claim, args, top = nil)
|
|
44
174
|
value =
|
|
45
175
|
case claim[:expected]
|
|
46
176
|
in String => key then read(args, key)
|
|
47
177
|
in { literal: literal } then literal
|
|
48
178
|
in { from: String => key } then read(args, key)
|
|
179
|
+
in { observe: String => path } then dig(read(top, path.split(".").first), path.split(".").drop(1).join(".").then { _1.empty? ? nil : _1 })
|
|
49
180
|
end
|
|
50
181
|
claim[:expected_view] == "json" ? JSON.parse(value) : value
|
|
51
182
|
end
|
|
@@ -53,6 +184,7 @@ module Nukadoko
|
|
|
53
184
|
VIEWS = %w[text lines comma-list json].freeze
|
|
54
185
|
|
|
55
186
|
# A view reads a string as something else before `at` walks into it.
|
|
187
|
+
#: (String?, untyped) -> untyped
|
|
56
188
|
def self.view(name, value)
|
|
57
189
|
return value unless name
|
|
58
190
|
raise ArgumentError, "view #{name} reads a string, and the value is #{value.inspect}" unless value.is_a?(String)
|
|
@@ -66,19 +198,23 @@ module Nukadoko
|
|
|
66
198
|
end
|
|
67
199
|
end
|
|
68
200
|
|
|
201
|
+
#: (untyped, String | Symbol) -> untyped
|
|
69
202
|
def self.read(hash, key)
|
|
70
203
|
hash = hash.transform_keys(&:to_s) if hash.is_a?(Hash)
|
|
71
204
|
hash.fetch(key.to_s)
|
|
72
205
|
end
|
|
73
206
|
|
|
207
|
+
#: (untyped, untyped) -> untyped
|
|
74
208
|
def self.dig(value, path)
|
|
75
209
|
return value unless path
|
|
76
210
|
|
|
77
211
|
path.to_s.split(".").reduce(value) { |v, part| v.is_a?(Array) ? v[Integer(part)] : read(v, part) }
|
|
78
212
|
end
|
|
79
213
|
|
|
214
|
+
#: (untyped, untyped) -> bool
|
|
80
215
|
def self.same?(a, b) = normal(a) == normal(b)
|
|
81
216
|
|
|
217
|
+
#: (untyped) -> untyped
|
|
82
218
|
def self.normal(value)
|
|
83
219
|
case value
|
|
84
220
|
when Hash then value.to_h { |k, v| [k.to_s, normal(v)] }
|
|
@@ -88,23 +224,264 @@ module Nukadoko
|
|
|
88
224
|
end
|
|
89
225
|
end
|
|
90
226
|
|
|
91
|
-
|
|
227
|
+
# A pattern that backtracks gets this long before the claim is left unjudged.
|
|
228
|
+
PATTERN_TIMEOUT = 1.0
|
|
229
|
+
|
|
230
|
+
# What `is-a` names, as the TypeScript engine lists the kinds.
|
|
231
|
+
VALUE_KINDS = %w[string text number integer boolean list array object null uuid date timestamp url].freeze
|
|
232
|
+
DATE = /\A(\d{4})-(\d{2})-(\d{2})\z/
|
|
233
|
+
# Seconds are required and a leap second refused, as a program's clock
|
|
234
|
+
# never prints 60; the separator and the offset take the spellings
|
|
235
|
+
# RFC 3339 and ISO 8601 allow.
|
|
236
|
+
TIMESTAMP = /\A(\d{4})-(\d{2})-(\d{2})[Tt ](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:[Zz]|[+-](\d{2})(?::?(\d{2}))?)\z/
|
|
237
|
+
UUID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
|
|
238
|
+
|
|
239
|
+
#: (Integer, Integer, Integer) -> bool
|
|
240
|
+
def self.calendar_day?(year, month, day)
|
|
241
|
+
return false if month < 1 || month > 12 || day < 1
|
|
242
|
+
|
|
243
|
+
leap = (year % 4).zero? && !(year % 100).zero? || (year % 400).zero?
|
|
244
|
+
day <= [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
#: (untyped) -> bool
|
|
248
|
+
def self.date?(value)
|
|
249
|
+
match = value.is_a?(String) ? DATE.match(value) : nil
|
|
250
|
+
return false unless match
|
|
251
|
+
|
|
252
|
+
year, month, day = match.captures.map(&:to_i)
|
|
253
|
+
calendar_day?(year.to_i, month.to_i, day.to_i)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
#: (untyped) -> bool
|
|
257
|
+
def self.timestamp?(value)
|
|
258
|
+
match = value.is_a?(String) ? TIMESTAMP.match(value) : nil
|
|
259
|
+
return false unless match
|
|
260
|
+
|
|
261
|
+
year, month, day, hour, minute, second = match.captures.map(&:to_i)
|
|
262
|
+
return false unless calendar_day?(year.to_i, month.to_i, day.to_i)
|
|
263
|
+
|
|
264
|
+
hour = hour.to_i
|
|
265
|
+
minute = minute.to_i
|
|
266
|
+
second = second.to_i
|
|
267
|
+
offset = match[7].nil? || (match[7].to_i <= 23 && match[8].to_i <= 59)
|
|
268
|
+
hour <= 23 && minute <= 59 && second <= 59 && offset
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# A text a URL parser accepts can still be no URL a program wrote, so
|
|
272
|
+
# the scheme needs "//" and no character a reader cannot see may appear.
|
|
273
|
+
#: (untyped) -> bool
|
|
274
|
+
def self.url?(value)
|
|
275
|
+
return false unless value.is_a?(String) && value.match?(%r{\Ahttps?://}i) && !value.match?(/[\s\p{Cc}\p{Cf}]/)
|
|
276
|
+
|
|
277
|
+
uri = URI.parse(value)
|
|
278
|
+
%w[http https].include?(uri.scheme&.downcase) && !uri.host.to_s.empty?
|
|
279
|
+
rescue URI::Error
|
|
280
|
+
false
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
KIND_TESTS = {
|
|
284
|
+
"string" => ->(v) { v.is_a?(String) }, "text" => ->(v) { v.is_a?(String) },
|
|
285
|
+
"number" => ->(v) { v.is_a?(Numeric) }, "integer" => ->(v) { v.is_a?(Integer) || (v.is_a?(Float) && v.finite? && v == v.floor) },
|
|
286
|
+
"boolean" => ->(v) { [true, false].include?(v) }, "list" => ->(v) { v.is_a?(Array) }, "array" => ->(v) { v.is_a?(Array) },
|
|
287
|
+
"object" => ->(v) { v.is_a?(Hash) }, "null" => ->(v) { v.nil? }, "uuid" => ->(v) { v.is_a?(String) && v.match?(UUID) },
|
|
288
|
+
"date" => method(:date?), "timestamp" => method(:timestamp?), "url" => method(:url?)
|
|
289
|
+
}.freeze
|
|
290
|
+
|
|
291
|
+
# The test of the kind `name` names, read trimmed and in any case.
|
|
292
|
+
#: (String) -> untyped
|
|
293
|
+
def self.value_kind(name) = KIND_TESTS[name.strip.downcase]
|
|
294
|
+
|
|
295
|
+
#: (untyped) -> String
|
|
296
|
+
def self.json_type(value)
|
|
297
|
+
case value
|
|
298
|
+
when nil then "null"
|
|
299
|
+
when true, false then "boolean"
|
|
300
|
+
when Numeric then "number"
|
|
301
|
+
when String then "string"
|
|
302
|
+
when Array then "array"
|
|
303
|
+
else "object"
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
#: (String) -> String
|
|
308
|
+
def self.article(type) = %w[array object].include?(type) ? "an #{type}" : "a #{type}"
|
|
309
|
+
|
|
310
|
+
# JavaScript's trim, which takes Unicode spaces and a byte order mark.
|
|
311
|
+
#: (String) -> bool
|
|
312
|
+
def self.blank?(text) = text.match?(/\A[[:space:]\u{feff}]*\z/)
|
|
313
|
+
|
|
314
|
+
# Structural equality over JSON values: key order does not count, and
|
|
315
|
+
# 1 equals 1.0, as JSON writes one number.
|
|
316
|
+
#: (untyped, untyped) -> bool
|
|
317
|
+
def self.deep_equal?(a, b)
|
|
318
|
+
type = json_type(a)
|
|
319
|
+
return false unless type == json_type(b)
|
|
320
|
+
|
|
321
|
+
case type
|
|
322
|
+
when "array" then a.size == b.size && a.each_index.all? { deep_equal?(a[_1], b[_1]) }
|
|
323
|
+
when "object"
|
|
324
|
+
left = normal(a)
|
|
325
|
+
right = normal(b)
|
|
326
|
+
left.size == right.size && left.all? { |key, value| right.key?(key) && deep_equal?(value, right[key]) }
|
|
327
|
+
else a == b
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
# The same items, each as many times, in any order.
|
|
332
|
+
#: (Array[untyped], Array[untyped]) -> bool
|
|
333
|
+
def self.same_items?(a, b)
|
|
334
|
+
return false unless a.size == b.size
|
|
335
|
+
|
|
336
|
+
unmatched = b.dup
|
|
337
|
+
a.all? do |item|
|
|
338
|
+
index = unmatched.index { deep_equal?(item, _1) }
|
|
339
|
+
index && unmatched.delete_at(index)
|
|
340
|
+
!index.nil?
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# The first place `expected` does not fit `actual` under has-fields, or
|
|
345
|
+
# nil: an object fits one that holds each of its fields with a value
|
|
346
|
+
# that fits, an array one of its length whose items fit, and anything
|
|
347
|
+
# else an equal value.
|
|
348
|
+
#: (untyped, untyped, ?Array[String | Integer]) -> Hash[Symbol, untyped]?
|
|
349
|
+
def self.field_misfit(actual, expected, path = [])
|
|
350
|
+
case json_type(expected)
|
|
351
|
+
when "object"
|
|
352
|
+
return { kind: "differs", path: path, actual: actual, expected: expected } unless json_type(actual) == "object"
|
|
353
|
+
|
|
354
|
+
have = normal(actual)
|
|
355
|
+
normal(expected).each do |key, value|
|
|
356
|
+
return { kind: "missing", path: [*path, key], expected: value } unless have.key?(key)
|
|
357
|
+
|
|
358
|
+
found = field_misfit(have[key], value, [*path, key])
|
|
359
|
+
return found if found
|
|
360
|
+
end
|
|
361
|
+
nil
|
|
362
|
+
when "array"
|
|
363
|
+
return { kind: "differs", path: path, actual: actual, expected: expected } unless actual.is_a?(Array)
|
|
364
|
+
return { kind: "length", path: path, actual: actual.size, expected: expected.size } unless actual.size == expected.size
|
|
365
|
+
|
|
366
|
+
expected.each_index do |index|
|
|
367
|
+
found = field_misfit(actual[index], expected[index], [*path, index.to_s])
|
|
368
|
+
return found if found
|
|
369
|
+
end
|
|
370
|
+
nil
|
|
371
|
+
else deep_equal?(actual, expected) ? nil : { kind: "differs", path: path, actual: actual, expected: expected }
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# The path of the first object with no field, at any depth, or nil.
|
|
376
|
+
#: (untyped, ?Array[String | Integer]) -> Array[String | Integer]?
|
|
377
|
+
def self.fieldless_object(value, path = [])
|
|
378
|
+
type = json_type(value)
|
|
379
|
+
return nil unless %w[object array].include?(type)
|
|
380
|
+
|
|
381
|
+
entries = type == "array" ? value.each_with_index.map { |item, index| [index.to_s, item] } : normal(value).to_a
|
|
382
|
+
return path if type == "object" && entries.empty?
|
|
383
|
+
|
|
384
|
+
entries.each do |key, item|
|
|
385
|
+
found = fieldless_object(item, [*path, key])
|
|
386
|
+
return found if found
|
|
387
|
+
end
|
|
388
|
+
nil
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
# { holds: } or { reason: } when the two sides cannot be compared, or
|
|
392
|
+
# { stopped: } when nothing judged them. A comparison across two JSON
|
|
393
|
+
# types never holds for the reason the claim states, so it breaks with
|
|
394
|
+
# both types named, and so does an expected value every actual holds.
|
|
395
|
+
#: (String, untyped, untyped) -> Hash[Symbol, untyped]
|
|
396
|
+
def self.compare(compare, actual, expected)
|
|
397
|
+
a_type = json_type(actual)
|
|
398
|
+
e_type = json_type(expected)
|
|
92
399
|
case compare
|
|
93
|
-
when "equal"
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
when "
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
400
|
+
when "equal", "not-equal"
|
|
401
|
+
return { reason: "#{compare} compares #{article(a_type)} with #{article(e_type)}" } unless a_type == e_type
|
|
402
|
+
|
|
403
|
+
same = deep_equal?(actual, expected)
|
|
404
|
+
{ holds: compare == "equal" ? same : !same }
|
|
405
|
+
when "contains-text", "starts-with-text", "ends-with-text", "matches"
|
|
406
|
+
return { reason: "#{compare} reads two strings, and compares #{article(a_type)} with #{article(e_type)}" } unless a_type == "string" && e_type == "string"
|
|
407
|
+
|
|
408
|
+
if compare != "matches"
|
|
409
|
+
return { reason: "#{compare} has an expected text of only whitespace or nothing, which almost every string holds" } if blank?(expected)
|
|
410
|
+
|
|
411
|
+
return { holds: { "contains-text" => :include?, "starts-with-text" => :start_with?, "ends-with-text" => :end_with? }.then { actual.send(_1[compare], expected) } }
|
|
412
|
+
end
|
|
413
|
+
return { reason: "matches has an empty expected value, which every string holds" } if expected.empty?
|
|
414
|
+
|
|
415
|
+
pattern = begin
|
|
416
|
+
Regexp.new(expected, timeout: PATTERN_TIMEOUT)
|
|
417
|
+
rescue RegexpError => e
|
|
418
|
+
return { reason: "matches could not build a regular expression: #{e.message}" }
|
|
419
|
+
end
|
|
420
|
+
return { reason: "matches has a pattern that matches the empty string, so every string holds it" } if pattern.match?("")
|
|
421
|
+
|
|
422
|
+
begin
|
|
423
|
+
{ holds: pattern.match?(actual) }
|
|
424
|
+
rescue Regexp::TimeoutError
|
|
425
|
+
{ stopped: "matches ran for more than #{PATTERN_TIMEOUT} seconds, so nukadoko stopped judging it" }
|
|
426
|
+
end
|
|
427
|
+
when "fits-text"
|
|
428
|
+
return { reason: "fits-text reads two strings, and compares #{article(a_type)} with #{article(e_type)}" } unless a_type == "string" && e_type == "string"
|
|
429
|
+
return { reason: "fits-text has an expected text of only whitespace, ... lines, [..], and [..:name], which almost every text holds" } if TextFit.only_stand_ins?(expected)
|
|
430
|
+
|
|
431
|
+
{ holds: TextFit.fit(actual, expected)[:fits] }
|
|
432
|
+
when "fits-aria-snapshot"
|
|
433
|
+
return { reason: "fits-aria-snapshot reads two ARIA snapshots as texts, and compares #{article(a_type)} with #{article(e_type)}" } unless a_type == "string" && e_type == "string"
|
|
434
|
+
|
|
435
|
+
judged = AriaSnapshot.judge(actual, expected)
|
|
436
|
+
return { reason: judged[:broken] } if judged[:broken]
|
|
437
|
+
return { stopped: judged[:unjudged] } if judged[:unjudged]
|
|
438
|
+
|
|
439
|
+
{ holds: judged[:fits] }
|
|
440
|
+
when "is-a"
|
|
441
|
+
return { reason: "is-a reads the name of a kind as a string, and the expected value is #{article(e_type)}" } unless e_type == "string"
|
|
442
|
+
|
|
443
|
+
kind = value_kind(expected) or return { reason: "is-a names one of the kinds #{VALUE_KINDS.join(", ")}, and the expected value is #{JSON.generate(expected)}" }
|
|
444
|
+
{ holds: kind.(actual) }
|
|
445
|
+
when "lacks-field"
|
|
446
|
+
return { reason: "lacks-field reads an object, and the actual value is #{article(a_type)}" } unless a_type == "object"
|
|
447
|
+
return { reason: "lacks-field reads the name of a field as a string, and the expected value is #{article(e_type)}" } unless e_type == "string"
|
|
448
|
+
return { reason: "lacks-field has a blank field name, which almost every object lacks" } if blank?(expected)
|
|
449
|
+
|
|
450
|
+
{ holds: !normal(actual).key?(expected) }
|
|
451
|
+
when "has-item"
|
|
452
|
+
return { reason: "has-item reads an array, and the actual value is #{article(a_type)}" } unless a_type == "array"
|
|
453
|
+
return { reason: "has-item looks for #{article(e_type)}, and no item is one" } if actual.any? && actual.none? { json_type(_1) == e_type }
|
|
454
|
+
|
|
455
|
+
{ holds: actual.any? { deep_equal?(_1, expected) } }
|
|
456
|
+
when "has-same-items"
|
|
457
|
+
return { reason: "has-same-items compares two arrays, and compares #{article(a_type)} with #{article(e_type)}" } unless a_type == "array" && e_type == "array"
|
|
458
|
+
|
|
459
|
+
{ holds: same_items?(actual, expected) }
|
|
460
|
+
when "has-fields"
|
|
461
|
+
return { reason: "has-fields compares two objects, and compares #{article(a_type)} with #{article(e_type)}" } unless a_type == "object" && e_type == "object"
|
|
462
|
+
|
|
463
|
+
empty = fieldless_object(expected)
|
|
464
|
+
return { reason: "has-fields has an expected object with no field#{" at #{empty.join(".")}" if empty&.any?}, which every object holds" } if empty
|
|
465
|
+
|
|
466
|
+
{ holds: field_misfit(actual, expected).nil? }
|
|
467
|
+
when "greater-than", "at-least", "less-than", "at-most"
|
|
468
|
+
return { reason: "#{compare} compares two numbers, and compares #{article(a_type)} with #{article(e_type)}" } unless a_type == "number" && e_type == "number"
|
|
469
|
+
|
|
470
|
+
{ holds: actual.send({ "greater-than" => :>, "at-least" => :>=, "less-than" => :<, "at-most" => :<= }[compare], expected) }
|
|
471
|
+
when "judged-as"
|
|
472
|
+
# The run reads a judged value and compares nothing: whether it
|
|
473
|
+
# meets the quality is a judgment the table reads later
|
|
474
|
+
# (lib/nukadoko/judgments.rb), so it holds the step up nowhere.
|
|
475
|
+
{ holds: true, for_judgment: true }
|
|
476
|
+
else
|
|
477
|
+
# A step whose claim names another compare never loads, so this
|
|
478
|
+
# is a fault of nukadoko's, and the claim breaks saying so.
|
|
479
|
+
{ reason: "#{compare} is no compare nukadoko knows" }
|
|
106
480
|
end
|
|
107
481
|
end
|
|
482
|
+
|
|
483
|
+
#: (String, untyped, untyped) -> bool
|
|
484
|
+
def self.holds?(compare, actual, expected) = compare(compare, actual, expected)[:holds] == true
|
|
108
485
|
end
|
|
109
486
|
end
|
|
110
487
|
end
|