From 562f8425d1d89b739964a3c0fd54234132b2fb9e Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sat, 22 Jul 2023 19:53:27 +0200 Subject: [PATCH 1/9] 003 tutorial --- 003-debug-gno-code/README.md | 23 +++++++++++++++++++++++ 003-debug-gno-code/queue.gno | 15 +++++++++++++++ 003-debug-gno-code/queue_test.gno | 17 +++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 003-debug-gno-code/queue.gno create mode 100644 003-debug-gno-code/queue_test.gno diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index e69de29..5e97a48 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -0,0 +1,23 @@ +# 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. + +As you will see Gno debugging is pretty much the same as Go debugging. + +- `queue.gno` in this section implements a basic FIFO queue with 2 functions +`Push` and `Pop`. +- `queue_test.gno` contains a test that asserts the expected behavior of `Pop` and +`Push`. + +## Steps + +- Run the test: +``` +$ gno test . -verbose -run TestQueue +``` +The test fails because `Pop` and `Push` are not implemented properly. + +- Implement `Pop` and `Push` +- Run the test again until it succeeds diff --git a/003-debug-gno-code/queue.gno b/003-debug-gno-code/queue.gno new file mode 100644 index 0000000..4aefcdd --- /dev/null +++ b/003-debug-gno-code/queue.gno @@ -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 "" +} diff --git a/003-debug-gno-code/queue_test.gno b/003-debug-gno-code/queue_test.gno new file mode 100644 index 0000000..a3d5c6f --- /dev/null +++ b/003-debug-gno-code/queue_test.gno @@ -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) + } + } +} From 66415e28c5cec752fd152d0e79cffc0003b503df Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sat, 22 Jul 2023 20:28:22 +0200 Subject: [PATCH 2/9] add hidden by default solution --- 003-debug-gno-code/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index 5e97a48..95f2666 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -21,3 +21,23 @@ The test fails because `Pop` and `Push` are not implemented properly. - Implement `Pop` and `Push` - Run the test again until it succeeds + +
+ Solution (only if you're stuck!) + +``` +func Push(s string) { + q = append(q, s) +} + +func Pop() string { + if len(q) == 0 { + return "" + } + s := q[0] + q = q[1:] + return s +} +``` + +
From 789d21d9d5fad9fe1b240f48f7162a5572a0fa92 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sat, 22 Jul 2023 20:33:33 +0200 Subject: [PATCH 3/9] syntax --- 003-debug-gno-code/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index 95f2666..609a5b2 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -25,7 +25,7 @@ The test fails because `Pop` and `Push` are not implemented properly.
Solution (only if you're stuck!) -``` +```go func Push(s string) { q = append(q, s) } From 8ce377d1d75cc10339eab58baf8d5f9416ce9d5d Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sun, 23 Jul 2023 12:32:57 +0200 Subject: [PATCH 4/9] Update 003-debug-gno-code/README.md Co-authored-by: Morgan --- 003-debug-gno-code/README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index 609a5b2..7e5f694 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -4,7 +4,12 @@ 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. -As you will see Gno debugging is pretty much the same as Go debugging. +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 section implements a basic FIFO queue with 2 functions `Push` and `Pop`. From 178950932361ab1e02dc402c0793fe391dd3c542 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sun, 23 Jul 2023 12:34:17 +0200 Subject: [PATCH 5/9] Update 003-debug-gno-code/README.md Co-authored-by: Morgan --- 003-debug-gno-code/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index 7e5f694..8bffed7 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -11,8 +11,7 @@ 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 section implements a basic FIFO queue with 2 functions -`Push` and `Pop`. +- `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 asserts the expected behavior of `Pop` and `Push`. From 9fb6736ed5183d346515528edde2fcac2c7c36af Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sun, 23 Jul 2023 12:34:34 +0200 Subject: [PATCH 6/9] Update 003-debug-gno-code/README.md Co-authored-by: Morgan --- 003-debug-gno-code/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index 8bffed7..f5dfcf5 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -12,8 +12,8 @@ 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 asserts the expected behavior of `Pop` and -`Push`. +- `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 From 685db4bbeeb41b4285a23a39679b41087d355e3e Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sun, 23 Jul 2023 12:41:26 +0200 Subject: [PATCH 7/9] Update 003-debug-gno-code/README.md Co-authored-by: Morgan --- 003-debug-gno-code/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index f5dfcf5..716c775 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -19,7 +19,7 @@ considered failed. - Run the test: ``` -$ gno test . -verbose -run TestQueue +$ gno test . -verbose ``` The test fails because `Pop` and `Push` are not implemented properly. From 313131e700a462a70c2da6b78c2e4c3bd0320f09 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sun, 23 Jul 2023 12:42:38 +0200 Subject: [PATCH 8/9] Update 003-debug-gno-code/README.md Co-authored-by: Morgan --- 003-debug-gno-code/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index 716c775..cc54828 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -21,7 +21,9 @@ considered failed. ``` $ gno test . -verbose ``` -The test fails because `Pop` and `Push` are not implemented properly. +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` - Run the test again until it succeeds From 96d1bb9f3442be175574ba4db57744a3a0b9fe56 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Sun, 23 Jul 2023 12:42:51 +0200 Subject: [PATCH 9/9] Update 003-debug-gno-code/README.md Co-authored-by: Morgan --- 003-debug-gno-code/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/003-debug-gno-code/README.md b/003-debug-gno-code/README.md index cc54828..8338fc7 100644 --- a/003-debug-gno-code/README.md +++ b/003-debug-gno-code/README.md @@ -25,7 +25,9 @@ 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` +- 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