| 1 |
package format |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"net/url" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/onsi/ginkgo/v2" |
| 11 |
"github.com/onsi/gomega" |
| 12 |
|
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 15 |
) |
| 16 |
|
| 17 |
type subPropStruct struct { |
| 18 |
Value string |
| 19 |
} |
| 20 |
|
| 21 |
type subStruct struct { |
| 22 |
Value string |
| 23 |
} |
| 24 |
|
| 25 |
type testStruct struct { |
| 26 |
Signed int `default:"0" key:"signed"` |
| 27 |
Unsigned uint |
| 28 |
Duration time.Duration `default:"10s" key:"duration"` |
| 29 |
Str string `default:"notempty" key:"str"` |
| 30 |
StrSlice []string |
| 31 |
StrArray [3]string |
| 32 |
Sub subStruct |
| 33 |
TestEnum int `default:"None" key:"testenum"` |
| 34 |
SubProp subPropStruct |
| 35 |
SubSlice []subStruct |
| 36 |
SubPropSlice []subPropStruct |
| 37 |
SubPropPtrSlice []*subPropStruct |
| 38 |
StrMap map[string]string |
| 39 |
IntMap map[string]int |
| 40 |
Int8Map map[string]int8 |
| 41 |
Int16Map map[string]int16 |
| 42 |
Int32Map map[string]int32 |
| 43 |
Int64Map map[string]int64 |
| 44 |
UintMap map[string]uint |
| 45 |
Uint8Map map[string]int8 |
| 46 |
Uint16Map map[string]int16 |
| 47 |
Uint32Map map[string]int32 |
| 48 |
Uint64Map map[string]int64 |
| 49 |
} |
| 50 |
|
| 51 |
type testStructBadDefault struct { |
| 52 |
standard.EnumlessConfig |
| 53 |
|
| 54 |
Value int `default:"NaN" key:"value"` |
| 55 |
} |
| 56 |
|
| 57 |
var _ = ginkgo.BeforeSuite(func() { |
| 58 |
// Disable color output for tests using environment variable |
| 59 |
os.Setenv("NO_COLOR", "true") |
| 60 |
}) |
| 61 |
|
| 62 |
var ( |
| 63 |
testEnum = CreateEnumFormatter([]string{"None", "Foo", "Bar"}) |
| 64 |
enums = map[string]types.EnumFormatter{ |
| 65 |
"TestEnum": testEnum, |
| 66 |
} |
| 67 |
) |
| 68 |
|
| 69 |
var _ = ginkgo.Describe("the format package", func() { |
| 70 |
ginkgo.Describe("Generic Format Utils", func() { |
| 71 |
ginkgo.When("parsing a bool", func() { |
| 72 |
testParseValidBool := func(raw string, expected bool) { |
| 73 |
parsed, ok := ParseBool(raw, !expected) |
| 74 |
gomega.Expect(parsed).To(gomega.Equal(expected)) |
| 75 |
gomega.Expect(ok).To(gomega.BeTrue()) |
| 76 |
} |
| 77 |
|
| 78 |
ginkgo.It("should parse truthy values as true", func() { |
| 79 |
testParseValidBool("true", true) |
| 80 |
testParseValidBool("1", true) |
| 81 |
testParseValidBool("yes", true) |
| 82 |
}) |
| 83 |
ginkgo.It("should parse falsy values as false", func() { |
| 84 |
testParseValidBool("false", false) |
| 85 |
testParseValidBool("0", false) |
| 86 |
testParseValidBool("no", false) |
| 87 |
}) |
| 88 |
ginkgo.It("should match regardless of case", func() { |
| 89 |
testParseValidBool("trUE", true) |
| 90 |
}) |
| 91 |
ginkgo.It("should return the default if no value matches", func() { |
| 92 |
parsed, ok := ParseBool("bad", true) |
| 93 |
gomega.Expect(parsed).To(gomega.BeTrue()) |
| 94 |
gomega.Expect(ok).To(gomega.BeFalse()) |
| 95 |
parsed, ok = ParseBool("values", false) |
| 96 |
gomega.Expect(parsed).To(gomega.BeFalse()) |
| 97 |
gomega.Expect(ok).To(gomega.BeFalse()) |
| 98 |
}) |
| 99 |
}) |
| 100 |
ginkgo.When("printing a bool", func() { |
| 101 |
ginkgo.It("should return yes or no", func() { |
| 102 |
gomega.Expect(PrintBool(true)).To(gomega.Equal("Yes")) |
| 103 |
gomega.Expect(PrintBool(false)).To(gomega.Equal("No")) |
| 104 |
}) |
| 105 |
}) |
| 106 |
ginkgo.When("checking for number-like strings", func() { |
| 107 |
ginkgo.It("should be true for numbers", func() { |
| 108 |
gomega.Expect(IsNumber("1.5")).To(gomega.BeTrue()) |
| 109 |
gomega.Expect(IsNumber("0")).To(gomega.BeTrue()) |
| 110 |
gomega.Expect(IsNumber("NaN")).To(gomega.BeTrue()) |
| 111 |
}) |
| 112 |
ginkgo.It("should be false for non-numbers", func() { |
| 113 |
gomega.Expect(IsNumber("baNaNa")).To(gomega.BeFalse()) |
| 114 |
}) |
| 115 |
}) |
| 116 |
}) |
| 117 |
ginkgo.Describe("Enum Formatter", func() { |
| 118 |
ginkgo.It("should return all enum values on listing", func() { |
| 119 |
gomega.Expect(testEnum.Names()).To(gomega.ConsistOf("None", "Foo", "Bar")) |
| 120 |
}) |
| 121 |
}) |
| 122 |
}) |
| 123 |
|
| 124 |
func (s *subPropStruct) GetPropValue() (string, error) { |
| 125 |
return "@" + s.Value, nil |
| 126 |
} |
| 127 |
|
| 128 |
func (s *subPropStruct) SetFromProp(propValue string) error { |
| 129 |
if len(propValue) < 1 || propValue[0] != '@' { |
| 130 |
return errors.New("invalid value") |
| 131 |
} |
| 132 |
|
| 133 |
s.Value = propValue[1:] |
| 134 |
|
| 135 |
return nil |
| 136 |
} |
| 137 |
|
| 138 |
func (t *testStruct) Enums() map[string]types.EnumFormatter { |
| 139 |
return enums |
| 140 |
} |
| 141 |
|
| 142 |
func (t *testStruct) GetURL() *url.URL { |
| 143 |
panic("not implemented") |
| 144 |
} |
| 145 |
|
| 146 |
func (t *testStruct) SetURL(_ *url.URL) error { |
| 147 |
panic("not implemented") |
| 148 |
} |
| 149 |
|
| 150 |
func (t *testStructBadDefault) GetURL() *url.URL { |
| 151 |
panic("not implemented") |
| 152 |
} |
| 153 |
|
| 154 |
func (t *testStructBadDefault) SetURL(_ *url.URL) error { |
| 155 |
panic("not implemented") |
| 156 |
} |
| 157 |
|
| 158 |
func TestFormat(t *testing.T) { |
| 159 |
t.Parallel() |
| 160 |
gomega.RegisterFailHandler(ginkgo.Fail) |
| 161 |
ginkgo.RunSpecs(t, "Shoutrrr Format Suite") |
| 162 |
} |