Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Tests #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reformat with syntax tree
  • Loading branch information
kddnewton committed May 24, 2022
commit ac41ba75a79ee63b62700fc31ee9dcabb8890ec6
7 changes: 6 additions & 1 deletion lib/syntax_tree/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class Formatter < PrettierPrint
attr_reader :quote, :trailing_comma
alias trailing_comma? trailing_comma

def initialize(source, *args, quote: OPTIONS[:quote], trailing_comma: OPTIONS[:trailing_comma])
def initialize(
source,
*args,
quote: OPTIONS[:quote],
trailing_comma: OPTIONS[:trailing_comma]
)
super(*args)

@source = source
Expand Down
20 changes: 5 additions & 15 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2148,9 +2148,7 @@ def format(q)
#
q.text(" ")
format_array_contents(q, array)
in [
Paren[contents: { body: [ArrayLiteral => statement] }]
]
in [Paren[contents: { body: [ArrayLiteral => statement] }]]
# Here we have a single argument that is a set of parentheses wrapping
# an array literal that has 0 or 1 elements. We're going to skip the
# parentheses but print the array itself. This would be like if we
Expand Down Expand Up @@ -2178,19 +2176,15 @@ def format(q)
#
q.text(" ")
q.format(statement)
in [
Paren => part
]
in [Paren => part]
# Here we have a single argument that is a set of parentheses. We're
# going to print the parentheses themselves as if they were the set of
# arguments. This would be like if we had:
#
# break(foo.bar)
#
q.format(part)
in [
ArrayLiteral[contents: { parts: [_, _, *] }] => array
]
in [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
# Here there is a single argument that is an array literal with at
# least two elements. We skip directly into the array literal's
# elements in order to print the contents. This would be like if we
Expand All @@ -2204,9 +2198,7 @@ def format(q)
#
q.text(" ")
format_array_contents(q, array)
in [
ArrayLiteral => part
]
in [ArrayLiteral => part]
# Here there is a single argument that is an array literal with 0 or 1
# elements. In this case we're going to print the array as it is
# because skipping the brackets would change the remaining. This would
Expand All @@ -2217,9 +2209,7 @@ def format(q)
#
q.text(" ")
q.format(part)
in [
_
]
in [_]
# Here there is a single argument that hasn't matched one of our
# previous cases. We're going to print the argument as it is. This
# would be like if we had:
Expand Down
20 changes: 13 additions & 7 deletions test/language_server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ def test_formatting
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
]
assert_equal("class Bar\nend\n", new_text)
end
end
Expand All @@ -131,8 +133,10 @@ def test_inlay_hints
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } },
{ id: 2, result: { before:, after: } }
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: { before:, after: } }
]
assert_equal(1, before.length)
assert_equal(2, after.length)
end
Expand All @@ -147,7 +151,7 @@ def test_visualizing
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } }, { id: 2, result: }
in [{ id: 1, result: { capabilities: Hash } }, { id: 2, result: }]
assert_equal(
"(program (statements ((binary (int \"1\") + (int \"2\")))))\n",
result
Expand All @@ -167,8 +171,10 @@ def test_reading_file
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
in [
{ id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
]
assert_equal("class Foo\nend\n", new_text)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_deconstruct_keys
location = Location.fixed(line: 1, char: 0, column: 0)

case location
in { start_line: 1 }
in start_line: 1
end
end
end
Expand Down
12 changes: 3 additions & 9 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,15 @@ def test_parses_ripper_methods
end

def test_errors_on_missing_token_with_location
assert_raises(Parser::ParseError) do
SyntaxTree.parse("\"foo")
end
assert_raises(Parser::ParseError) { SyntaxTree.parse("\"foo") }
end

def test_errors_on_missing_token_without_location
assert_raises(Parser::ParseError) do
SyntaxTree.parse(":\"foo")
end
assert_raises(Parser::ParseError) { SyntaxTree.parse(":\"foo") }
end

def test_handles_strings_with_non_terminated_embedded_expressions
assert_raises(Parser::ParseError) do
SyntaxTree.parse('"#{"')
end
assert_raises(Parser::ParseError) { SyntaxTree.parse('"#{"') }
end
end
end