add tests for regex

This commit is contained in:
Petr Korolev 2015-05-25 14:21:23 +03:00
parent 757f6d40b4
commit 171e536e76
3 changed files with 35 additions and 12 deletions

View File

@ -12,7 +12,7 @@ Metrics/AbcSize:
# Offense count: 4 # Offense count: 4
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/ClassLength: Metrics/ClassLength:
Max: 166 Enabled: false
# Offense count: 3 # Offense count: 3
Metrics/CyclomaticComplexity: Metrics/CyclomaticComplexity:

View File

@ -133,7 +133,7 @@ module GitHubChangelogGenerator
end end
def self.detect_user_and_project(options) def self.detect_user_and_project(options)
user_project_from_option(options) options[:user], options[:project] = user_project_from_option(ARGV[0], ARGV[1], options[:github_site])
if !options[:user] || !options[:project] if !options[:user] || !options[:project]
if ENV["RUBYLIB"] =~ /ruby-debug-ide/ if ENV["RUBYLIB"] =~ /ruby-debug-ide/
options[:user] = "skywinder" options[:user] = "skywinder"
@ -145,31 +145,38 @@ module GitHubChangelogGenerator
end end
end end
def self.user_project_from_option(options) # Try to find user and project name from git remote output
if ARGV[0] && !ARGV[1] #
github_site = options[:github_site] ? options[:github_site] : "github.com" # @param [String] output of git remote command
# @return [Array] user and project
def self.user_project_from_option(arg0, arg2, github_site = "github.com")
user = nil
project = nil
if arg0 && !arg2
# this match should parse strings such "https://github.com/skywinder/Github-Changelog-Generator" or "skywinder/Github-Changelog-Generator" to user and name # this match should parse strings such "https://github.com/skywinder/Github-Changelog-Generator" or "skywinder/Github-Changelog-Generator" to user and name
match = /(?:.+#{Regexp.escape(github_site)}\/)?(.+)\/(.+)/.match(ARGV[0]) match = /(?:.+#{Regexp.escape(github_site)}\/)?(.+)\/(.+)/.match(arg0)
begin begin
param = match[2].nil? param = match[2].nil?
rescue rescue
puts "Can't detect user and name from first parameter: '#{ARGV[0]}' -> exit'" puts "Can't detect user and name from first parameter: '#{arg0}' -> exit'"
exit exit
end end
if param if param
exit exit
else else
options[:user] = match[1] user = match[1]
options[:project] = match[2] project = match[2]
end end
end end
[user, project]
end end
# Try to find user and project name from git remote output # Try to find user and project name from git remote output
# #
# @param [String] output of git remote command # @param [String] output of git remote command
# @return [Arrajy] user and project # @return [Array] user and project
def self.user_project_from_remote(remote) def self.user_project_from_remote(remote)
# try to find repo in format: # try to find repo in format:
# origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch) # origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch)
@ -202,7 +209,7 @@ module GitHubChangelogGenerator
end end
if __FILE__ == $PROGRAM_NAME if __FILE__ == $PROGRAM_NAME
remote = "origin https://github.com/skywinder/Changelog.Merger (fetch)" remote = "invalid reference to project"
p GitHubChangelogGenerator::Parser.user_project_from_remote(remote)[0] p user_project_from_option(ARGV[0], ARGV[1], remote)
end end
end end

View File

@ -26,4 +26,20 @@ describe GitHubChangelogGenerator::Parser do
it { is_expected.to match_array([nil, nil]) } it { is_expected.to match_array([nil, nil]) }
end end
end end
describe "#self.user_project_from_option" do
# context "when option is invalid" do
# it("should exit") { expect { GitHubChangelogGenerator::Parser.user_project_from_option("blah", nil) }.to raise_error(SystemExit) }
# end
context "when option is valid" do
subject { GitHubChangelogGenerator::Parser.user_project_from_option("skywinder/ActionSheetPicker-3.0", nil) }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array(["skywinder", "ActionSheetPicker-3.0"]) }
end
context "when option nil" do
subject { GitHubChangelogGenerator::Parser.user_project_from_option(nil, nil) }
it { is_expected.to be_a(Array) }
it { is_expected.to match_array([nil, nil]) }
end
end
end end