Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions 003-debug-gno-code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Debugging Gno code

In this section, you will learn to debug your Gno contract. As contracts aren't
modifiable, it is crucial to debug them properly before their publication to
the blockchain.

Debugging Gno code functions in many ways as in Go;
the biggest tool at our disposal are test files. These are identified
as source files whose names end with `_test.go`. Every function of
the kind `func TestXxx(t *testing.T)` is automatically added as a test,
run when using `gno test`. If the function calls `t.Errorf`, then it is
considered failed.

- `queue.gno`, in this tutorial's directory `003-debug-gno-code`, is a source file containing 2 stub functions `Push` and `Pop`. Your goal in this tutorial is to implement them correctly and see the tests succeed.
- `queue_test.gno` contains a test that checks the behavior of `Pop` and `Push`.
If you implement them correctly, running `gno test` will succeed without errors.

## Steps

- Run the test:
```
$ gno test . -verbose
```
The test will fail, because `Pop` and `Push` are currently not
implemented -- if you open the `queue.gno` file, you will see
that they just contain two `// TODO` comments.

- Implement `Pop` and `Push`. These need to implement a
FIFO (First In, First Out) queue -- so the last element added
using `Push` will be the first one to be removed using `Pop`.
- Run the test again until it succeeds

<details>
<summary>Solution (only if you're stuck!)</summary>

```go
func Push(s string) {
q = append(q, s)
}

func Pop() string {
if len(q) == 0 {
return ""
}
s := q[0]
q = q[1:]
return s
}
```

</details>
15 changes: 15 additions & 0 deletions 003-debug-gno-code/queue.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package queue

var q []string

// Push appends s to q.
func Push(s string) {
// TODO
}

// Pop removes the first element of q and returns it.
// If q is empty, Pop returns an empty string.
func Pop() string {
// TODO
return ""
}
17 changes: 17 additions & 0 deletions 003-debug-gno-code/queue_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package queue

import "testing"

func TestQueue(t *testing.T) {
// Push 2 items
Push("alice")
Push("bob")
// Call Pop() 3 times
// Third time should return an empty string because the queue is empty
for i, expected := range []string{"alice", "bob", ""} {
res := Pop()
if res != expected {
t.Errorf("Pop()#%d want %q, got %q", i, expected, res)
}
}
}