Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
Further changes and new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-alvarenga committed Sep 10, 2022
1 parent 89a861f commit d6a990e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 18 deletions.
3 changes: 0 additions & 3 deletions cli/cliopts/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import (
)

func EditOption() bool {
ListOption("")
err := ngops.Edit(getEditInfo())

if err != nil {
styles.ShowAsError(ng.Config.Colors, "Unable to edit task", err.Error())
}

ListOption("")

return false
}

Expand Down
4 changes: 1 addition & 3 deletions cli/cliopts/help.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cliopts

import (
"fmt"

"github.com/leo-alvarenga/to-go/cli/clihelper"
"github.com/leo-alvarenga/to-go/ng"
"github.com/leo-alvarenga/to-go/shared/styles"
Expand All @@ -20,7 +18,7 @@ func HelpMessage() bool {
}

func InvalidOptionAlert(input string) bool {
fmt.Printf("\"%s\" is not a valid option!\n\n", input)
styles.ShowAsError(ng.Config.Colors, "\""+input+"\" is not a valid option", "")

return HelpMessage()
}
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main

import (
"fmt"
"os"

"github.com/leo-alvarenga/to-go/cli"
)

func main() {
fmt.Println("ok")
cli.Entrypoint(os.Args)
}
2 changes: 1 addition & 1 deletion ng/ngops/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Edit(n, old task.Task) error {

err := ng.TaskList.Edit(old, n)
if err != nil {
return nil
return err
}

return writeToYamlWrapper()
Expand Down
2 changes: 2 additions & 0 deletions ng/storage/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ func RetrieveTasksFromYaml(taskFilenames [3]string, list *task.TaskList) error {
)
}

list.SyncNextId()

return nil
}
8 changes: 3 additions & 5 deletions shared/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ type ColorScheme struct {
}

type ConfigValue struct {
UseUnicode bool `yaml:"useUnicode"`
Storage string `yaml:"storage"`
TasksOlderThan int `yaml:"tasksOlderThan"`
Colors ColorScheme `yaml:"colors"`
UseUnicode bool `yaml:"useUnicode"`
Storage string `yaml:"storage"`
Colors ColorScheme `yaml:"colors"`
}

func (cfg *ConfigValue) New() {
cfg.UseUnicode = true
cfg.Storage = "sqlite"
cfg.TasksOlderThan = 10
cfg.Colors = ColorScheme{
Priority: PriorityColor{
High: "red",
Expand Down
17 changes: 13 additions & 4 deletions shared/task/task_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ type TaskList struct {
Low *[]Task
Medium *[]Task
High *[]Task
nextId int
}

/* Initializes a newly allocated TaskList */
func (t *TaskList) New() {
t.Low = new([]Task)
t.Medium = new([]Task)
t.High = new([]Task)

t.nextId = 0
}

/* Updates the value meant to be the next task id */
func (t *TaskList) SyncNextId() {
t.nextId = (len(*t.High) + len(*t.Medium) + len(*t.Low)) - 3
}

/* Empty one of the slices, based on the 'priority' parameter */
Expand Down Expand Up @@ -84,7 +92,6 @@ func (t *TaskList) GetTaskByTitle(title string) (Task, error) {
}

/* Finds the index of the 'ts' Task; Returns an index of -1 and an error if it could not be found */
// todo -> make so ids aare unique, even in yaml mode
func (t *TaskList) FindIndex(ts Task) (int, error) {
if !IsThisAPriority(ts.Priority) {
return -1, errors.New("Task does not have a valid priority.")
Expand Down Expand Up @@ -122,16 +129,18 @@ func (t *TaskList) Add(ts Task) error {

switch ts.Priority {
case high:
ts.Id = len((*t.High))
ts.Id = t.nextId
(*t.High) = append((*t.High), ts)
case medium:
ts.Id = len((*t.Medium))
ts.Id = t.nextId
(*t.Medium) = append((*t.Medium), ts)
case low:
ts.Id = len((*t.Low))
ts.Id = t.nextId
(*t.Low) = append((*t.Low), ts)
}

t.nextId++

return nil
}

Expand Down
74 changes: 74 additions & 0 deletions shared/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,77 @@ func TestIsThisAPriority(t *testing.T) {
t.Errorf("With argument '%s':\nExpected 'true'; Got 'false'", arg)
}
}

func TestTaskListOps(t *testing.T) {
task := Task{
Title: "ok",
Description: "",
Priority: "",
}

list := new(TaskList)
list.New()

t.Log("Adding task with no priority...")
err := list.Add(task)

if err == nil {
t.Error("Expected error when adding task with no priority; Got nil")
}

task.Priority = low
t.Log("Adding task with priority...")
err = list.Add(task)

if err != nil {
t.Errorf("Expected no error; Got '%s'\n", err.Error())
}

task.Status = done
t.Log("Updating task status...")
err = list.Update(task)

if err != nil {
t.Errorf("Expected no error; Got '%s'\n", err.Error())
}

if (*list.Low)[0].Status != done {
t.Errorf("Expected status od 'done' after update; Got '%s'\n", (*list.Low)[0].Status)
}

t.Log("Removing task...")
err = list.Remove(task.Title)

if err != nil {
t.Errorf("Expected no error; Got '%s'\n", err.Error())
}

if len(*list.Low) > 0 {
t.Errorf("Expected length of 0 after removal; Got '%d'\n", len(*list.Low))
}

task.Priority = medium
t.Log("Adding task with priority again...")
err = list.Add(task)

if err != nil {
t.Errorf("Expected no error; Got '%s'\n", err.Error())
}

old := task
task.Title = "a"
task.Priority = high

t.Log("Editing task priority and title...")
err = list.Edit(old, task)

if err != nil {
t.Errorf("Expected no error; Got '%s'\n", err.Error())
}

if len(*list.Medium) > len(*list.High) {
t.Errorf("Expected to change the task priority; Got '%s' instead", (*list.Medium)[0].Priority)
} else if (*list.High)[0].Title != task.Title {
t.Errorf("Expected to change the task title; Expected '%s'; Got '%s' instead", task.Title, (*list.High)[0].Title)
}
}

0 comments on commit d6a990e

Please sign in to comment.