forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'teritori/master' into daosdk
- Loading branch information
Showing
13 changed files
with
480 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module gno.land/p/demo/todolist | ||
|
||
require gno.land/p/demo/avl v0.0.0-latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package todolist | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
|
||
"gno.land/p/demo/avl" | ||
) | ||
|
||
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.NewTree(), | ||
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) GetTodolistOwner() std.Address { | ||
return tl.Owner | ||
} | ||
|
||
func (tl *TodoList) GetTodolistTitle() string { | ||
return tl.Title | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package todolist | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
) | ||
|
||
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()) | ||
} | ||
|
||
if len(todoList.GetTasks()) != 0 { | ||
t.Errorf("Expected 0 tasks, got %d", len(todoList.GetTasks())) | ||
} | ||
|
||
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) | ||
|
||
if task.Title != title { | ||
t.Errorf("Expected title %q, got %q", title, task.Title) | ||
} | ||
|
||
if task.Done { | ||
t.Errorf("Expected task to be not done, but it is done") | ||
} | ||
} | ||
|
||
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)) | ||
} | ||
|
||
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") | ||
} | ||
|
||
ToggleTaskStatus(task) | ||
|
||
if task.Done { | ||
t.Errorf("Expected task to be not done, but it is done") | ||
} | ||
} | ||
|
||
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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 | ||
) |
Oops, something went wrong.