🧪 Test TextUnmarshaler implementation for Entry
This commit is contained in:
parent
e8fb298ea5
commit
5b60305de7
2 changed files with 83 additions and 5 deletions
|
|
@ -70,3 +70,7 @@ func (e Entry) MarshalText() ([]byte, error) {
|
||||||
|
|
||||||
return buff.Bytes(), nil
|
return buff.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Entry) UnmarshalText(in []byte) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding"
|
"encoding"
|
||||||
//"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -12,8 +12,9 @@ import (
|
||||||
|
|
||||||
// Type assertions
|
// Type assertions
|
||||||
var _ encoding.TextMarshaler = Entry{}
|
var _ encoding.TextMarshaler = Entry{}
|
||||||
|
var _ encoding.TextUnmarshaler = new(Entry)
|
||||||
|
|
||||||
func TestEntry(t *testing.T) {
|
func TestEntryMarshal(t *testing.T) {
|
||||||
when := time.Now()
|
when := time.Now()
|
||||||
whens := when.Format(DateFormat)
|
whens := when.Format(DateFormat)
|
||||||
simple := []Meta{}
|
simple := []Meta{}
|
||||||
|
|
@ -62,12 +63,11 @@ func TestEntry(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, getEntryTestRunner(tt.title, tt.date, tt.fields, tt.first, tt.lines, tt.err))
|
t.Run(tt.name, getEntryMarshalTestRunner(tt.title, tt.date, tt.fields, tt.first, tt.lines, tt.err))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEntryTestRunner(title string, date time.Time, fields []Meta, first string, lines []string, err error) func(*testing.T) {
|
func getEntryMarshalTestRunner(title string, date time.Time, fields []Meta, first string, lines []string, err error) func(*testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
en := Entry{title, date, fields}
|
en := Entry{title, date, fields}
|
||||||
o, er := en.MarshalText()
|
o, er := en.MarshalText()
|
||||||
|
|
@ -87,3 +87,77 @@ func getEntryTestRunner(title string, date time.Time, fields []Meta, first strin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEntryUnmarshal(t *testing.T) {
|
||||||
|
when := time.Now()
|
||||||
|
whens := when.Format(DateFormat)
|
||||||
|
simple := []Meta{}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
in string
|
||||||
|
title string
|
||||||
|
date time.Time
|
||||||
|
fields []Meta
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{"one-line", "@begin " + whens + " - A Title @end", "A Title", when, simple, nil},
|
||||||
|
{"multi-title", "@begin " + whens + " - A Title\nwith break @end", "A Title\nwith break", when, simple, nil},
|
||||||
|
{"no-title", "@begin " + whens + " - @end", "", when, simple, errors.New("Missing title")},
|
||||||
|
{"no-date", "@begin - A Title @end", "A Title", when, simple, errors.New("Missing date")},
|
||||||
|
{"one-field", "@begin " + whens + " - A Title\n@age 41 @end", "A Title", when, []Meta{{"age", 41}}, nil},
|
||||||
|
{
|
||||||
|
"two-fields",
|
||||||
|
"@begin " + whens + " - A Title\n@age 41\n@cool true @end",
|
||||||
|
"A Title",
|
||||||
|
when,
|
||||||
|
[]Meta{{"age", 41}, {"cool", true}},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"obj-field",
|
||||||
|
"@begin " + whens + " - A Title\n" + `@me {"name":"Dan","coder":true} @end`,
|
||||||
|
"A Title",
|
||||||
|
when,
|
||||||
|
[]Meta{{"me", json.RawMessage(`{"name":"Dan","coder":true}`)}},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
/* uncomment when implemented
|
||||||
|
{
|
||||||
|
"json-field",
|
||||||
|
"@begin " + whens + " - Some Guy\n" + `@json {"name":"Dan","coder":true} @end`,
|
||||||
|
"A Title",
|
||||||
|
when,
|
||||||
|
[]Meta{{"name", "Dan"}, {"coder", true}},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, getEntryUnmarshalTestRunner(tt.in, tt.title, tt.date, tt.fields, tt.err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEntryUnmarshalTestRunner(in string, title string, date time.Time, fields []Meta, err error) func(*testing.T) {
|
||||||
|
return func(t *testing.T) {
|
||||||
|
e := &Entry{}
|
||||||
|
er := e.UnmarshalText([]byte(in))
|
||||||
|
assert.Equal(t, err, er)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, title, e.Title)
|
||||||
|
assert.WithinRange(t, e.Date, date.Add(-time.Second), date.Add(time.Second))
|
||||||
|
for _, f := range fields {
|
||||||
|
got := false
|
||||||
|
for _, m := range e.Fields {
|
||||||
|
if m == f {
|
||||||
|
got = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.Truef(t, got, "Couldn't find field %+v", f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue