Skip to content

Commit c2b928d

Browse files
committed
Update tests for hash label fixes
1 parent 6ca43fa commit c2b928d

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

lib/syntax_tree/cli.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,21 @@ def highlight_error(error, source)
332332
# Take a line of Ruby source and colorize the output.
333333
def colorize_line(line)
334334
require "irb"
335-
IRB::Color.colorize_code(line, complete: false, ignore_error: true)
335+
IRB::Color.colorize_code(line, **colorize_options)
336+
end
337+
338+
# These are the options we're going to pass into IRB::Color.colorize_code.
339+
# Since we support multiple versions of IRB, we're going to need to do
340+
# some reflection to make sure we always pass valid options.
341+
def colorize_options
342+
options = { complete: false }
343+
344+
parameters = IRB::Color.method(:colorize_code).parameters
345+
if parameters.any? { |(_type, name)| name == :ignore_error }
346+
options[:ignore_error] = true
347+
end
348+
349+
options
336350
end
337351
end
338352
end

lib/syntax_tree/node.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -1388,14 +1388,19 @@ def format(q)
13881388
module HashKeyFormatter
13891389
# Formats the keys of a hash literal using labels.
13901390
class Labels
1391+
LABEL = /^[@$_A-Za-z]([_A-Za-z0-9]*)?([!_=?A-Za-z0-9])?$/
1392+
13911393
def format_key(q, key)
13921394
case key
1393-
when Label
1395+
in Label
13941396
q.format(key)
1395-
when SymbolLiteral
1397+
in SymbolLiteral
13961398
q.format(key.value)
13971399
q.text(":")
1398-
when DynaSymbol
1400+
in DynaSymbol[parts: [TStringContent[value: LABEL] => part]]
1401+
q.format(part)
1402+
q.text(":")
1403+
in DynaSymbol
13991404
q.format(key)
14001405
q.text(":")
14011406
end

test/cli_test.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ def test_help_default
119119
end
120120

121121
def test_no_arguments
122-
*, stderr = capture_io { SyntaxTree::CLI.run(["check"]) }
123-
assert_includes(stderr, "stree help")
122+
$stdin.stub(:tty?, true) do
123+
*, stderr = capture_io { SyntaxTree::CLI.run(["check"]) }
124+
assert_includes(stderr, "stree help")
125+
end
124126
end
125127

126128
def test_no_arguments_no_tty
@@ -134,7 +136,7 @@ def test_no_arguments_no_tty
134136
end
135137

136138
def test_generic_error
137-
SyntaxTree.stub(:format, -> (*) { raise }) do
139+
SyntaxTree.stub(:format, ->(*) { raise }) do
138140
result = run_cli("format")
139141
refute_equal(0, result.status)
140142
end
@@ -154,9 +156,7 @@ def run_cli(command, file: nil)
154156

155157
status = nil
156158
stdio, stderr =
157-
capture_io do
158-
status = SyntaxTree::CLI.run([command, file.path])
159-
end
159+
capture_io { status = SyntaxTree::CLI.run([command, file.path]) }
160160

161161
Result.new(status: status, stdio: stdio, stderr: stderr)
162162
ensure

test/fixtures/assoc.rb

+6
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@
4040
}
4141
% # >= 3.1.0
4242
{ foo: }
43+
%
44+
{ "foo": "bar" }
45+
-
46+
{ foo: "bar" }
47+
%
48+
{ "foo #{bar}": "baz" }

test/fixtures/bare_assoc_hash.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
%
88
foo(:"bar" => bar)
99
-
10-
foo("bar": bar)
10+
foo(bar: bar)
1111
%
1212
foo(bar => bar, baz: baz)
1313
-

test/fixtures/dyna_symbol.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
%
1616
{ %s[foo] => bar }
1717
-
18-
{ "foo": bar }
18+
{ foo: bar }
1919
%
2020
%s[
2121
foo

test/fixtures/hash.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
%
1010
{ :"bar" => bar }
1111
-
12-
{ "bar": bar }
12+
{ bar: bar }
1313
%
1414
{ bar => bar, baz: baz }
1515
-

0 commit comments

Comments
 (0)