generated from mattbrictson/gem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rudimentary regexp-based run by line number (#3)
* Refactor to add a fixtures_path test helper * Add rudimentary regexp-based run by line number * Small regexp tweaks
- Loading branch information
1 parent
ae0de3c
commit 9421d21
Showing
8 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module MightyTest | ||
class TestParser | ||
def initialize(test_path) | ||
@path = test_path.to_s | ||
end | ||
|
||
def test_name_at_line(number) | ||
method_name = nil | ||
lines = File.read(path).lines | ||
lines[2...number].reverse_each.find do |line| | ||
method_name = | ||
match_minitest_method_name(line) || | ||
match_active_support_test_string(line)&.then { "test_#{_1.gsub(/\s+/, '_')}" } | ||
end | ||
method_name | ||
end | ||
|
||
private | ||
|
||
attr_reader :path | ||
|
||
def match_minitest_method_name(line) | ||
line[/^\s*(?:focus\s+)?def\s+(test_\w+)/, 1] | ||
end | ||
|
||
def match_active_support_test_string(line) | ||
match = line.match(/^\s*test\s+(?:"(.+?)"|'(.+?)')\s*do\s*(?:#.*?)?$/) | ||
return unless match | ||
|
||
match.captures.compact.first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require "rails_helper" | ||
|
||
class ActiveSupportTest < ActiveSupport::TestCase | ||
test "it's important" do # Trailing comment OK | ||
assert true # rubocop:disable Minitest/UselessAssertion | ||
end | ||
|
||
test 'it does something "interesting"' do | ||
assert true # rubocop:disable Minitest/UselessAssertion | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "test_helper" | ||
|
||
module MightyTest | ||
class TestParserTest < Minitest::Test | ||
include FixturesPath | ||
|
||
def test_returns_nil_if_no_test_found | ||
name = test_name_at_line(fixtures_path.join("example_project/test/example_test.rb"), 3) | ||
assert_nil name | ||
end | ||
|
||
def test_finds_traditional_test_name_by_line_number | ||
name = test_name_at_line(fixtures_path.join("example_project/test/example_test.rb"), 5) | ||
assert_equal "test_that_it_has_a_version_number", name | ||
end | ||
|
||
def test_finds_traditional_test_name_by_line_number_even_with_focus | ||
name = test_name_at_line(fixtures_path.join("example_project/test/focused_test.rb"), 5) | ||
assert_equal "test_this_test_is_run", name | ||
end | ||
|
||
def test_finds_active_support_test_name_by_line_number | ||
name = test_name_at_line(fixtures_path.join("active_support_test.rb"), 5) | ||
assert_equal "test_it's_important", name | ||
end | ||
|
||
def test_finds_single_quoted_active_support_test_name_by_line_number | ||
name = test_name_at_line(fixtures_path.join("active_support_test.rb"), 9) | ||
assert_equal 'test_it_does_something_"interesting"', name | ||
end | ||
|
||
private | ||
|
||
def test_name_at_line(path, line_number) | ||
TestParser.new(path).test_name_at_line(line_number) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module FixturesPath | ||
private | ||
|
||
def fixtures_path | ||
Pathname.new(File.expand_path("../fixtures", __dir__)) | ||
end | ||
end |