Skip to content

Commit 926b00c

Browse files
committed
Add tests
1 parent 2a393ea commit 926b00c

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

r/reward_entry/entry_test.gno

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package reward_entry
2+
3+
import (
4+
"std"
5+
"strings"
6+
"testing"
7+
8+
"gno.land/p/demo/ufmt"
9+
)
10+
11+
func TestRewardEntry(t *testing.T) {
12+
// Override whitelist for testing
13+
whitelist = []string{std.Address("address1"), std.Address("address2"), std.Address("address3")}
14+
15+
// Add reward entry for `foo`` and `bar``
16+
std.TestSetOrigCaller(std.Address("address1"))
17+
SetRewardEntry("foo", 1000, "oof")
18+
SetRewardEntry("bar", 1500, "rab")
19+
20+
// `address2` modify foo's points
21+
std.TestSetOrigCaller(std.Address("address2"))
22+
SetRewardEntry("foo", 1200, "oof; 200 more for good handwriting")
23+
24+
// `unauthorized` address tries to modify foo's points
25+
std.TestSetOrigCaller(std.Address("unauthorized"))
26+
func() {
27+
defer func() {
28+
if r := recover(); r == nil {
29+
t.Errorf("expected panic for unauthorized address")
30+
}
31+
}()
32+
SetRewardEntry("foo", 1200, "oof")
33+
}()
34+
35+
// Note: Render() prints entries in sorted order
36+
// (sorted by points; high -> low)
37+
expectedRows := []string{
38+
"# Reward entries:",
39+
"",
40+
"| Address | Points | Reason | Updated-by | Updated-at |",
41+
"| --------------- | --------- | --------------- | ---------- |",
42+
"| bar | 1500points | rab | address1 |",
43+
"| foo | 1200points | oof; 200 more for good handwriting | address2 |",
44+
}
45+
46+
out := Render("")
47+
// Split the actual output into rows
48+
actualRows := strings.Split(out, "\n")
49+
50+
// Check each row one by one
51+
for i, expectedRow := range expectedRows {
52+
if !strings.HasPrefix(actualRows[i], expectedRow) {
53+
t.Errorf("Row %d does not match:\nExpected:\n%s\nGot:\n%s", i+1, expectedRow, actualRows[i])
54+
}
55+
}
56+
}

r/reward_entry/whitelist_test.gno

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package reward_entry
2+
3+
import (
4+
"std"
5+
"testing"
6+
)
7+
8+
func TestAssertIsWhiteListed(t *testing.T) {
9+
// Override whitelist for testing
10+
whitelist = []string{std.Address("address1"), std.Address("address2"), std.Address("address3")}
11+
12+
// Test with a valid address
13+
validAddress := std.Address("address1")
14+
assertIsWhiteListed(validAddress) // This should not panic
15+
16+
// Test with an invalid address; should cause a panic
17+
invalidAddress := std.Address("invalid_address")
18+
func() {
19+
defer func() {
20+
if r := recover(); r == nil {
21+
t.Errorf("expected panic for invalid address")
22+
}
23+
}()
24+
assertIsWhiteListed(invalidAddress)
25+
}()
26+
27+
// Test with an empty whitelist; should cause a panic
28+
whitelist = []string{}
29+
func() {
30+
defer func() {
31+
if r := recover(); r == nil {
32+
t.Errorf("expected panic for empty whitelist")
33+
}
34+
}()
35+
assertIsWhiteListed(validAddress)
36+
}()
37+
}

0 commit comments

Comments
 (0)