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.

ledger_test.gno

1.25 Kb · 49 lines
 1package flag
 2
 3import "testing"
 4
 5const (
 6	ash   = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")
 7	briar = address("g19cyxvfmzdnxlf4ul6328wqq4hy3ssqmqcgul0k")
 8	cask  = address("g1rh0vd3xzwjpcqehrf0tpjlqeq35079wf4zklar")
 9)
10
11func TestLedgerRecord(t *testing.T) {
12	l := NewLedger()
13	if l.Captured(ash) {
14		t.Fatal("empty ledger reports a capture")
15	}
16	c, fresh := l.Record(ash)
17	if !fresh || c.Order != 1 || c.Player != ash {
18		t.Fatalf("first record = %+v, fresh=%v", c, fresh)
19	}
20	testing.SkipHeights(3)
21	again, fresh := l.Record(ash)
22	if fresh || again != c {
23		t.Fatalf("second record of same player should return the first: %+v", again)
24	}
25	c2, _ := l.Record(briar)
26	if c2.Order != 2 || c2.Height != c.Height+3 {
27		t.Fatalf("second player capture = %+v", c2)
28	}
29	if l.Count() != 2 || !l.Captured(briar) || l.Captured(cask) {
30		t.Fatal("count or membership wrong")
31	}
32}
33
34func TestLedgerRecent(t *testing.T) {
35	l := NewLedger()
36	if got := l.Recent(5); len(got) != 0 {
37		t.Fatalf("Recent on empty ledger = %v", got)
38	}
39	l.Record(ash)
40	l.Record(briar)
41	l.Record(cask)
42	got := l.Recent(2)
43	if len(got) != 2 || got[0].Player != cask || got[1].Player != briar {
44		t.Fatalf("Recent(2) = %+v", got)
45	}
46	if len(l.Recent(10)) != 3 {
47		t.Fatal("Recent should clamp to the ledger size")
48	}
49}