chore: add more tests (#3006)

master
Kevin Wan 2 years ago committed by GitHub
parent 7a0c04bc21
commit 3a493cd6a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,8 @@ package codec
import ( import (
"bytes" "bytes"
"compress/gzip"
"errors"
"fmt" "fmt"
"testing" "testing"
@ -21,3 +23,45 @@ func TestGzip(t *testing.T) {
assert.True(t, len(bs) < buf.Len()) assert.True(t, len(bs) < buf.Len())
assert.Equal(t, buf.Bytes(), actual) assert.Equal(t, buf.Bytes(), actual)
} }
func TestGunzip(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
expectedErr error
}{
{
name: "valid input",
input: func() []byte {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
gz.Write([]byte("hello"))
gz.Close()
return buf.Bytes()
}(),
expected: []byte("hello"),
expectedErr: nil,
},
{
name: "invalid input",
input: []byte("invalid input"),
expected: nil,
expectedErr: gzip.ErrHeader,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := Gunzip(test.input)
if !bytes.Equal(result, test.expected) {
t.Errorf("unexpected result: %v", result)
}
if !errors.Is(err, test.expectedErr) {
t.Errorf("unexpected error: %v", err)
}
})
}
}

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package fs package fs

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package fs package fs

@ -1,5 +1,4 @@
//go:build fuzz //go:build fuzz
// +build fuzz
package mr package mr

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package proc package proc

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package proc package proc

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package proc package proc

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package proc package proc

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package proc package proc

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package proc package proc

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package proc package proc

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package proc package proc

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package proc package proc

@ -1,5 +1,4 @@
//go:build debug //go:build debug
// +build debug
package search package search

@ -1,5 +1,4 @@
//go:build !linux //go:build !linux
// +build !linux
package stat package stat

@ -1,5 +1,4 @@
//go:build linux //go:build linux
// +build linux
package stat package stat

@ -1,5 +1,4 @@
//go:build linux //go:build linux
// +build linux
package stat package stat

@ -278,10 +278,8 @@ func runningInUserNS() bool {
var a, b, c int64 var a, b, c int64
fmt.Sscanf(line, "%d %d %d", &a, &b, &c) fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
/* // We assume we are in the initial user namespace if we have a full
* We assume we are in the initial user namespace if we have a full // range - 4294967295 uids starting at uid 0.
* range - 4294967295 uids starting at uid 0.
*/
if a == 0 && b == 0 && c == 4294967295 { if a == 0 && b == 0 && c == 4294967295 {
return return
} }

@ -1,5 +1,4 @@
//go:build !linux //go:build !linux
// +build !linux
package internal package internal

@ -1,6 +1,3 @@
//go:build go1.18
// +build go1.18
package stringx package stringx
import ( import (

@ -15,8 +15,10 @@ func TestCompareVersions(t *testing.T) {
out bool out bool
}{ }{
{"1", "1.0.1", ">", false}, {"1", "1.0.1", ">", false},
{"1.0.1", "1.0", "<", false},
{"1", "0.9.9", ">", true}, {"1", "0.9.9", ">", true},
{"1", "1.0-1", "<", true}, {"1", "1.0-1", "<", true},
{"1", "1.0-1", "!", false},
{"1.0.1", "1-0.1", "<", false}, {"1.0.1", "1-0.1", "<", false},
{"1.0.1", "1.0.1", "==", true}, {"1.0.1", "1.0.1", "==", true},
{"1.0.1", "1.0.2", "==", false}, {"1.0.1", "1.0.2", "==", false},
@ -37,3 +39,21 @@ func TestCompareVersions(t *testing.T) {
}) })
} }
} }
func TestStrsToInts(t *testing.T) {
testCases := []struct {
input []string
expected []int64
}{
{[]string{}, nil},
{[]string{"1", "2", "3"}, []int64{1, 2, 3}},
}
for _, tc := range testCases {
tc := tc
t.Run("", func(t *testing.T) {
actual := strsToInts(tc.input)
assert.Equal(t, tc.expected, actual)
})
}
}

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package migrate package migrate

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package migrate package migrate

@ -1,5 +1,4 @@
//go:build windows //go:build windows
// +build windows
package pathx package pathx

@ -1,5 +1,4 @@
//go:build linux || darwin //go:build linux || darwin
// +build linux darwin
package pathx package pathx

Loading…
Cancel
Save