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