Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

bot_test.gno

1.52 Kb · 56 lines
 1package bot
 2
 3import "testing"
 4
 5func TestApplyStaysOnBoard(t *testing.T) {
 6	corner := Pos{0, 0}
 7	if got := Apply(corner, Up); got != corner {
 8		t.Errorf("Up from corner: got %v", got)
 9	}
10	if got := Apply(corner, Left); got != corner {
11		t.Errorf("Left from corner: got %v", got)
12	}
13	if got := Apply(corner, Right); got != (Pos{1, 0}) {
14		t.Errorf("Right: got %v", got)
15	}
16	edge := Pos{Size - 1, Size - 1}
17	if got := Apply(edge, Down); got != edge {
18		t.Errorf("Down from bottom edge: got %v", got)
19	}
20	if got := Apply(Pos{3, 3}, Attack); got != (Pos{3, 3}) {
21		t.Errorf("Attack must not move: got %v", got)
22	}
23}
24
25func TestStepTowardAndAway(t *testing.T) {
26	if a := StepToward(Pos{0, 0}, Pos{2, 5}); a != Right {
27		t.Errorf("want right, got %v", a)
28	}
29	if a := StepToward(Pos{2, 0}, Pos{2, 5}); a != Down {
30		t.Errorf("want down, got %v", a)
31	}
32	if a := StepToward(Pos{2, 5}, Pos{2, 5}); a != Stay {
33		t.Errorf("want stay, got %v", a)
34	}
35	from, danger := Pos{4, 4}, Pos{4, 5}
36	if d := Dist(Apply(from, StepAway(from, danger)), danger); d != 2 {
37		t.Errorf("StepAway should increase distance to 2, got %d", d)
38	}
39}
40
41func TestNearestCoin(t *testing.T) {
42	s := State{Me: Pos{0, 0}, Coins: []Pos{{5, 5}, {1, 2}, {8, 0}}}
43	c, ok := s.NearestCoin()
44	if !ok || c != (Pos{1, 2}) {
45		t.Errorf("got %v %v", c, ok)
46	}
47	if _, ok := (State{}).NearestCoin(); ok {
48		t.Error("no coins should report false")
49	}
50}
51
52func TestActionNames(t *testing.T) {
53	if Attack.String() != "attack" || Action(42).String() != "invalid" || Action(-1).Valid() {
54		t.Error("bad action names")
55	}
56}