From e517ba2e5b6434e7d2370de7703f481dcb8eecba Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Thu, 1 Jun 2023 17:05:35 +0900 Subject: Bump up syntax_suggest-1.1.0 --- spec/syntax_suggest/integration/exe_cli_spec.rb | 3 +- .../integration/ruby_command_line_spec.rb | 18 ++++ .../integration/syntax_suggest_spec.rb | 40 ++++++-- spec/syntax_suggest/spec_helper.rb | 6 ++ spec/syntax_suggest/unit/around_block_scan_spec.rb | 12 +-- spec/syntax_suggest/unit/block_expand_spec.rb | 30 ++++++ .../unit/capture/before_after_keyword_ends_spec.rb | 47 +++++++++ .../unit/capture/falling_indent_lines_spec.rb | 44 ++++++++ .../unit/capture_code_context_spec.rb | 31 +++++- spec/syntax_suggest/unit/clean_document_spec.rb | 43 ++++---- spec/syntax_suggest/unit/code_line_spec.rb | 1 + spec/syntax_suggest/unit/core_ext_spec.rb | 34 ++++++ .../unit/display_invalid_blocks_spec.rb | 2 + spec/syntax_suggest/unit/scan_history_spec.rb | 114 +++++++++++++++++++++ 14 files changed, 389 insertions(+), 36 deletions(-) create mode 100644 spec/syntax_suggest/unit/capture/before_after_keyword_ends_spec.rb create mode 100644 spec/syntax_suggest/unit/capture/falling_indent_lines_spec.rb create mode 100644 spec/syntax_suggest/unit/core_ext_spec.rb create mode 100644 spec/syntax_suggest/unit/scan_history_spec.rb (limited to 'spec') diff --git a/spec/syntax_suggest/integration/exe_cli_spec.rb b/spec/syntax_suggest/integration/exe_cli_spec.rb index f0b49b4386..b9a3173715 100644 --- a/spec/syntax_suggest/integration/exe_cli_spec.rb +++ b/spec/syntax_suggest/integration/exe_cli_spec.rb @@ -13,7 +13,8 @@ module SyntaxSuggest end def exe(cmd) - out = run!("#{exe_path} #{cmd}", raise_on_nonzero_exit: false) + ruby = ENV.fetch("RUBY", "ruby") + out = run!("#{ruby} #{exe_path} #{cmd}", raise_on_nonzero_exit: false) puts out if ENV["SYNTAX_SUGGEST_DEBUG"] out end diff --git a/spec/syntax_suggest/integration/ruby_command_line_spec.rb b/spec/syntax_suggest/integration/ruby_command_line_spec.rb index 61102dad2a..b41a4c86e3 100644 --- a/spec/syntax_suggest/integration/ruby_command_line_spec.rb +++ b/spec/syntax_suggest/integration/ruby_command_line_spec.rb @@ -46,6 +46,24 @@ module SyntaxSuggest end end + # Since Ruby 3.2 includes syntax_suggest as a default gem, we might accidentally + # be requiring the default gem instead of this library under test. Assert that's + # not the case + it "tests current version of syntax_suggest" do + Dir.mktmpdir do |dir| + tmpdir = Pathname(dir) + script = tmpdir.join("script.rb") + contents = <<~'EOM' + puts "suggest_version is #{SyntaxSuggest::VERSION}" + EOM + script.write(contents) + + out = `#{ruby} -I#{lib_dir} -rsyntax_suggest/version #{script} 2>&1` + + expect(out).to include("suggest_version is #{SyntaxSuggest::VERSION}").once + end + end + it "detects require error and adds a message with auto mode" do Dir.mktmpdir do |dir| tmpdir = Pathname(dir) diff --git a/spec/syntax_suggest/integration/syntax_suggest_spec.rb b/spec/syntax_suggest/integration/syntax_suggest_spec.rb index bb50fafce7..64dafabcdd 100644 --- a/spec/syntax_suggest/integration/syntax_suggest_spec.rb +++ b/spec/syntax_suggest/integration/syntax_suggest_spec.rb @@ -21,10 +21,11 @@ module SyntaxSuggest filename: file ) end - debug_display(io.string) - debug_display(benchmark) end + debug_display(io.string) + debug_display(benchmark) + expect(io.string).to include(<<~'EOM') 6 class SyntaxTree < Ripper 170 def self.parse(source) @@ -115,9 +116,6 @@ module SyntaxSuggest expect(io.string).to include(<<~'EOM') 5 module DerailedBenchmarks 6 class RequireTree - 7 REQUIRED_BY = {} - 9 attr_reader :name - 10 attr_writer :cost > 13 def initialize(name) > 18 def self.reset! > 25 end @@ -160,7 +158,6 @@ module SyntaxSuggest out = io.string expect(out).to include(<<~EOM) 16 class Rexe - 18 VERSION = '1.5.1' > 77 class Lookups > 140 def format_requires > 148 end @@ -207,5 +204,36 @@ module SyntaxSuggest > 4 end EOM end + + it "empty else" do + source = <<~'EOM' + class Foo + def foo + if cond? + foo + else + + end + end + + # ... + + def bar + if @recv + end_is_missing_here + end + end + EOM + + io = StringIO.new + SyntaxSuggest.call( + io: io, + source: source + ) + out = io.string + expect(out).to include(<<~EOM) + end_is_missing_here + EOM + end end end diff --git a/spec/syntax_suggest/spec_helper.rb b/spec/syntax_suggest/spec_helper.rb index 67d401888b..89bc9f4ab1 100644 --- a/spec/syntax_suggest/spec_helper.rb +++ b/spec/syntax_suggest/spec_helper.rb @@ -16,6 +16,12 @@ RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = :expect end + + if config.color_mode == :automatic + if config.color_enabled? && ((ENV["TERM"] == "dumb") || ENV["NO_COLOR"]&.slice(0)) + config.color_mode = :off + end + end end # Used for debugging modifications to diff --git a/spec/syntax_suggest/unit/around_block_scan_spec.rb b/spec/syntax_suggest/unit/around_block_scan_spec.rb index 6053c3947e..d6756448bd 100644 --- a/spec/syntax_suggest/unit/around_block_scan_spec.rb +++ b/spec/syntax_suggest/unit/around_block_scan_spec.rb @@ -13,7 +13,7 @@ module SyntaxSuggest code_lines = CodeLine.from_source(source) block = CodeBlock.new(lines: code_lines[1]) expand = AroundBlockScan.new(code_lines: code_lines, block: block) - .scan_neighbors + .scan_neighbors_not_empty expect(expand.code_block.to_s).to eq(source) expand.scan_while { |line| false } @@ -104,8 +104,8 @@ module SyntaxSuggest expand = AroundBlockScan.new(code_lines: code_lines, block: block) expand.scan_while { true } - expect(expand.before_index).to eq(0) - expect(expand.after_index).to eq(6) + expect(expand.lines.first.index).to eq(0) + expect(expand.lines.last.index).to eq(6) expect(expand.code_block.to_s).to eq(source_string) end @@ -149,9 +149,9 @@ module SyntaxSuggest block = CodeBlock.new(lines: code_lines[3]) expand = AroundBlockScan.new(code_lines: code_lines, block: block) - expand.skip(:empty?) - expand.skip(:hidden?) - expand.scan_neighbors + expand.force_add_empty + expand.force_add_hidden + expand.scan_neighbors_not_empty expect(expand.code_block.to_s).to eq(<<~EOM.indent(4)) diff --git a/spec/syntax_suggest/unit/block_expand_spec.rb b/spec/syntax_suggest/unit/block_expand_spec.rb index ba0b0457a1..5cff73621d 100644 --- a/spec/syntax_suggest/unit/block_expand_spec.rb +++ b/spec/syntax_suggest/unit/block_expand_spec.rb @@ -4,6 +4,36 @@ require_relative "../spec_helper" module SyntaxSuggest RSpec.describe BlockExpand do + it "empty line in methods" do + source_string = <<~EOM + class Dog # index 0 + def bark # index 1 + + end # index 3 + + def sit # index 5 + print "sit" # index 6 + end # index 7 + end # index 8 + end # extra end + EOM + + code_lines = code_line_array(source_string) + + sit = code_lines[4..7] + sit.each(&:mark_invisible) + + block = CodeBlock.new(lines: sit) + expansion = BlockExpand.new(code_lines: code_lines) + block = expansion.expand_neighbors(block) + + expect(block.to_s).to eq(<<~EOM.indent(2)) + def bark # index 1 + + end # index 3 + EOM + end + it "captures multiple empty and hidden lines" do source_string = <<~EOM def foo diff --git a/spec/syntax_suggest/unit/capture/before_after_keyword_ends_spec.rb b/spec/syntax_suggest/unit/capture/before_after_keyword_ends_spec.rb new file mode 100644 index 0000000000..02d9be4387 --- /dev/null +++ b/spec/syntax_suggest/unit/capture/before_after_keyword_ends_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require_relative "../../spec_helper" + +module SyntaxSuggest + RSpec.describe Capture::BeforeAfterKeywordEnds do + it "before after keyword ends" do + source = <<~'EOM' + def nope + print 'not me' + end + + def lol + print 'lol' + end + + def hello # 8 + + def yolo + print 'haha' + end + + def nada + print 'nope' + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[8]) + + expect(block.to_s).to include("def hello") + + lines = Capture::BeforeAfterKeywordEnds.new( + block: block, + code_lines: code_lines + ).call + lines.sort! + + expect(lines.join).to include(<<~'EOM') + def lol + end + def yolo + end + EOM + end + end +end diff --git a/spec/syntax_suggest/unit/capture/falling_indent_lines_spec.rb b/spec/syntax_suggest/unit/capture/falling_indent_lines_spec.rb new file mode 100644 index 0000000000..61d1642d97 --- /dev/null +++ b/spec/syntax_suggest/unit/capture/falling_indent_lines_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require_relative "../../spec_helper" + +module SyntaxSuggest + RSpec.describe Capture::FallingIndentLines do + it "on_falling_indent" do + source = <<~'EOM' + class OH + def lol + print 'lol + end + + def hello + it "foo" do + end + + def yolo + print 'haha' + end + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[6]) + + lines = [] + Capture::FallingIndentLines.new( + block: block, + code_lines: code_lines + ).call do |line| + lines << line + end + lines.sort! + + expect(lines.join).to eq(<<~'EOM') + class OH + def hello + end + end + EOM + end + end +end diff --git a/spec/syntax_suggest/unit/capture_code_context_spec.rb b/spec/syntax_suggest/unit/capture_code_context_spec.rb index e1bc281c13..46f13e8961 100644 --- a/spec/syntax_suggest/unit/capture_code_context_spec.rb +++ b/spec/syntax_suggest/unit/capture_code_context_spec.rb @@ -4,6 +4,32 @@ require_relative "../spec_helper" module SyntaxSuggest RSpec.describe CaptureCodeContext do + it "capture_before_after_kws two" do + source = <<~'EOM' + class OH + + def hello + + def hai + end + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[2]) + + display = CaptureCodeContext.new( + blocks: [block], + code_lines: code_lines + ) + display.capture_before_after_kws(block) + expect(display.sorted_lines.join).to eq(<<~'EOM'.indent(2)) + def hello + def hai + end + EOM + end + it "capture_before_after_kws" do source = <<~'EOM' def sit @@ -16,13 +42,14 @@ module SyntaxSuggest EOM code_lines = CleanDocument.new(source: source).call.lines - block = CodeBlock.new(lines: code_lines[0]) + block = CodeBlock.new(lines: code_lines[3]) display = CaptureCodeContext.new( blocks: [block], code_lines: code_lines ) - lines = display.call + + lines = display.capture_before_after_kws(block).sort expect(lines.join).to eq(<<~'EOM') def sit end diff --git a/spec/syntax_suggest/unit/clean_document_spec.rb b/spec/syntax_suggest/unit/clean_document_spec.rb index 4fb79efd62..25a62e4454 100644 --- a/spec/syntax_suggest/unit/clean_document_spec.rb +++ b/spec/syntax_suggest/unit/clean_document_spec.rb @@ -72,6 +72,24 @@ module SyntaxSuggest EOM end + it "joins multi-line chained methods when separated by comments" do + source = <<~EOM + User. + # comment + where(name: 'schneems'). + # another comment + first + EOM + + doc = CleanDocument.new(source: source).join_consecutive! + code_lines = doc.lines + + expect(code_lines[0].to_s.count($/)).to eq(5) + code_lines[1..-1].each do |line| + expect(line.to_s.strip.length).to eq(0) + end + end + it "helper method: take_while_including" do source = <<~EOM User @@ -92,27 +110,10 @@ module SyntaxSuggest # yolo EOM - out = CleanDocument.new(source: source).lines.join - expect(out.to_s).to eq(<<~EOM) - - puts "what" - - EOM - end - - it "whitespace: removes whitespace" do - source = " \n" + <<~EOM - puts "what" - EOM - - out = CleanDocument.new(source: source).lines.join - expect(out.to_s).to eq(<<~EOM) - - puts "what" - EOM - - expect(source.lines.first.to_s).to_not eq("\n") - expect(out.lines.first.to_s).to eq("\n") + lines = CleanDocument.new(source: source).lines + expect(lines[0].to_s).to eq($/) + expect(lines[1].to_s).to eq('puts "what"' + $/) + expect(lines[2].to_s).to eq($/) end it "trailing slash: does not join trailing do" do diff --git a/spec/syntax_suggest/unit/code_line_spec.rb b/spec/syntax_suggest/unit/code_line_spec.rb index cc4fa48bc9..d5b568fd19 100644 --- a/spec/syntax_suggest/unit/code_line_spec.rb +++ b/spec/syntax_suggest/unit/code_line_spec.rb @@ -48,6 +48,7 @@ module SyntaxSuggest # Indicates line 1 can join 2, 2 can join 3, but 3 won't join it's next line expect(code_lines.map(&:ignore_newline_not_beg?)).to eq([true, true, false, false]) end + it "trailing if" do code_lines = CodeLine.from_source(<<~'EOM') puts "lol" if foo diff --git a/spec/syntax_suggest/unit/core_ext_spec.rb b/spec/syntax_suggest/unit/core_ext_spec.rb new file mode 100644 index 0000000000..802d03ecc0 --- /dev/null +++ b/spec/syntax_suggest/unit/core_ext_spec.rb @@ -0,0 +1,34 @@ +require_relative "../spec_helper" + +module SyntaxSuggest + RSpec.describe "Core extension" do + it "SyntaxError monkepatch ensures there is a newline to the end of the file" do + skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2") + + Dir.mktmpdir do |dir| + tmpdir = Pathname(dir) + file = tmpdir.join("file.rb") + file.write(<<~'EOM'.strip) + print 'no newline + EOM + + core_ext_file = lib_dir.join("syntax_suggest").join("core_ext") + require_relative core_ext_file + + original_message = "blerg" + error = SyntaxError.new(original_message) + def error.set_tmp_path_for_testing=(path) + @tmp_path_for_testing = path + end + error.set_tmp_path_for_testing = file + def error.path + @tmp_path_for_testing + end + + detailed = error.detailed_message(highlight: false, syntax_suggest: true) + expect(detailed).to include("'no newline\n#{original_message}") + expect(detailed).to_not include("print 'no newline#{original_message}") + end + end + end +end diff --git a/spec/syntax_suggest/unit/display_invalid_blocks_spec.rb b/spec/syntax_suggest/unit/display_invalid_blocks_spec.rb index 2c59d9dc78..b11d7d242e 100644 --- a/spec/syntax_suggest/unit/display_invalid_blocks_spec.rb +++ b/spec/syntax_suggest/unit/display_invalid_blocks_spec.rb @@ -144,6 +144,7 @@ module SyntaxSuggest expect(io.string).to include([ " 1 class OH", "> 2 def hello", + " 3 def hai", " 4 end", " 5 end", "" @@ -162,6 +163,7 @@ module SyntaxSuggest [ " 1 class OH", ["> 2 ", DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT, " def hello"].join, + " 3 def hai", " 4 end", " 5 end", "" diff --git a/spec/syntax_suggest/unit/scan_history_spec.rb b/spec/syntax_suggest/unit/scan_history_spec.rb new file mode 100644 index 0000000000..0e75ac66ce --- /dev/null +++ b/spec/syntax_suggest/unit/scan_history_spec.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +require_relative "../spec_helper" + +module SyntaxSuggest + RSpec.describe ScanHistory do + it "retains commits" do + source = <<~'EOM' + class OH # 0 + def lol # 1 + print 'lol # 2 + end # 3 + + def hello # 5 + it "foo" do # 6 + end # 7 + + def yolo # 8 + print 'haha' # 9 + end # 10 + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[6]) + + scanner = ScanHistory.new(code_lines: code_lines, block: block) + scanner.scan(up: ->(_, _, _) { true }, down: ->(_, _, _) { true }) + + expect(scanner.changed?).to be_truthy + scanner.commit_if_changed + expect(scanner.changed?).to be_falsey + + expect(scanner.lines).to eq(code_lines) + + scanner.stash_changes # Assert does nothing if changes are already committed + expect(scanner.lines).to eq(code_lines) + + scanner.revert_last_commit + + expect(scanner.lines.join).to eq(code_lines[6].to_s) + end + + it "is stashable" do + source = <<~'EOM' + class OH # 0 + def lol # 1 + print 'lol # 2 + end # 3 + + def hello # 5 + it "foo" do # 6 + end # 7 + + def yolo # 8 + print 'haha' # 9 + end # 10 + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[6]) + + scanner = ScanHistory.new(code_lines: code_lines, block: block) + scanner.scan(up: ->(_, _, _) { true }, down: ->(_, _, _) { true }) + + expect(scanner.lines).to eq(code_lines) + expect(scanner.changed?).to be_truthy + expect(scanner.next_up).to be_falsey + expect(scanner.next_down).to be_falsey + + scanner.stash_changes + + expect(scanner.changed?).to be_falsey + + expect(scanner.next_up).to eq(code_lines[5]) + expect(scanner.lines.join).to eq(code_lines[6].to_s) + expect(scanner.next_down).to eq(code_lines[7]) + end + + it "doesnt change if you dont't change it" do + source = <<~'EOM' + class OH # 0 + def lol # 1 + print 'lol # 2 + end # 3 + + def hello # 5 + it "foo" do # 6 + end # 7 + + def yolo # 8 + print 'haha' # 9 + end # 10 + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[6]) + + scanner = ScanHistory.new(code_lines: code_lines, block: block) + + lines = scanner.lines + expect(scanner.changed?).to be_falsey + expect(scanner.next_up).to eq(code_lines[5]) + expect(scanner.next_down).to eq(code_lines[7]) + + expect(scanner.stash_changes.lines).to eq(lines) + expect(scanner.revert_last_commit.lines).to eq(lines) + + expect(scanner.scan(up: ->(_, _, _) { false }, down: ->(_, _, _) { false }).lines).to eq(lines) + end + end +end -- cgit v1.2.3