Skip to content

Commit

Permalink
Extract RubyIndexer::TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Aug 16, 2023
1 parent 56cef3c commit 646d2c6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 75 deletions.
34 changes: 2 additions & 32 deletions lib/ruby_indexer/test/classes_and_modules_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# typed: true
# frozen_string_literal: true

require "test_helper"
require_relative "test_case"

module RubyIndexer
class ClassesAndModulesTest < Minitest::Test
def setup
@index = Index.new
end

class ClassesAndModulesTest < TestCase
def test_empty_statements_class
index(<<~RUBY)
class Foo
Expand Down Expand Up @@ -204,31 +200,5 @@ class Foo; end
second_foo_entry = @index["Foo"][1]
assert_equal("# This is another Foo comment\n", second_foo_entry.comments.join)
end

private

def index(source)
@index.index_single("/fake/path/foo.rb", source)
end

def assert_entry(expected_name, type, expected_location)
entries = @index[expected_name]
refute_empty(entries, "Expected #{expected_name} to be indexed")

entry = entries.first
assert_instance_of(type, entry, "Expected #{expected_name} to be a #{type}")

location = entry.location
location_string =
"#{entry.file_path}:#{location.start_line - 1}-#{location.start_column}" \
":#{location.end_line - 1}-#{location.end_column}"

assert_equal(expected_location, location_string)
end

def refute_entry(expected_name)
entries = @index[expected_name]
assert_nil(entries, "Expected #{expected_name} to not be indexed")
end
end
end
40 changes: 3 additions & 37 deletions lib/ruby_indexer/test/constant_test.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
# typed: true
# frozen_string_literal: true

require "test_helper"
require_relative "test_case"

module RubyIndexer
class ConstantTest < Minitest::Test
def setup
@index = Index.new
end

def teardown
@index.clear
end

class ConstantTest < TestCase
def test_constant_writes
index(<<~RUBY)
FOO = 1
Expand Down Expand Up @@ -110,33 +102,7 @@ def test_variable_path_constants_are_ignored
self.class::FOO = 1
RUBY

assert_empty(@index.entries)
end

private

def index(source)
RubyIndexer.index_single(@index, "/fake/path/foo.rb", source)
end

def assert_entry(expected_name, type, expected_location)
entries = @index[expected_name]
refute_empty(entries, "Expected #{expected_name} to be indexed")

entry = entries.first
assert_instance_of(type, entry, "Expected #{expected_name} to be a #{type}")

location = entry.location
location_string =
"#{entry.file_path}:#{location.start_line - 1}-#{location.start_column}" \
":#{location.end_line - 1}-#{location.end_column}"

assert_equal(expected_location, location_string)
end

def refute_entry(expected_name)
entries = @index[expected_name]
assert_nil(entries, "Expected #{expected_name} to not be indexed")
assert_no_entry
end
end
end
8 changes: 2 additions & 6 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# typed: true
# frozen_string_literal: true

require "test_helper"
require_relative "test_case"

module RubyIndexer
class IndexTest < Minitest::Test
def setup
@index = Index.new
end

class IndexTest < TestCase
def test_deleting_one_entry_for_a_class
@index.index_single("/fake/path/foo.rb", <<~RUBY)
class Foo
Expand Down
42 changes: 42 additions & 0 deletions lib/ruby_indexer/test/test_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# typed: true
# frozen_string_literal: true

require "test_helper"

module RubyIndexer
class TestCase < Minitest::Test
def setup
@index = Index.new
end

private

def index(source)
@index.index_single("/fake/path/foo.rb", source)
end

def assert_entry(expected_name, type, expected_location)
entries = @index[expected_name]
refute_empty(entries, "Expected #{expected_name} to be indexed")

entry = entries.first
assert_instance_of(type, entry, "Expected #{expected_name} to be a #{type}")

location = entry.location
location_string =
"#{entry.file_path}:#{location.start_line - 1}-#{location.start_column}" \
":#{location.end_line - 1}-#{location.end_column}"

assert_equal(expected_location, location_string)
end

def refute_entry(expected_name)
entries = @index[expected_name]
assert_nil(entries, "Expected #{expected_name} to not be indexed")
end

def assert_no_entry
assert_empty(@index.instance_variable_get(:@entries), "Expected nothing to be indexed")
end
end
end

0 comments on commit 646d2c6

Please sign in to comment.