Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
init <name>
to better validate <name> (#250)
- Loading branch information
1 parent
eba5013
commit 2b3d174
Showing
4 changed files
with
90 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "../../spec_helper" | ||
|
||
private def validate(string) | ||
LuckyCli::Generators::Validators::ProjectName.valid? string | ||
end | ||
|
||
private def sanitize(string) | ||
LuckyCli::Generators::Validators::ProjectName.sanitize string | ||
end | ||
|
||
describe LuckyCli::Generators::Validators::ProjectName do | ||
hyphenate = "sector-seven" | ||
special_characters = "this string is emphatic" | ||
title = "From the Earth to the Moon" | ||
|
||
it "doesn't allow special characters" do | ||
validate(special_characters).should be_false | ||
end | ||
|
||
it "corrects special characters" do | ||
expected = "this_string_is_emphatic" | ||
sanitize(special_characters).should eq expected | ||
end | ||
|
||
it "allows hyphens" do | ||
validate(hyphenate).should be_true | ||
end | ||
|
||
it "doesn't allow uppercase characters" do | ||
validate(title).should be_false | ||
end | ||
|
||
it "corrects uppercase characters" do | ||
expected = "from_the_earth_to_the_moon" | ||
sanitize(title).should eq expected | ||
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 @@ | ||
module LuckyCli::Generators::Validators::ProjectName | ||
extend self | ||
|
||
def sanitize(name) | ||
name.downcase.gsub(/[^a-z0-9_-]/, "_").strip('_') | ||
end | ||
|
||
def valid?(name) | ||
sanitize(name) == name | ||
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