-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}() | ||
} |