89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding"
|
|
//"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Type assertions
|
|
var _ encoding.TextMarshaler = Entry{}
|
|
|
|
func TestEntry(t *testing.T) {
|
|
when := time.Now()
|
|
whens := when.Format(DateFormat)
|
|
simple := []Meta{}
|
|
nolines := []string{}
|
|
tests := []struct {
|
|
name string
|
|
title string
|
|
date time.Time
|
|
fields []Meta
|
|
first string
|
|
lines []string
|
|
err error
|
|
}{
|
|
{"no-title", "", when, simple, "", nolines, errors.New("Empty title")},
|
|
{"zero-date", "Empty title", time.Time{}, simple, "", nolines, errors.New("Empty date")},
|
|
{"one-line", "A Title", when, simple, "@begin " + whens + " - A Title @end", nolines, nil},
|
|
{
|
|
"one-field",
|
|
"Title 2",
|
|
when,
|
|
[]Meta{{"age", 41}},
|
|
"@begin " + whens + " - Title 2",
|
|
[]string{"@age 41 @end"},
|
|
nil,
|
|
},
|
|
{
|
|
"three-fields",
|
|
"Title 3",
|
|
when,
|
|
[]Meta{{"age", 41}, {"cool", true}, {"name", "Jim"}},
|
|
"@begin " + whens + " - Title 3",
|
|
[]string{"@age 41", "@cool true", "@name Jim"},
|
|
nil,
|
|
},
|
|
/* uncomment when implemented
|
|
{
|
|
"json-field",
|
|
"Title J",
|
|
when,
|
|
[]Meta{{"json", json.RawMessage(`{"age": 41, "cool": true, "name": "Jim"}`)}},
|
|
"@begin " + whens + " - Title J",
|
|
[]string{"@age 41", "@cool true", "@name Jim"},
|
|
nil,
|
|
},
|
|
*/
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, getEntryTestRunner(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) {
|
|
return func(t *testing.T) {
|
|
en := Entry{title, date, fields}
|
|
o, er := en.MarshalText()
|
|
assert.Equal(t, err, er)
|
|
if first == "" {
|
|
return
|
|
}
|
|
if len(lines) == 0 {
|
|
assert.Equal(t, first, string(o))
|
|
return
|
|
}
|
|
|
|
os := string(o)
|
|
assert.Regexp(t, "^"+first, os)
|
|
for _, line := range lines {
|
|
assert.Regexp(t, "(?m)^"+line, os)
|
|
}
|
|
}
|
|
}
|