From bfce4e914bee4971cfcf93f858b99bdd627dd714 Mon Sep 17 00:00:00 2001 From: MalekLahbib Date: Thu, 21 Mar 2024 18:23:37 +0100 Subject: [PATCH 1/7] added todolist pckg and realm --- examples/gno.land/p/demo/todolist/gno.mod | 6 + .../gno.land/p/demo/todolist/todolist.gno | 68 +++++++ .../p/demo/todolist/todolist_test.gno | 51 +++++ examples/gno.land/r/demo/todolist/gno.mod | 7 + .../gno.land/r/demo/todolist/todolist.gno | 188 ++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 examples/gno.land/p/demo/todolist/gno.mod create mode 100644 examples/gno.land/p/demo/todolist/todolist.gno create mode 100644 examples/gno.land/p/demo/todolist/todolist_test.gno create mode 100644 examples/gno.land/r/demo/todolist/gno.mod create mode 100644 examples/gno.land/r/demo/todolist/todolist.gno diff --git a/examples/gno.land/p/demo/todolist/gno.mod b/examples/gno.land/p/demo/todolist/gno.mod new file mode 100644 index 00000000000..ffd0ed437e1 --- /dev/null +++ b/examples/gno.land/p/demo/todolist/gno.mod @@ -0,0 +1,6 @@ +module gno.land/p/demo/todolist + +require ( + gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/ufmt v0.0.0-latest +) diff --git a/examples/gno.land/p/demo/todolist/todolist.gno b/examples/gno.land/p/demo/todolist/todolist.gno new file mode 100644 index 00000000000..5bde86896bc --- /dev/null +++ b/examples/gno.land/p/demo/todolist/todolist.gno @@ -0,0 +1,68 @@ +package todolist + +import ( + "std" + "strconv" + + "gno.land/p/demo/avl" + "gno.land/p/demo/ufmt" +) + +type TodoList struct { + Title string + Tasks avl.Tree + Owner std.Address +} + +type Task struct { + Title string + Done bool +} + +func NewTodoList(title string) *TodoList { + return &TodoList{ + Title: title, + Tasks: avl.Tree{}, + Owner: std.GetOrigCaller(), + } +} + +func NewTask(title string) *Task { + return &Task{ + Title: title, + Done: false, + } +} + +func (tl *TodoList) AddTask(id int, task *Task) { + tl.Tasks.Set(strconv.Itoa(id), task) +} + +func ToggleTaskStatus(task *Task) { + task.Done = !task.Done +} + +func (tl *TodoList) RemoveTask(taskId string) { + tl.Tasks.Remove(taskId) +} + +func (tl *TodoList) GetTasks() []*Task { + tasks := make([]*Task, 0, tl.Tasks.Size()) + tl.Tasks.Iterate("", "", func(key string, value interface{}) bool { + tasks = append(tasks, value.(*Task)) + return false + }) + return tasks +} + +func (tl *TodoList) String() string { + return ufmt.Sprintf("TodoList{Title: %q, Tasks: %v, Owner: %v}", tl.Title, tl.GetTasks(), tl.Owner) +} + +func (tl *TodoList) GetTodolistOwner() std.Address { + return tl.Owner +} + +func (tl *TodoList) GetTodolistTitle() string { + return tl.Title +} diff --git a/examples/gno.land/p/demo/todolist/todolist_test.gno b/examples/gno.land/p/demo/todolist/todolist_test.gno new file mode 100644 index 00000000000..3193892bc28 --- /dev/null +++ b/examples/gno.land/p/demo/todolist/todolist_test.gno @@ -0,0 +1,51 @@ +package todolist + +import ( + "testing" + + "gno.land/p/demo/avl" +) + +func TestTodolistNew(t *testing.T) { + newtl := &TodoList{ + Title: "new todolist", + Tasks: avl.Tree{}, + } + tl := NewTodoList("new todolist") + if tl.Title != newtl.Title { + t.Fatalf("title is not good") + } + if tl.Tasks != newtl.Tasks { + t.Fatalf("tasks not good") + } + + // test new task + newtask := &Task{ + Title: "new task", + Done: false, + } + nt := NewTask("new task") + if nt.Title != newtask.Title { + t.Fatalf("title is not good") + } + if nt.Done != newtask.Done { + t.Fatalf("done is not good") + } + + // test add a task + id := tl.Tasks.Size() + tl.AddTask(id, nt) + if tl.Tasks.Size() != 1 { + t.Fatalf("task not added") + } + + // test ToggleTaskStatus + task, exists := tl.Tasks.Get("0") + if !exists { + t.Fatalf("task not found") + } + ToggleTaskStatus(task.(*Task)) + if task.(*Task).Done != true { + t.Fatalf("task not toggled") + } +} diff --git a/examples/gno.land/r/demo/todolist/gno.mod b/examples/gno.land/r/demo/todolist/gno.mod new file mode 100644 index 00000000000..e23286a1402 --- /dev/null +++ b/examples/gno.land/r/demo/todolist/gno.mod @@ -0,0 +1,7 @@ +module gno.land/r/demo/todolist + +require ( + gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/todolist v0.0.0-latest + gno.land/p/demo/ufmt v0.0.0-latest +) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno new file mode 100644 index 00000000000..b4ab5df3e82 --- /dev/null +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -0,0 +1,188 @@ +package todolist + +import ( + "bytes" + "std" + "strconv" + + "gno.land/p/demo/avl" + "gno.land/p/demo/todolist" + "gno.land/p/demo/ufmt" +) + +// State variables +var ( + todolistTree *avl.Tree + tlid int +) + +// Constructor +func init() { + todolistTree = avl.NewTree() +} + +func NewTodolist(title string) (int, string) { + // Get user who sent the transaction + txSender := std.GetOrigCaller() + + tl := todolist.NewTodoList(title) + // Update AVL tree with new state + success := todolistTree.Set(strconv.Itoa(tlid), tl) + tlid++ + return tlid-1, "created successfully" +} + +func AddTask(todolistID int, title string) string { + // Get user who sent the transaction + txSender := std.GetOrigCaller() + + // Get Todolist from AVL tree + tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) + if !ok { + return "Todolist not found" + } + + // get the number of tasks in the todolist + id := tl.(*todolist.TodoList).Tasks.Size() + + // create the task + task := todolist.NewTask(title) + + // Cast raw data from tree into Todolist struct + tl.(*todolist.TodoList).AddTask(id, task) + + return "task added successfully" +} + +func ToggleTaskStatus(todolistID int, taskID int) string { + // Get user who sent the transaction + // txSender := std.GetOrigCaller() + + // Get Todolist from AVL tree + tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) + if !ok { + return "Todolist not found" + } + + // Get the task from the todolist + task, ok := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) + if !ok { + return "Task not found" + } + + // Change the status of the task + todolist.ToggleTaskStatus(task.(*todolist.Task)) + + return "task status changed successfully" +} + +func RemoveTask(todolistID int, taskID int) string { + // Get user who sent the transaction + // txSender := std.GetOrigCaller() + + // Get Todolist from AVL tree + tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) + if !ok { + return "Todolist not found" + } + + // Get the task from the todolist + task, ok := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) + if !ok { + return "Task not found" + } + + // Change the status of the task + tl.(*todolist.TodoList).RemoveTask(strconv.Itoa(taskID)) + + return "task status changed successfully" +} + +func RemoveTodoList(todolistID int) string { + // Get user who sent the transaction + // txSender := std.GetOrigCaller() + + // Get Todolist from AVL tree + _, ok := todolistTree.Get(strconv.Itoa(todolistID)) + if !ok { + return "Todolist not found" + } + + // Remove the todolist + todolistTree.Remove(strconv.Itoa(todolistID)) + + return "Todolist removed successfully" +} + +func Render(path string) string { + if path == "" { + return renderHomepage() + } + + return "unknown page" +} + +func renderHomepage() string { + // Define empty buffer + var b bytes.Buffer + + b.WriteString("# Welcome to ToDolist\n\n") + + // If no todolists have been created + if todolistTree.Size() == 0 { + b.WriteString("### No todolists available currently!") + return b.String() + } + + // Iterate through AVL tree + todolistTree.Iterate("", "", func(key string, value interface{}) bool { + // cast raw data from tree into Todolist struct + tl := value.(*todolist.TodoList) + + // Add Todolist name + b.WriteString( + ufmt.Sprintf( + "## Todolist #%s: %s\n", + key, // Todolist ID + tl.GetTodolistTitle(), + ), + ) + + // Add Todolist owner + b.WriteString( + ufmt.Sprintf( + "#### Todolist owner : %s\n", + tl.GetTodolistOwner(), + ), + ) + + // List all todos that are currently Todolisted + if todos := tl.GetTasks(); len(todos) > 0 { + b.WriteString( + ufmt.Sprintf("Currently Todo tasks: %d\n\n", len(todos)), + ) + + for index, todo := range todos { + b.WriteString( + ufmt.Sprintf("#%d - %s ", index, todo.Title), + ) + if todo.Done { + b.WriteString( + "☑\n\n", + ) + } else { + b.WriteString( + "☐\n\n", + ) + } + } + } else { + b.WriteString("No tasks in this list currently\n") + } + + b.WriteString("\n") + return false + }) + + return b.String() +} From 49af02464d69b38d0aaeccf83158f667fbb53668 Mon Sep 17 00:00:00 2001 From: MalekLahbib Date: Fri, 22 Mar 2024 15:30:11 +0100 Subject: [PATCH 2/7] feat: Added todolist pkg and realm --- examples/gno.land/r/demo/todolist/todolist.gno | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno index b4ab5df3e82..964d1e81377 100644 --- a/examples/gno.land/r/demo/todolist/todolist.gno +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -2,7 +2,6 @@ package todolist import ( "bytes" - "std" "strconv" "gno.land/p/demo/avl" @@ -13,7 +12,7 @@ import ( // State variables var ( todolistTree *avl.Tree - tlid int + tlid int ) // Constructor @@ -23,18 +22,18 @@ func init() { func NewTodolist(title string) (int, string) { // Get user who sent the transaction - txSender := std.GetOrigCaller() + // txSender := std.GetOrigCaller() tl := todolist.NewTodoList(title) // Update AVL tree with new state - success := todolistTree.Set(strconv.Itoa(tlid), tl) + _ := todolistTree.Set(strconv.Itoa(tlid), tl) tlid++ - return tlid-1, "created successfully" + return tlid - 1, "created successfully" } func AddTask(todolistID int, title string) string { // Get user who sent the transaction - txSender := std.GetOrigCaller() + // txSender := std.GetOrigCaller() // Get Todolist from AVL tree tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) @@ -87,7 +86,7 @@ func RemoveTask(todolistID int, taskID int) string { } // Get the task from the todolist - task, ok := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) + _, ok := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) if !ok { return "Task not found" } From 5896691c9e10ca7ef6b7300a9e83e761abcbe1ab Mon Sep 17 00:00:00 2001 From: MalekLahbib Date: Fri, 22 Mar 2024 15:36:27 +0100 Subject: [PATCH 3/7] feat: Added todolist pkg and realm --- examples/gno.land/r/demo/todolist/todolist.gno | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno index 964d1e81377..c772055f51c 100644 --- a/examples/gno.land/r/demo/todolist/todolist.gno +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -21,20 +21,15 @@ func init() { } func NewTodolist(title string) (int, string) { - // Get user who sent the transaction - // txSender := std.GetOrigCaller() - + // Create new Todolist tl := todolist.NewTodoList(title) // Update AVL tree with new state - _ := todolistTree.Set(strconv.Itoa(tlid), tl) + todolistTree.Set(strconv.Itoa(tlid), tl) tlid++ return tlid - 1, "created successfully" } func AddTask(todolistID int, title string) string { - // Get user who sent the transaction - // txSender := std.GetOrigCaller() - // Get Todolist from AVL tree tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) if !ok { @@ -54,9 +49,6 @@ func AddTask(todolistID int, title string) string { } func ToggleTaskStatus(todolistID int, taskID int) string { - // Get user who sent the transaction - // txSender := std.GetOrigCaller() - // Get Todolist from AVL tree tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) if !ok { From 049a77c584a04e3d03cb7afcac7bb860bc468dc5 Mon Sep 17 00:00:00 2001 From: MalekLahbib Date: Fri, 22 Mar 2024 15:43:02 +0100 Subject: [PATCH 4/7] feat: Added todolist pkg and realm --- examples/gno.land/r/demo/todolist/todolist.gno | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno index c772055f51c..1b309d87aca 100644 --- a/examples/gno.land/r/demo/todolist/todolist.gno +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -56,8 +56,8 @@ func ToggleTaskStatus(todolistID int, taskID int) string { } // Get the task from the todolist - task, ok := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) - if !ok { + task, found := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) + if !found { return "Task not found" } @@ -78,7 +78,7 @@ func RemoveTask(todolistID int, taskID int) string { } // Get the task from the todolist - _, ok := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) + _, ok = tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) if !ok { return "Task not found" } From f0cdb06b6f673fbec880f5119d18b55cec76c26d Mon Sep 17 00:00:00 2001 From: MalekLahbib Date: Fri, 29 Mar 2024 10:50:13 +0100 Subject: [PATCH 5/7] feat(examples): add todolist package & realm --- examples/gno.land/p/demo/todolist/gno.mod | 5 +- .../gno.land/p/demo/todolist/todolist.gno | 9 +- .../p/demo/todolist/todolist_test.gno | 94 ++++++++++++------- examples/gno.land/r/demo/todolist/gno.mod | 1 + .../gno.land/r/demo/todolist/todolist.gno | 9 +- .../r/demo/todolist/todolist_test.gno | 72 ++++++++++++++ 6 files changed, 143 insertions(+), 47 deletions(-) create mode 100644 examples/gno.land/r/demo/todolist/todolist_test.gno diff --git a/examples/gno.land/p/demo/todolist/gno.mod b/examples/gno.land/p/demo/todolist/gno.mod index ffd0ed437e1..a51528b9500 100644 --- a/examples/gno.land/p/demo/todolist/gno.mod +++ b/examples/gno.land/p/demo/todolist/gno.mod @@ -1,6 +1,3 @@ module gno.land/p/demo/todolist -require ( - gno.land/p/demo/avl v0.0.0-latest - gno.land/p/demo/ufmt v0.0.0-latest -) +require gno.land/p/demo/avl v0.0.0-latest diff --git a/examples/gno.land/p/demo/todolist/todolist.gno b/examples/gno.land/p/demo/todolist/todolist.gno index 5bde86896bc..a675344655f 100644 --- a/examples/gno.land/p/demo/todolist/todolist.gno +++ b/examples/gno.land/p/demo/todolist/todolist.gno @@ -5,12 +5,11 @@ import ( "strconv" "gno.land/p/demo/avl" - "gno.land/p/demo/ufmt" ) type TodoList struct { Title string - Tasks avl.Tree + Tasks *avl.Tree Owner std.Address } @@ -22,7 +21,7 @@ type Task struct { func NewTodoList(title string) *TodoList { return &TodoList{ Title: title, - Tasks: avl.Tree{}, + Tasks: avl.NewTree(), Owner: std.GetOrigCaller(), } } @@ -55,10 +54,6 @@ func (tl *TodoList) GetTasks() []*Task { return tasks } -func (tl *TodoList) String() string { - return ufmt.Sprintf("TodoList{Title: %q, Tasks: %v, Owner: %v}", tl.Title, tl.GetTasks(), tl.Owner) -} - func (tl *TodoList) GetTodolistOwner() std.Address { return tl.Owner } diff --git a/examples/gno.land/p/demo/todolist/todolist_test.gno b/examples/gno.land/p/demo/todolist/todolist_test.gno index 3193892bc28..5b2bb361881 100644 --- a/examples/gno.land/p/demo/todolist/todolist_test.gno +++ b/examples/gno.land/p/demo/todolist/todolist_test.gno @@ -1,51 +1,81 @@ package todolist import ( + "std" "testing" - - "gno.land/p/demo/avl" ) -func TestTodolistNew(t *testing.T) { - newtl := &TodoList{ - Title: "new todolist", - Tasks: avl.Tree{}, +func TestNewTodoList(t *testing.T) { + title := "My Todo List" + todoList := NewTodoList(title) + + if todoList.GetTodolistTitle() != title { + t.Errorf("Expected title %q, got %q", title, todoList.GetTodolistTitle()) } - tl := NewTodoList("new todolist") - if tl.Title != newtl.Title { - t.Fatalf("title is not good") + + if len(todoList.GetTasks()) != 0 { + t.Errorf("Expected 0 tasks, got %d", len(todoList.GetTasks())) } - if tl.Tasks != newtl.Tasks { - t.Fatalf("tasks not good") + + if todoList.GetTodolistOwner() != std.GetOrigCaller() { + t.Errorf("Expected owner %v, got %v", std.GetOrigCaller(), todoList.GetTodolistOwner()) } +} + +func TestNewTask(t *testing.T) { + title := "My Task" + task := NewTask(title) - // test new task - newtask := &Task{ - Title: "new task", - Done: false, + if task.Title != title { + t.Errorf("Expected title %q, got %q", title, task.Title) } - nt := NewTask("new task") - if nt.Title != newtask.Title { - t.Fatalf("title is not good") + + if task.Done { + t.Errorf("Expected task to be not done, but it is done") } - if nt.Done != newtask.Done { - t.Fatalf("done is not good") +} + +func TestAddTask(t *testing.T) { + todoList := NewTodoList("My Todo List") + task := NewTask("My Task") + + todoList.AddTask(1, task) + + tasks := todoList.GetTasks() + if len(tasks) != 1 { + t.Errorf("Expected 1 task, got %d", len(tasks)) } - // test add a task - id := tl.Tasks.Size() - tl.AddTask(id, nt) - if tl.Tasks.Size() != 1 { - t.Fatalf("task not added") + if tasks[0] != task { + t.Errorf("Expected task %v, got %v", task, tasks[0]) + } +} + +func TestToggleTaskStatus(t *testing.T) { + task := NewTask("My Task") + + ToggleTaskStatus(task) + + if !task.Done { + t.Errorf("Expected task to be done, but it is not done") } - // test ToggleTaskStatus - task, exists := tl.Tasks.Get("0") - if !exists { - t.Fatalf("task not found") + ToggleTaskStatus(task) + + if task.Done { + t.Errorf("Expected task to be not done, but it is done") } - ToggleTaskStatus(task.(*Task)) - if task.(*Task).Done != true { - t.Fatalf("task not toggled") +} + +func TestRemoveTask(t *testing.T) { + todoList := NewTodoList("My Todo List") + task := NewTask("My Task") + todoList.AddTask(1, task) + + todoList.RemoveTask("1") + + tasks := todoList.GetTasks() + if len(tasks) != 0 { + t.Errorf("Expected 0 tasks, got %d", len(tasks)) } } diff --git a/examples/gno.land/r/demo/todolist/gno.mod b/examples/gno.land/r/demo/todolist/gno.mod index e23286a1402..563bab74ad5 100644 --- a/examples/gno.land/r/demo/todolist/gno.mod +++ b/examples/gno.land/r/demo/todolist/gno.mod @@ -2,6 +2,7 @@ module gno.land/r/demo/todolist require ( gno.land/p/demo/avl v0.0.0-latest + gno.land/p/demo/seqid v0.0.0-latest gno.land/p/demo/todolist v0.0.0-latest gno.land/p/demo/ufmt v0.0.0-latest ) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno index 1b309d87aca..97e8a835437 100644 --- a/examples/gno.land/r/demo/todolist/todolist.gno +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -5,6 +5,7 @@ import ( "strconv" "gno.land/p/demo/avl" + "gno.land/p/demo/seqid" "gno.land/p/demo/todolist" "gno.land/p/demo/ufmt" ) @@ -12,7 +13,7 @@ import ( // State variables var ( todolistTree *avl.Tree - tlid int + tlid seqid.ID ) // Constructor @@ -24,8 +25,8 @@ func NewTodolist(title string) (int, string) { // Create new Todolist tl := todolist.NewTodoList(title) // Update AVL tree with new state - todolistTree.Set(strconv.Itoa(tlid), tl) - tlid++ + todolistTree.Set(tlid.String(), tl) + tlid.Next() return tlid - 1, "created successfully" } @@ -134,7 +135,7 @@ func renderHomepage() string { b.WriteString( ufmt.Sprintf( "## Todolist #%s: %s\n", - key, // Todolist ID + key+1, // Todolist ID tl.GetTodolistTitle(), ), ) diff --git a/examples/gno.land/r/demo/todolist/todolist_test.gno b/examples/gno.land/r/demo/todolist/todolist_test.gno new file mode 100644 index 00000000000..bdba05b530c --- /dev/null +++ b/examples/gno.land/r/demo/todolist/todolist_test.gno @@ -0,0 +1,72 @@ +package todolist + +import ( + "testing" +) + +func TestNewTodoList(t *testing.T) { + title := "My Todo List" + todoList := NewTodoList(title) + + if todoList.GetTodolistTitle() != title { + t.Errorf("Expected title to be %s, but got %s", title, todoList.GetTodolistTitle()) + } + + if todoList.GetTodolistOwner() != std.GetOrigCaller() { + t.Errorf("Expected owner to be %s, but got %s", std.GetOrigCaller(), todoList.GetTodolistOwner()) + } + + if len(todoList.GetTasks()) != 0 { + t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(todoList.GetTasks())) + } +} + +func TestAddTask(t *testing.T) { + todoList := NewTodoList("My Todo List") + task := NewTask("Task 1") + + todoList.AddTask(1, task) + + tasks := todoList.GetTasks() + if len(tasks) != 1 { + t.Errorf("Expected 1 task in the todo list, but got %d tasks", len(tasks)) + } + + if tasks[0].Title != "Task 1" { + t.Errorf("Expected task title to be 'Task 1', but got '%s'", tasks[0].Title) + } + + if tasks[0].Done { + t.Errorf("Expected task to be not done, but it is marked as done") + } +} + +func TestToggleTaskStatus(t *testing.T) { + task := NewTask("Task 1") + + ToggleTaskStatus(task) + + if !task.Done { + t.Errorf("Expected task to be done, but it is not marked as done") + } + + ToggleTaskStatus(task) + + if task.Done { + t.Errorf("Expected task to be not done, but it is marked as done") + } +} + +func TestRemoveTask(t *testing.T) { + todoList := NewTodoList("My Todo List") + task := NewTask("Task 1") + + todoList.AddTask(1, task) + + todoList.RemoveTask("1") + + tasks := todoList.GetTasks() + if len(tasks) != 0 { + t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(tasks)) + } +} From a48d20f83d7e4f61a88da340c9b6cf84b500cdf2 Mon Sep 17 00:00:00 2001 From: MalekLahbib Date: Fri, 29 Mar 2024 11:00:56 +0100 Subject: [PATCH 6/7] feat(examples): add todolist package & realm --- .../gno.land/r/demo/todolist/todolist.gno | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno index 97e8a835437..5cf2a3a3713 100644 --- a/examples/gno.land/r/demo/todolist/todolist.gno +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -25,16 +25,16 @@ func NewTodolist(title string) (int, string) { // Create new Todolist tl := todolist.NewTodoList(title) // Update AVL tree with new state - todolistTree.Set(tlid.String(), tl) tlid.Next() - return tlid - 1, "created successfully" + todolistTree.Set(strconv.Itoa(int(tlid)), tl) + return int(tlid), "created successfully" } func AddTask(todolistID int, title string) string { // Get Todolist from AVL tree tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) if !ok { - return "Todolist not found" + panic("Todolist not found") } // get the number of tasks in the todolist @@ -53,13 +53,13 @@ func ToggleTaskStatus(todolistID int, taskID int) string { // Get Todolist from AVL tree tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) if !ok { - return "Todolist not found" + panic("Todolist not found") } // Get the task from the todolist task, found := tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) if !found { - return "Task not found" + panic("Task not found") } // Change the status of the task @@ -69,19 +69,16 @@ func ToggleTaskStatus(todolistID int, taskID int) string { } func RemoveTask(todolistID int, taskID int) string { - // Get user who sent the transaction - // txSender := std.GetOrigCaller() - // Get Todolist from AVL tree tl, ok := todolistTree.Get(strconv.Itoa(todolistID)) if !ok { - return "Todolist not found" + panic("Todolist not found") } // Get the task from the todolist _, ok = tl.(*todolist.TodoList).Tasks.Get(strconv.Itoa(taskID)) if !ok { - return "Task not found" + panic("Task not found") } // Change the status of the task @@ -91,13 +88,10 @@ func RemoveTask(todolistID int, taskID int) string { } func RemoveTodoList(todolistID int) string { - // Get user who sent the transaction - // txSender := std.GetOrigCaller() - // Get Todolist from AVL tree _, ok := todolistTree.Get(strconv.Itoa(todolistID)) if !ok { - return "Todolist not found" + panic("Todolist not found") } // Remove the todolist @@ -135,7 +129,7 @@ func renderHomepage() string { b.WriteString( ufmt.Sprintf( "## Todolist #%s: %s\n", - key+1, // Todolist ID + key, // Todolist ID tl.GetTodolistTitle(), ), ) @@ -158,15 +152,17 @@ func renderHomepage() string { b.WriteString( ufmt.Sprintf("#%d - %s ", index, todo.Title), ) + // displays a checked box if task is marked as done, an empty box if not if todo.Done { b.WriteString( "☑\n\n", ) - } else { - b.WriteString( - "☐\n\n", - ) + continue } + + b.WriteString( + "☐\n\n", + ) } } else { b.WriteString("No tasks in this list currently\n") From a1435e05ab51d4697193ba8b935fc7e12b396e32 Mon Sep 17 00:00:00 2001 From: Malek Lahbib <111009238+MalekLahbib@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:00:44 +0200 Subject: [PATCH 7/7] modified realm pkg name and todolist realm testfile --- .../gno.land/r/demo/todolist/todolist.gno | 4 +- .../r/demo/todolist/todolist_test.gno | 66 +++++++++++-------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/examples/gno.land/r/demo/todolist/todolist.gno b/examples/gno.land/r/demo/todolist/todolist.gno index 5cf2a3a3713..5790aac630a 100644 --- a/examples/gno.land/r/demo/todolist/todolist.gno +++ b/examples/gno.land/r/demo/todolist/todolist.gno @@ -1,4 +1,4 @@ -package todolist +package todolistrealm import ( "bytes" @@ -21,7 +21,7 @@ func init() { todolistTree = avl.NewTree() } -func NewTodolist(title string) (int, string) { +func NewTodoList(title string) (int, string) { // Create new Todolist tl := todolist.NewTodoList(title) // Update AVL tree with new state diff --git a/examples/gno.land/r/demo/todolist/todolist_test.gno b/examples/gno.land/r/demo/todolist/todolist_test.gno index bdba05b530c..db55110851e 100644 --- a/examples/gno.land/r/demo/todolist/todolist_test.gno +++ b/examples/gno.land/r/demo/todolist/todolist_test.gno @@ -1,33 +1,45 @@ -package todolist +package todolistrealm import ( + "std" + "strconv" "testing" + + "gno.land/p/demo/todolist" +) + +var ( + node interface{} + tdl *todolist.TodoList ) func TestNewTodoList(t *testing.T) { title := "My Todo List" - todoList := NewTodoList(title) - - if todoList.GetTodolistTitle() != title { - t.Errorf("Expected title to be %s, but got %s", title, todoList.GetTodolistTitle()) + tlid, _ := NewTodoList(title) + if tlid != 1 { + t.Errorf("Expected tlid to be 1, but got %d", tlid) } - if todoList.GetTodolistOwner() != std.GetOrigCaller() { - t.Errorf("Expected owner to be %s, but got %s", std.GetOrigCaller(), todoList.GetTodolistOwner()) - } + // get the todolist node from the tree + node, _ = todolistTree.Get(strconv.Itoa(tlid)) + // convert the node to a TodoList struct + tdl = node.(*todolist.TodoList) - if len(todoList.GetTasks()) != 0 { - t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(todoList.GetTasks())) + if tdl.Title != title { + t.Errorf("Expected title to be %s, but got %s", title, tdl.Title) + } + if tdl.Owner != std.GetOrigCaller() { + t.Errorf("Expected owner to be %s, but got %s", std.GetOrigCaller(), tdl.Owner) + } + if len(tdl.GetTasks()) != 0 { + t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(tdl.GetTasks())) } } func TestAddTask(t *testing.T) { - todoList := NewTodoList("My Todo List") - task := NewTask("Task 1") - - todoList.AddTask(1, task) + AddTask(1, "Task 1") - tasks := todoList.GetTasks() + tasks := tdl.GetTasks() if len(tasks) != 1 { t.Errorf("Expected 1 task in the todo list, but got %d tasks", len(tasks)) } @@ -42,15 +54,14 @@ func TestAddTask(t *testing.T) { } func TestToggleTaskStatus(t *testing.T) { - task := NewTask("Task 1") - - ToggleTaskStatus(task) + ToggleTaskStatus(1, 0) + task := tdl.GetTasks()[0] if !task.Done { t.Errorf("Expected task to be done, but it is not marked as done") } - ToggleTaskStatus(task) + ToggleTaskStatus(1, 0) if task.Done { t.Errorf("Expected task to be not done, but it is marked as done") @@ -58,15 +69,18 @@ func TestToggleTaskStatus(t *testing.T) { } func TestRemoveTask(t *testing.T) { - todoList := NewTodoList("My Todo List") - task := NewTask("Task 1") - - todoList.AddTask(1, task) + RemoveTask(1, 0) + tasks := tdl.GetTasks() - todoList.RemoveTask("1") - - tasks := todoList.GetTasks() if len(tasks) != 0 { t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(tasks)) } } + +func TestRemoveTodoList(t *testing.T) { + RemoveTodoList(1) + + if todolistTree.Size() != 0 { + t.Errorf("Expected no tasks in the todo list, but got %d tasks", todolistTree.Size()) + } +}