-
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.
Merge pull request #2 from felipeog/exercism-021
add exercism 021
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
practice/exercism/021-welcome-to-tech-palace/welcome_to_tech_palace.go
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,20 @@ | ||
package techpalace | ||
|
||
import "strings" | ||
|
||
// WelcomeMessage returns a welcome message for the customer. | ||
func WelcomeMessage(customer string) string { | ||
return "Welcome to the Tech Palace, " + strings.ToUpper(customer) | ||
} | ||
|
||
// AddBorder adds a border to a welcome message. | ||
func AddBorder(welcomeMsg string, numStarsPerLine int) string { | ||
border := strings.Repeat("*", numStarsPerLine) | ||
|
||
return border + "\n" + welcomeMsg + "\n" + border | ||
} | ||
|
||
// CleanupMessage cleans up an old marketing message. | ||
func CleanupMessage(oldMsg string) string { | ||
return strings.TrimSpace(strings.ReplaceAll(oldMsg, "*", "")) | ||
} |
102 changes: 102 additions & 0 deletions
102
practice/exercism/021-welcome-to-tech-palace/welcome_to_tech_palace_test.go
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,102 @@ | ||
package techpalace | ||
|
||
import "testing" | ||
|
||
func TestWelcomeMessage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
customer string | ||
want string | ||
}{ | ||
{ | ||
name: "Welcome message for customer with first letter capitalized", | ||
customer: "Judy", | ||
want: "Welcome to the Tech Palace, JUDY", | ||
}, | ||
{ | ||
name: "Welcome message for customer with only lowercase letters", | ||
customer: "lars", | ||
want: "Welcome to the Tech Palace, LARS", | ||
}, | ||
{ | ||
name: "Welcome message for customer with dash in name", | ||
customer: "Peter-James", | ||
want: "Welcome to the Tech Palace, PETER-JAMES", | ||
}, | ||
{ | ||
name: "Welcome message for customer with only uppercase letters", | ||
customer: "MJ", | ||
want: "Welcome to the Tech Palace, MJ", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := WelcomeMessage(tt.customer); got != tt.want { | ||
t.Errorf("WelcomeMessage(\"%s\") = \"%s\", want \"%s\"", tt.customer, got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAddBorder(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
welcomeMessage string | ||
numStarsPerLine int | ||
want string | ||
}{ | ||
{ | ||
name: "Add border with 10 stars per line", | ||
welcomeMessage: "Welcome!", | ||
numStarsPerLine: 10, | ||
want: "**********\nWelcome!\n**********", | ||
}, | ||
{ | ||
name: "Add border with 2 stars per line", | ||
welcomeMessage: "Hi", | ||
numStarsPerLine: 2, | ||
want: "**\nHi\n**", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := AddBorder(tt.welcomeMessage, tt.numStarsPerLine); got != tt.want { | ||
t.Errorf("AddBorder(\"%s\", %d) = \"%s\", want \"%s\"", tt.welcomeMessage, tt.numStarsPerLine, got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCleanupMessage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
oldMessage string | ||
want string | ||
}{ | ||
{ | ||
name: "Cleanup message with many stars and leading and trailing whitespace", | ||
oldMessage: "**************************\n* BUY NOW, SAVE 10% *\n**************************", | ||
want: "BUY NOW, SAVE 10%", | ||
}, | ||
{ | ||
name: "Cleanup message without leading or trailing whitespace", | ||
oldMessage: "**********\n*DISCOUNT*\n**********", | ||
want: "DISCOUNT", | ||
}, | ||
{ | ||
name: "Cleanup message without leading whitespace", | ||
oldMessage: "*****\n SALE\n*****", | ||
want: "SALE", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := CleanupMessage(tt.oldMessage); got != tt.want { | ||
t.Errorf("CleanupMessage(\"%s\") = \"%s\", want \"%s\"", tt.oldMessage, got, tt.want) | ||
} | ||
}) | ||
} | ||
} |