Skip to content

Commit

Permalink
Merge pull request #2 from felipeog/exercism-021
Browse files Browse the repository at this point in the history
add exercism 021
  • Loading branch information
felipeog authored Sep 9, 2024
2 parents bbfe150 + 2370d96 commit abb9470
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
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, "*", ""))
}
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)
}
})
}
}

0 comments on commit abb9470

Please sign in to comment.