File: util_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,332 kB
  • sloc: sh: 61; makefile: 5
file content (105 lines) | stat: -rw-r--r-- 3,478 bytes parent folder | download
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
package util_test

import (
	"fmt"
	"reflect"
	"testing"

	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"

	"github.com/nicholas-fedor/shoutrrr/internal/meta"
	"github.com/nicholas-fedor/shoutrrr/pkg/util"
)

func TestUtil(t *testing.T) {
	gomega.RegisterFailHandler(ginkgo.Fail)
	ginkgo.RunSpecs(t, "Shoutrrr Util Suite")
}

const (
	a = 10
	b = 20
)

var _ = ginkgo.Describe("the util package", func() {
	ginkgo.When("calling function Min", func() {
		ginkgo.It("should return the smallest of two integers", func() {
			gomega.Expect(util.Min(a, b)).To(gomega.Equal(a))
			gomega.Expect(util.Min(b, a)).To(gomega.Equal(a))
		})
	})

	ginkgo.When("calling function Max", func() {
		ginkgo.It("should return the largest of two integers", func() {
			gomega.Expect(util.Max(a, b)).To(gomega.Equal(b))
			gomega.Expect(util.Max(b, a)).To(gomega.Equal(b))
		})
	})

	ginkgo.When("checking if a supplied kind is of the signed integer kind", func() {
		ginkgo.It("should be true if the kind is Int", func() {
			gomega.Expect(util.IsSignedInt(reflect.Int)).To(gomega.BeTrue())
		})
		ginkgo.It("should be false if the kind is String", func() {
			gomega.Expect(util.IsSignedInt(reflect.String)).To(gomega.BeFalse())
		})
	})

	ginkgo.When("checking if a supplied kind is of the unsigned integer kind", func() {
		ginkgo.It("should be true if the kind is Uint", func() {
			gomega.Expect(util.IsUnsignedInt(reflect.Uint)).To(gomega.BeTrue())
		})
		ginkgo.It("should be false if the kind is Int", func() {
			gomega.Expect(util.IsUnsignedInt(reflect.Int)).To(gomega.BeFalse())
		})
	})

	ginkgo.When("checking if a supplied kind is of the collection kind", func() {
		ginkgo.It("should be true if the kind is slice", func() {
			gomega.Expect(util.IsCollection(reflect.Slice)).To(gomega.BeTrue())
		})
		ginkgo.It("should be false if the kind is map", func() {
			gomega.Expect(util.IsCollection(reflect.Map)).To(gomega.BeFalse())
		})
	})

	ginkgo.When("calling function StripNumberPrefix", func() {
		ginkgo.It("should return the default base if none is found", func() {
			_, base := util.StripNumberPrefix("46")
			gomega.Expect(base).To(gomega.Equal(0))
		})
		ginkgo.It("should remove # prefix and return base 16 if found", func() {
			number, base := util.StripNumberPrefix("#ab")
			gomega.Expect(number).To(gomega.Equal("ab"))
			gomega.Expect(base).To(gomega.Equal(16))
		})
	})

	ginkgo.When("checking if a supplied kind is numeric", func() {
		ginkgo.It("should be true if supplied a constant integer", func() {
			gomega.Expect(util.IsNumeric(reflect.TypeOf(5).Kind())).To(gomega.BeTrue())
		})
		ginkgo.It("should be true if supplied a constant float", func() {
			gomega.Expect(util.IsNumeric(reflect.TypeOf(2.5).Kind())).To(gomega.BeTrue())
		})
		ginkgo.It("should be false if supplied a constant string", func() {
			gomega.Expect(util.IsNumeric(reflect.TypeOf("3").Kind())).To(gomega.BeFalse())
		})
	})

	ginkgo.When("calling function DocsURL", func() {
		ginkgo.It("should return the expected URL", func() {
			expectedBase := fmt.Sprintf(
				`https://nicholas-fedor.github.io/shoutrrr/%s/`,
				meta.GetVersion(),
			)
			gomega.Expect(util.DocsURL(``)).To(gomega.Equal(expectedBase))
			gomega.Expect(util.DocsURL(`services/logger`)).
				To(gomega.Equal(expectedBase + `services/logger`))
		})
		ginkgo.It("should strip the leading slash from the path", func() {
			gomega.Expect(util.DocsURL(`/foo`)).To(gomega.Equal(util.DocsURL(`foo`)))
		})
	})
})