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
Next Next commit
Tests for the language server
  • Loading branch information
kddnewton committed May 24, 2022
commit f1e1c10f18a24e492e55437edcc044f0aed380ed
4 changes: 1 addition & 3 deletions lib/syntax_tree/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def run
id:,
params: { textDocument: { uri: } }
}
output = []
PP.pp(SyntaxTree.parse(store[uri]), output)
write(id: id, result: output.join)
write(id: id, result: PP.pp(SyntaxTree.parse(store[uri]), +""))
in method: %r{\$/.+}
# ignored
else
Expand Down
9 changes: 9 additions & 0 deletions lib/syntax_tree/language_server/inlay_hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def visit(node)
#
def visit_assign(node)
parentheses(node.location) if stack[-2].is_a?(Params)
super
end

# Adds parentheses around binary expressions to make it clear which
Expand All @@ -57,6 +58,8 @@ def visit_binary(node)
parentheses(node.location)
else
end

super
end

# Adds parentheses around ternary operators contained within certain
Expand All @@ -73,6 +76,8 @@ def visit_if_op(node)
if stack[-2] in Assign | Binary | IfOp | OpAssign
parentheses(node.location)
end

super
end

# Adds the implicitly rescued StandardError into a bare rescue clause. For
Expand All @@ -92,6 +97,8 @@ def visit_rescue(node)
if node.exception.nil?
after[node.location.start_char + "rescue".length] << " StandardError"
end

super
end

# Adds parentheses around unary statements using the - operator that are
Expand All @@ -107,6 +114,8 @@ def visit_unary(node)
if stack[-2].is_a?(Binary) && (node.operator == "-")
parentheses(node.location)
end

super
end

def self.find(program)
Expand Down
32 changes: 22 additions & 10 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2129,11 +2129,13 @@ def format(q)
#
# break
#
in [Paren[
contents: {
body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
}
]]
in [
Paren[
contents: {
body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
}
]
]
# Here we have a single argument that is a set of parentheses wrapping
# an array literal that has at least 2 elements. We're going to print
# the contents of the array directly. This would be like if we had:
Expand All @@ -2146,7 +2148,9 @@ 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 @@ -2174,15 +2178,19 @@ 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 @@ -2196,7 +2204,9 @@ 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 @@ -2207,7 +2217,9 @@ 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
201 changes: 201 additions & 0 deletions test/language_server_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# frozen_string_literal: true

require_relative "test_helper"
require "syntax_tree/language_server"

module SyntaxTree
class LanguageServerTest < Minitest::Test
class Initialize < Struct.new(:id)
def to_hash
{ method: "initialize", id: id }
end
end

class Shutdown
def to_hash
{ method: "shutdown" }
end
end

class TextDocumentDidOpen < Struct.new(:uri, :text)
def to_hash
{
method: "textDocument/didOpen",
params: {
textDocument: {
uri: uri,
text: text
}
}
}
end
end

class TextDocumentDidChange < Struct.new(:uri, :text)
def to_hash
{
method: "textDocument/didChange",
params: {
textDocument: {
uri: uri
},
contentChanges: [{ text: text }]
}
}
end
end

class TextDocumentDidClose < Struct.new(:uri)
def to_hash
{
method: "textDocument/didClose",
params: {
textDocument: {
uri: uri
}
}
}
end
end

class TextDocumentFormatting < Struct.new(:id, :uri)
def to_hash
{
method: "textDocument/formatting",
id: id,
params: {
textDocument: {
uri: uri
}
}
}
end
end

class TextDocumentInlayHints < Struct.new(:id, :uri)
def to_hash
{
method: "textDocument/inlayHints",
id: id,
params: {
textDocument: {
uri: uri
}
}
}
end
end

class SyntaxTreeVisualizing < Struct.new(:id, :uri)
def to_hash
{
method: "syntaxTree/visualizing",
id: id,
params: {
textDocument: {
uri: uri
}
}
}
end
end

def test_formatting
messages = [
Initialize.new(1),
TextDocumentDidOpen.new("file:///path/to/file.rb", "class Foo; end"),
TextDocumentDidChange.new("file:///path/to/file.rb", "class Bar; end"),
TextDocumentFormatting.new(2, "file:///path/to/file.rb"),
TextDocumentDidClose.new("file:///path/to/file.rb"),
Shutdown.new
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
assert_equal("class Bar\nend\n", new_text)
end
end

def test_inlay_hints
messages = [
Initialize.new(1),
TextDocumentDidOpen.new("file:///path/to/file.rb", <<~RUBY),
begin
1 + 2 * 3
rescue
end
RUBY
TextDocumentInlayHints.new(2, "file:///path/to/file.rb"),
Shutdown.new
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } },
{ id: 2, result: { before:, after: } }
assert_equal(1, before.length)
assert_equal(2, after.length)
end
end

def test_visualizing
messages = [
Initialize.new(1),
TextDocumentDidOpen.new("file:///path/to/file.rb", "1 + 2"),
SyntaxTreeVisualizing.new(2, "file:///path/to/file.rb"),
Shutdown.new
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } }, { id: 2, result: }
assert_equal(
"(program (statements ((binary (int \"1\") + (int \"2\")))))\n",
result
)
end
end

def test_reading_file
Tempfile.open(%w[test- .rb]) do |file|
file.write("class Foo; end")
file.rewind

messages = [
Initialize.new(1),
TextDocumentFormatting.new(2, "file://#{file.path}"),
Shutdown.new
]

case run_server(messages)
in { id: 1, result: { capabilities: Hash } },
{ id: 2, result: [{ newText: new_text }] }
assert_equal("class Foo\nend\n", new_text)
end
end
end

private

def write(content)
request = content.to_hash.merge(jsonrpc: "2.0").to_json
"Content-Length: #{request.bytesize}\r\n\r\n#{request}"
end

def read(content)
[].tap do |messages|
while (headers = content.gets("\r\n\r\n"))
source = content.read(headers[/Content-Length: (\d+)/i, 1].to_i)
messages << JSON.parse(source, symbolize_names: true)
end
end
end

def run_server(messages)
input = StringIO.new(messages.map { |message| write(message) }.join)
output = StringIO.new

LanguageServer.new(input: input, output: output).run
read(output.tap(&:rewind))
end
end
end