Skip to content

Commit

Permalink
Merge pull request #4 from felipeog/exercism-003
Browse files Browse the repository at this point in the history
add exercism 003
  • Loading branch information
felipeog authored Sep 9, 2024
2 parents 4426bf1 + c0e17a5 commit 3899cf2
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
22 changes: 22 additions & 0 deletions practice/exercism/003-party-robot/party_robot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package partyrobot

import "fmt"

// Welcome greets a person by name.
func Welcome(name string) string {
return fmt.Sprintf("Welcome to my party, %s!", name)
}

// HappyBirthday wishes happy birthday to the birthday person and exclaims their age.
func HappyBirthday(name string, age int) string {
return fmt.Sprintf("Happy birthday %s! You are now %d years old!", name, age)
}

// AssignTable assigns a table to each guest.
func AssignTable(name string, table int, neighbor, direction string, distance float64) string {
line1 := fmt.Sprintf("Welcome to my party, %s!", name)
line2 := fmt.Sprintf("You have been assigned to table %03d. Your table is %s, exactly %.1f meters from here.", table, direction, distance)
line3 := fmt.Sprintf("You will be sitting next to %s.", neighbor)

return line1 + "\n" + line2 + "\n" + line3
}
133 changes: 133 additions & 0 deletions practice/exercism/003-party-robot/party_robot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package partyrobot

import (
"strings"
"testing"
)

func TestWelcome(t *testing.T) {
tests := []struct {
description string
name string
want string
}{
{
description: "Greet Chihiro with a welcoming message",
name: "Chihiro",
want: "Welcome to my party, Chihiro!",
},
{
description: "Greet Xuân Jing with a welcoming message",
name: "Xuân Jing",
want: "Welcome to my party, Xuân Jing!",
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
if got := Welcome(tt.name); got != tt.want {
t.Errorf("Welcome(%s) = %s, want %s", tt.name, got, tt.want)
}
})
}
}

func TestHappyBirthday(t *testing.T) {
tests := []struct {
description string
name string
age int
want string
}{
{
description: "Wish Chihiro Happy Birthday with name and age",
name: "Chihiro",
age: 61,
want: "Happy birthday Chihiro! You are now 61 years old!",
},
{
description: "Wish Xuân Jing Happy Birthday with name and age",
name: "Xuân Jing",
age: 17,
want: "Happy birthday Xuân Jing! You are now 17 years old!",
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
if got := HappyBirthday(tt.name, tt.age); got != tt.want {
t.Errorf("HappyBirthday(%s, %d) = %s, want %s", tt.name, tt.age, got, tt.want)
}
})
}
}

func TestAssignTable(t *testing.T) {
tests := []struct {
description string
name string
direction string
tableNumber int
distance float64
seatmate string
want string
}{
{
description: "Greet Chihiro and give them directions to their seat",
name: "Chihiro",
direction: "straight ahead",
tableNumber: 22,
distance: 9.2394381,
seatmate: "Akachi Chikondi",
want: "Welcome to my party, Chihiro!\nYou have been assigned to table 022. Your table is straight ahead, exactly 9.2 meters from here.\nYou will be sitting next to Akachi Chikondi.",
},
{
description: "Greet Xuân Jing and give them directions to their seat",
name: "Xuân Jing",
direction: "by the façade",
tableNumber: 4,
distance: 23.470103,
seatmate: "Renée",
want: "Welcome to my party, Xuân Jing!\nYou have been assigned to table 004. Your table is by the façade, exactly 23.5 meters from here.\nYou will be sitting next to Renée.",
},
{
description: "Greet Paula and give them directions to their seat",
name: "Paula",
direction: "on the right",
tableNumber: 101,
distance: 100.0,
seatmate: "Chioma",
want: "Welcome to my party, Paula!\nYou have been assigned to table 101. Your table is on the right, exactly 100.0 meters from here.\nYou will be sitting next to Chioma.",
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
if got := AssignTable(tt.name, tt.tableNumber, tt.seatmate, tt.direction, tt.distance); got != tt.want {
t.Errorf(`AssignTable(%s,%d,%s,%s,%f)
got:
%q
want:
%q`,
tt.name, tt.tableNumber, tt.seatmate, tt.direction, tt.distance, got, tt.want)

wantLen := len(tt.want)
gotLen := len(got)
wantNewlines := strings.Count(tt.want, "\n")
gotNewlines := strings.Count(got, "\n")
wantSpaces := strings.Count(tt.want, " ")
gotSpaces := strings.Count(got, " ")

if wantLen != gotLen {
t.Errorf("String lengths do not match - got: %d, want: %d", gotLen, wantLen)
}

if wantNewlines != gotNewlines {
t.Errorf("Check your newline characters - got: %d, want: %d", gotNewlines, wantNewlines)
}

if wantSpaces != gotSpaces {
t.Errorf("Check your space characters - got: %d, want: %d", gotSpaces, wantSpaces)
}

}
})
}
}

0 comments on commit 3899cf2

Please sign in to comment.