Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Sep 26, 2023
1 parent 0779a24 commit 9660391
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
56 changes: 56 additions & 0 deletions r/reward_entry/entry_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package reward_entry

import (
"std"
"strings"
"testing"

"gno.land/p/demo/ufmt"
)

func TestRewardEntry(t *testing.T) {
// Override whitelist for testing
whitelist = []string{std.Address("address1"), std.Address("address2"), std.Address("address3")}

// Add reward entry for `foo`` and `bar``
std.TestSetOrigCaller(std.Address("address1"))
SetRewardEntry("foo", 1000, "oof")
SetRewardEntry("bar", 1500, "rab")

// `address2` modify foo's points
std.TestSetOrigCaller(std.Address("address2"))
SetRewardEntry("foo", 1200, "oof; 200 more for good handwriting")

// `unauthorized` address tries to modify foo's points
std.TestSetOrigCaller(std.Address("unauthorized"))
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for unauthorized address")
}
}()
SetRewardEntry("foo", 1200, "oof")
}()

// Note: Render() prints entries in sorted order
// (sorted by points; high -> low)
expectedRows := []string{
"# Reward entries:",
"",
"| Address | Points | Reason | Updated-by | Updated-at |",
"| --------------- | --------- | --------------- | ---------- |",
"| bar | 1500points | rab | address1 |",
"| foo | 1200points | oof; 200 more for good handwriting | address2 |",
}

out := Render("")
// Split the actual output into rows
actualRows := strings.Split(out, "\n")

// Check each row one by one
for i, expectedRow := range expectedRows {
if !strings.HasPrefix(actualRows[i], expectedRow) {
t.Errorf("Row %d does not match:\nExpected:\n%s\nGot:\n%s", i+1, expectedRow, actualRows[i])
}
}
}
37 changes: 37 additions & 0 deletions r/reward_entry/whitelist_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package reward_entry

import (
"std"
"testing"
)

func TestAssertIsWhiteListed(t *testing.T) {
// Override whitelist for testing
whitelist = []string{std.Address("address1"), std.Address("address2"), std.Address("address3")}

// Test with a valid address
validAddress := std.Address("address1")
assertIsWhiteListed(validAddress) // This should not panic

// Test with an invalid address; should cause a panic
invalidAddress := std.Address("invalid_address")
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for invalid address")
}
}()
assertIsWhiteListed(invalidAddress)
}()

// Test with an empty whitelist; should cause a panic
whitelist = []string{}
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for empty whitelist")
}
}()
assertIsWhiteListed(validAddress)
}()
}

0 comments on commit 9660391

Please sign in to comment.