Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,35 @@ func Benchmark_ToUpperBytes(b *testing.B) {
require.Equal(b, want, res)
})
}

func Test_ToLowerBytes_Edge(t *testing.T) {
t.Parallel()

cases := [][]byte{
{},
[]byte("123"),
[]byte("!@#"),
}
for _, c := range cases {
t.Run(string(c), func(t *testing.T) {
t.Parallel()
require.Equal(t, bytes.ToLower(c), ToLowerBytes(c))
})
}
}

func Test_ToUpperBytes_Edge(t *testing.T) {
t.Parallel()

cases := [][]byte{
{},
[]byte("123"),
[]byte("!@#"),
}
for _, c := range cases {
t.Run(string(c), func(t *testing.T) {
t.Parallel()
require.Equal(t, bytes.ToUpper(c), ToUpperBytes(c))
})
}
}
21 changes: 21 additions & 0 deletions byteseq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,24 @@ func Benchmark_TrimBytes(b *testing.B) {
require.Equal(b, expected, res)
})
}

func Test_Trim_Edge(t *testing.T) {
t.Parallel()

cases := []struct {
input string
cut byte
exp string
}{
{"foobar", 'x', "foobar"},
{"", ' ', ""},
{"xxfoobarxx", 'x', "foobar"},
}
for _, c := range cases {
t.Run(c.input, func(t *testing.T) {
t.Parallel()
require.Equal(t, c.exp, Trim(c.input, c.cut))
require.Equal(t, []byte(c.exp), Trim([]byte(c.input), c.cut))
})
}
}
9 changes: 9 additions & 0 deletions cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ func Test_DefaultCBORDecoderWithUnitializedStruct(t *testing.T) {
require.NoError(t, err)
require.Equal(t, emptySs, ss)
}

func Test_CBORDecodeInvalid(t *testing.T) {
t.Parallel()

data := []byte{0xff, 0xff}
var ss sampleStructure
err := cbor.Unmarshal(data, &ss)
require.Error(t, err)
}
10 changes: 10 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ func Test_GetArgument(t *testing.T) {
require.False(t, GetArgument("missing-arg"))
}

func Test_GetArgument_Multiple(t *testing.T) {
original := os.Args
defer func() { os.Args = original }()

os.Args = []string{"cmd", "-a", "-b", "--flag"}
require.True(t, GetArgument("-a"))
require.True(t, GetArgument("--flag"))
require.False(t, GetArgument("-c"))
}

func Test_IncrementIPRange(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 5 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func Test_ToString(t *testing.T) {
require.Equal(t, tc.expected, res)
})
}

t.Run("nil", func(t *testing.T) {
t.Parallel()
require.Equal(t, "<nil>", ToString(nil))
})
}

func TestCopyBytes(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,11 @@ func Test_ReadFile_NoFS(t *testing.T) {
require.NoError(t, err)
require.Contains(t, string(data), "doe")
}

func Test_ReadFile_SubDir(t *testing.T) {
t.Parallel()

data, err := ReadFile("example/example1.txt", http.FS(os.DirFS(".github/tests")))
require.NoError(t, err)
require.NotNil(t, data)
Comment thread
gaby marked this conversation as resolved.
}
9 changes: 9 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Test_GetMIME(t *testing.T) {
require.Equal(t, "application/javascript", res)
}
require.NoError(t, err)

require.Equal(t, "application/json", GetMIME(".JSON"))
require.Equal(t, MIMEOctetStream, GetMIME(" .json"))
}

// go test -v -run=^$ -bench=Benchmark_GetMIME -benchmem -count=2
Expand Down Expand Up @@ -208,6 +211,12 @@ func Test_StatusMessage(t *testing.T) {

res = StatusMessage(600)
require.Equal(t, "", res)

res = StatusMessage(100)
require.Equal(t, "Continue", res)

res = StatusMessage(http.StatusTeapot)
require.Equal(t, "I'm a teapot", res)
}

// go test -run=^$ -bench=Benchmark_StatusMessage -benchmem -count=2
Expand Down
9 changes: 9 additions & 0 deletions ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ func Test_IsIPv6_EdgeCases(t *testing.T) {
require.False(t, IsIPv6("1::2:3:4:5:6:7:8"))
}

func Test_IPWhitespace(t *testing.T) {
t.Parallel()

require.False(t, IsIPv4(" 1.1.1.1"))
require.False(t, IsIPv4("1.1.1.1 "))
require.False(t, IsIPv6(" ::1"))
require.False(t, IsIPv6("::1 "))
}

// go test -v -run=^$ -bench=UnsafeString -benchmem -count=2
func Benchmark_IsIPv4(b *testing.B) {
ip := "174.23.33.100"
Expand Down
8 changes: 8 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ func Test_DefaultJSONDecoder(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "Hello World", ss.ImportantString)
}

func Test_JSONDecodeInvalid(t *testing.T) {
t.Parallel()

var ss sampleStructure
err := json.Unmarshal([]byte("{invalid}"), &ss)
require.Error(t, err)
}
8 changes: 8 additions & 0 deletions msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ func Test_MsgpackDecoder(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "Hello World", decoded.ImportantString)
}

func Test_MsgpackDecodeInvalid(t *testing.T) {
t.Parallel()

var decoded sampleMsgPackStructure
err := msgpack.Unmarshal([]byte{0xff, 0xff}, &decoded)
require.Error(t, err)
}
16 changes: 16 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func Test_ParseUint(t *testing.T) {
}
}

func Test_ParseUint_Whitespace(t *testing.T) {
t.Parallel()

v, ok := ParseUint(" 123")
require.False(t, ok)
require.Equal(t, uint64(0), v)
}

func Benchmark_ParseUint(b *testing.B) {
input := "123456789"

Expand Down Expand Up @@ -110,6 +118,14 @@ func Test_ParseInt_SignOnly(t *testing.T) {
}
}

func Test_ParseInt_Whitespace(t *testing.T) {
t.Parallel()

v, ok := ParseInt(" 42")
require.False(t, ok)
require.Equal(t, int64(0), v)
}

func Test_ParseUnsigned_SignOnly(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 7 additions & 0 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ func Test_NonASCII_Unchanged(t *testing.T) {
require.Equal(t, nonASCII, ToUpper(nonASCII), "ToUpper altered non-ASCII")
require.Equal(t, nonASCII, ToLower(nonASCII), "ToLower altered non-ASCII")
})

mixed := "Goµ"
t.Run("mixed", func(t *testing.T) {
t.Parallel()
require.Equal(t, "GOµ", ToUpper(mixed))
require.Equal(t, "goµ", ToLower(mixed))
})
}

func Benchmark_ToUpper(b *testing.B) {
Expand Down
14 changes: 14 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,17 @@ func Benchmark_CalculateTimestamp(b *testing.B) {
}
})
}

func Test_StartStopRepeat(t *testing.T) {
timerTestMu.Lock()
defer timerTestMu.Unlock()

StartTimeStampUpdater()
StopTimeStampUpdater()
StartTimeStampUpdater()
defer StopTimeStampUpdater()

time.Sleep(50 * time.Millisecond)
ts := Timestamp()
require.NotZero(t, ts)
Comment thread
gaby marked this conversation as resolved.
}
8 changes: 8 additions & 0 deletions xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ func Benchmark_DefaultXMLDecoder(b *testing.B) {
}
}
}

func Test_XMLDecodeInvalid(t *testing.T) {
t.Parallel()

var ss serversXMLStructure
err := xml.Unmarshal([]byte("<invalid>"), &ss)
require.Error(t, err)
}
Loading