Skip to content

Commit

Permalink
feat(examples): add simplest "counter" realm (#2815)
Browse files Browse the repository at this point in the history
I must have used this as an example a thousand times, but I just
realised it doesn't exist as an example.

<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
thehowl authored Sep 20, 2024
1 parent 5450f64 commit ca31100
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/gno.land/r/demo/counter/counter.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package counter

import "strconv"

var counter int

func Increment() int {
counter++
return counter
}

func Render(_ string) string {
return strconv.Itoa(counter)
}
22 changes: 22 additions & 0 deletions examples/gno.land/r/demo/counter/counter_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package counter

import "testing"

func TestIncrement(t *testing.T) {
counter = 0
val := Increment()
if val != 1 {
t.Fatalf("result from Increment(): %d != 1", val)
}
if counter != val {
t.Fatalf("counter (%d) != val (%d)", counter, val)
}
}

func TestRender(t *testing.T) {
counter = 1337
res := Render("")
if res != "1337" {
t.Fatalf("render result %q != %q", res, "1337")
}
}
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/counter/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/counter

0 comments on commit ca31100

Please sign in to comment.