|
| 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 | +} |
0 commit comments