Skip to content Search Apps Documentation Source Content 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.

governor_test.gno

1.79 Kb · 70 lines
 1package governor
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestComputeState(t *testing.T) {
10	tests := []struct {
11		name string
12		p    *Proposal
13		h    int64
14		want string
15	}{
16		{
17			name: "before deadline is active",
18			p:    &Proposal{Deadline: 100, For: 0, Against: 0},
19			h:    50,
20			want: StateActive,
21		},
22		{
23			name: "at deadline with for>against succeeds",
24			p:    &Proposal{Deadline: 100, For: 3, Against: 1},
25			h:    100,
26			want: StateSucceeded,
27		},
28		{
29			name: "past deadline tie is defeated",
30			p:    &Proposal{Deadline: 100, For: 2, Against: 2},
31			h:    120,
32			want: StateDefeated,
33		},
34		{
35			name: "past deadline against wins is defeated",
36			p:    &Proposal{Deadline: 100, For: 1, Against: 5},
37			h:    101,
38			want: StateDefeated,
39		},
40		{
41			name: "executed overrides everything",
42			p:    &Proposal{Deadline: 100, For: 9, Against: 0, Executed: true},
43			h:    200,
44			want: StateExecuted,
45		},
46	}
47	for _, tt := range tests {
48		got := computeState(tt.p, tt.h)
49		uassert.Equal(t, tt.want, got, tt.name)
50	}
51}
52
53func TestBar(t *testing.T) {
54	// empty total renders all-empty of exact width
55	uassert.Equal(t, "░░░░░░░░░░", bar(0, 0, 10), "zero total")
56	// full value fills entirely
57	uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓", bar(10, 10, 10), "full")
58	// half value fills half
59	uassert.Equal(t, "▓▓▓▓▓░░░░░", bar(5, 10, 10), "half")
60	// zero width yields empty string
61	uassert.Equal(t, "", bar(1, 2, 0), "zero width")
62}
63
64func TestIDKey(t *testing.T) {
65	// keys are zero-padded so lexical order matches numeric order
66	uassert.Equal(t, "000000000001", idKey(1), "id 1")
67	uassert.Equal(t, "000000000042", idKey(42), "id 42")
68	uassert.True(t, idKey(2) < idKey(10), "2 sorts before 10")
69	uassert.True(t, idKey(9) < idKey(100), "9 sorts before 100")
70}