wordle/game/words/words_test.go

109 lines
2.3 KiB
Go

package words
import (
"reflect"
"testing"
)
func TestCheck(t *testing.T) {
type args struct {
word string
solution string
}
tests := []struct {
name string
args args
wantCorrect []int
wantPresent []int
}{
{
name: "incorrect",
args: args{word: "grass", solution: "hello"},
wantCorrect: []int{},
wantPresent: []int{},
},
{
name: "incorrect position",
args: args{word: "crash", solution: "hello"},
wantCorrect: []int{},
wantPresent: []int{4},
},
{
name: "correct position",
args: args{word: "hrass", solution: "hello"},
wantCorrect: []int{0},
wantPresent: []int{},
},
{
name: "incorrect position double letter",
// here there is a double letter in the word, only one being in the solution
args: args{word: "green", solution: "hello"},
wantCorrect: []int{},
wantPresent: []int{2},
},
{
name: "partial double letter",
// here there is a double letter in the word, with one being in the correct place
args: args{word: "hllxy", solution: "hello"},
wantCorrect: []int{0, 2},
wantPresent: []int{1},
},
{
name: "correct",
args: args{word: "hello", solution: "hello"},
wantCorrect: []int{0, 1, 2, 3, 4},
wantPresent: []int{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCorrect, gotPresent := Check(tt.args.word, tt.args.solution)
if !reflect.DeepEqual(gotCorrect, tt.wantCorrect) {
t.Errorf("Check() gotCorrect = %v, want %v", gotCorrect, tt.wantCorrect)
}
if !reflect.DeepEqual(gotPresent, tt.wantPresent) {
t.Errorf("Check() gotPresent = %v, want %v", gotPresent, tt.wantPresent)
}
})
}
}
func TestIsValid(t *testing.T) {
type args struct {
word string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "valid",
args: args{word: "hello"},
want: true,
},
{
name: "too short",
args: args{word: "egg"},
want: false,
},
{
name: "too long",
args: args{word: "jellyfish"},
want: false,
},
{
name: "not a word",
args: args{word: "aaaaa"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValid(tt.args.word); got != tt.want {
t.Errorf("IsValid() = %v, want %v", got, tt.want)
}
})
}
}