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

2.10 Kb · 74 lines
 1package governor
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nym-moultest123/kit/store/v0"
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10func TestComputeState(t *testing.T) {
11	tests := []struct {
12		name string
13		p    *Proposal
14		h    int64
15		want string
16	}{
17		{
18			name: "before deadline is active",
19			p:    &Proposal{Deadline: 100, For: 0, Against: 0},
20			h:    50,
21			want: StateActive,
22		},
23		{
24			name: "at deadline with for>against succeeds",
25			p:    &Proposal{Deadline: 100, For: 3, Against: 1},
26			h:    100,
27			want: StateSucceeded,
28		},
29		{
30			name: "past deadline tie is defeated",
31			p:    &Proposal{Deadline: 100, For: 2, Against: 2},
32			h:    120,
33			want: StateDefeated,
34		},
35		{
36			name: "past deadline against wins is defeated",
37			p:    &Proposal{Deadline: 100, For: 1, Against: 5},
38			h:    101,
39			want: StateDefeated,
40		},
41		{
42			name: "executed overrides everything",
43			p:    &Proposal{Deadline: 100, For: 9, Against: 0, Executed: true},
44			h:    200,
45			want: StateExecuted,
46		},
47	}
48	for _, tt := range tests {
49		got := computeState(tt.p, tt.h)
50		uassert.Equal(t, tt.want, got, tt.name)
51	}
52}
53
54func TestBar(t *testing.T) {
55	// empty total renders all-empty of exact width
56	uassert.Equal(t, "░░░░░░░░░░", bar(0, 0, 10), "zero total")
57	// full value fills entirely
58	uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓", bar(10, 10, 10), "full")
59	// half value fills half
60	uassert.Equal(t, "▓▓▓▓▓░░░░░", bar(5, 10, 10), "half")
61	// zero width yields empty string
62	uassert.Equal(t, "", bar(1, 2, 0), "zero width")
63}
64
65// v0 asserted that its own idKey() padded to width 12. That rule held only
66// below the width: idKey(10^12) is 13 characters and sorts before
67// idKey(10^12-1), so the proposal list would have rendered out of order from
68// that id on. The store key has no width to outgrow.
69func TestIDKey(t *testing.T) {
70	uassert.True(t, store.ID(2).Key() < store.ID(10).Key(), "2 sorts before 10")
71	uassert.True(t, store.ID(9).Key() < store.ID(100).Key(), "9 sorts before 100")
72	uassert.True(t, store.ID(999999999999).Key() < store.ID(1000000000000).Key(),
73		"and it keeps holding one past v0's width-12 ceiling")
74}