test marshalling a just-a-string todo returns just-a-string
This commit is contained in:
@@ -2,6 +2,7 @@ package pttodo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user