Skip to content

Commit

Permalink
chore(examples): Use uassert/urequire instead of testing in some demo…
Browse files Browse the repository at this point in the history
… packages (gnolang#2491)

Related with gnolang#2481

This PR handles the following tasks:

1. Adds `NotEqual()` and `NotEmpty()` functions to the `p/demo/uassert`
and `p/demo/ urequire` packages (including unit tests)
2. Uses `p/demo/uassert` and `p/demo/urequire` packages for writing unit
tests for some demo packages at `examples/p/demo`
3. Todo : Do the same things with `examples/r/demo` (done)

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
linhpn99 authored and gfanton committed Jul 23, 2024
1 parent ec6ef43 commit f2794ae
Show file tree
Hide file tree
Showing 45 changed files with 659 additions and 887 deletions.
18 changes: 6 additions & 12 deletions examples/gno.land/p/demo/acl/acl_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

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

func Test(t *testing.T) {
Expand Down Expand Up @@ -110,31 +112,23 @@ func Test(t *testing.T) {
func shouldHasRole(t *testing.T, dir *Directory, addr std.Address, role string) {
t.Helper()
check := dir.HasRole(addr, role)
if !check {
t.Errorf("%q should has role %q", addr.String(), role)
}
uassert.Equal(t, true, check, ufmt.Sprintf("%s should has role %s", addr.String(), role))
}

func shouldNotHasRole(t *testing.T, dir *Directory, addr std.Address, role string) {
t.Helper()
check := dir.HasRole(addr, role)
if check {
t.Errorf("%q should not has role %q", addr.String(), role)
}
uassert.Equal(t, false, check, ufmt.Sprintf("%s should not has role %s", addr.String(), role))
}

func shouldHasPerm(t *testing.T, dir *Directory, addr std.Address, verb string, resource string) {
t.Helper()
check := dir.HasPerm(addr, verb, resource)
if !check {
t.Errorf("%q should has perm for %q - %q", addr.String(), verb, resource)
}
uassert.Equal(t, true, check, ufmt.Sprintf("%s should has perm for %s - %s", addr.String(), verb, resource))
}

func shouldNotHasPerm(t *testing.T, dir *Directory, addr std.Address, verb string, resource string) {
t.Helper()
check := dir.HasPerm(addr, verb, resource)
if check {
t.Errorf("%q should not has perm for %q - %q", addr.String(), verb, resource)
}
uassert.Equal(t, false, check, ufmt.Sprintf("%s should not has perm for %s - %s", addr.String(), verb, resource))
}
2 changes: 2 additions & 0 deletions examples/gno.land/p/demo/acl/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ module gno.land/p/demo/acl
require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
)
5 changes: 4 additions & 1 deletion examples/gno.land/p/demo/gnorkle/agent/gno.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module gno.land/p/demo/gnorkle/agent

require gno.land/p/demo/avl v0.0.0-latest
require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
)
39 changes: 10 additions & 29 deletions examples/gno.land/p/demo/gnorkle/agent/whitelist_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,25 @@ import (
"testing"

"gno.land/p/demo/gnorkle/agent"
"gno.land/p/demo/uassert"
)

func TestWhitelist(t *testing.T) {
var whitelist agent.Whitelist

if whitelist.HasDefinition() {
t.Error("whitelist should not be defined initially")
}
uassert.False(t, whitelist.HasDefinition(), "whitelist should not be defined initially")

whitelist.AddAddresses([]string{"a", "b"})
if !whitelist.HasAddress("a") {
t.Error(`whitelist should have address "a"`)
}
if !whitelist.HasAddress("b") {
t.Error(`whitelist should have address "b"`)
}

if !whitelist.HasDefinition() {
t.Error("whitelist should be defined after adding addresses")
}
uassert.True(t, whitelist.HasAddress("a"), `whitelist should have address "a"`)
uassert.True(t, whitelist.HasAddress("b"), `whitelist should have address "b"`)
uassert.True(t, whitelist.HasDefinition(), "whitelist should be defined after adding addresses")

whitelist.RemoveAddress("a")
if whitelist.HasAddress("a") {
t.Error(`whitelist should not have address "a"`)
}
if !whitelist.HasAddress("b") {
t.Error(`whitelist should still have address "b"`)
}
uassert.False(t, whitelist.HasAddress("a"), `whitelist should not have address "a"`)
uassert.True(t, whitelist.HasAddress("b"), `whitelist should still have address "b"`)

whitelist.ClearAddresses()
if whitelist.HasAddress("a") {
t.Error(`whitelist cleared; should not have address "a"`)
}
if whitelist.HasAddress("b") {
t.Error(`whitelist cleared; should still have address "b"`)
}

if whitelist.HasDefinition() {
t.Error("whitelist cleared; should not be defined")
}
uassert.False(t, whitelist.HasAddress("a"), `whitelist cleared; should not have address "a"`)
uassert.False(t, whitelist.HasAddress("b"), `whitelist cleared; should still have address "b"`)
uassert.False(t, whitelist.HasDefinition(), "whitelist cleared; should not be defined")
}
63 changes: 17 additions & 46 deletions examples/gno.land/p/demo/gnorkle/feeds/static/feed_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"gno.land/p/demo/gnorkle/ingester"
"gno.land/p/demo/gnorkle/message"
"gno.land/p/demo/gnorkle/storage/simple"
"gno.land/p/demo/uassert"
"gno.land/p/demo/urequire"
)

type mockIngester struct {
Expand Down Expand Up @@ -44,20 +46,15 @@ func (i *mockIngester) CommitValue(storage gnorkle.Storage, providerAddress stri

func TestNewSingleValueFeed(t *testing.T) {
staticFeed := static.NewSingleValueFeed("1", "")
if staticFeed.ID() != "1" {
t.Errorf("expected ID to be 1, got %s", staticFeed.ID())
}

if staticFeed.Type() != feed.TypeStatic {
t.Errorf("expected static feed type, got %s", staticFeed.Type())
}
uassert.Equal(t, "1", staticFeed.ID())
uassert.Equal(t, int(feed.TypeStatic), int(staticFeed.Type()))
}

func TestFeed_Ingest(t *testing.T) {
var undefinedFeed *static.Feed
if undefinedFeed.Ingest("", "", "") != feed.ErrUndefined {
t.Errorf("expected ErrUndefined, got nil")
}
err := undefinedFeed.Ingest("", "", "")
uassert.ErrorIs(t, err, feed.ErrUndefined)

tests := []struct {
name string
Expand Down Expand Up @@ -157,15 +154,11 @@ func TestFeed_Ingest(t *testing.T) {
errText = err.Error()
}

if errText != tt.expErrText {
t.Fatalf("expected error text %s, got %s", tt.expErrText, errText)
}
urequire.Equal(t, tt.expErrText, errText)

if tt.doCommit {
err := staticFeed.Ingest(message.FuncTypeCommit, "", "")
if err != nil {
t.Fatalf("follow up commit failed: %s", err.Error())
}
urequire.NoError(t, err, "follow up commit failed")
}

if tt.verifyIsLocked {
Expand All @@ -174,31 +167,16 @@ func TestFeed_Ingest(t *testing.T) {
errText = err.Error()
}

if errText != "feed locked" {
t.Fatalf("expected error text feed locked, got %s", errText)
}
urequire.Equal(t, "feed locked", errText)
}

if tt.ingester.providerAddress != tt.providerAddress {
t.Errorf("expected provider address %s, got %s", tt.providerAddress, tt.ingester.providerAddress)
}
uassert.Equal(t, tt.providerAddress, tt.ingester.providerAddress)

feedValue, dataType, isLocked := staticFeed.Value()
if feedValue.String != tt.expFeedValueString {
t.Errorf("expected feed value string %s, got %s", tt.expFeedValueString, feedValue.String)
}

if dataType != "string" {
t.Errorf("expected data type string, got %s", dataType)
}

if isLocked != tt.verifyIsLocked {
t.Errorf("expected is locked %t, got %t", tt.verifyIsLocked, isLocked)
}

if staticFeed.IsActive() != tt.expIsActive {
t.Errorf("expected is active %t, got %t", tt.expIsActive, staticFeed.IsActive())
}
uassert.Equal(t, tt.expFeedValueString, feedValue.String)
uassert.Equal(t, "string", dataType)
uassert.Equal(t, tt.verifyIsLocked, isLocked)
uassert.Equal(t, tt.expIsActive, staticFeed.IsActive())
})
}
}
Expand Down Expand Up @@ -262,23 +240,16 @@ func TestFeed_Tasks(t *testing.T) {
tt.tasks...,
)

if len(staticFeed.Tasks()) != len(tt.tasks) {
t.Fatalf("expected %d tasks, got %d", len(tt.tasks), len(staticFeed.Tasks()))
}
urequire.Equal(t, len(tt.tasks), len(staticFeed.Tasks()))

var errText string
json, err := staticFeed.MarshalJSON()
if err != nil {
errText = err.Error()
}

if errText != tt.expErrText {
t.Fatalf("expected error text %s, got %s", tt.expErrText, errText)
}

if string(json) != tt.expJSON {
t.Errorf("expected json %s, got %s", tt.expJSON, string(json))
}
urequire.Equal(t, tt.expErrText, errText)
urequire.Equal(t, tt.expJSON, string(json))
})
}
}
2 changes: 2 additions & 0 deletions examples/gno.land/p/demo/gnorkle/feeds/static/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ require (
gno.land/p/demo/gnorkle/ingesters/single v0.0.0-latest
gno.land/p/demo/gnorkle/message v0.0.0-latest
gno.land/p/demo/gnorkle/storage/simple v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/urequire v0.0.0-latest
)
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/gnorkle/ingesters/single/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ require (
gno.land/p/demo/gnorkle/gnorkle v0.0.0-latest
gno.land/p/demo/gnorkle/ingester v0.0.0-latest
gno.land/p/demo/gnorkle/storage/simple v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
)
39 changes: 16 additions & 23 deletions examples/gno.land/p/demo/gnorkle/ingesters/single/ingester_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,31 @@ import (
"gno.land/p/demo/gnorkle/ingester"
"gno.land/p/demo/gnorkle/ingesters/single"
"gno.land/p/demo/gnorkle/storage/simple"
"gno.land/p/demo/uassert"
)

func TestValueIngester(t *testing.T) {
storage := simple.NewStorage(1)

var undefinedIngester *single.ValueIngester
if _, err := undefinedIngester.Ingest("asdf", "gno11111"); err != ingester.ErrUndefined {
t.Error("undefined ingester call to Ingest should return ingester.ErrUndefined")
}
if err := undefinedIngester.CommitValue(storage, "gno11111"); err != ingester.ErrUndefined {
t.Error("undefined ingester call to CommitValue should return ingester.ErrUndefined")
}
_, err := undefinedIngester.Ingest("asdf", "gno11111")
uassert.ErrorIs(t, err, ingester.ErrUndefined, "undefined ingester call to Ingest should return ingester.ErrUndefined")

err = undefinedIngester.CommitValue(storage, "gno11111")
uassert.ErrorIs(t, err, ingester.ErrUndefined, "undefined ingester call to CommitValue should return ingester.ErrUndefined")

var valueIngester single.ValueIngester
if typ := valueIngester.Type(); typ != ingester.TypeSingle {
t.Error("single value ingester should return type ingester.TypeSingle")
}
typ := valueIngester.Type()
uassert.Equal(t, int(ingester.TypeSingle), int(typ), "single value ingester should return type ingester.TypeSingle")

ingestValue := "value"
autocommit, err := valueIngester.Ingest(ingestValue, "gno11111")
if !autocommit {
t.Error("single value ingester should return autocommit true")
}
if err != nil {
t.Errorf("unexpected ingest error %s", err.Error())
}

if err := valueIngester.CommitValue(storage, "gno11111"); err != nil {
t.Errorf("unexpected commit error %s", err.Error())
}

if latestValue := storage.GetLatest(); latestValue.String != ingestValue {
t.Errorf("expected latest value of %s, got %s", ingestValue, latestValue.String)
}
uassert.True(t, autocommit, "single value ingester should return autocommit true")
uassert.NoError(t, err)

err = valueIngester.CommitValue(storage, "gno11111")
uassert.NoError(t, err)

latestValue := storage.GetLatest()
uassert.Equal(t, ingestValue, latestValue.String)
}
2 changes: 2 additions & 0 deletions examples/gno.land/p/demo/gnorkle/message/gno.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module gno.land/p/demo/gnorkle/message

require gno.land/p/demo/uassert v0.0.0-latest
9 changes: 3 additions & 6 deletions examples/gno.land/p/demo/gnorkle/message/parse_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"gno.land/p/demo/gnorkle/message"
"gno.land/p/demo/uassert"
)

func TestParseFunc(t *testing.T) {
Expand Down Expand Up @@ -38,13 +39,9 @@ func TestParseFunc(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
funcType, remainder := message.ParseFunc(tt.input)
if funcType != tt.expFuncType {
t.Errorf("expected func type %s, got %s", tt.expFuncType, funcType)
}

if remainder != tt.expRemainder {
t.Errorf("expected remainder of %s, got %s", tt.expRemainder, remainder)
}
uassert.Equal(t, string(tt.expFuncType), string(funcType))
uassert.Equal(t, tt.expRemainder, remainder)
})
}
}
3 changes: 3 additions & 0 deletions examples/gno.land/p/demo/gnorkle/storage/simple/gno.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ module gno.land/p/demo/gnorkle/storage/simple
require (
gno.land/p/demo/gnorkle/feed v0.0.0-latest
gno.land/p/demo/gnorkle/storage v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/urequire v0.0.0-latest
)
Loading

0 comments on commit f2794ae

Please sign in to comment.