moth

Monarch Of The Hill game server
git clone https://git.woozle.org/neale/moth.git

moth / pkg / award
Neale Pickett  ·  2020-08-21

award_test.go

 1package award
 2
 3import (
 4	"sort"
 5	"testing"
 6)
 7
 8func TestAward(t *testing.T) {
 9	entry := "1536958399 1a2b3c4d counting 10"
10	a, err := Parse(entry)
11	if err != nil {
12		t.Error(err)
13		return
14	}
15	if a.TeamID != "1a2b3c4d" {
16		t.Error("TeamID parsed wrong")
17	}
18	if a.Category != "counting" {
19		t.Error("Category parsed wrong")
20	}
21	if a.Points != 10 {
22		t.Error("Points parsed wrong")
23	}
24
25	if a.String() != entry {
26		t.Error("String conversion wonky")
27	}
28
29	b, err := Parse(entry[2:])
30	if err != nil {
31		t.Error(err)
32	}
33	if !a.Equal(b) {
34		t.Error("Different timestamp events do not compare equal")
35	}
36
37	c, err := Parse(entry[:len(entry)-1])
38	if err != nil {
39		t.Error(err)
40	}
41	if a.Equal(c) {
42		t.Error("Different pount values compare equal")
43	}
44
45	ja, err := a.MarshalJSON()
46	if err != nil {
47		t.Error(err)
48	} else if string(ja) != `[1536958399,"1a2b3c4d","counting",10]` {
49		t.Error("JSON wrong")
50	}
51
52	if _, err := Parse("bad bad bad 1"); err == nil {
53		t.Error("Not throwing error on bad timestamp")
54	}
55	if _, err := Parse("1 bad bad bad"); err == nil {
56		t.Error("Not throwing error on bad points")
57	}
58
59	if err := b.UnmarshalJSON(ja); err != nil {
60		t.Error(err)
61	} else if !b.Equal(a) {
62		t.Error("UnmarshalJSON didn't work")
63	}
64
65	for _, s := range []string{`12`, `"moo"`, `{"a":1}`, `[1 2 3 4]`, `[]`, `[1,"a"]`, `[1,"a","b",4, 5]`} {
66		buf := []byte(s)
67		if err := a.UnmarshalJSON(buf); err == nil {
68			t.Error("Bad unmarshal didn't return error:", s)
69		}
70	}
71
72}
73
74func TestAwardList(t *testing.T) {
75	a, _ := Parse("1536958399 1a2b3c4d counting 1")
76	b, _ := Parse("1536958400 1a2b3c4d counting 1")
77	c, _ := Parse("1536958300 1a2b3c4d counting 1")
78	list := List{a, b, c}
79
80	if sort.IsSorted(list) {
81		t.Error("Unsorted list thinks it's sorted")
82	}
83
84	sort.Stable(list)
85	if (list[0] != c) || (list[1] != a) || (list[2] != b) {
86		t.Error("Sorting didn't")
87	}
88
89	if !sort.IsSorted(list) {
90		t.Error("Sorted list thinks it isn't")
91	}
92}