Skip to content

Commit

Permalink
Handling word-length errors in the game class.
Browse files Browse the repository at this point in the history
  • Loading branch information
archy-bold committed Jan 6, 2022
1 parent 1db5348 commit b621fe6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
6 changes: 6 additions & 0 deletions game/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package game

import "errors"

// ErrWrongWordLength error to represent when a word of the incorrect length is entered
var ErrWrongWordLength = errors.New("The entered word length is wrong, should be")
7 changes: 6 additions & 1 deletion game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package game
import (
"fmt"
"strings"

"github.com/pkg/errors"
)

// CellStatus represents whether a cell is in a state of empty, correct, incorrect or wrong
Expand Down Expand Up @@ -48,7 +50,10 @@ type game struct {
}

func (g *game) Play(word string) (bool, error) {
// TODO check the word length here and error if too long/short
// Check the word length here and error if too long/short
if len(word) != len(g.answer) {
return false, errors.Wrap(ErrWrongWordLength, fmt.Sprint(len(word)))
}

// Create the row for the grid
word = strings.ToLower(word)
Expand Down
26 changes: 21 additions & 5 deletions game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ var gamePlayTests = map[string]struct {
expected: []bool{false, false, false, true},
expectedGrid: gameTapirFinished.grid,
},
"5-letter, try 4-letter word": {
g: gameTapirStart,
tries: []string{"tape"},
expectedErr: "The entered word length is wrong, should be: 5",
},
"5-letter, try 6-letter word": {
g: gameTapirStart,
tries: []string{"strong"},
expectedErr: "The entered word length is wrong, should be: 5",
},
"2-letter, lost": {
g: gameAtStart,
tries: []string{"ta"},
Expand All @@ -75,13 +85,19 @@ func Test_game_Play(t *testing.T) {
copy(g.grid[i], row)
}
for i, word := range tt.tries {
res, _ := g.Play(word)
res, err := g.Play(word)

// Make the assertions
assert.Equalf(t, tt.expected[i], res, "Expected play outcome to match for test '%s'", tn)
assert.Equalf(t, tt.expected[i], g.complete, "Expected complete to match for test '%s'", tn)
assert.Equalf(t, tt.expectedGrid[i], g.grid[i], "Expected grid row to match for test '%s'", tn)
assert.Equalf(t, i+1, g.attempts, "Expected attempts to match for test '%s'", tn)
if tt.expectedErr != "" {
assert.Falsef(t, res, "Expected res false for test '%s'", tn)
assert.Errorf(t, err, "Expected error to match for test '%s'", tn)
} else {
assert.NoErrorf(t, err, "Expected nil error for test '%s'", tn)
assert.Equalf(t, tt.expected[i], res, "Expected play outcome to match for test '%s'", tn)
assert.Equalf(t, tt.expected[i], g.complete, "Expected complete to match for test '%s'", tn)
assert.Equalf(t, tt.expectedGrid[i], g.grid[i], "Expected grid row to match for test '%s'", tn)
assert.Equalf(t, i+1, g.attempts, "Expected attempts to match for test '%s'", tn)
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/archy-bold/wordle-go

go 1.12

require github.com/stretchr/testify v1.6.1
require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down

0 comments on commit b621fe6

Please sign in to comment.