From c0e17a5f654bda7d7f95d0bd46468196a61ea85e Mon Sep 17 00:00:00 2001 From: felipeog <17603069+felipeog@users.noreply.github.com> Date: Mon, 9 Sep 2024 20:54:53 -0300 Subject: [PATCH] add exercism 003 --- .../exercism/003-party-robot/party_robot.go | 22 +++ .../003-party-robot/party_robot_test.go | 133 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 practice/exercism/003-party-robot/party_robot.go create mode 100644 practice/exercism/003-party-robot/party_robot_test.go diff --git a/practice/exercism/003-party-robot/party_robot.go b/practice/exercism/003-party-robot/party_robot.go new file mode 100644 index 0000000..38aaf68 --- /dev/null +++ b/practice/exercism/003-party-robot/party_robot.go @@ -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 +} \ No newline at end of file diff --git a/practice/exercism/003-party-robot/party_robot_test.go b/practice/exercism/003-party-robot/party_robot_test.go new file mode 100644 index 0000000..f13322d --- /dev/null +++ b/practice/exercism/003-party-robot/party_robot_test.go @@ -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) + } + + } + }) + } +} \ No newline at end of file