diff --git a/test/language_server/inlay_hints_test.rb b/test/language_server/inlay_hints_test.rb deleted file mode 100644 index d3741894..00000000 --- a/test/language_server/inlay_hints_test.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -require_relative "../test_helper" -require "syntax_tree/language_server" - -module SyntaxTree - class LanguageServer - class InlayHintsTest < Minitest::Test - def test_assignments_in_parameters - assert_hints(2, "def foo(a = b = c); end") - end - - def test_operators_in_binaries - assert_hints(2, "1 + 2 * 3") - end - - def test_binaries_in_assignments - assert_hints(2, "a = 1 + 2") - end - - def test_nested_ternaries - assert_hints(2, "a ? b : c ? d : e") - end - - def test_bare_rescue - assert_hints(1, "begin; rescue; end") - end - - def test_unary_in_binary - assert_hints(2, "-a + b") - end - - private - - def assert_hints(expected, source) - visitor = InlayHints.new - SyntaxTree.parse(source).accept(visitor) - - assert_equal(expected, visitor.hints.length) - end - end - end -end diff --git a/test/language_server_test.rb b/test/language_server_test.rb deleted file mode 100644 index 466bf737..00000000 --- a/test/language_server_test.rb +++ /dev/null @@ -1,287 +0,0 @@ -# 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 < Struct.new(:id) - def to_hash - { method: "shutdown", id: id } - 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 TextDocumentInlayHint < Struct.new(:id, :uri) - def to_hash - { - method: "textDocument/inlayHint", - 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(3) - ] - - case run_server(messages) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: [{ newText: new_text }] }, - { id: 3, result: {} } - ] - assert_equal("class Bar\nend\n", new_text) - end - end - - def test_formatting_failure - messages = [ - Initialize.new(1), - TextDocumentDidOpen.new("file:///path/to/file.rb", "<>"), - TextDocumentFormatting.new(2, "file:///path/to/file.rb"), - Shutdown.new(3) - ] - - case run_server(messages) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: }, - { id: 3, result: {} } - ] - assert_nil(result) - end - end - - def test_formatting_print_width - contents = "#{"a" * 40} + #{"b" * 40}\n" - messages = [ - Initialize.new(1), - TextDocumentDidOpen.new("file:///path/to/file.rb", contents), - TextDocumentFormatting.new(2, "file:///path/to/file.rb"), - TextDocumentDidClose.new("file:///path/to/file.rb"), - Shutdown.new(3) - ] - - case run_server(messages, print_width: 100) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: [{ newText: new_text }] }, - { id: 3, result: {} } - ] - assert_equal(contents, new_text) - end - end - - def test_inlay_hint - messages = [ - Initialize.new(1), - TextDocumentDidOpen.new("file:///path/to/file.rb", <<~RUBY), - begin - 1 + 2 * 3 - rescue - end - RUBY - TextDocumentInlayHint.new(2, "file:///path/to/file.rb"), - Shutdown.new(3) - ] - - case run_server(messages) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: hints }, - { id: 3, result: {} } - ] - assert_equal(3, hints.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(3) - ] - - case run_server(messages) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: }, - { id: 3, 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(3) - ] - - case run_server(messages) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: [{ newText: new_text }] }, - { id: 3, result: {} } - ] - assert_equal("class Foo\nend\n", new_text) - end - end - end - - def test_bogus_request - assert_raises(ArgumentError) do - run_server([{ method: "textDocument/bogus" }]) - end - end - - def test_clean_shutdown - messages = [Initialize.new(1), Shutdown.new(2)] - - case run_server(messages) - in [{ id: 1, result: { capabilities: Hash } }, { id: 2, result: {} }] - assert_equal(true, true) - end - end - - def test_file_that_does_not_exist - messages = [ - Initialize.new(1), - TextDocumentFormatting.new(2, "file:///path/to/file.rb"), - Shutdown.new(3) - ] - - case run_server(messages) - in [ - { id: 1, result: { capabilities: Hash } }, - { id: 2, result: nil }, - { id: 3, result: {} } - ] - assert_equal(true, true) - 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, print_width: DEFAULT_PRINT_WIDTH) - input = StringIO.new(messages.map { |message| write(message) }.join) - output = StringIO.new - - LanguageServer.new( - input: input, - output: output, - print_width: print_width - ).run - read(output.tap(&:rewind)) - end - end -end diff --git a/test/location_test.rb b/test/location_test.rb index 2a697281..892982d0 100644 --- a/test/location_test.rb +++ b/test/location_test.rb @@ -14,18 +14,18 @@ def test_lines def test_deconstruct location = Location.fixed(line: 1, char: 0, column: 0) - case location - in [start_line, 0, 0, *] - assert_equal(1, start_line) + case; when ((__m__ = location)) && false + when (((start_line,) = nil) || (__m__.respond_to?(:deconstruct) && (((__m_arr__ = __m__.deconstruct) || true) && (Array === __m_arr__ || Kernel.raise(TypeError, "#deconstruct must return Array"))) && (((start_line = __m_arr__[0]) || true) && ((0 === __m_arr__[1]) && ((0 === __m_arr__[2]) && __m_arr__[3..-1]))))) + assert_equal(1, start_line); else; Kernel.raise(NoMatchingPatternError, __m__.inspect) end end def test_deconstruct_keys location = Location.fixed(line: 1, char: 0, column: 0) - case location - in start_line: - assert_equal(1, start_line) + case; when ((__m__ = location)) && false + when (((start_line,) = nil) || (__m__.respond_to?(:deconstruct_keys) && (((__m_hash__ = __m__.deconstruct_keys([:start_line])) || true) && (Hash === __m_hash__ || Kernel.raise(TypeError, "#deconstruct_keys must return Hash"))) && (__m_hash__.key?(:start_line) && ((start_line = __m_hash__[:start_line]) || true)))) + assert_equal(1, start_line); else; Kernel.raise(NoMatchingPatternError, __m__.inspect) end end end diff --git a/test/node_test.rb b/test/node_test.rb index 1a5af125..e2dde34f 100644 --- a/test/node_test.rb +++ b/test/node_test.rb @@ -759,9 +759,9 @@ def test_program program = parser.parse refute(parser.error?) - case program - in statements: { body: [statement] } - assert_kind_of(VCall, statement) + case; when ((__m__ = program)) && false + when (((statement,) = nil) || (__m__.respond_to?(:deconstruct_keys) && (((__m_hash__ = __m__.deconstruct_keys([:statements])) || true) && (Hash === __m_hash__ || Kernel.raise(TypeError, "#deconstruct_keys must return Hash"))) && (__m_hash__.key?(:statements) && (__m_hash__[:statements].respond_to?(:deconstruct_keys) && (((__m_hash__k0__ = __m_hash__[:statements].deconstruct_keys([:body])) || true) && (Hash === __m_hash__k0__ || Kernel.raise(TypeError, "#deconstruct_keys must return Hash"))) && (__m_hash__k0__.key?(:body) && (__m_hash__k0__[:body].respond_to?(:deconstruct) && (((__m_hash__k0__k1__ = __m_hash__k0__[:body].deconstruct) || true) && (Array === __m_hash__k0__k1__ || Kernel.raise(TypeError, "#deconstruct must return Array"))) && (1 == __m_hash__k0__k1__.size) && ((statement = __m_hash__k0__k1__[0]) || true))))))) + assert_kind_of(VCall, statement); else; Kernel.raise(NoMatchingPatternError, __m__.inspect) end json = JSON.parse(program.to_json) @@ -1047,11 +1047,11 @@ def test_multibyte_column_positions def test_root_class_raises_not_implemented_errors { - accept: [nil], - child_nodes: [], - deconstruct: [], - deconstruct_keys: [[]], - format: [nil] + accept: [nil], + child_nodes: [], + deconstruct: [], + deconstruct_keys: [[]], + format: [nil] }.each do |method, arguments| assert_raises(NotImplementedError) do Node.new.public_send(method, *arguments) @@ -1063,22 +1063,22 @@ def test_root_class_raises_not_implemented_errors def location(lines: 1..1, chars: 0..0, columns: 0..0) Location.new( - start_line: lines.begin, - start_char: chars.begin, - start_column: columns.begin, - end_line: lines.end, - end_char: chars.end, - end_column: columns.end + start_line: lines.begin, + start_char: chars.begin, + start_column: columns.begin, + end_line: lines.end, + end_char: chars.end, + end_column: columns.end ) end def assert_node(kind, source, at: nil) at ||= - location( - lines: 1..[1, source.count("\n")].max, - chars: 0..source.chomp.size, - columns: 0..source.chomp.size - ) + location( + lines: 1..[1, source.count("\n")].max, + chars: 0..source.chomp.size, + columns: 0..source.chomp.size + ) # Parse the example, get the outputted parse tree, and assert that it was # able to successfully parse. diff --git a/test/test_helper.rb b/test/test_helper.rb index 80e514f0..ef8b5ff6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -70,12 +70,12 @@ def assert_syntax_tree(node) # Get a match expression from the node, then assert that it can in fact # match the node. # rubocop:disable all - assert(eval(<<~RUBY)) - case node - in #{node.construct_keys} - true - end - RUBY + # assert(eval(<<~RUBY)) + # case node + # in #{node.construct_keys} + # true + # end + # RUBY end Minitest::Test.include(self)