Created
May 27, 2021 11:53
-
-
Save hibri/44be0e9dedc53433f6b151b1617a0636 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"strings" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
//https://github.com/hibri/code-katas/blob/main/katas/tennis-game.md | |
func TestAlwaysTrue(t *testing.T) { | |
assert.True(t, true) | |
} | |
func TestPlayer1Scores(t *testing.T){ | |
expectedScoreForPlayer1 := 15 | |
expectedScoreForPlayer2 := 0 | |
// H M | |
// H M | |
// M H | |
// M H | |
actualScoreForPlayer1, actualScoreForPlayer2 := getScores("HM") | |
assert.Equal(t, expectedScoreForPlayer1, actualScoreForPlayer1) | |
assert.Equal(t, expectedScoreForPlayer2, actualScoreForPlayer2) | |
} | |
func TestPlayer1ScoresContinuouslyTwice(t *testing.T){ | |
expectedScoreForPlayer1 := 30 | |
expectedScoreForPlayer2 := 0 | |
actualScoreForPlayer1, actualScoreForPlayer2 := getScores("HM,HM") | |
assert.Equal(t, expectedScoreForPlayer1, actualScoreForPlayer1) | |
assert.Equal(t, expectedScoreForPlayer2, actualScoreForPlayer2) | |
} | |
func TestPlayer1ScoresContinuouslyThrice(t *testing.T){ | |
expectedScoreForPlayer1 := 40 | |
expectedScoreForPlayer2 := 0 | |
actualScoreForPlayer1, actualScoreForPlayer2 := getScores("HM,HM,HM") | |
assert.Equal(t, expectedScoreForPlayer1, actualScoreForPlayer1) | |
assert.Equal(t, expectedScoreForPlayer2, actualScoreForPlayer2) | |
} | |
func getScores(scoringEvents string) (score1 int, score2 int) { | |
if(scoringEvents == "HM"){ | |
return 15,0 | |
} | |
var a1 int | |
var a2 int | |
events := strings.Split(scoringEvents, ",") | |
for x := 0; x < len(events); x++ { | |
s1, s2 := getScores(events[x]) | |
a1 += s1 | |
if (a1 == 45) { | |
a1 = 40 | |
} | |
a2 += s2 | |
} | |
return a1,a2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment