Files
zombie/parse_test.go
T

169 lines
3.5 KiB
Go

package zombie
import (
"github.com/wahyd4/zombie/models"
"reflect"
"testing"
)
func TestInit(t *testing.T) {
type args struct {
input string
}
tests := []struct {
name string
args args
want *Application
wantErr bool
}{
{
"can parse the input data file and init an application",
args{input: "test_data/input_test.txt"},
&Application{
GridSize: 4,
Zombie: &models.Zombie{
GridSize: 4,
Coordinate: models.Coordinate{
XAxis: 2,
YAxis: 1,
},
},
Creatures: []*models.Creature{
&models.Creature{Coordinate: models.Coordinate{0, 1}},
&models.Creature{Coordinate: models.Coordinate{XAxis: 1, YAxis: 2}},
&models.Creature{Coordinate: models.Coordinate{XAxis: 3, YAxis: 1}},
},
Commands: []MoveCommand{
&MoveDownCommand{},
&MoveLeftCommand{},
&MoveUpCommand{},
&MoveUpCommand{},
&MoveRightCommand{},
&MoveRightCommand{},
},
},
false,
},
{
"fail to init application due to file is not found",
args{input: "test_data/not_exist.txt"},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Init(tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Init() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseCoordinate(t *testing.T) {
type args struct {
coordinate string
}
tests := []struct {
name string
args args
want models.Coordinate
}{
{
"can parse a valid coordinate string",
args{coordinate: "(2,3)"},
models.Coordinate{
XAxis: 2,
YAxis: 3,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseCoordinate(tt.args.coordinate); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseCoordinate() = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseCreatures(t *testing.T) {
type args struct {
coordinates string
}
tests := []struct {
name string
args args
want []*models.Creature
}{
{
"can parse all the creatures from string",
args{"(0,1)"},
[]*models.Creature{
&models.Creature{Coordinate: models.Coordinate{0, 1}},
},
},
{
"can parse all the creatures from string",
args{"(0,1) (1,2) (3,1)"},
[]*models.Creature{
&models.Creature{Coordinate: models.Coordinate{0, 1}},
&models.Creature{Coordinate: models.Coordinate{1, 2}},
&models.Creature{Coordinate: models.Coordinate{3, 1}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseCreatures(tt.args.coordinates); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseCreatures() = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseCommands(t *testing.T) {
type args struct {
commandString string
}
tests := []struct {
name string
args args
want []MoveCommand
}{
{
"can parse the commands string into commands",
args{"LRRUD"},
[]MoveCommand{
&MoveLeftCommand{},
&MoveRightCommand{},
&MoveRightCommand{},
&MoveUpCommand{},
&MoveDownCommand{},
},
},
{
"can parse the commands string into commands",
args{"RRUU"},
[]MoveCommand{
&MoveRightCommand{},
&MoveRightCommand{},
&MoveUpCommand{},
&MoveUpCommand{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseCommands(tt.args.commandString); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseCommands() = %v, want %v", got, tt.want)
}
})
}
}