github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/todolist/todolist_test.gno (about)

     1  package todolistrealm
     2  
     3  import (
     4  	"std"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"gno.land/p/demo/todolist"
     9  )
    10  
    11  var (
    12  	node interface{}
    13  	tdl  *todolist.TodoList
    14  )
    15  
    16  func TestNewTodoList(t *testing.T) {
    17  	title := "My Todo List"
    18  	tlid, _ := NewTodoList(title)
    19  	if tlid != 1 {
    20  		t.Errorf("Expected tlid to be 1, but got %d", tlid)
    21  	}
    22  
    23  	// get the todolist node from the tree
    24  	node, _ = todolistTree.Get(strconv.Itoa(tlid))
    25  	// convert the node to a TodoList struct
    26  	tdl = node.(*todolist.TodoList)
    27  
    28  	if tdl.Title != title {
    29  		t.Errorf("Expected title to be %s, but got %s", title, tdl.Title)
    30  	}
    31  	if tdl.Owner != std.GetOrigCaller() {
    32  		t.Errorf("Expected owner to be %s, but got %s", std.GetOrigCaller(), tdl.Owner)
    33  	}
    34  	if len(tdl.GetTasks()) != 0 {
    35  		t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(tdl.GetTasks()))
    36  	}
    37  }
    38  
    39  func TestAddTask(t *testing.T) {
    40  	AddTask(1, "Task 1")
    41  
    42  	tasks := tdl.GetTasks()
    43  	if len(tasks) != 1 {
    44  		t.Errorf("Expected 1 task in the todo list, but got %d tasks", len(tasks))
    45  	}
    46  
    47  	if tasks[0].Title != "Task 1" {
    48  		t.Errorf("Expected task title to be 'Task 1', but got '%s'", tasks[0].Title)
    49  	}
    50  
    51  	if tasks[0].Done {
    52  		t.Errorf("Expected task to be not done, but it is marked as done")
    53  	}
    54  }
    55  
    56  func TestToggleTaskStatus(t *testing.T) {
    57  	ToggleTaskStatus(1, 0)
    58  	task := tdl.GetTasks()[0]
    59  
    60  	if !task.Done {
    61  		t.Errorf("Expected task to be done, but it is not marked as done")
    62  	}
    63  
    64  	ToggleTaskStatus(1, 0)
    65  
    66  	if task.Done {
    67  		t.Errorf("Expected task to be not done, but it is marked as done")
    68  	}
    69  }
    70  
    71  func TestRemoveTask(t *testing.T) {
    72  	RemoveTask(1, 0)
    73  	tasks := tdl.GetTasks()
    74  
    75  	if len(tasks) != 0 {
    76  		t.Errorf("Expected no tasks in the todo list, but got %d tasks", len(tasks))
    77  	}
    78  }
    79  
    80  func TestRemoveTodoList(t *testing.T) {
    81  	RemoveTodoList(1)
    82  
    83  	if todolistTree.Size() != 0 {
    84  		t.Errorf("Expected no tasks in the todo list, but got %d tasks", todolistTree.Size())
    85  	}
    86  }