nukadoko 0.1.0 → 0.3.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 +417 -0
- data/README.ja.md +62 -7
- data/README.md +19 -5
- 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 +294 -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/claim_sides.rb +469 -0
- data/lib/nukadoko/claims.rb +576 -37
- data/lib/nukadoko/cli.rb +814 -22
- data/lib/nukadoko/codes.json +947 -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 +88 -0
- data/lib/nukadoko/features.rb +80 -10
- data/lib/nukadoko/fixtures.rb +329 -11
- data/lib/nukadoko/http_recording.rb +68 -0
- data/lib/nukadoko/init.rb +129 -0
- data/lib/nukadoko/js_json.rb +274 -0
- data/lib/nukadoko/js_pattern.rb +531 -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 +43 -22
- data/lib/nukadoko/pattern.rb +73 -3
- data/lib/nukadoko/poll.rb +21 -8
- data/lib/nukadoko/program.rb +105 -17
- data/lib/nukadoko/project_files.rb +17 -0
- data/lib/nukadoko/project_fixtures.rb +17 -2
- data/lib/nukadoko/project_program.rb +119 -0
- 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 +898 -91
- data/lib/nukadoko/run_record.rb +535 -0
- data/lib/nukadoko/scaffold.rb +1015 -0
- data/lib/nukadoko/schema.rb +120 -21
- data/lib/nukadoko/secrets.rb +375 -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/spaces.rb +28 -0
- data/lib/nukadoko/step.rb +68 -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 +122 -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 +60 -2
- data/llms.txt +297 -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 +142 -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/claim_sides.rbs +207 -0
- data/sig/generated/nukadoko/claims.rbs +208 -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 +34 -0
- data/sig/generated/nukadoko/features.rbs +101 -0
- data/sig/generated/nukadoko/fixtures.rbs +175 -0
- data/sig/generated/nukadoko/http_recording.rbs +29 -0
- data/sig/generated/nukadoko/init.rbs +64 -0
- data/sig/generated/nukadoko/js_json.rbs +109 -0
- data/sig/generated/nukadoko/js_pattern.rbs +194 -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 +62 -0
- data/sig/generated/nukadoko/poll.rbs +41 -0
- data/sig/generated/nukadoko/program.rbs +101 -0
- data/sig/generated/nukadoko/project_files.rbs +19 -0
- data/sig/generated/nukadoko/project_fixtures.rbs +57 -0
- data/sig/generated/nukadoko/project_program.rbs +55 -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 +264 -0
- data/sig/generated/nukadoko/run_record.rbs +175 -0
- data/sig/generated/nukadoko/scaffold.rbs +328 -0
- data/sig/generated/nukadoko/schema.rbs +155 -0
- data/sig/generated/nukadoko/secrets.rbs +172 -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/spaces.rbs +31 -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 +26 -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 +342 -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 +109 -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 +133 -1
|
@@ -0,0 +1,1008 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# rbs_inline: enabled
|
|
3
|
+
|
|
4
|
+
require "json"
|
|
5
|
+
require_relative "aria_tree"
|
|
6
|
+
|
|
7
|
+
# What an ARIA snapshot says, and whether an expected one fits an actual
|
|
8
|
+
# one (`fits-aria-snapshot`), in the TypeScript engine's words. An ARIA
|
|
9
|
+
# snapshot is the YAML text a browser library prints for a page's
|
|
10
|
+
# accessibility tree: a list of nodes, each a role with an optional name
|
|
11
|
+
# and states, its text, and its children. This module reads two such
|
|
12
|
+
# texts, the one nukadoko took (the actual side) and the docstring a line
|
|
13
|
+
# states (the expected side), and judges whether every node the expected
|
|
14
|
+
# side names is in the actual tree, at the place it names it, leaving the
|
|
15
|
+
# rest of the tree unasked. For a broken claim it names the first expected
|
|
16
|
+
# node that fits nothing and the actual node nearest to it.
|
|
17
|
+
#
|
|
18
|
+
# It is pure: it never touches a page. The claim judges the text the
|
|
19
|
+
# runner took, so the record shows the one value that was judged, and a
|
|
20
|
+
# reader can judge the record again from its two texts alone. That is why
|
|
21
|
+
# the matching reads the printed text and not the browser's own tree: a
|
|
22
|
+
# state the print leaves out (an unchecked box prints no `[checked]`) is
|
|
23
|
+
# absent here too, so `[checked=false]` holds on any node that prints no
|
|
24
|
+
# checked state.
|
|
25
|
+
#
|
|
26
|
+
# The reader takes the part of YAML an ARIA snapshot uses: a block list of
|
|
27
|
+
# items, each a key alone or one key with a text or a list of children,
|
|
28
|
+
# plain, single-quoted, or double-quoted scalars, and comments. Anything
|
|
29
|
+
# else is refused with its line, and the refusal says how to write what
|
|
30
|
+
# the line meant. On the expected side a pattern between slashes, the
|
|
31
|
+
# state `[active]`, and a `/` property at the top of a list that names one
|
|
32
|
+
# node are refused too, since nukadoko would otherwise accept a line it
|
|
33
|
+
# does not judge.
|
|
34
|
+
module Nukadoko
|
|
35
|
+
module Engine
|
|
36
|
+
module AriaSnapshot
|
|
37
|
+
FRAGMENT = "fragment"
|
|
38
|
+
# JavaScript's \s, which the TypeScript engine's texts are cut by.
|
|
39
|
+
SPACE = Spaces::CLASS #: String
|
|
40
|
+
WS = Spaces::SPACE #: Regexp
|
|
41
|
+
# YAML's indicators: a plain scalar may not start with one, since
|
|
42
|
+
# YAML reads it as structure.
|
|
43
|
+
PLAIN_START_REFUSED = [",", "[", "]", "{", "}", "&", "*", "!", "|", ">", "%", "@", "`"].freeze
|
|
44
|
+
DOUBLE_QUOTED_ESCAPES = {
|
|
45
|
+
"0" => "\0", "a" => "\a", "b" => "\b", "t" => "\t", "\t" => "\t", "n" => "\n", "v" => "\v", "f" => "\f", "r" => "\r",
|
|
46
|
+
"e" => "\e", " " => " ", '"' => '"', "/" => "/", "\\" => "\\", "N" => "\u0085", "_" => " ", "L" => "
", "P" => "
"
|
|
47
|
+
}.freeze
|
|
48
|
+
HEX_ESCAPE_LENGTH = { "x" => 2, "u" => 4, "U" => 8 }.freeze
|
|
49
|
+
# What YAML's core schema reads a plain scalar as when it is not a
|
|
50
|
+
# string: a writer who means the text "3.50" and writes it plain gets
|
|
51
|
+
# the number 3.5, so such a value is refused, not read as other text.
|
|
52
|
+
YAML_NULL = /\A(?:~|null|Null|NULL)\z/
|
|
53
|
+
YAML_BOOLEAN = /\A(?:true|True|TRUE|false|False|FALSE)\z/
|
|
54
|
+
YAML_NUMBER = /\A(?:[-+]?[0-9]+|0o[0-7]+|0x[0-9a-fA-F]+|[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][-+]?[0-9]+)?|[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)\z/
|
|
55
|
+
STATE_NAMES = %w[checked disabled expanded invalid level pressed selected].freeze
|
|
56
|
+
PRINTED_STATE = /\A(checked|disabled|expanded|invalid|level|pressed|selected)(?:=(mixed|grammar|spelling|[0-9]+))?\z/
|
|
57
|
+
SOURCE_LIMIT = 120
|
|
58
|
+
# The most actual lines an excerpt shows.
|
|
59
|
+
EXCERPT_LINES = 12
|
|
60
|
+
|
|
61
|
+
module_function
|
|
62
|
+
|
|
63
|
+
# --- the YAML layer ---
|
|
64
|
+
|
|
65
|
+
#: (Integer, String) -> Problem
|
|
66
|
+
def problem_at(line, message) = Problem.new("line #{line} #{message}")
|
|
67
|
+
|
|
68
|
+
#: (String) -> String
|
|
69
|
+
def trim(text) = Spaces.trim(text)
|
|
70
|
+
|
|
71
|
+
#: (String) -> String
|
|
72
|
+
def trim_end(text) = Spaces.trim_end(text)
|
|
73
|
+
|
|
74
|
+
#: (String?) -> bool
|
|
75
|
+
def space?(char) = !char.nil? && char.match?(/\A#{WS}\z/)
|
|
76
|
+
|
|
77
|
+
#: (String) -> Array[Line]
|
|
78
|
+
def content_lines(text)
|
|
79
|
+
text.split(/\r?\n/, -1).each_with_index.filter_map do |raw, index|
|
|
80
|
+
number = index + 1
|
|
81
|
+
lead = raw[/\A[ \t]*/].to_s
|
|
82
|
+
content = raw[lead.size..].to_s
|
|
83
|
+
next if content.empty? || content.start_with?("#")
|
|
84
|
+
raise problem_at(number, "is indented with a tab, and YAML indents with spaces only") if lead.include?("\t")
|
|
85
|
+
raise problem_at(number, "is a YAML document marker, and an ARIA snapshot is one list") if lead.empty? && content.match?(/\A(---|\.\.\.)(#{WS}|\z)/)
|
|
86
|
+
|
|
87
|
+
Line.new(number: number, indent: lead.size, content: content)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# A quoted scalar at `start` of `text`: its value and the index after
|
|
92
|
+
# its closing quote.
|
|
93
|
+
#: (String, Integer, Integer) -> [String, Integer]
|
|
94
|
+
def read_quoted(text, start, line)
|
|
95
|
+
quote = text[start]
|
|
96
|
+
value = +""
|
|
97
|
+
index = start + 1
|
|
98
|
+
while index < text.size
|
|
99
|
+
char = text[index].to_s
|
|
100
|
+
if quote == "'"
|
|
101
|
+
if char == "'"
|
|
102
|
+
if text[index + 1] == "'"
|
|
103
|
+
value << "'"
|
|
104
|
+
index += 2
|
|
105
|
+
next
|
|
106
|
+
end
|
|
107
|
+
return [value, index + 1]
|
|
108
|
+
end
|
|
109
|
+
value << char
|
|
110
|
+
index += 1
|
|
111
|
+
next
|
|
112
|
+
end
|
|
113
|
+
return [value, index + 1] if char == '"'
|
|
114
|
+
|
|
115
|
+
unless char == "\\"
|
|
116
|
+
value << char
|
|
117
|
+
index += 1
|
|
118
|
+
next
|
|
119
|
+
end
|
|
120
|
+
escape = text[index + 1]
|
|
121
|
+
break if escape.nil?
|
|
122
|
+
|
|
123
|
+
if (hex_length = HEX_ESCAPE_LENGTH[escape])
|
|
124
|
+
hex = text[index + 2, hex_length].to_s
|
|
125
|
+
raise problem_at(line, "has \\#{escape} in a double-quoted text without #{hex_length} hexadecimal digits after it") unless hex.match?(/\A[0-9a-fA-F]{#{hex_length}}\z/)
|
|
126
|
+
|
|
127
|
+
value << code_point(hex.to_i(16))
|
|
128
|
+
index += 2 + hex_length
|
|
129
|
+
next
|
|
130
|
+
end
|
|
131
|
+
replacement = DOUBLE_QUOTED_ESCAPES[escape]
|
|
132
|
+
raise problem_at(line, "has the escape \\#{escape}, which a double-quoted YAML text does not have") if replacement.nil?
|
|
133
|
+
|
|
134
|
+
value << replacement
|
|
135
|
+
index += 2
|
|
136
|
+
end
|
|
137
|
+
raise problem_at(line, "opens a #{quote == '"' ? "double" : "single"}-quoted text that does not close on the line")
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A code point as text; a lone surrogate, which a Ruby string cannot
|
|
141
|
+
# hold, is the replacement character.
|
|
142
|
+
#: (Integer) -> String
|
|
143
|
+
def code_point(number)
|
|
144
|
+
number.chr(Encoding::UTF_8)
|
|
145
|
+
rescue RangeError
|
|
146
|
+
"�"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Where a plain scalar that starts at `start` ends: before a `:` that
|
|
150
|
+
# a space or the end follows, when `key` is true, or before a ` #`
|
|
151
|
+
# comment either way.
|
|
152
|
+
#: (String, Integer, bool) -> Integer
|
|
153
|
+
def plain_end(text, start, key)
|
|
154
|
+
(start...text.size).each do |index|
|
|
155
|
+
char = text[index]
|
|
156
|
+
return index if char == "#" && index > start && space?(text[index - 1])
|
|
157
|
+
return index if key && char == ":" && (index + 1 == text.size || space?(text[index + 1]))
|
|
158
|
+
end
|
|
159
|
+
text.size
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
#: (String, Integer, bool, Integer) -> [Scalar, Integer]
|
|
163
|
+
def read_scalar(text, start, key, line)
|
|
164
|
+
first = text[start].to_s
|
|
165
|
+
if first == '"' || first == "'"
|
|
166
|
+
value, ending = read_quoted(text, start, line)
|
|
167
|
+
return [Scalar.new(text: value, plain: false), ending]
|
|
168
|
+
end
|
|
169
|
+
if PLAIN_START_REFUSED.include?(first)
|
|
170
|
+
raise problem_at(line, if first == "|" || first == ">"
|
|
171
|
+
"starts a YAML block text with #{first}, which an ARIA snapshot does not use: write the text on the line, in double quotes with \\n for a new line"
|
|
172
|
+
else
|
|
173
|
+
"starts a text with #{first}, which YAML reads as structure, not text: put the text in quotes"
|
|
174
|
+
end)
|
|
175
|
+
end
|
|
176
|
+
if %w[- ? :].include?(first) && (start + 1 == text.size || space?(text[start + 1]))
|
|
177
|
+
raise problem_at(line, first == "-" ? "holds a list inside a list item, and each item is one node" : "starts with #{first} and a space, which YAML reads as structure, not text: put the text in quotes")
|
|
178
|
+
end
|
|
179
|
+
value = trim_end(text[start...plain_end(text, start, key)].to_s)
|
|
180
|
+
raise problem_at(line, 'holds a text with ": " in it, which YAML reads as a second key: put the text in quotes') if !key && value.match?(/:#{WS}/)
|
|
181
|
+
|
|
182
|
+
[Scalar.new(text: value, plain: true), start + value.size]
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
#: (String, Integer) -> Integer
|
|
186
|
+
def skip_spaces(text, index)
|
|
187
|
+
index += 1 while index < text.size && [" ", "\t"].include?(text[index])
|
|
188
|
+
index
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# The rest of a line after a scalar may hold only spaces and a comment.
|
|
192
|
+
#: (String, Integer) -> bool
|
|
193
|
+
def ends_here?(text, index)
|
|
194
|
+
at = skip_spaces(text, index)
|
|
195
|
+
at == text.size || (text[at] == "#" && at > index)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# One item's content after `- `: its key, whether a colon follows
|
|
199
|
+
# it, its text when one does, and the item as written.
|
|
200
|
+
#: (String, Integer) -> [Scalar, bool, Scalar?, String]
|
|
201
|
+
def read_entry(content, line)
|
|
202
|
+
key, index = read_scalar(content, 0, true, line)
|
|
203
|
+
after = skip_spaces(content, index)
|
|
204
|
+
unless content[after] == ":"
|
|
205
|
+
raise problem_at(line, "has text after #{JSON.generate(key.text)} that YAML does not read: put the whole key in quotes") unless ends_here?(content, index)
|
|
206
|
+
|
|
207
|
+
return [key, false, nil, trim(content[0...index].to_s)]
|
|
208
|
+
end
|
|
209
|
+
index = after + 1
|
|
210
|
+
if index < content.size && !space?(content[index])
|
|
211
|
+
raise problem_at(line, "has a colon with no space after it, which YAML reads as part of the key: put a space after the colon, or the key in quotes")
|
|
212
|
+
end
|
|
213
|
+
value_start = skip_spaces(content, index)
|
|
214
|
+
return [key, true, nil, trim(content[0...after].to_s)] if value_start == content.size || content[value_start] == "#"
|
|
215
|
+
|
|
216
|
+
value, ending = read_scalar(content, value_start, false, line)
|
|
217
|
+
raise problem_at(line, "has text after its value that YAML does not read: put the value in quotes") unless ends_here?(content, ending)
|
|
218
|
+
|
|
219
|
+
[key, true, value, trim(content[0...ending].to_s)]
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
#: (Array[Line], Integer, Integer) -> [Array[Item], Integer]
|
|
223
|
+
def read_list(lines, start, indent)
|
|
224
|
+
items = [] #: Array[Item]
|
|
225
|
+
index = start
|
|
226
|
+
while index < lines.size
|
|
227
|
+
line = lines.fetch(index)
|
|
228
|
+
break if line.indent < indent
|
|
229
|
+
raise problem_at(line.number, "is indented more than the item above it, which has no colon to open children") if line.indent > indent
|
|
230
|
+
unless line.content == "-" || line.content.start_with?("- ")
|
|
231
|
+
raise problem_at(line.number, 'is no list item: an ARIA snapshot is a YAML list, and each item starts with "- "')
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
offset = skip_spaces(line.content, 1)
|
|
235
|
+
raise problem_at(line.number, "is a list item with nothing in it") if offset == line.content.size || line.content[offset] == "#"
|
|
236
|
+
|
|
237
|
+
key, colon, value, source = read_entry(line.content[offset..].to_s, line.number)
|
|
238
|
+
index += 1
|
|
239
|
+
key_column = line.indent + offset
|
|
240
|
+
deeper = index < lines.size && lines.fetch(index).indent > line.indent ? lines.fetch(index) : nil
|
|
241
|
+
if colon && value.nil?
|
|
242
|
+
if deeper.nil?
|
|
243
|
+
raise problem_at(line.number, "ends #{JSON.generate(key.text)} with a colon and has nothing after it: write its text after the colon, its children on the lines below, or drop the colon")
|
|
244
|
+
end
|
|
245
|
+
raise problem_at(deeper.number, "is indented less than the key of the item it belongs to") if deeper.indent < key_column
|
|
246
|
+
|
|
247
|
+
children, index = read_list(lines, index, deeper.indent)
|
|
248
|
+
items << Item.new(line: line.number, source: source, key: key, value: nil, children: children)
|
|
249
|
+
next
|
|
250
|
+
end
|
|
251
|
+
if deeper
|
|
252
|
+
raise problem_at(deeper.number, if deeper.indent == key_column && !deeper.content.start_with?("-")
|
|
253
|
+
'is a second key of one list item, and each item names one node: start it with "- " as an item of its own'
|
|
254
|
+
else
|
|
255
|
+
"is indented under an item that has no colon to open children"
|
|
256
|
+
end)
|
|
257
|
+
end
|
|
258
|
+
items << Item.new(line: line.number, source: source, key: key, value: value, children: nil)
|
|
259
|
+
end
|
|
260
|
+
[items, index]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
#: (String) -> Array[Item]
|
|
264
|
+
def read_items(text)
|
|
265
|
+
lines = content_lines(text)
|
|
266
|
+
return [] if lines.empty?
|
|
267
|
+
|
|
268
|
+
items, ending = read_list(lines, 0, lines.fetch(0).indent)
|
|
269
|
+
raise problem_at(lines.fetch(ending).number, "is indented less than the first item of the list") if ending < lines.size
|
|
270
|
+
|
|
271
|
+
items
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# The text a plain scalar on the expected side states, or why YAML
|
|
275
|
+
# reads it as something else. A number or a boolean whose own text
|
|
276
|
+
# YAML gives back unchanged (`42`, `true`) states that text.
|
|
277
|
+
#: (Scalar, Integer, String) -> String
|
|
278
|
+
def stated_text(scalar, line, what)
|
|
279
|
+
return scalar.text unless scalar.plain
|
|
280
|
+
|
|
281
|
+
text = scalar.text
|
|
282
|
+
raise problem_at(line, "has #{what} #{text}, which YAML reads as null: put the text in quotes") if text.match?(YAML_NULL)
|
|
283
|
+
|
|
284
|
+
read = if text.match?(YAML_BOOLEAN) then (text.downcase == "true").to_s
|
|
285
|
+
elsif text.match?(YAML_NUMBER) then yaml_number_text(text)
|
|
286
|
+
else text
|
|
287
|
+
end
|
|
288
|
+
raise problem_at(line, "has #{what} #{text}, which YAML reads as #{read}: put the text in quotes, \"#{text}\", to state it as written") if read != text
|
|
289
|
+
|
|
290
|
+
text
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# The text of the number YAML reads a plain scalar as, spelled as
|
|
294
|
+
# JavaScript spells a number, since the TypeScript engine's messages
|
|
295
|
+
# name it so.
|
|
296
|
+
#: (String) -> String
|
|
297
|
+
def yaml_number_text(text)
|
|
298
|
+
return "NaN" if text.match?(/\.nan\z/i)
|
|
299
|
+
return text.start_with?("-") ? "-Infinity" : "Infinity" if text.match?(/\.inf\z/i)
|
|
300
|
+
return text[2..].to_s.to_i(8).to_s if text.start_with?("0o")
|
|
301
|
+
return text[2..].to_s.to_i(16).to_s if text.start_with?("0x")
|
|
302
|
+
|
|
303
|
+
js_number(text.to_f)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# A float as JavaScript's Number#toString writes it: its shortest
|
|
307
|
+
# digits, as a plain number from 1e-7 to 1e21, and with an exponent
|
|
308
|
+
# outside that.
|
|
309
|
+
#: (Float) -> String
|
|
310
|
+
def js_number(number)
|
|
311
|
+
return "0" if number.zero?
|
|
312
|
+
|
|
313
|
+
shown = number.abs.to_s
|
|
314
|
+
mantissa, exponent = shown.include?("e") ? shown.split("e") : [shown, nil]
|
|
315
|
+
whole, fraction = mantissa.to_s.split(".")
|
|
316
|
+
whole = whole.to_s
|
|
317
|
+
fraction = fraction.to_s
|
|
318
|
+
if exponent
|
|
319
|
+
digits = whole + fraction
|
|
320
|
+
point = whole.size + exponent.to_i
|
|
321
|
+
elsif whole == "0"
|
|
322
|
+
digits = fraction.sub(/\A0+/, "")
|
|
323
|
+
point = digits.size - fraction.size
|
|
324
|
+
else
|
|
325
|
+
digits = whole + fraction
|
|
326
|
+
point = whole.size
|
|
327
|
+
end
|
|
328
|
+
digits = digits.sub(/0+\z/, "")
|
|
329
|
+
k = digits.size
|
|
330
|
+
text = if k <= point && point <= 21 then digits + ("0" * (point - k))
|
|
331
|
+
elsif point.positive? && point <= 21 then "#{digits[0, point]}.#{digits[point..]}"
|
|
332
|
+
elsif point > -6 && point <= 0 then "0.#{"0" * -point}#{digits}"
|
|
333
|
+
else
|
|
334
|
+
e = point - 1
|
|
335
|
+
"#{digits[0]}#{k > 1 ? ".#{digits[1..]}" : ""}e#{e.negative? ? "-" : "+"}#{e.abs}"
|
|
336
|
+
end
|
|
337
|
+
(number.negative? ? "-" : "") + text
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# --- the tree ---
|
|
341
|
+
|
|
342
|
+
# White space folded the way the snapshot folds a page's text.
|
|
343
|
+
#: (String) -> String
|
|
344
|
+
def folded(text) = trim(text.gsub(/[]/, "").gsub(/#{WS}+/, " "))
|
|
345
|
+
|
|
346
|
+
#: (String) -> ExpectedText
|
|
347
|
+
def expected_text(raw) = ExpectedText.new(raw: raw, normalized: folded(raw))
|
|
348
|
+
|
|
349
|
+
# A key's role, name, and states, read the way a docstring writes
|
|
350
|
+
# them: a role, then a name in double quotes, then states in brackets.
|
|
351
|
+
#: (String, Integer) -> [String, String, Hash[String, untyped]]
|
|
352
|
+
def expected_key(key, line)
|
|
353
|
+
index = skip_spaces(key, 0)
|
|
354
|
+
role_start = index
|
|
355
|
+
index += 1 while index < key.size && key[index].to_s.match?(/\A[a-zA-Z]\z/)
|
|
356
|
+
role = key[role_start...index].to_s
|
|
357
|
+
raise problem_at(line, "names no role in #{JSON.generate(key)}: an item starts with a role, such as heading or button") if role.empty?
|
|
358
|
+
|
|
359
|
+
index = skip_spaces(key, index)
|
|
360
|
+
name = ""
|
|
361
|
+
if key[index] == '"'
|
|
362
|
+
value = +""
|
|
363
|
+
index += 1
|
|
364
|
+
closed = false
|
|
365
|
+
while index < key.size
|
|
366
|
+
char = key[index].to_s
|
|
367
|
+
index += 1
|
|
368
|
+
if char == "\\" && index < key.size
|
|
369
|
+
value << key[index].to_s
|
|
370
|
+
index += 1
|
|
371
|
+
elsif char == '"'
|
|
372
|
+
closed = true
|
|
373
|
+
break
|
|
374
|
+
else
|
|
375
|
+
value << char
|
|
376
|
+
end
|
|
377
|
+
end
|
|
378
|
+
unless closed
|
|
379
|
+
raise problem_at(line, "opens the name of #{role} with a quote that does not close. A \": \" inside a name ends a key in YAML, so put such a key in single quotes")
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
name = folded(value)
|
|
383
|
+
elsif key[index] == "/"
|
|
384
|
+
raise problem_at(line, "names #{role} with a pattern between slashes, which fits-aria-snapshot does not judge: state the name in double quotes, or claim the part that changes with another claim")
|
|
385
|
+
end
|
|
386
|
+
states = {} #: Hash[String, untyped]
|
|
387
|
+
loop do
|
|
388
|
+
index = skip_spaces(key, index)
|
|
389
|
+
break unless key[index] == "["
|
|
390
|
+
|
|
391
|
+
close = key.index("]", index)
|
|
392
|
+
raise problem_at(line, "opens a state of #{role} with [ and does not close it") if close.nil?
|
|
393
|
+
|
|
394
|
+
body = trim(key[(index + 1)...close].to_s)
|
|
395
|
+
match = body.match(/\A([a-zA-Z]+)[#{SPACE}]*(?:=[#{SPACE}]*([^#{SPACE}]*))?\z/)
|
|
396
|
+
raise problem_at(line, "has the state [#{body}], which is no state a snapshot prints") if match.nil?
|
|
397
|
+
|
|
398
|
+
apply_state(states, match[1].to_s, match[2].nil? || match[2].to_s.empty? ? "true" : match[2].to_s, line)
|
|
399
|
+
index = close + 1
|
|
400
|
+
end
|
|
401
|
+
if index < key.size
|
|
402
|
+
raise problem_at(line, "has text after the role#{name.empty? ? "" : " and the name"} of #{role} that the key does not read: #{JSON.generate(key[index..].to_s)}. A name goes in double quotes")
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
[role, name, states]
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
#: (Hash[String, untyped], String, String, Integer) -> void
|
|
409
|
+
def apply_state(states, state, value, line)
|
|
410
|
+
flag = lambda do |allowed, what|
|
|
411
|
+
raise problem_at(line, "gives [#{state}] the value #{value}, and #{state} is #{what}") unless allowed.include?(value)
|
|
412
|
+
|
|
413
|
+
{ "true" => true, "false" => false }.fetch(value, value)
|
|
414
|
+
end
|
|
415
|
+
case state
|
|
416
|
+
when "checked", "pressed" then states[state] = flag.(%w[true false mixed], "true, false, or mixed")
|
|
417
|
+
when "disabled", "expanded", "selected" then states[state] = flag.(%w[true false], "true or false")
|
|
418
|
+
when "invalid" then states[state] = flag.(%w[true false grammar spelling], "true, false, grammar, or spelling")
|
|
419
|
+
when "level"
|
|
420
|
+
raise problem_at(line, "gives [level] the value #{value}, and a level is a whole number") unless value.match?(/\A[0-9]+\z/)
|
|
421
|
+
|
|
422
|
+
states[state] = value.to_i
|
|
423
|
+
when "active" then raise problem_at(line, "states [active], which the snapshot nukadoko takes does not print, so nothing could judge it")
|
|
424
|
+
else raise problem_at(line, "has the state [#{state}], which is none of #{STATE_NAMES.join(", ")}")
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
# The children a list of items names, into `container`, with the
|
|
429
|
+
# first property item at the top kept in `first_prop`.
|
|
430
|
+
#: (Array[Item], Node, ?Hash[Symbol, Item]) -> void
|
|
431
|
+
def expected_children(items, container, first_prop = {})
|
|
432
|
+
items.each do |item|
|
|
433
|
+
if item.key.plain && [YAML_NULL, YAML_BOOLEAN, YAML_NUMBER].any? { item.key.text.match?(_1) }
|
|
434
|
+
raise problem_at(item.line, "has the key #{item.key.text}, which YAML reads as no text: an item starts with a role, such as heading or button")
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
key = item.key.text
|
|
438
|
+
if item.value.nil? && item.children.nil?
|
|
439
|
+
role, name, states = expected_key(key, item.line)
|
|
440
|
+
container.children << role_node(item, role, name, states, [])
|
|
441
|
+
next
|
|
442
|
+
end
|
|
443
|
+
if key == "text" || key.start_with?("/")
|
|
444
|
+
value_scalar = item.value
|
|
445
|
+
raise problem_at(item.line, "gives #{key} children, and it takes a text after the colon") if value_scalar.nil?
|
|
446
|
+
|
|
447
|
+
value = stated_text(value_scalar, item.line, "the #{key == "text" ? "text" : "value"}")
|
|
448
|
+
if key == "text"
|
|
449
|
+
container.children << Node.new(kind: :text, text: expected_text(value), line: item.line, source: item.source)
|
|
450
|
+
elsif key == "/children"
|
|
451
|
+
raise problem_at(item.line, "gives /children the value #{value}, and it is contain, equal, or deep-equal") unless %w[contain equal deep-equal].include?(value)
|
|
452
|
+
|
|
453
|
+
container.children_mode = value
|
|
454
|
+
else
|
|
455
|
+
raise problem_at(item.line, "names a property with no name after the /") if key.size == 1
|
|
456
|
+
|
|
457
|
+
container.props[key[1..].to_s] = expected_text(value)
|
|
458
|
+
first_prop[:item] ||= item
|
|
459
|
+
end
|
|
460
|
+
next
|
|
461
|
+
end
|
|
462
|
+
role, name, states = expected_key(key, item.line)
|
|
463
|
+
if (value_scalar = item.value)
|
|
464
|
+
text = stated_text(value_scalar, item.line, "the text")
|
|
465
|
+
container.children << role_node(item, role, name, states, [Node.new(kind: :text, text: expected_text(text), line: item.line, source: item.source)])
|
|
466
|
+
next
|
|
467
|
+
end
|
|
468
|
+
node = role_node(item, role, name, states, [])
|
|
469
|
+
expected_children(item.children || [], node)
|
|
470
|
+
container.children << node
|
|
471
|
+
end
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
#: (Item, String, String, Hash[String, untyped], Array[Node]) -> Node
|
|
475
|
+
def role_node(item, role, name, states, children) = Node.new(kind: :role, role: role, name: name, states: states, props: {}, children: children, line: item.line, source: item.source)
|
|
476
|
+
|
|
477
|
+
# The tree a docstring states: [root, named], where root is the node
|
|
478
|
+
# the matching searches for (a fragment when it names several at the
|
|
479
|
+
# top, or states a children mode there) and named how many nodes it
|
|
480
|
+
# names; or a String saying why it reads as no snapshot.
|
|
481
|
+
#: (String) -> ([Node, Integer] | String)
|
|
482
|
+
def read_expected(text)
|
|
483
|
+
top = Node.new(kind: :role, role: FRAGMENT, line: 0, source: "the list")
|
|
484
|
+
first_prop = {} #: Hash[Symbol, Item]
|
|
485
|
+
expected_children(read_items(text), top, first_prop)
|
|
486
|
+
# One node at the top is searched for alone, anywhere in the tree;
|
|
487
|
+
# several are searched for together, as children of one node, which
|
|
488
|
+
# a property at the top describes.
|
|
489
|
+
single = top.children.size == 1 && [nil, "contain"].include?(top.children_mode)
|
|
490
|
+
prop = first_prop[:item]
|
|
491
|
+
if single && prop
|
|
492
|
+
raise problem_at(prop.line, "states #{prop.key.text} at the top of a list that names one node, so it describes no node: put it under the node it describes")
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
fragment = Node.new(kind: :role, role: FRAGMENT, props: top.props, children: top.children, children_mode: top.children_mode,
|
|
496
|
+
line: prop&.line || 0, source: prop&.source || "the list")
|
|
497
|
+
[single ? top.children.fetch(0) : fragment, count_named(top.children) + stated_props(top.props)]
|
|
498
|
+
rescue Problem => e
|
|
499
|
+
e.message
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
# The properties a node states that a page could lack: each one
|
|
503
|
+
# whose text is not empty, since an empty text fits any text.
|
|
504
|
+
#: (Hash[String, untyped]) -> Integer
|
|
505
|
+
def stated_props(props) = props.values.count { !_1.normalized.empty? }
|
|
506
|
+
|
|
507
|
+
# The nodes a list names that a page could lack: every role, and
|
|
508
|
+
# every text that is not empty.
|
|
509
|
+
#: (Array[Node]) -> Integer
|
|
510
|
+
def count_named(nodes) = nodes.sum { _1.text? ? (_1.text.normalized.empty? ? 0 : 1) : 1 + count_named(_1.children) }
|
|
511
|
+
|
|
512
|
+
# A key as the snapshot prints it: a role, then a name as a JSON
|
|
513
|
+
# string, or as it is when it starts and ends with a slash, then its
|
|
514
|
+
# states.
|
|
515
|
+
#: (String, Integer) -> [String, String, Hash[String, untyped]]
|
|
516
|
+
def actual_key(key, line)
|
|
517
|
+
role = key[/\A[a-zA-Z]+/]
|
|
518
|
+
raise problem_at(line, "prints no role in #{JSON.generate(key)}") if role.nil?
|
|
519
|
+
|
|
520
|
+
rest = key[role.size..].to_s
|
|
521
|
+
states = {} #: Hash[String, untyped]
|
|
522
|
+
# The printer writes the states last, from a fixed set, so they are
|
|
523
|
+
# read from the end: a name may hold brackets of its own.
|
|
524
|
+
loop do
|
|
525
|
+
state = rest.match(/ \[([^\]#{SPACE}]+)\]\z/)
|
|
526
|
+
break if state.nil?
|
|
527
|
+
|
|
528
|
+
parsed = state[1].to_s.match(PRINTED_STATE)
|
|
529
|
+
break if parsed.nil?
|
|
530
|
+
|
|
531
|
+
states[parsed[1].to_s] = parsed[1] == "level" ? parsed[2].to_s.to_i : parsed[2] || true
|
|
532
|
+
rest = rest[0...state.begin(0)].to_s
|
|
533
|
+
end
|
|
534
|
+
return [role, "", states] if rest.empty?
|
|
535
|
+
raise problem_at(line, "prints #{JSON.generate(key)}, which is no role, name, and states") unless rest.start_with?(" ")
|
|
536
|
+
|
|
537
|
+
written = rest[1..].to_s
|
|
538
|
+
if written.start_with?('"')
|
|
539
|
+
name = begin
|
|
540
|
+
JSON.parse(written)
|
|
541
|
+
rescue JSON::ParserError
|
|
542
|
+
nil
|
|
543
|
+
end
|
|
544
|
+
return [role, name, states] if name.is_a?(String)
|
|
545
|
+
|
|
546
|
+
raise problem_at(line, "prints the name #{written}, which is no JSON string")
|
|
547
|
+
end
|
|
548
|
+
return [role, written, states] if written.size > 1 && written.start_with?("/") && written.end_with?("/")
|
|
549
|
+
|
|
550
|
+
raise problem_at(line, "prints #{JSON.generate(key)}, which is no role, name, and states")
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
#: (Array[Item], Node) -> void
|
|
554
|
+
def actual_children(items, container)
|
|
555
|
+
items.each do |item|
|
|
556
|
+
key = item.key.text
|
|
557
|
+
if item.value.nil? && item.children.nil?
|
|
558
|
+
role, name, states = actual_key(key, item.line)
|
|
559
|
+
container.children << Node.new(kind: :role, role: role, name: name, states: states, line: item.line, source: item.source)
|
|
560
|
+
next
|
|
561
|
+
end
|
|
562
|
+
if key == "text" || key.start_with?("/")
|
|
563
|
+
value = item.value
|
|
564
|
+
raise problem_at(item.line, "prints #{key} with children") if value.nil?
|
|
565
|
+
|
|
566
|
+
if key == "text" then container.children << Node.new(kind: :text, text: value.text, line: item.line, source: item.source)
|
|
567
|
+
else container.props[key[1..].to_s] = value.text
|
|
568
|
+
end
|
|
569
|
+
next
|
|
570
|
+
end
|
|
571
|
+
role, name, states = actual_key(key, item.line)
|
|
572
|
+
node = Node.new(kind: :role, role: role, name: name, states: states, line: item.line, source: item.source)
|
|
573
|
+
if (value = item.value) then node.children << Node.new(kind: :text, text: value.text, line: item.line, source: item.source)
|
|
574
|
+
else actual_children(item.children || [], node)
|
|
575
|
+
end
|
|
576
|
+
container.children << node
|
|
577
|
+
end
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
# The tree a printed snapshot shows, under a node of role `fragment`
|
|
581
|
+
# that holds its list, or a String saying why it does not read. A
|
|
582
|
+
# plain scalar is the text it spells: the printer quotes every text
|
|
583
|
+
# YAML would read otherwise.
|
|
584
|
+
#: (String) -> (Node | String)
|
|
585
|
+
def read_actual(text)
|
|
586
|
+
root = Node.new(kind: :role, role: FRAGMENT, line: 0, source: "the snapshot")
|
|
587
|
+
actual_children(read_items(text), root)
|
|
588
|
+
root
|
|
589
|
+
rescue Problem => e
|
|
590
|
+
e.message
|
|
591
|
+
end
|
|
592
|
+
|
|
593
|
+
# --- fitting ---
|
|
594
|
+
|
|
595
|
+
#: (String?, ExpectedText) -> bool
|
|
596
|
+
def text_fits?(actual, expected)
|
|
597
|
+
return true if expected.normalized.empty?
|
|
598
|
+
return false if actual.nil? || actual.empty?
|
|
599
|
+
|
|
600
|
+
actual == expected.normalized || actual == expected.raw
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
#: (String, Hash[String, untyped], Hash[String, untyped]) -> bool
|
|
604
|
+
def state_fits?(state, actual, expected)
|
|
605
|
+
stated = expected[state]
|
|
606
|
+
return true if stated.nil?
|
|
607
|
+
|
|
608
|
+
printed = actual[state]
|
|
609
|
+
return printed.nil? if stated == false
|
|
610
|
+
|
|
611
|
+
printed == stated
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
# Whether `actual` is the node `expected` names, by its own line
|
|
615
|
+
# alone: its role, name, states, and properties, and for a text, its
|
|
616
|
+
# text.
|
|
617
|
+
#: (Node, Node) -> bool
|
|
618
|
+
def node_fits?(actual, expected)
|
|
619
|
+
return actual.text? && text_fits?(actual.text, expected.text) if expected.text?
|
|
620
|
+
return false if actual.text?
|
|
621
|
+
return false if expected.role != FRAGMENT && expected.role != actual.role
|
|
622
|
+
return false unless STATE_NAMES.all? { state_fits?(_1, actual.states, expected.states) }
|
|
623
|
+
return false if !expected.name.empty? && actual.name != expected.name
|
|
624
|
+
|
|
625
|
+
expected.props.all? { |prop, text| text_fits?(actual.props[prop], text) }
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
# How `expected`'s children are read, as [contain, deep]: its own mode
|
|
629
|
+
# wins, and under a `deep-equal` above it, every list is read whole.
|
|
630
|
+
#: (Node, bool) -> [bool, bool]
|
|
631
|
+
def children_reading(expected, deep)
|
|
632
|
+
case expected.children_mode
|
|
633
|
+
when "contain" then [true, false]
|
|
634
|
+
when "equal" then [false, false]
|
|
635
|
+
when "deep-equal" then [false, true]
|
|
636
|
+
else deep ? [false, true] : [true, false]
|
|
637
|
+
end
|
|
638
|
+
end
|
|
639
|
+
|
|
640
|
+
#: (Node, Node, bool) -> bool
|
|
641
|
+
def tree_fits?(actual, expected, deep)
|
|
642
|
+
return false unless node_fits?(actual, expected)
|
|
643
|
+
return true if expected.text?
|
|
644
|
+
|
|
645
|
+
children = actual.children
|
|
646
|
+
contain, deeper = children_reading(expected, deep)
|
|
647
|
+
unless contain
|
|
648
|
+
return children.size == expected.children.size && expected.children.each_with_index.all? { |child, index| tree_fits?(children.fetch(index), child, deeper) }
|
|
649
|
+
end
|
|
650
|
+
|
|
651
|
+
# In order, each expected child at the first actual child after the
|
|
652
|
+
# one before it that fits: taking the first never leaves a later
|
|
653
|
+
# expected child fewer places to fit.
|
|
654
|
+
following = 0
|
|
655
|
+
expected.children.each do |child|
|
|
656
|
+
following += 1 while following < children.size && !tree_fits?(children.fetch(following), child, false)
|
|
657
|
+
return false if following == children.size
|
|
658
|
+
|
|
659
|
+
following += 1
|
|
660
|
+
end
|
|
661
|
+
true
|
|
662
|
+
end
|
|
663
|
+
|
|
664
|
+
#: (Node) -> Array[Node]
|
|
665
|
+
def every_node(node) = [node, *(node.text? ? [] : node.children.flat_map { every_node(_1) })]
|
|
666
|
+
|
|
667
|
+
#: (Node) -> Array[Node]
|
|
668
|
+
def below(node) = node.children.flat_map { every_node(_1) }
|
|
669
|
+
|
|
670
|
+
# Whether the tree an expected snapshot names is in the actual tree:
|
|
671
|
+
# its top node fits some actual node at any depth, each of its
|
|
672
|
+
# children fits a child of that node, in order, with other children
|
|
673
|
+
# between them unasked, and so on down, unless a `/children` mode
|
|
674
|
+
# asks for every child.
|
|
675
|
+
#: (Node, Node) -> bool
|
|
676
|
+
def snapshot_fits?(actual, expected) = every_node(actual).any? { tree_fits?(_1, expected, false) }
|
|
677
|
+
|
|
678
|
+
# --- explaining a misfit ---
|
|
679
|
+
|
|
680
|
+
# The text an expected line states after its key (`- listitem: one`),
|
|
681
|
+
# which the line reads as part of the node it names.
|
|
682
|
+
#: (Node) -> ExpectedText?
|
|
683
|
+
def inline_text(expected)
|
|
684
|
+
return nil if expected.text? || expected.children.size != 1
|
|
685
|
+
|
|
686
|
+
only = expected.children.fetch(0)
|
|
687
|
+
only.text? && only.line == expected.line ? only.text : nil
|
|
688
|
+
end
|
|
689
|
+
|
|
690
|
+
# Whether `actual` is the node an expected line names, its inline
|
|
691
|
+
# text included.
|
|
692
|
+
#: (Node, Node) -> bool
|
|
693
|
+
def line_fits?(actual, expected)
|
|
694
|
+
return false unless node_fits?(actual, expected)
|
|
695
|
+
|
|
696
|
+
text = inline_text(expected)
|
|
697
|
+
text.nil? || actual.children.any? { _1.text? && text_fits?(_1.text, text) }
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
# The edit distance between two texts, each cut to its first 200
|
|
701
|
+
# characters, so a long name costs no more than a short one.
|
|
702
|
+
#: (String, String) -> Integer
|
|
703
|
+
def edit_distance(a, b)
|
|
704
|
+
left = a[0, 200].to_s
|
|
705
|
+
right = b[0, 200].to_s
|
|
706
|
+
previous = (0..right.size).to_a
|
|
707
|
+
(1..left.size).each do |i|
|
|
708
|
+
current = [i]
|
|
709
|
+
(1..right.size).each do |j|
|
|
710
|
+
current[j] = [previous.fetch(j) + 1, current.fetch(j - 1) + 1, previous.fetch(j - 1) + (left[i - 1] == right[j - 1] ? 0 : 1)].min
|
|
711
|
+
end
|
|
712
|
+
previous = current
|
|
713
|
+
end
|
|
714
|
+
previous.fetch(right.size)
|
|
715
|
+
end
|
|
716
|
+
|
|
717
|
+
# Of `candidates`, the one most like `expected`: a node of its role,
|
|
718
|
+
# or a text for a text, whose name or text is nearest to the one it
|
|
719
|
+
# states, the earliest of equals; nil when none is of its role.
|
|
720
|
+
#: (Node, Array[Node]) -> Node?
|
|
721
|
+
def most_alike(expected, candidates)
|
|
722
|
+
best = nil #: Node?
|
|
723
|
+
distance = Float::INFINITY #: Float | Integer
|
|
724
|
+
candidates.each do |candidate|
|
|
725
|
+
if expected.text?
|
|
726
|
+
next unless candidate.text?
|
|
727
|
+
|
|
728
|
+
d = edit_distance(candidate.text, expected.text.normalized) #: Float | Integer
|
|
729
|
+
elsif expected.role == FRAGMENT
|
|
730
|
+
next if candidate.text?
|
|
731
|
+
next unless expected.props.keys.any? { candidate.props.key?(_1) }
|
|
732
|
+
|
|
733
|
+
d = expected.props.sum { |prop, text| edit_distance(candidate.props[prop] || "", text.normalized) }
|
|
734
|
+
else
|
|
735
|
+
next if candidate.text? || candidate.role != expected.role
|
|
736
|
+
|
|
737
|
+
text = inline_text(expected)
|
|
738
|
+
d = edit_distance(candidate.name, expected.name) +
|
|
739
|
+
(text.nil? ? 0 : (candidate.children.map { _1.text? ? edit_distance(_1.text, text.normalized) : text.normalized.size }.min || Float::INFINITY))
|
|
740
|
+
end
|
|
741
|
+
if d < distance
|
|
742
|
+
best = candidate
|
|
743
|
+
distance = d
|
|
744
|
+
end
|
|
745
|
+
end
|
|
746
|
+
best
|
|
747
|
+
end
|
|
748
|
+
|
|
749
|
+
# A misfit where an expected node fits nothing: where it was looked
|
|
750
|
+
# for (nil for anywhere), the nearest actual node, and the place the
|
|
751
|
+
# previous sibling left it.
|
|
752
|
+
#: (Node, Node?, Node, ?Node?, ?Array[Node]) -> Hash[Symbol, untyped]
|
|
753
|
+
def unmatched(expected, inside, root, after = nil, place = [])
|
|
754
|
+
near = lambda do |node, relation|
|
|
755
|
+
{ kind: :unmatched, expected: expected, inside: inside, after: after, place: place,
|
|
756
|
+
nearest: node && { node: node, relation: relation == :child && line_fits?(node, expected) ? :fits : relation } }
|
|
757
|
+
end
|
|
758
|
+
return near.(most_alike(expected, below(root)), :child) if inside.nil?
|
|
759
|
+
|
|
760
|
+
child = most_alike(expected, inside.children)
|
|
761
|
+
child ? near.(child, :child) : near.(most_alike(expected, below(inside)), :deeper)
|
|
762
|
+
end
|
|
763
|
+
|
|
764
|
+
#: (Scores, Node, Node, bool, Node) -> Hash[Symbol, untyped]?
|
|
765
|
+
def explain_at(scores, expected, node, deep, root)
|
|
766
|
+
return nil if expected.text?
|
|
767
|
+
|
|
768
|
+
contain, deeper = children_reading(expected, deep)
|
|
769
|
+
unless contain
|
|
770
|
+
expected.children.each_with_index do |child, index|
|
|
771
|
+
at = node.children[index]
|
|
772
|
+
return { kind: :missing, expected: child, inside: node } if at.nil?
|
|
773
|
+
if scores.of(child, at, deeper).zero?
|
|
774
|
+
return { kind: :unmatched, expected: child, inside: node, nearest: { node: at, relation: :at_place }, after: index.zero? ? nil : node.children[index - 1], place: [at] }
|
|
775
|
+
end
|
|
776
|
+
|
|
777
|
+
found = explain_at(scores, child, at, deeper, root)
|
|
778
|
+
return found if found
|
|
779
|
+
end
|
|
780
|
+
extra = node.children[expected.children.size]
|
|
781
|
+
return extra && { kind: :extra, expected: expected, actual: node, extra: extra }
|
|
782
|
+
end
|
|
783
|
+
# A fragment's best place may be the top of the snapshot, whose list
|
|
784
|
+
# is no node a reader can look at.
|
|
785
|
+
inside = node.equal?(root) ? nil : node
|
|
786
|
+
_, pairs = scores.aligned(expected.children, node.children)
|
|
787
|
+
expected.children.each_with_index do |child, index|
|
|
788
|
+
at = pairs[index]
|
|
789
|
+
if at.nil?
|
|
790
|
+
# Every earlier child is paired, so the place starts after the
|
|
791
|
+
# previous one's pair and ends before the next sibling's.
|
|
792
|
+
previous = pairs[index - 1]
|
|
793
|
+
later = (index + 1...expected.children.size).find { pairs.key?(_1) }
|
|
794
|
+
from = (previous || -1) + 1
|
|
795
|
+
place = later ? node.children[from...pairs.fetch(later)] : node.children[from..]
|
|
796
|
+
return unmatched(child, inside, root, previous && node.children.fetch(previous), place || [])
|
|
797
|
+
end
|
|
798
|
+
found = explain_at(scores, child, node.children.fetch(at), false, root)
|
|
799
|
+
return found if found
|
|
800
|
+
end
|
|
801
|
+
nil
|
|
802
|
+
end
|
|
803
|
+
|
|
804
|
+
# Where `expected` first parts from `actual`, or nil when it fits. The
|
|
805
|
+
# walk starts at the actual node the expected tree fits best, the
|
|
806
|
+
# earliest of equals, and follows the docstring's order from there.
|
|
807
|
+
#: (Node, Node) -> Hash[Symbol, untyped]?
|
|
808
|
+
def misfit(actual, expected)
|
|
809
|
+
return nil if snapshot_fits?(actual, expected)
|
|
810
|
+
|
|
811
|
+
scores = Scores.new
|
|
812
|
+
best = nil #: Node?
|
|
813
|
+
best_score = 0
|
|
814
|
+
every_node(actual).each do |node|
|
|
815
|
+
score = scores.of(expected, node, false)
|
|
816
|
+
next unless score > best_score
|
|
817
|
+
|
|
818
|
+
best = node
|
|
819
|
+
best_score = score
|
|
820
|
+
end
|
|
821
|
+
# A fragment fits every node of a role, so its best place counts the
|
|
822
|
+
# children it fits; with none, the first of them fits nothing anywhere.
|
|
823
|
+
top = (!expected.text? && expected.role == FRAGMENT ? expected.children.first : expected) || expected
|
|
824
|
+
return unmatched(top, nil, actual) if best.nil?
|
|
825
|
+
|
|
826
|
+
explain_at(scores, expected, best, false, actual) || unmatched(top, nil, actual)
|
|
827
|
+
end
|
|
828
|
+
|
|
829
|
+
#: (String) -> String
|
|
830
|
+
def source_text(source) = source.size > SOURCE_LIMIT ? "#{source[0, SOURCE_LIMIT]}..." : source
|
|
831
|
+
|
|
832
|
+
#: (Node) -> String
|
|
833
|
+
def actual_named(node) = "#{source_text(node.source)} (actual line #{node.line})"
|
|
834
|
+
|
|
835
|
+
#: (Node) -> String
|
|
836
|
+
def expected_named(node) = "expected line #{node.line} (#{source_text(node.source)})"
|
|
837
|
+
|
|
838
|
+
# What keeps an actual node of the expected line's role and name from
|
|
839
|
+
# fitting it: the first state or property it prints otherwise, or nil
|
|
840
|
+
# when the two lines already show it.
|
|
841
|
+
#: (Node, Node) -> String?
|
|
842
|
+
def line_difference(actual, expected)
|
|
843
|
+
return nil if actual.text? || expected.text?
|
|
844
|
+
return nil if expected.role != FRAGMENT && (actual.role != expected.role || (!expected.name.empty? && actual.name != expected.name))
|
|
845
|
+
|
|
846
|
+
STATE_NAMES.each do |state|
|
|
847
|
+
next if state_fits?(state, actual.states, expected.states)
|
|
848
|
+
|
|
849
|
+
printed = actual.states[state]
|
|
850
|
+
return "which prints no #{state} state" if printed.nil?
|
|
851
|
+
|
|
852
|
+
return "which prints [#{printed == true ? state : "#{state}=#{printed}"}]"
|
|
853
|
+
end
|
|
854
|
+
expected.props.each do |prop, text|
|
|
855
|
+
next if text_fits?(actual.props[prop], text)
|
|
856
|
+
|
|
857
|
+
printed = actual.props[prop]
|
|
858
|
+
return printed.nil? ? "which has no /#{prop}" : "whose /#{prop} is #{source_text(printed)}"
|
|
859
|
+
end
|
|
860
|
+
nil
|
|
861
|
+
end
|
|
862
|
+
|
|
863
|
+
#: (Node) -> String
|
|
864
|
+
def role_words(expected)
|
|
865
|
+
return "a text" if expected.text?
|
|
866
|
+
return "a node with #{expected.props.keys.map { "/#{_1}" }.join(" and ")}" if expected.role == FRAGMENT
|
|
867
|
+
|
|
868
|
+
expected.role.match?(/\A[aeiou]/i) ? "an #{expected.role}" : "a #{expected.role}"
|
|
869
|
+
end
|
|
870
|
+
|
|
871
|
+
#: (Node) -> Integer
|
|
872
|
+
def last_line(node) = every_node(node).map(&:line).max || node.line
|
|
873
|
+
|
|
874
|
+
# A misfit as one line: the expected node by its docstring line,
|
|
875
|
+
# where it was looked for, and the nearest actual node by its
|
|
876
|
+
# snapshot line.
|
|
877
|
+
#: (Hash[Symbol, untyped]) -> String
|
|
878
|
+
def misfit_text(misfit)
|
|
879
|
+
case misfit[:kind]
|
|
880
|
+
when :extra
|
|
881
|
+
stated = misfit[:expected].role == FRAGMENT ? "the expected list" : expected_named(misfit[:expected])
|
|
882
|
+
"#{stated} states /children: #{misfit[:expected].children_mode || "deep-equal"}, and #{actual_named(misfit[:actual])} holds a child it does not name: #{actual_named(misfit[:extra])}"
|
|
883
|
+
when :missing
|
|
884
|
+
count = misfit[:inside].children.size
|
|
885
|
+
"#{expected_named(misfit[:expected])} fits no child of #{actual_named(misfit[:inside])}, which has #{count == 1 ? "1 child" : "#{count} children"} and none at that place"
|
|
886
|
+
else
|
|
887
|
+
where = misfit[:inside].nil? ? "no node of the snapshot" : "no child of #{actual_named(misfit[:inside])}"
|
|
888
|
+
head = "#{expected_named(misfit[:expected])} fits #{where}"
|
|
889
|
+
nearest = misfit[:nearest]
|
|
890
|
+
if nearest.nil?
|
|
891
|
+
none = "#{head}, and no node there is #{role_words(misfit[:expected])}"
|
|
892
|
+
place = misfit[:place]
|
|
893
|
+
return none if place.empty?
|
|
894
|
+
return "#{none}; the child at that place is #{actual_named(place.first)}" if place.size == 1
|
|
895
|
+
|
|
896
|
+
return "#{none}; the #{place.size} children at that place are actual lines #{place.first.line} to #{last_line(place.last)}"
|
|
897
|
+
end
|
|
898
|
+
case nearest[:relation]
|
|
899
|
+
when :child
|
|
900
|
+
detail = line_difference(nearest[:node], misfit[:expected])
|
|
901
|
+
"#{head}; nearest: #{actual_named(nearest[:node])}#{detail ? ", #{detail}" : ""}"
|
|
902
|
+
when :at_place then "#{head}; the child at that place is #{actual_named(nearest[:node])}"
|
|
903
|
+
when :deeper then "#{head}; nearest: #{actual_named(nearest[:node])}, which sits deeper than a child"
|
|
904
|
+
else "#{head}; nearest: #{actual_named(nearest[:node])}, which fits the line but an earlier line took it, or it sits out of the order the lines give"
|
|
905
|
+
end
|
|
906
|
+
end
|
|
907
|
+
end
|
|
908
|
+
|
|
909
|
+
# Why an expected text is no tree a claim can be judged on, as
|
|
910
|
+
# [:unreadable, line] or [:names_nothing]: either breaks the claim on
|
|
911
|
+
# every page.
|
|
912
|
+
#: ([Symbol, String] | [Symbol]) -> String
|
|
913
|
+
def problem_reason(problem)
|
|
914
|
+
if problem.first == :unreadable
|
|
915
|
+
"the expected ARIA snapshot does not read: #{problem[1]}"
|
|
916
|
+
else
|
|
917
|
+
"fits-aria-snapshot has an expected ARIA snapshot that names no node, which every page holds"
|
|
918
|
+
end
|
|
919
|
+
end
|
|
920
|
+
|
|
921
|
+
#: (String) -> (Node | [Symbol, String] | [Symbol])
|
|
922
|
+
def expected_tree(text)
|
|
923
|
+
read = read_expected(text)
|
|
924
|
+
return [:unreadable, read] if read.is_a?(String)
|
|
925
|
+
|
|
926
|
+
read[1].zero? ? [:names_nothing] : read[0]
|
|
927
|
+
end
|
|
928
|
+
|
|
929
|
+
# Why an expected text breaks a claim on every page, or nil: what the
|
|
930
|
+
# check reports from the line alone, before a run.
|
|
931
|
+
#: (String) -> ([Symbol, String] | [Symbol])?
|
|
932
|
+
def expected_problem(text)
|
|
933
|
+
tree = expected_tree(text)
|
|
934
|
+
tree.is_a?(Node) ? nil : tree
|
|
935
|
+
end
|
|
936
|
+
|
|
937
|
+
# Whether a claim's actual snapshot fits its expected one, from the
|
|
938
|
+
# two texts the record keeps: { fits: }, { broken: }, or { unjudged: }.
|
|
939
|
+
# An expected text that is no tree, or names nothing, breaks the
|
|
940
|
+
# claim, since the line states it. An actual text that does not read
|
|
941
|
+
# leaves the claim unjudged: the browser printed something this
|
|
942
|
+
# reader does not know, which says nothing about the page.
|
|
943
|
+
#: (String, String) -> Hash[Symbol, untyped]
|
|
944
|
+
def judge(actual_text, expected_text)
|
|
945
|
+
expected = expected_tree(expected_text)
|
|
946
|
+
return { broken: problem_reason(expected) } unless expected.is_a?(Node)
|
|
947
|
+
|
|
948
|
+
actual = read_actual(actual_text)
|
|
949
|
+
return { unjudged: "the ARIA snapshot nukadoko took does not read: #{actual}, so the claim was not judged" } if actual.is_a?(String)
|
|
950
|
+
|
|
951
|
+
{ fits: snapshot_fits?(actual, expected) }
|
|
952
|
+
end
|
|
953
|
+
|
|
954
|
+
#: (String, String) -> Hash[Symbol, untyped]?
|
|
955
|
+
def misfit_of(actual_text, expected_text)
|
|
956
|
+
expected = expected_tree(expected_text)
|
|
957
|
+
actual = read_actual(actual_text)
|
|
958
|
+
return nil unless expected.is_a?(Node) && actual.is_a?(Node)
|
|
959
|
+
|
|
960
|
+
misfit(actual, expected)
|
|
961
|
+
end
|
|
962
|
+
|
|
963
|
+
# Where a broken claim's two snapshots part, as one line, or nil when
|
|
964
|
+
# they fit or either does not read.
|
|
965
|
+
#: (String, String) -> String?
|
|
966
|
+
def difference(actual_text, expected_text)
|
|
967
|
+
found = misfit_of(actual_text, expected_text)
|
|
968
|
+
found && misfit_text(found)
|
|
969
|
+
end
|
|
970
|
+
|
|
971
|
+
# The actual lines a misfit points at, so a line that cuts a long
|
|
972
|
+
# snapshot shows where the fix goes: from the child the previous
|
|
973
|
+
# expected sibling fit (or the node it was looked for under) to the
|
|
974
|
+
# nearest node, or with no node of its role, through the children at
|
|
975
|
+
# its place; the end of the node with a child missing; or the extra
|
|
976
|
+
# child. At most EXCERPT_LINES of them, kept at the end the named node
|
|
977
|
+
# is on, as { first:, last:, text: }.
|
|
978
|
+
#: (String, String) -> Hash[Symbol, untyped]?
|
|
979
|
+
def excerpt(actual_text, expected_text)
|
|
980
|
+
found = misfit_of(actual_text, expected_text)
|
|
981
|
+
return nil if found.nil?
|
|
982
|
+
|
|
983
|
+
span =
|
|
984
|
+
case found[:kind]
|
|
985
|
+
when :missing then [found[:inside].line, last_line(found[:inside]), :end]
|
|
986
|
+
when :extra then [found[:actual].line, found[:extra].line, :end]
|
|
987
|
+
else
|
|
988
|
+
start = found[:after]&.line || found[:inside]&.line
|
|
989
|
+
if (nearest = found[:nearest])
|
|
990
|
+
lines = [start || nearest[:node].line, nearest[:node].line]
|
|
991
|
+
[lines.min, lines.max, :end]
|
|
992
|
+
elsif (first = found[:place].first)
|
|
993
|
+
[start || first.line, last_line(found[:place].last), :start]
|
|
994
|
+
elsif found[:inside]
|
|
995
|
+
[found[:inside].line, last_line(found[:inside]), :start]
|
|
996
|
+
end
|
|
997
|
+
end
|
|
998
|
+
return nil if span.nil?
|
|
999
|
+
|
|
1000
|
+
from = [1, span[0]].max
|
|
1001
|
+
first = span[2] == :end ? [from, span[1] - EXCERPT_LINES + 1].max : from
|
|
1002
|
+
last = span[2] == :end ? span[1] : [span[1], from + EXCERPT_LINES - 1].min
|
|
1003
|
+
text = (actual_text.split(/\r?\n/, -1)[(first - 1)...last] || []).map { source_text(_1) }.join("\n")
|
|
1004
|
+
{ first: first, last: last, text: text }
|
|
1005
|
+
end
|
|
1006
|
+
end
|
|
1007
|
+
end
|
|
1008
|
+
end
|