diff --git a/game/errors.go b/game/errors.go new file mode 100644 index 0000000..d83df35 --- /dev/null +++ b/game/errors.go @@ -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") diff --git a/game/game.go b/game/game.go index b1edb3d..b48c53f 100644 --- a/game/game.go +++ b/game/game.go @@ -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 @@ -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) diff --git a/game/game_test.go b/game/game_test.go index 0e58bf9..7d093fe 100644 --- a/game/game_test.go +++ b/game/game_test.go @@ -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"}, @@ -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) + } } } } diff --git a/go.mod b/go.mod index c236232..4b58ae6 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 4659b57..28639fe 100644 --- a/go.sum +++ b/go.sum @@ -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=