-
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.
- Loading branch information
Showing
7 changed files
with
102 additions
and
12 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
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'yaml' | ||
|
||
module TestMap | ||
# Natural mapping determines the test file for a given source file by | ||
# applying common and configurable rules and transformation. | ||
class NaturalMapping | ||
CommonRule = Struct.new(:file) do | ||
def self.call(file) = new(file).call | ||
|
||
def call | ||
if File.exist?('test') | ||
transform('test') | ||
elsif File.exist?('spec') | ||
transform('spec') | ||
end | ||
end | ||
|
||
def transform(type) | ||
test_file = "#{File.basename(file, '.rb')}_#{type}.rb" | ||
test_path = File.dirname(file).sub('app/', '') | ||
test_path = nil if test_path == '.' | ||
[type, test_path, test_file].compact.join('/') | ||
end | ||
end | ||
|
||
attr_reader :file | ||
|
||
def initialize(file) = @file = file | ||
def test_files = Array(transform(file)) | ||
|
||
def transform(file) | ||
self.class.registered_rules.each do |rule| | ||
test_files = rule.call(file) | ||
|
||
return test_files if test_files | ||
end | ||
|
||
nil | ||
end | ||
|
||
def self.registered_rules | ||
@registered_rules ||= [ | ||
Config.config[:natural_mapping], | ||
CommonRule | ||
].compact | ||
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
# Tests for mapping. | ||
class NaturalMappingTest < Minitest::Test | ||
def test_mapping | ||
mapping = subject.new('lib/animal.rb') | ||
|
||
assert_equal ['test/lib/animal_test.rb'], mapping.test_files | ||
end | ||
|
||
def test_rails_mapping | ||
mapping = subject.new('app/model/animal.rb') | ||
|
||
assert_equal ['test/model/animal_test.rb'], mapping.test_files | ||
end | ||
|
||
def test_specs | ||
File.stub(:exist?, ->(type) { type == 'spec' }) do | ||
mapping = subject.new('app/model/animal.rb') | ||
|
||
assert_equal ['spec/model/animal_spec.rb'], mapping.test_files | ||
end | ||
end | ||
|
||
def test_no_known_test_lib | ||
File.stub(:exist?, ->(_) { false }) do | ||
mapping = subject.new('lib/animal.rb') | ||
puts mapping.test_files | ||
|
||
assert_empty mapping.test_files | ||
end | ||
end | ||
|
||
private | ||
|
||
def subject = TestMap::NaturalMapping | ||
end |