/
bool_string_test.go
87 lines (77 loc) · 1.8 KB
/
bool_string_test.go
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
package libyaml_test
import (
"strings"
"testing"
"github.com/replicatedhq/libyaml"
validator "gopkg.in/go-playground/validator.v8"
"gopkg.in/yaml.v2"
)
func TestBoolStringMarshal(t *testing.T) {
type S struct {
Bool libyaml.BoolString `yaml:"is_true"`
}
s := S{Bool: libyaml.BoolString("true")}
b, err := yaml.Marshal(s)
if err != nil {
t.Fatal(err)
}
if string(b) != "is_true: true\n" {
t.Errorf(`expecting "is_true: true\n", got %q`, b)
}
s = S{Bool: libyaml.BoolString("")}
b, err = yaml.Marshal(s)
if err != nil {
t.Fatal(err)
}
if string(b) != "is_true: false\n" {
t.Errorf(`expecting "is_true: true\n", got %q`, b)
}
s = S{Bool: libyaml.BoolString("{{repl blah }}")}
b, err = yaml.Marshal(s)
if err != nil {
t.Fatal(err)
}
if string(b) != "is_true: '{{repl blah }}'\n" {
t.Errorf(`expecting "is_true: '{{repl blah }}'\n", got %q`, b)
}
}
func TestBoolStringValidate(t *testing.T) {
v := validator.New(
&validator.Config{TagName: "validate"},
)
err := v.RegisterValidation("bool", libyaml.IsBoolValidation)
if err != nil {
t.Fatal(err)
}
type S struct {
Bool libyaml.BoolString `yaml:"is_true" validate:"bool"`
}
s := S{Bool: libyaml.BoolString("true")}
err = v.Struct(s)
if err != nil {
t.Fatal(err)
}
s = S{Bool: libyaml.BoolString("0")}
err = v.Struct(s)
if err != nil {
t.Fatal(err)
}
s = S{Bool: libyaml.BoolString("{{repl blah }}")}
err = v.Struct(s)
if err != nil {
t.Fatal(err)
}
s = S{Bool: libyaml.BoolString("blah")}
err = v.Struct(s)
if err == nil {
t.Fatal(err)
}
if !strings.Contains(err.Error(), "failed on the 'bool' tag") {
t.Errorf("unexpected error, %v", err)
}
s = S{Bool: libyaml.BoolString("")}
err = v.Struct(s)
if !strings.Contains(err.Error(), "failed on the 'bool' tag") {
t.Errorf("unexpected error, %v", err)
}
}