-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties_test.go
More file actions
132 lines (103 loc) · 3.82 KB
/
properties_test.go
File metadata and controls
132 lines (103 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package properties
import (
"context"
"github.com/araddon/dateparse"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
const validFrontMatter = `
---
description: test description
number: 221
flag: true
date: 2006-01-02T15:04:05Z07:00
---
test body
`
const noFrontMatter = `test body without front matter`
const invalidFrontMatter1 = `
---
description: test description
test body
`
type PropertiesSuite struct {
suite.Suite
factory Factory
}
func (suite *PropertiesSuite) SetupSuite() {
suite.factory = ThePropertiesFactory
}
func (suite *PropertiesSuite) TearDownSuite() {
}
func (suite *PropertiesSuite) TestMutableProperties() {
ctx := context.Background()
props := suite.factory.EmptyMutable(ctx)
suite.NotNil(props, "Ensure initialization")
suite.Equal(uint(0), props.Size(ctx), "Should be zero")
prop, ok, err := props.Add(ctx, "custom", suite)
suite.False(ok, "Should not have been created")
suite.NotNil(err, "Should have gotten an error")
prop, ok, err = props.Add(ctx, "text", "Test text")
suite.True(ok, "Should have been created")
suite.IsType(&DefaultTextProperty{}, prop, "Should have been created")
prop, ok, err = props.Add(ctx, "number", 100)
prop, ok, err = props.Add(ctx, "flag", true)
prop, ok, err = props.Add(ctx, "date", time.Now())
prop, ok, err = props.Add(ctx, "textList", []string{"one", "two"})
}
func (suite *PropertiesSuite) TestNoFrontMatter() {
ctx := context.Background()
bodyBytes, props, count, err := suite.factory.MutableFromFrontMatter(ctx, []byte(noFrontMatter), nil)
body := string(bodyBytes)
suite.Nil(err, "Shouldn't have any errors")
suite.Nil(props, "Should not be initialized, there is no front matter")
suite.Equal(uint(0), count, "Should not have any front matter")
suite.Equal(body, noFrontMatter)
}
func (suite *PropertiesSuite) TestValidFrontMatter() {
ctx := context.Background()
bodyBytes, props, count, err := suite.factory.MutableFromFrontMatter(ctx, []byte(validFrontMatter), nil)
body := string(bodyBytes)
suite.Nil(err, "Shouldn't have any errors")
suite.NotNil(props, "Should be initialized")
suite.Equal(uint(4), count, "Should have four items")
suite.Equal(body, "test body")
prop, _ := props.Named(ctx, "description")
suite.Equal("test description", prop.AnyValue(ctx))
prop, _ = props.Named(ctx, "number")
suite.Equal(int64(221), prop.AnyValue(ctx))
prop, _ = props.Named(ctx, "flag")
suite.Equal(true, prop.AnyValue(ctx))
prop, _ = props.Named(ctx, "date")
suite.Equal("2006-01-02T15:04:05Z07:00", prop.AnyValue(ctx))
}
func (suite *PropertiesSuite) TestValidSmartParsedFrontMatter() {
ctx := context.Background()
bodyBytes, props, count, err := suite.factory.MutableFromFrontMatter(ctx, []byte(validFrontMatter), nil)
body := string(bodyBytes)
suite.Nil(err, "Shouldn't have any errors")
suite.NotNil(props, "Should be initialized")
suite.Equal(uint(4), count, "Should have four items")
suite.Equal(body, "test body")
prop, _ := props.Named(ctx, "description")
suite.Equal("test description", prop.AnyValue(ctx))
prop, _ = props.Named(ctx, "number")
suite.Equal(int64(221), prop.AnyValue(ctx))
prop, _ = props.Named(ctx, "flag")
suite.Equal(true, prop.AnyValue(ctx))
prop, _ = props.Named(ctx, "date")
date, _ := dateparse.ParseAny("2006-01-02T15:04:05Z07:00")
suite.Equal(date, prop.AnyValue(ctx))
}
func (suite *PropertiesSuite) TestInvalidFrontMatter() {
ctx := context.Background()
bodyBytes, props, count, err := suite.factory.MutableFromFrontMatter(ctx, []byte(invalidFrontMatter1), nil)
suite.EqualError(err, "Unexplained front matter parser error; insideFrontMatter: true, yamlStartIndex: 5, yamlEndIndex: 0")
suite.Nil(props, "Should not be initialized")
suite.Equal(uint(0), count, "Should not have any front matter")
suite.Nil(bodyBytes, "Body should be empty")
}
func TestSuite(t *testing.T) {
suite.Run(t, new(PropertiesSuite))
}