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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
package format
import (
"errors"
"net/url"
"testing"
"github.com/fatih/color"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
"github.com/nicholas-fedor/shoutrrr/pkg/types"
)
func TestFormat(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Shoutrrr Format Suite")
}
var _ = ginkgo.BeforeSuite(func() {
// Disable color output for tests to have them match the string format rather than the colors
color.NoColor = true
})
var _ = ginkgo.Describe("the format package", func() {
ginkgo.Describe("Generic Format Utils", func() {
ginkgo.When("parsing a bool", func() {
testParseValidBool := func(raw string, expected bool) {
parsed, ok := ParseBool(raw, !expected)
gomega.Expect(parsed).To(gomega.Equal(expected))
gomega.Expect(ok).To(gomega.BeTrue())
}
ginkgo.It("should parse truthy values as true", func() {
testParseValidBool("true", true)
testParseValidBool("1", true)
testParseValidBool("yes", true)
})
ginkgo.It("should parse falsy values as false", func() {
testParseValidBool("false", false)
testParseValidBool("0", false)
testParseValidBool("no", false)
})
ginkgo.It("should match regardless of case", func() {
testParseValidBool("trUE", true)
})
ginkgo.It("should return the default if no value matches", func() {
parsed, ok := ParseBool("bad", true)
gomega.Expect(parsed).To(gomega.BeTrue())
gomega.Expect(ok).To(gomega.BeFalse())
parsed, ok = ParseBool("values", false)
gomega.Expect(parsed).To(gomega.BeFalse())
gomega.Expect(ok).To(gomega.BeFalse())
})
})
ginkgo.When("printing a bool", func() {
ginkgo.It("should return yes or no", func() {
gomega.Expect(PrintBool(true)).To(gomega.Equal("Yes"))
gomega.Expect(PrintBool(false)).To(gomega.Equal("No"))
})
})
ginkgo.When("checking for number-like strings", func() {
ginkgo.It("should be true for numbers", func() {
gomega.Expect(IsNumber("1.5")).To(gomega.BeTrue())
gomega.Expect(IsNumber("0")).To(gomega.BeTrue())
gomega.Expect(IsNumber("NaN")).To(gomega.BeTrue())
})
ginkgo.It("should be false for non-numbers", func() {
gomega.Expect(IsNumber("baNaNa")).To(gomega.BeFalse())
})
})
})
ginkgo.Describe("Enum Formatter", func() {
ginkgo.It("should return all enum values on listing", func() {
gomega.Expect(testEnum.Names()).To(gomega.ConsistOf("None", "Foo", "Bar"))
})
})
})
type testStruct struct {
Signed int `default:"0" key:"signed"`
Unsigned uint
Str string `default:"notempty" key:"str"`
StrSlice []string
StrArray [3]string
Sub subStruct
TestEnum int `default:"None" key:"testenum"`
SubProp subPropStruct
SubSlice []subStruct
SubPropSlice []subPropStruct
SubPropPtrSlice []*subPropStruct
StrMap map[string]string
IntMap map[string]int
Int8Map map[string]int8
Int16Map map[string]int16
Int32Map map[string]int32
Int64Map map[string]int64
UintMap map[string]uint
Uint8Map map[string]int8
Uint16Map map[string]int16
Uint32Map map[string]int32
Uint64Map map[string]int64
}
func (t *testStruct) GetURL() *url.URL {
panic("not implemented")
}
func (t *testStruct) SetURL(_ *url.URL) error {
panic("not implemented")
}
func (t *testStruct) Enums() map[string]types.EnumFormatter {
return enums
}
type subStruct struct {
Value string
}
type subPropStruct struct {
Value string
}
func (s *subPropStruct) SetFromProp(propValue string) error {
if len(propValue) < 1 || propValue[0] != '@' {
return errors.New("invalid value")
}
s.Value = propValue[1:]
return nil
}
func (s *subPropStruct) GetPropValue() (string, error) {
return "@" + s.Value, nil
}
var (
testEnum = CreateEnumFormatter([]string{"None", "Foo", "Bar"})
enums = map[string]types.EnumFormatter{
"TestEnum": testEnum,
}
)
type testStructBadDefault struct {
standard.EnumlessConfig
Value int `default:"NaN" key:"value"`
}
func (t *testStructBadDefault) GetURL() *url.URL {
panic("not implemented")
}
func (t *testStructBadDefault) SetURL(_ *url.URL) error {
panic("not implemented")
}
|