test marshalling a just-a-string todo returns just-a-string
parent
b11d76a8f6
commit
6c83f4197d
|
|
@ -16,6 +16,14 @@ func (todo Todo) Triggered() bool {
|
||||||
return err == nil && time.Now().After(next)
|
return err == nil && time.Now().After(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (todo Todo) MarshalYAML() (interface{}, error) {
|
||||||
|
if todo == (Todo{Todo: todo.Todo}) {
|
||||||
|
return todo.Todo, nil
|
||||||
|
}
|
||||||
|
type Alt Todo
|
||||||
|
return (Alt)(todo), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
func (todo *Todo) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
*todo = Todo{}
|
*todo = Todo{}
|
||||||
if err := unmarshal(&todo.Todo); err == nil {
|
if err := unmarshal(&todo.Todo); err == nil {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package pttodo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
yaml "gopkg.in/yaml.v2"
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
|
@ -82,3 +83,44 @@ todo:
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarshalTodo(t *testing.T) {
|
||||||
|
t.Run(`empty should return ""`, func(t *testing.T) {
|
||||||
|
var todo Todo
|
||||||
|
if b, err := yaml.Marshal(Todo{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if s := string(b); strings.TrimSpace(s) != `""` {
|
||||||
|
t.Fatalf("%q", s)
|
||||||
|
} else if err := yaml.Unmarshal(b, &todo); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if todo.Todo != "" {
|
||||||
|
t.Fatal(todo.Todo)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run(`just "a" should return a`, func(t *testing.T) {
|
||||||
|
var todo Todo
|
||||||
|
if b, err := yaml.Marshal(Todo{Todo: "a"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if s := string(b); strings.TrimSpace(s) != `a` {
|
||||||
|
t.Fatalf("%q", s)
|
||||||
|
} else if err := yaml.Unmarshal(b, &todo); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if todo.Todo != "a" {
|
||||||
|
t.Fatal(todo.Todo)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run(`struct should return struct`, func(t *testing.T) {
|
||||||
|
var todo Todo
|
||||||
|
if b, err := yaml.Marshal(Todo{Todo: "a", Detail: "b"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if err := yaml.Unmarshal(b, &todo); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if todo.Todo != "a" {
|
||||||
|
t.Fatal(todo.Todo)
|
||||||
|
} else if todo.Detail != "b" {
|
||||||
|
t.Fatal(todo.Detail)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue