diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..3172e53 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,44 @@ +linters: + enable: + - errorlint + - gci + - gocritic + - gofumpt + - misspell + - nonamedreturns + - testifylint + - errcheck + +linters-settings: + errorlint: + # Check whether fmt.Errorf uses the %w verb for formatting errors. + # See the https://github.com/polyfloyd/go-errorlint for caveats. + errorf: true + # Permit more than 1 %w verb, valid per Go 1.20 (Requires errorf:true) + errorf-multi: true + # Check for plain type assertions and type switches. + asserts: true + # Check for plain error comparisons. + comparison: true + gci: + sections: + - standard + - default + - prefix(github.com/go-sprout) + testifylint: + disable: + - float-compare + - go-require + enable: + - bool-compare + - compares + - empty + - error-is-as + - error-nil + - expected-actual + - len + - require-error + - suite-dont-use-pkg + - suite-extra-assert-call +run: + timeout: 5m diff --git a/handler_test.go b/handler_test.go index d66faed..03416d7 100644 --- a/handler_test.go +++ b/handler_test.go @@ -2,9 +2,8 @@ package sprout import ( "errors" - "testing" - "log/slog" + "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -159,7 +158,6 @@ func TestDefaultHandler_AddRegistry_Error_RegisteriesNotices(t *testing.T) { mockRegistry.AssertCalled(t, "RegisterFunctions", dh.cachedFuncsMap) mockRegistry.AssertCalled(t, "RegisterNotices", &dh.notices) - } // TestDefaultHandler_AddRegistry tests the AddRegistry method of DefaultHandler. diff --git a/internal/helpers/helpers_test.go b/internal/helpers/helpers_test.go index f20b931..c6bd43e 100644 --- a/internal/helpers/helpers_test.go +++ b/internal/helpers/helpers_test.go @@ -69,7 +69,6 @@ func TestEmpty(t *testing.T) { assert.False(t, Empty(true)) assert.False(t, Empty(uint(1))) assert.False(t, Empty(uint8(1))) - } func TestUntilStep(t *testing.T) { diff --git a/pesticide/rand_test_helpers.go b/pesticide/rand_test_helpers.go index d5ebfab..43f30bd 100644 --- a/pesticide/rand_test_helpers.go +++ b/pesticide/rand_test_helpers.go @@ -3,9 +3,10 @@ package pesticide import ( "testing" - "github.com/go-sprout/sprout" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/go-sprout/sprout" ) type RegexpTestCase struct { diff --git a/pesticide/test_helpers.go b/pesticide/test_helpers.go index 13c3758..8afd0e3 100644 --- a/pesticide/test_helpers.go +++ b/pesticide/test_helpers.go @@ -11,13 +11,14 @@ import ( "testing" "text/template" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/go-sprout/sprout" "github.com/go-sprout/sprout/registry/maps" "github.com/go-sprout/sprout/registry/reflect" "github.com/go-sprout/sprout/registry/slices" "github.com/go-sprout/sprout/registry/strings" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) type TestCase struct { diff --git a/registry/backward/functions.go b/registry/backward/functions.go index 1433a5c..57accd8 100644 --- a/registry/backward/functions.go +++ b/registry/backward/functions.go @@ -50,7 +50,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, er dict := map[string]any{} parsedURL, err := url.Parse(v) if err != nil { - return dict, fmt.Errorf("unable to parse url: %s", err) + return dict, fmt.Errorf("unable to parse url: %w", err) } dict["scheme"] = parsedURL.Scheme dict["host"] = parsedURL.Host @@ -85,7 +85,6 @@ func (bcr *BackwardCompatibilityRegistry) UrlParse(v string) (map[string]any, er // // {{ dict scheme="https" host="example.com" path="/path" query="query=1" opaque="opaque" fragment="fragment" | urlJoin }} // Output: "https://example.com/path?query=1#fragment" func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, error) { - resURL := url.URL{ Scheme: bcr.get(d, "scheme").(string), Host: bcr.get(d, "host").(string), @@ -99,7 +98,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, err if userinfo != "" { tempURL, err := url.Parse(fmt.Sprintf("proto://%s@host", userinfo)) if err != nil { - return "", fmt.Errorf("unable to parse userinfo in dict: %s", err) + return "", fmt.Errorf("unable to parse userinfo in dict: %w", err) } user = tempURL.User } @@ -128,7 +127,7 @@ func (bcr *BackwardCompatibilityRegistry) UrlJoin(d map[string]any) (string, err func (bcr *BackwardCompatibilityRegistry) GetHostByName(name string) (string, error) { addrs, err := net.LookupHost(name) if err != nil { - return "", fmt.Errorf("unable to resolve hostname: %s", err) + return "", fmt.Errorf("unable to resolve hostname: %w", err) } return addrs[rand.Intn(len(addrs))], nil } diff --git a/registry/backward/functions_test.go b/registry/backward/functions_test.go index 086ba97..210aee7 100644 --- a/registry/backward/functions_test.go +++ b/registry/backward/functions_test.go @@ -8,8 +8,7 @@ import ( ) func TestFail(t *testing.T) { - - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{fail "This is an error"}}`, ExpectedErr: "This is an error"}, } @@ -17,8 +16,7 @@ func TestFail(t *testing.T) { } func TestUrlParse(t *testing.T) { - - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ urlParse "https://example.com" | urlJoin }}`, ExpectedOutput: "https://example.com"}, {Input: `{{ urlParse "https://example.com/path" | urlJoin }}`, ExpectedOutput: "https://example.com/path"}, {Input: `{{ urlParse "https://user:pass@example.com/path?query=1" | urlJoin }}`, ExpectedOutput: "https://user:pass@example.com/path?query=1"}, @@ -34,7 +32,7 @@ func TestGetHostByName(t *testing.T) { ipAddressRegexp := ipv4 + `|` + ipv6 - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Template: `{{ getHostByName "example.com" }}`, Regexp: ipAddressRegexp, Length: -1}, {Template: `{{ getHostByName "github.com" }}`, Regexp: ipAddressRegexp, Length: -1}, {Template: `{{ getHostByName "127.0.0.1" }}`, Regexp: ipAddressRegexp, Length: -1}, diff --git a/registry/crypto/crypto.go b/registry/crypto/crypto.go index 2503cbb..1194bdc 100644 --- a/registry/crypto/crypto.go +++ b/registry/crypto/crypto.go @@ -26,11 +26,13 @@ var masterPasswordSeed = "com.lyndir.masterpassword" var passwordTypeTemplates = map[string][][]byte{ "maximum": {[]byte("anoxxxxxxxxxxxxxxxxx"), []byte("axxxxxxxxxxxxxxxxxno")}, - "long": {[]byte("CvcvnoCvcvCvcv"), []byte("CvcvCvcvnoCvcv"), []byte("CvcvCvcvCvcvno"), []byte("CvccnoCvcvCvcv"), []byte("CvccCvcvnoCvcv"), + "long": { + []byte("CvcvnoCvcvCvcv"), []byte("CvcvCvcvnoCvcv"), []byte("CvcvCvcvCvcvno"), []byte("CvccnoCvcvCvcv"), []byte("CvccCvcvnoCvcv"), []byte("CvccCvcvCvcvno"), []byte("CvcvnoCvccCvcv"), []byte("CvcvCvccnoCvcv"), []byte("CvcvCvccCvcvno"), []byte("CvcvnoCvcvCvcc"), []byte("CvcvCvcvnoCvcc"), []byte("CvcvCvcvCvccno"), []byte("CvccnoCvccCvcv"), []byte("CvccCvccnoCvcv"), []byte("CvccCvccCvcvno"), []byte("CvcvnoCvccCvcc"), []byte("CvcvCvccnoCvcc"), []byte("CvcvCvccCvccno"), []byte("CvccnoCvcvCvcc"), []byte("CvccCvcvnoCvcc"), - []byte("CvccCvcvCvccno")}, + []byte("CvccCvcvCvccno"), + }, "medium": {[]byte("CvcnoCvc"), []byte("CvcCvcno")}, "short": {[]byte("Cvcn")}, "basic": {[]byte("aaanaaan"), []byte("aannaaan"), []byte("aaannaaa")}, diff --git a/registry/crypto/functions.go b/registry/crypto/functions.go index 19d24dd..ec0d0b8 100644 --- a/registry/crypto/functions.go +++ b/registry/crypto/functions.go @@ -75,7 +75,7 @@ func (ch *CryptoRegistry) Htpasswd(username string, password string) (string, er // // {{ derivePassword 0 "bcrypt" "password" "user" "site" }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO" func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password, user, site string) (string, error) { - var templates = passwordTypeTemplates[passwordType] + templates := passwordTypeTemplates[passwordType] if templates == nil { return "", fmt.Errorf("cannot find password template %s", passwordType) } @@ -96,10 +96,10 @@ func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password, buffer.WriteString(site) _ = binary.Write(&buffer, binary.BigEndian, counter) - var hmacv = hmac.New(sha256.New, key) + hmacv := hmac.New(sha256.New, key) hmacv.Write(buffer.Bytes()) - var seed = hmacv.Sum(nil) - var temp = templates[int(seed[0])%len(templates)] + seed := hmacv.Sum(nil) + temp := templates[int(seed[0])%len(templates)] buffer.Truncate(0) for i, element := range temp { diff --git a/registry/encoding/functions.go b/registry/encoding/functions.go index afff6de..bb5a792 100644 --- a/registry/encoding/functions.go +++ b/registry/encoding/functions.go @@ -8,8 +8,9 @@ import ( "fmt" "strings" - "github.com/go-sprout/sprout" "gopkg.in/yaml.v3" + + "github.com/go-sprout/sprout" ) // Base64Encode encodes a string into its Base64 representation. @@ -47,7 +48,7 @@ func (er *EncodingRegistry) Base64Encode(str string) string { func (er *EncodingRegistry) Base64Decode(str string) (string, error) { bytes, err := base64.StdEncoding.DecodeString(str) if err != nil { - return "", fmt.Errorf("base64 decode error: %v", err) + return "", fmt.Errorf("base64 decode error: %w", err) } return string(bytes), nil } @@ -87,7 +88,7 @@ func (er *EncodingRegistry) Base32Encode(str string) string { func (er *EncodingRegistry) Base32Decode(str string) (string, error) { bytes, err := base32.StdEncoding.DecodeString(str) if err != nil { - return "", fmt.Errorf("base32 decode error: %v", err) + return "", fmt.Errorf("base32 decode error: %w", err) } return string(bytes), nil } @@ -111,7 +112,7 @@ func (er *EncodingRegistry) FromJson(v string) (any, error) { var output any err := json.Unmarshal([]byte(v), &output) if err != nil { - return nil, fmt.Errorf("json decode error: %v", err) + return nil, fmt.Errorf("json decode error: %w", err) } return output, err } @@ -134,7 +135,7 @@ func (er *EncodingRegistry) FromJson(v string) (any, error) { func (er *EncodingRegistry) ToJson(v any) (string, error) { output, err := json.Marshal(v) if err != nil { - return "", fmt.Errorf("json encode error: %v", err) + return "", fmt.Errorf("json encode error: %w", err) } return string(output), nil } @@ -157,7 +158,7 @@ func (er *EncodingRegistry) ToJson(v any) (string, error) { func (er *EncodingRegistry) ToPrettyJson(v any) (string, error) { output, err := json.MarshalIndent(v, "", " ") if err != nil { - return "", fmt.Errorf("json encode error: %v", err) + return "", fmt.Errorf("json encode error: %w", err) } return string(output), nil } @@ -183,7 +184,7 @@ func (er *EncodingRegistry) ToRawJson(v any) (string, error) { enc.SetEscapeHTML(false) err := enc.Encode(&v) if err != nil { - return "", fmt.Errorf("json encode error: %v", err) + return "", fmt.Errorf("json encode error: %w", err) } return strings.TrimSuffix(buf.String(), "\n"), nil } @@ -206,7 +207,7 @@ func (er *EncodingRegistry) FromYAML(str string) (any, error) { m := make(map[string]any) if err := yaml.Unmarshal([]byte(str), &m); err != nil { - return nil, fmt.Errorf("yaml decode error: %v", err) + return nil, fmt.Errorf("yaml decode error: %w", err) } return m, nil @@ -237,7 +238,7 @@ func (er *EncodingRegistry) ToYAML(v any) (out string, err error) { // code unreachable because yaml.Marshal always panic on error and never // returns an error, but we still need to handle the error for the sake of // consistency. The error message is set by ErrRecoverPanic. - return "", fmt.Errorf("yaml encode error: %v", err) + return "", fmt.Errorf("yaml encode error: %w", err) } return strings.TrimSuffix(string(data), "\n"), nil diff --git a/registry/env/functions_test.go b/registry/env/functions_test.go index 262fc82..0cda5c3 100644 --- a/registry/env/functions_test.go +++ b/registry/env/functions_test.go @@ -10,7 +10,7 @@ import ( func TestEnv(t *testing.T) { os.Setenv("__SPROUT_TEST_ENV_KEY", "sprout will grow!") - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmpty", Input: `{{ env "" }}`, ExpectedOutput: ""}, {Name: "TestNonExistent", Input: `{{ env "NON_EXISTENT_ENV_VAR" }}`, ExpectedOutput: ""}, {Name: "TestExisting", Input: `{{ env "__SPROUT_TEST_ENV_KEY" }}`, ExpectedOutput: "sprout will grow!"}, @@ -23,7 +23,7 @@ func TestEnv(t *testing.T) { func TestExpandEnv(t *testing.T) { os.Setenv("__SPROUT_TEST_ENV_KEY", "sprout will grow!") - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmpty", Input: `{{ expandEnv "" }}`, ExpectedOutput: ""}, {Name: "TestNonExistent", Input: `{{ expandEnv "Hey" }}`, ExpectedOutput: "Hey"}, {Name: "TestNonExistent", Input: `{{ expandEnv "$NON_EXISTENT_ENV_VAR" }}`, ExpectedOutput: ""}, diff --git a/registry/filesystem/filesystem_functions_linux_test.go b/registry/filesystem/filesystem_functions_linux_test.go index 41b86bc..167c55e 100644 --- a/registry/filesystem/filesystem_functions_linux_test.go +++ b/registry/filesystem/filesystem_functions_linux_test.go @@ -8,7 +8,7 @@ import ( ) func TestOsBase(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ osBase "" }}`, ExpectedOutput: "."}, {Name: "TestRootPath", Input: `{{ osBase "/" }}`, ExpectedOutput: "/"}, {Name: "TestWithoutExtension", Input: `{{ osBase "/path/to/file" }}`, ExpectedOutput: "file"}, @@ -21,7 +21,7 @@ func TestOsBase(t *testing.T) { } func TestOsDir(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ osDir "" }}`, ExpectedOutput: "."}, {Name: "TestRootPath", Input: `{{ osDir "/" }}`, ExpectedOutput: "/"}, {Name: "TestWithoutExtension", Input: `{{ osDir "/path/to/file" }}`, ExpectedOutput: "/path/to"}, @@ -34,7 +34,7 @@ func TestOsDir(t *testing.T) { } func TestOsExt(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ osExt "" }}`, ExpectedOutput: ""}, {Name: "TestRootPath", Input: `{{ osExt "/" }}`, ExpectedOutput: ""}, {Name: "TestWithoutExtension", Input: `{{ osExt "/path/to/file" }}`, ExpectedOutput: ""}, @@ -47,7 +47,7 @@ func TestOsExt(t *testing.T) { } func TestOsClean(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ osClean "" }}`, ExpectedOutput: "."}, {Name: "TestRootPath", Input: `{{ osClean "/" }}`, ExpectedOutput: "/"}, {Name: "TestWithoutExtension", Input: `{{ osClean "/path///to/file" }}`, ExpectedOutput: "/path/to/file"}, @@ -57,7 +57,7 @@ func TestOsClean(t *testing.T) { } func TestOsIsAbs(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ osIsAbs "" }}`, ExpectedOutput: "false"}, {Name: "TestRootPath", Input: `{{ osIsAbs "/" }}`, ExpectedOutput: "true"}, {Name: "TestRelativePath", Input: `{{ osIsAbs "path/to/file" }}`, ExpectedOutput: "false"}, diff --git a/registry/filesystem/filesystem_functions_test.go b/registry/filesystem/filesystem_functions_test.go index f977404..9c3e1c2 100644 --- a/registry/filesystem/filesystem_functions_test.go +++ b/registry/filesystem/filesystem_functions_test.go @@ -8,7 +8,7 @@ import ( ) func TestPathBase(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ pathBase "" }}`, ExpectedOutput: "."}, {Name: "TestRootPath", Input: `{{ pathBase "/" }}`, ExpectedOutput: "/"}, {Name: "TestWithoutExtension", Input: `{{ pathBase "/path/to/file" }}`, ExpectedOutput: "file"}, @@ -21,7 +21,7 @@ func TestPathBase(t *testing.T) { } func TestPathDir(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ pathDir "" }}`, ExpectedOutput: "."}, {Name: "TestRootPath", Input: `{{ pathDir "/" }}`, ExpectedOutput: "/"}, {Name: "TestWithoutExtension", Input: `{{ pathDir "/path/to/file" }}`, ExpectedOutput: "/path/to"}, @@ -34,7 +34,7 @@ func TestPathDir(t *testing.T) { } func TestPathExt(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ pathExt "" }}`, ExpectedOutput: ""}, {Name: "TestRootPath", Input: `{{ pathExt "/" }}`, ExpectedOutput: ""}, {Name: "TestWithoutExtension", Input: `{{ pathExt "/path/to/file" }}`, ExpectedOutput: ""}, @@ -47,7 +47,7 @@ func TestPathExt(t *testing.T) { } func TestPathClean(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ pathClean "" }}`, ExpectedOutput: "."}, {Name: "TestRootPath", Input: `{{ pathClean "/" }}`, ExpectedOutput: "/"}, {Name: "TestWithoutExtension", Input: `{{ pathClean "/path/to/file" }}`, ExpectedOutput: "/path/to/file"}, @@ -63,7 +63,7 @@ func TestPathClean(t *testing.T) { } func TestPathIsAbs(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyPath", Input: `{{ pathIsAbs "" }}`, ExpectedOutput: "false"}, {Name: "TestRootPath", Input: `{{ pathIsAbs "/" }}`, ExpectedOutput: "true"}, {Name: "TestRelativePath", Input: `{{ pathIsAbs "path/to/file" }}`, ExpectedOutput: "false"}, diff --git a/registry/maps/functions.go b/registry/maps/functions.go index e1a7808..68aff66 100644 --- a/registry/maps/functions.go +++ b/registry/maps/functions.go @@ -5,6 +5,7 @@ import ( "fmt" "dario.cat/mergo" + "github.com/go-sprout/sprout/deprecated" "github.com/go-sprout/sprout/internal/helpers" ) diff --git a/registry/maps/sprig_deprecated_functions_signature_test.go b/registry/maps/sprig_deprecated_functions_signature_test.go index 2391757..eec50b3 100644 --- a/registry/maps/sprig_deprecated_functions_signature_test.go +++ b/registry/maps/sprig_deprecated_functions_signature_test.go @@ -8,7 +8,7 @@ import ( ) func TestDeprecatedGet(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmpty", Input: `{{get . "a" }}`, ExpectedOutput: ""}, {Name: "TestWithKey", Input: `{{get . "a" }}`, ExpectedOutput: "1", Data: map[string]any{"a": 1}}, {Name: "TestWithNestedKeyNotFound", Input: `{{get . "b" }}`, ExpectedOutput: "", Data: map[string]any{"a": 1}}, @@ -20,7 +20,7 @@ func TestDeprecatedGet(t *testing.T) { } func TestDeprecatedSet(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestWithKey", Input: `{{$d := set . "a" 2}}{{$d}}`, ExpectedOutput: "map[a:2]", Data: map[string]any{"a": 1}}, {Name: "TestWithNewKey", Input: `{{$d := set . "b" 3}}{{$d}}`, ExpectedOutput: "map[a:1 b:3]", Data: map[string]any{"a": 1}}, {Name: "TestWithNilValue", Input: `{{$d := set .V "a" .Nil}}{{$d}}`, ExpectedOutput: "map[a:]", Data: map[string]any{"V": map[string]any{"a": 1}, "Nil": nil}}, @@ -33,7 +33,7 @@ func TestDeprecatedSet(t *testing.T) { } func TestDeprecatedUnset(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestWithKey", Input: `{{$d := unset . "a"}}{{$d}}`, ExpectedOutput: "map[]", Data: map[string]any{"a": 1}}, {Name: "TestWithNestedKeyNotFound", Input: `{{$d := unset . "b"}}{{$d}}`, ExpectedOutput: "map[a:1]", Data: map[string]any{"a": 1}}, {Name: "TestInvaidArguments", Input: `{{$d := unset . "a" "b"}}{{$d}}`, ExpectedErr: "expected 2 arguments, got 3", Data: map[string]any{"a": 1}}, @@ -45,7 +45,7 @@ func TestDeprecatedUnset(t *testing.T) { } func TestDeprecatedHasKey(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmpty", Input: `{{hasKey . "a"}}`, ExpectedOutput: "false"}, {Name: "TestWithKey", Input: `{{hasKey . "a"}}`, ExpectedOutput: "true", Data: map[string]any{"a": 1}}, {Name: "TestWithNestedKeyNotFound", Input: `{{hasKey . "b"}}`, ExpectedOutput: "false", Data: map[string]any{"a": 1}}, @@ -57,7 +57,7 @@ func TestDeprecatedHasKey(t *testing.T) { } func TestDeprecatedPick(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmpty", Input: `{{pick . "a" "b"}}`, ExpectedOutput: "map[]"}, {Name: "TestWithKeys", Input: `{{pick . "a" "b"}}`, ExpectedOutput: "map[a:1 b:2]", Data: map[string]any{"a": 1, "b": 2}}, {Name: "TestWithNestedKeyNotFound", Input: `{{pick . "a" "b"}}`, ExpectedOutput: "map[a:1]", Data: map[string]any{"a": 1}}, @@ -70,7 +70,7 @@ func TestDeprecatedPick(t *testing.T) { } func TestDeprecatedOmit(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmpty", Input: `{{omit . "a" "b"}}`, ExpectedOutput: "map[]"}, {Name: "TestWithKeys", Input: `{{omit . "a" "b"}}`, ExpectedOutput: "map[]", Data: map[string]any{"a": 1, "b": 2}}, {Name: "TestWithNestedKeyNotFound", Input: `{{omit . "b"}}`, ExpectedOutput: "map[a:1]", Data: map[string]any{"a": 1}}, diff --git a/registry/numeric/functions.go b/registry/numeric/functions.go index 3026dda..837b18c 100644 --- a/registry/numeric/functions.go +++ b/registry/numeric/functions.go @@ -4,8 +4,9 @@ import ( "math" "reflect" - "github.com/go-sprout/sprout" "github.com/spf13/cast" + + "github.com/go-sprout/sprout" ) // Floor returns the largest integer less than or equal to the provided number. diff --git a/registry/numeric/functions_test.go b/registry/numeric/functions_test.go index 138165e..28dbd7e 100644 --- a/registry/numeric/functions_test.go +++ b/registry/numeric/functions_test.go @@ -10,7 +10,7 @@ import ( ) func TestFloor(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ floor 1.5 }}`, ExpectedOutput: "1"}, {Input: `{{ floor 1 }}`, ExpectedOutput: "1"}, {Input: `{{ floor -1.5 }}`, ExpectedOutput: "-2"}, @@ -27,7 +27,7 @@ func TestFloor(t *testing.T) { } func TestCeil(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ ceil 1.5 }}`, ExpectedOutput: "2"}, {Input: `{{ ceil 1 }}`, ExpectedOutput: "1"}, {Input: `{{ ceil -1.5 }}`, ExpectedOutput: "-1"}, @@ -44,7 +44,7 @@ func TestCeil(t *testing.T) { } func TestRound(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ round 3.746 2 }}`, ExpectedOutput: "3.75"}, {Input: `{{ round 3.746 2 0.5 }}`, ExpectedOutput: "3.75"}, {Input: `{{ round 123.5555 3 }}`, ExpectedOutput: "123.556"}, @@ -60,7 +60,7 @@ func TestRound(t *testing.T) { } func TestAdd(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ add }}`, ExpectedOutput: "0"}, {Input: `{{ add 1 }}`, ExpectedOutput: "1"}, {Input: `{{ add 1 2 3 4 5 6 7 8 9 10 }}`, ExpectedOutput: "55"}, @@ -72,7 +72,7 @@ func TestAdd(t *testing.T) { } func TestAddf(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ addf }}`, ExpectedOutput: "0"}, {Input: `{{ addf 1 }}`, ExpectedOutput: "1"}, {Input: `{{ addf 1 2 3 4 5 6 7 8 9 10 }}`, ExpectedOutput: "55"}, @@ -84,7 +84,7 @@ func TestAddf(t *testing.T) { } func TestAdd1(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ add1 -1 }}`, ExpectedOutput: "0"}, {Input: `{{ add1f -1.0}}`, ExpectedOutput: "0"}, {Input: `{{ add1 1 }}`, ExpectedOutput: "2"}, @@ -96,7 +96,7 @@ func TestAdd1(t *testing.T) { } func TestAdd1f(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ add1f -1 }}`, ExpectedOutput: "0"}, {Input: `{{ add1f -1.0}}`, ExpectedOutput: "0"}, {Input: `{{ add1f 1 }}`, ExpectedOutput: "2"}, @@ -108,7 +108,7 @@ func TestAdd1f(t *testing.T) { } func TestSub(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ sub 1 1 }}`, ExpectedOutput: "0"}, {Input: `{{ sub 1 2 }}`, ExpectedOutput: "-1"}, {Input: `{{ sub 1.1 1.1 }}`, ExpectedOutput: "0"}, @@ -121,7 +121,7 @@ func TestSub(t *testing.T) { } func TestSubf(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ subf 1.1 1.1 }}`, ExpectedOutput: "0"}, {Input: `{{ subf 1.1 2.2 }}`, ExpectedOutput: "-1.1"}, {Input: `{{ round (3 | subf 4.5 1) 1 }}`, ExpectedOutput: "0.5"}, @@ -132,7 +132,7 @@ func TestSubf(t *testing.T) { } func TestMulInt(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ mul 1 1 }}`, ExpectedOutput: "1"}, {Input: `{{ mul 1 2 }}`, ExpectedOutput: "2"}, {Input: `{{ mul 1.1 1.1 }}`, ExpectedOutput: "1"}, @@ -145,7 +145,7 @@ func TestMulInt(t *testing.T) { } func TestMulFloat(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ round (mulf 1.1 1.1) 2 }}`, ExpectedOutput: "1.21"}, {Input: `{{ round (mulf 1.1 2.2) 2 }}`, ExpectedOutput: "2.42"}, {Input: `{{ round (3.3 | mulf 14.4) 2 }}`, ExpectedOutput: "47.52"}, @@ -156,7 +156,7 @@ func TestMulFloat(t *testing.T) { } func TestDivInt(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ div 1 1 }}`, ExpectedOutput: "1"}, {Input: `{{ div 1 2 }}`, ExpectedOutput: "0"}, {Input: `{{ div 1.1 1.1 }}`, ExpectedOutput: "1"}, @@ -169,7 +169,7 @@ func TestDivInt(t *testing.T) { } func TestDivFloat(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ round (divf 1.1 1.1) 2 }}`, ExpectedOutput: "1"}, {Input: `{{ round (divf 1.1 2.2) 2 }}`, ExpectedOutput: "0.5"}, {Input: `{{ 2 | divf 5 4 }}`, ExpectedOutput: "0.625"}, @@ -180,7 +180,7 @@ func TestDivFloat(t *testing.T) { } func TestMod(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ mod 10 4 }}`, ExpectedOutput: "2"}, {Input: `{{ mod 10 3 }}`, ExpectedOutput: "1"}, {Input: `{{ mod 10 2 }}`, ExpectedOutput: "0"}, @@ -197,7 +197,7 @@ func TestMod(t *testing.T) { } func TestMin(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ min 1 }}`, ExpectedOutput: "1"}, {Input: `{{ min 1 "1" }}`, ExpectedOutput: "1"}, {Input: `{{ min -1 0 1 }}`, ExpectedOutput: "-1"}, @@ -210,7 +210,7 @@ func TestMin(t *testing.T) { } func TestMinf(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ minf 1 }}`, ExpectedOutput: "1"}, {Input: `{{ minf 1 "1.1" }}`, ExpectedOutput: "1"}, {Input: `{{ minf -1.4 .0 2.1 }}`, ExpectedOutput: "-1.4"}, @@ -223,7 +223,7 @@ func TestMinf(t *testing.T) { } func TestMax(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ max 1 }}`, ExpectedOutput: "1"}, {Input: `{{ max 1 "1" }}`, ExpectedOutput: "1"}, {Input: `{{ max -1 0 1 }}`, ExpectedOutput: "1"}, @@ -236,7 +236,7 @@ func TestMax(t *testing.T) { } func TestMaxf(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ maxf 1 }}`, ExpectedOutput: "1"}, {Input: `{{ maxf 1.0 "1.1" }}`, ExpectedOutput: "1.1"}, {Input: `{{ maxf -1.5 0 1.4 }}`, ExpectedOutput: "1.4"}, diff --git a/registry/numeric/helpers.go b/registry/numeric/helpers.go index 0f96550..b65a8c0 100644 --- a/registry/numeric/helpers.go +++ b/registry/numeric/helpers.go @@ -3,8 +3,9 @@ package numeric import ( "reflect" - "github.com/go-sprout/sprout" "github.com/spf13/cast" + + "github.com/go-sprout/sprout" ) // operateNumeric applies a numericOperation to a slice of any type, converting diff --git a/registry/numeric/helpers_test.go b/registry/numeric/helpers_test.go index 3eee0d5..47b9c4d 100644 --- a/registry/numeric/helpers_test.go +++ b/registry/numeric/helpers_test.go @@ -8,7 +8,7 @@ import ( ) func TestOperateNumeric(t *testing.T) { - var tests = []struct { + tests := []struct { values []any op numericOperation initial any diff --git a/registry/random/functions_test.go b/registry/random/functions_test.go index a4d1237..b88933d 100644 --- a/registry/random/functions_test.go +++ b/registry/random/functions_test.go @@ -4,14 +4,15 @@ import ( "encoding/base64" "testing" - "github.com/go-sprout/sprout/pesticide" - "github.com/go-sprout/sprout/registry/random" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/go-sprout/sprout/pesticide" + "github.com/go-sprout/sprout/registry/random" ) func TestRandAlphaNumeric(t *testing.T) { - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Name: "TestRandAlphaNumWithNegativeValue", Template: `{{ randAlphaNum -1 }}`, Regexp: "", Length: 0}, {Name: "TestRandAlphaNumWithZero", Template: `{{ randAlphaNum 0 }}`, Regexp: "", Length: 0}, {Name: "TestRandAlphaNum", Template: `{{ randAlphaNum 100 }}`, Regexp: `^[a-zA-Z0-9]{100}$`, Length: 100}, @@ -21,7 +22,7 @@ func TestRandAlphaNumeric(t *testing.T) { } func TestRandAlpha(t *testing.T) { - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Name: "TestRandAlphaWithNegativeValue", Template: `{{ randAlpha -1 }}`, Regexp: "", Length: 0}, {Name: "TestRandAlphaWithZero", Template: `{{ randAlpha 0 }}`, Regexp: "", Length: 0}, {Name: "TestRandAlpha", Template: `{{ randAlpha 100 }}`, Regexp: `^[a-zA-Z]{100}$`, Length: 100}, @@ -31,7 +32,7 @@ func TestRandAlpha(t *testing.T) { } func TestRandAscii(t *testing.T) { - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Name: "TestRandAsciiWithNegativeValue", Template: `{{ randAscii -1 }}`, Regexp: "", Length: 0}, {Name: "TestRandAsciiWithZero", Template: `{{ randAscii 0 }}`, Regexp: "", Length: 0}, {Name: "TestRandAscii", Template: `{{ randAscii 100 }}`, Regexp: "^[[:ascii:]]{100}$", Length: 100}, @@ -41,7 +42,7 @@ func TestRandAscii(t *testing.T) { } func TestRandNumeric(t *testing.T) { - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Name: "TestRandNumericWithNegativeValue", Template: `{{ randNumeric -1 }}`, Regexp: "", Length: 0}, {Name: "TestRandNumericWithZero", Template: `{{ randNumeric 0 }}`, Regexp: "", Length: 0}, {Name: "TestRandNumeric", Template: `{{ randNumeric 100 }}`, Regexp: `^[0-9]{100}$`, Length: 100}, @@ -51,7 +52,7 @@ func TestRandNumeric(t *testing.T) { } func TestRandBytes(t *testing.T) { - var tests = []pesticide.RegexpTestCase{ + tests := []pesticide.RegexpTestCase{ {Name: "TestRandBytesWithNegativeValue", Template: `{{ randBytes -1 }}`, Regexp: "", Length: 0}, {Name: "TestRandBytesWithZero", Template: `{{ randBytes 0 }}`, Regexp: "", Length: 0}, {Name: "TestRandBytes", Template: `{{ randBytes 100 }}`, Regexp: "", Length: 100}, @@ -72,7 +73,7 @@ func TestRandBytes(t *testing.T) { } func TestRandInt(t *testing.T) { - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Name: "TestRandIntWithNegativeValue", Template: `{{ randInt -1 10 }}`, Regexp: "", Length: -1}, {Name: "BetweenZeroAndTen", Template: `{{ randInt 0 10 }}`, Regexp: `^[0-9]{1,2}$`, Length: -1}, {Name: "BetweenTenAndTwenty", Template: `{{ randInt 10 20 }}`, Regexp: `^[0-9]{1,2}$`, Length: -1}, diff --git a/registry/random/helpers_test.go b/registry/random/helpers_test.go index 6c35ee8..762dd6a 100644 --- a/registry/random/helpers_test.go +++ b/registry/random/helpers_test.go @@ -9,7 +9,7 @@ import ( func TestRandomString(t *testing.T) { rr := NewRegistry() - var tc = []struct { + tc := []struct { opts *randomOpts regexpString string length int diff --git a/registry/regexp/functions_test.go b/registry/regexp/functions_test.go index 706c8bd..f6a7d4f 100644 --- a/registry/regexp/functions_test.go +++ b/registry/regexp/functions_test.go @@ -8,7 +8,7 @@ import ( ) func TestRegexpFind(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexpFind", Input: `{{ regexFind "a(b+)" "aaabbb" }}`, ExpectedOutput: "abbb"}, {Name: "TestRegexpFindError", Input: `{{ regexFind "a(b+" "aaabbb" }}`, ExpectedErr: "error parsing regexp"}, } @@ -17,7 +17,7 @@ func TestRegexpFind(t *testing.T) { } func TestRegexpFindAll(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexpFindAllWithoutLimit", Input: `{{ regexFindAll "a(b+)" "aaabbb" -1 }}`, ExpectedOutput: "[abbb]"}, {Name: "TestRegexpFindAllWithLimit", Input: `{{ regexFindAll "a{2}" "aaaabbb" -1 }}`, ExpectedOutput: "[aa aa]"}, {Name: "TestRegexpFindAllWithNoMatch", Input: `{{ regexFindAll "a{2}" "none" -1 }}`, ExpectedOutput: "[]"}, @@ -28,7 +28,7 @@ func TestRegexpFindAll(t *testing.T) { } func TestRegexMatch(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexMatchValid", Input: `{{ regexMatch "^[a-zA-Z]+$" "Hello" }}`, ExpectedOutput: "true"}, {Name: "TestRegexMatchInvalidAlphaNumeric", Input: `{{ regexMatch "^[a-zA-Z]+$" "Hello123" }}`, ExpectedOutput: "false"}, {Name: "TestRegexMatchInvalidNumeric", Input: `{{ regexMatch "^[a-zA-Z]+$" "123" }}`, ExpectedOutput: "false"}, @@ -38,7 +38,7 @@ func TestRegexMatch(t *testing.T) { } func TestRegexSplit(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexpFindAllWithoutLimit", Input: `{{ regexSplit "a" "banana" -1 }}`, ExpectedOutput: "[b n n ]"}, {Name: "TestRegexpSplitZeroLimit", Input: `{{ regexSplit "a" "banana" 0 }}`, ExpectedOutput: "[]"}, {Name: "TestRegexpSplitOneLimit", Input: `{{ regexSplit "a" "banana" 1 }}`, ExpectedOutput: "[banana]"}, @@ -50,7 +50,7 @@ func TestRegexSplit(t *testing.T) { } func TestRegexReplaceAll(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexReplaceAllValid", Input: `{{ regexReplaceAll "a(x*)b" "-ab-axxb-" "T" }}`, ExpectedOutput: "-T-T-"}, {Name: "TestRegexReplaceAllWithDollarSign", Input: `{{ regexReplaceAll "a(x*)b" "-ab-axxb-" "$1" }}`, ExpectedOutput: "--xx-"}, {Name: "TestRegexReplaceAllWithDollarSignAndLetter", Input: `{{ regexReplaceAll "a(x*)b" "-ab-axxb-" "$1W" }}`, ExpectedOutput: "---"}, @@ -61,7 +61,7 @@ func TestRegexReplaceAll(t *testing.T) { } func TestRegexReplaceAllLiteral(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexReplaceAllLiteralValid", Input: `{{ regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "T" }}`, ExpectedOutput: "-T-T-"}, {Name: "TestRegexReplaceAllLiteralWithDollarSign", Input: `{{ regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "$1" }}`, ExpectedOutput: "-$1-$1-"}, {Name: "TestRegexReplaceAllLiteralWithDollarSignAndLetter", Input: `{{ regexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "$1W" }}`, ExpectedOutput: "-$1W-$1W-"}, @@ -72,7 +72,7 @@ func TestRegexReplaceAllLiteral(t *testing.T) { } func TestRegexQuoteMeta(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestRegexQuoteMetaALongLine", Input: `{{ regexQuoteMeta "Escaping $100? That's a lot." }}`, ExpectedOutput: "Escaping \\$100\\? That's a lot\\."}, {Name: "TestRegexQuoteMetaASemVer", Input: `{{ regexQuoteMeta "1.2.3" }}`, ExpectedOutput: "1\\.2\\.3"}, {Name: "TestRegexQuoteMetaNothing", Input: `{{ regexQuoteMeta "golang" }}`, ExpectedOutput: "golang"}, @@ -82,7 +82,7 @@ func TestRegexQuoteMeta(t *testing.T) { } func TestMustRegexFind(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ { Name: "TestMustRegexFindValid", Input: `{{ mustRegexFind "a(b+)" "aaabbb" }}`, @@ -101,7 +101,7 @@ func TestMustRegexFind(t *testing.T) { } func TestMustRegexFindAll(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ { Name: "TestMustRegexFindAllValid", Input: `{{ mustRegexFindAll "a(b+)" "aaabbb" -1 }}`, @@ -132,7 +132,7 @@ func TestMustRegexFindAll(t *testing.T) { } func TestMustRegexMatch(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ { Name: "TestMustRegexMatchValid", Input: `{{ mustRegexMatch "^[a-zA-Z]+$" "Hello" }}`, @@ -163,7 +163,7 @@ func TestMustRegexMatch(t *testing.T) { } func TestMustRegexSplit(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ { Name: "TestMustRegexSplitWithoutLimit", Input: `{{ mustRegexSplit "a" "banana" -1 }}`, @@ -206,7 +206,7 @@ func TestMustRegexSplit(t *testing.T) { } func TestMustRegexReplaceAll(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ { Name: "TestMustRegexReplaceAllValid", Input: `{{ mustRegexReplaceAll "a(x*)b" "-ab-axxb-" "T" }}`, @@ -243,7 +243,7 @@ func TestMustRegexReplaceAll(t *testing.T) { } func TestMustRegexReplaceAllLiteral(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ { Name: "TestMustRegexReplaceAllLiteralValid", Input: `{{ mustRegexReplaceAllLiteral "a(x*)b" "-ab-axxb-" "T" }}`, diff --git a/registry/semver/functions_test.go b/registry/semver/functions_test.go index a6dcf8b..8b86f64 100644 --- a/registry/semver/functions_test.go +++ b/registry/semver/functions_test.go @@ -8,8 +8,7 @@ import ( ) func TestSemver(t *testing.T) { - - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ semver "1.0.0" }}`, ExpectedOutput: "1.0.0"}, {Input: `{{ semver "1.0.0-alpha" }}`, ExpectedOutput: "1.0.0-alpha"}, {Input: `{{ semver "1.0.0-alpha.1" }}`, ExpectedOutput: "1.0.0-alpha.1"}, @@ -20,8 +19,7 @@ func TestSemver(t *testing.T) { } func TestSemverCompare(t *testing.T) { - - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ semverCompare "1.0.0" "1.0.0" }}`, ExpectedOutput: "true"}, {Input: `{{ semverCompare "1.0.0" "1.0.1" }}`, ExpectedOutput: "false"}, {Input: `{{ semverCompare "1.0.1" "1.0.0" }}`, ExpectedOutput: "false"}, @@ -34,7 +32,7 @@ func TestSemverCompare(t *testing.T) { pesticide.RunTestCases(t, semver.NewRegistry(), tc) - var mtc = []pesticide.TestCase{ + mtc := []pesticide.TestCase{ {Input: `{{ semverCompare "abc" "1.0.0" }}`, ExpectedErr: "improper constraint"}, {Input: `{{ semverCompare "1.0.0" "abc" }}`, ExpectedErr: "Invalid Semantic Version"}, } diff --git a/registry/slices/functions.go b/registry/slices/functions.go index 1dcce7b..1f724af 100644 --- a/registry/slices/functions.go +++ b/registry/slices/functions.go @@ -6,9 +6,10 @@ import ( "sort" "strings" + "github.com/spf13/cast" + "github.com/go-sprout/sprout/deprecated" "github.com/go-sprout/sprout/internal/helpers" - "github.com/spf13/cast" ) // List creates a list from the provided elements. diff --git a/registry/slices/functions_test.go b/registry/slices/functions_test.go index a8ad5ac..b1c50b4 100644 --- a/registry/slices/functions_test.go +++ b/registry/slices/functions_test.go @@ -8,7 +8,7 @@ import ( ) func TestList(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ list }}`, ExpectedOutput: "[]"}, {Input: `{{ .V | list "ab" true 4 5 }}`, ExpectedOutput: "[ab true 4 5 ]", Data: map[string]any{"V": nil}}, } @@ -17,7 +17,7 @@ func TestList(t *testing.T) { } func TestAppend(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ .V | append "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string{}}}, {Input: `{{ .V | append "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string(nil)}}, {Input: `{{ .V | append "a" }}`, ExpectedOutput: "[x a]", Data: map[string]any{"V": []string{"x"}}}, @@ -31,7 +31,7 @@ func TestAppend(t *testing.T) { } func TestPrepend(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ .V | prepend "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string{}}}, {Input: `{{ .V | prepend "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string(nil)}}, {Input: `{{ .V | prepend "a" }}`, ExpectedOutput: "[a x]", Data: map[string]any{"V": []string{"x"}}}, @@ -45,7 +45,7 @@ func TestPrepend(t *testing.T) { } func TestConcat(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ concat .V (list 1 2 3) }}`, ExpectedOutput: "[a 1 2 3]", Data: map[string]any{"V": []string{"a"}}}, {Input: `{{ list 4 5 | concat (list 1 2 3) }}`, ExpectedOutput: "[1 2 3 4 5]"}, {Input: `{{ concat .V (list 1 2 3) }}`, ExpectedOutput: "[1 2 3]", Data: map[string]any{"V": nil}}, @@ -58,7 +58,7 @@ func TestConcat(t *testing.T) { } func TestChunk(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ chunk 2 .V }}`, ExpectedOutput: "[[a b] [c d] [e]]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ chunk 2 .V }}`, ExpectedOutput: "[[a b] [c d]]", Data: map[string]any{"V": []string{"a", "b", "c", "d"}}}, {Input: `{{ chunk 2 .V }}`, ExpectedOutput: "[[a b]]", Data: map[string]any{"V": []string{"a", "b"}}}, @@ -71,7 +71,7 @@ func TestChunk(t *testing.T) { } func TestUniq(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ uniq .V }}`, ExpectedOutput: "[a b c]", Data: map[string]any{"V": []string{"a", "b", "c", "a", "b", "c"}}}, {Input: `{{ uniq .V }}`, ExpectedOutput: "[a b c]", Data: map[string]any{"V": []string{"a", "b", "c"}}}, {Input: `{{ uniq .V }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string{"a", "a", "a"}}}, @@ -85,7 +85,7 @@ func TestUniq(t *testing.T) { } func TestCompact(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ compact .V }}`, ExpectedOutput: "[a b c]", Data: map[string]any{"V": []string{"a", "", "b", "", "c"}}}, {Input: `{{ compact .V }}`, ExpectedOutput: "[a a]", Data: map[string]any{"V": []string{"a", "", "a"}}}, {Input: `{{ compact .V }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string{"a"}}}, @@ -101,7 +101,7 @@ func TestCompact(t *testing.T) { } func TestSlice(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ .V | slice }}`, ExpectedOutput: "[a b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ .V | slice 1 }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ .V | slice 1 3 }}`, ExpectedOutput: "[b c]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, @@ -119,7 +119,7 @@ func TestSlice(t *testing.T) { } func TestHas(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ .V | has "a" }}`, ExpectedOutput: "true", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ .V | has "a" }}`, ExpectedOutput: "false", Data: map[string]any{"V": []string{"b", "c", "d", "e"}}}, {Input: `{{ .V | has 1 }}`, ExpectedOutput: "true", Data: map[string]any{"V": []any{"b", 1, nil, struct{}{}}}}, @@ -134,7 +134,7 @@ func TestHas(t *testing.T) { } func TestWithout(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ .V | without "a" }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ .V | without "a" }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"b", "c", "d", "e"}}}, {Input: `{{ .V | without "a" }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"b", "c", "d", "e", "a"}}}, @@ -149,7 +149,7 @@ func TestWithout(t *testing.T) { } func TestRest(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ rest .V }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ rest .V }}`, ExpectedOutput: "[c d e]", Data: map[string]any{"V": []string{"b", "c", "d", "e"}}}, {Input: `{{ rest .V }}`, ExpectedOutput: "[c d e a]", Data: map[string]any{"V": []string{"b", "c", "d", "e", "a"}}}, @@ -163,7 +163,7 @@ func TestRest(t *testing.T) { } func TestInitial(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ initial .V }}`, ExpectedOutput: "[a b c d]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ initial .V }}`, ExpectedOutput: "[a b c]", Data: map[string]any{"V": []string{"a", "b", "c", "d"}}}, {Input: `{{ initial .V }}`, ExpectedOutput: "[]", Data: map[string]any{"V": []string{"a"}}}, @@ -176,7 +176,7 @@ func TestInitial(t *testing.T) { } func TestFirst(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ first .V }}`, ExpectedOutput: "a", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ first .V }}`, ExpectedOutput: "", Data: map[string]any{"V": []string{}}}, {Input: `{{ first .V }}`, Data: map[string]any{"V": nil}, ExpectedErr: "cannot first nil"}, @@ -187,7 +187,7 @@ func TestFirst(t *testing.T) { } func TestLast(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ last .V }}`, ExpectedOutput: "e", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ last .V }}`, ExpectedOutput: "", Data: map[string]any{"V": []string{}}}, {Input: `{{ last .V }}`, Data: map[string]any{"V": nil}, ExpectedErr: "cannot last nil"}, @@ -198,7 +198,7 @@ func TestLast(t *testing.T) { } func TestReverse(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ reverse .V }}`, ExpectedOutput: "[e d c b a]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ reverse .V }}`, ExpectedOutput: "[a b c d e]", Data: map[string]any{"V": []string{"e", "d", "c", "b", "a"}}}, {Input: `{{ reverse .V }}`, ExpectedOutput: "[]", Data: map[string]any{"V": []string{}}}, @@ -210,7 +210,7 @@ func TestReverse(t *testing.T) { } func TestSortAlpha(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ sortAlpha .V }}`, ExpectedOutput: "[a b c d e]", Data: map[string]any{"V": []string{"e", "d", "c", "b", "a"}}}, {Input: `{{ sortAlpha .V }}`, ExpectedOutput: "[1 2 3 4 5 a]", Data: map[string]any{"V": []any{5, 4, 3, 2, 1, "a"}}}, {Input: `{{ sortAlpha .V }}`, ExpectedOutput: "[]", Data: map[string]any{"V": []string{}}}, @@ -221,7 +221,7 @@ func TestSortAlpha(t *testing.T) { } func TestSplitList(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ .V | splitList "," }}`, ExpectedOutput: "[a b c d e]", Data: map[string]any{"V": "a,b,c,d,e"}}, {Input: `{{ .V | splitList "," }}`, ExpectedOutput: "[a b c d e ]", Data: map[string]any{"V": "a,b,c,d,e,"}}, {Input: `{{ .V | splitList "," }}`, ExpectedOutput: "[ a b c d e]", Data: map[string]any{"V": ",a,b,c,d,e"}}, @@ -233,7 +233,7 @@ func TestSplitList(t *testing.T) { } func TestStrSlice(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ strSlice .V }}`, ExpectedOutput: "[a b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ strSlice .V }}`, ExpectedOutput: "[5 4 3 2 1]", Data: map[string]any{"V": []int{5, 4, 3, 2, 1}}}, {Input: `{{ strSlice .V }}`, ExpectedOutput: "[5 a true 1]", Data: map[string]any{"V": []any{5, "a", true, nil, 1}}}, @@ -244,7 +244,7 @@ func TestStrSlice(t *testing.T) { } func TestUntil(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{range $i, $e := until 5}}({{$i}}{{$e}}){{end}}`, ExpectedOutput: "(00)(11)(22)(33)(44)"}, {Input: `{{range $i, $e := until -5}}({{$i}}{{$e}}){{end}}`, ExpectedOutput: "(00)(1-1)(2-2)(3-3)(4-4)"}, } @@ -253,7 +253,7 @@ func TestUntil(t *testing.T) { } func TestUntilStep(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{range $i, $e := untilStep 0 5 1}}({{$i}}{{$e}}){{end}}`, ExpectedOutput: "(00)(11)(22)(33)(44)"}, {Input: `{{range $i, $e := untilStep 3 6 1}}({{$i}}{{$e}}){{end}}`, ExpectedOutput: "(03)(14)(25)"}, {Input: `{{range $i, $e := untilStep 0 -10 -2}}({{$i}}{{$e}}){{end}}`, ExpectedOutput: "(00)(1-2)(2-4)(3-6)(4-8)"}, diff --git a/registry/slices/helpers_test.go b/registry/slices/helpers_test.go index 31c7c04..74dcb8f 100644 --- a/registry/slices/helpers_test.go +++ b/registry/slices/helpers_test.go @@ -26,7 +26,6 @@ func TestIsComparable(t *testing.T) { assert.False(t, r.isComparable(time.Second*5)) assert.False(t, r.isComparable(make(chan any))) assert.False(t, r.isComparable(nil)) - } func TestSlicesRegistry_inList(t *testing.T) { diff --git a/registry/slices/sprig_deprecated_functions_signature_test.go b/registry/slices/sprig_deprecated_functions_signature_test.go index 2ca7832..d374003 100644 --- a/registry/slices/sprig_deprecated_functions_signature_test.go +++ b/registry/slices/sprig_deprecated_functions_signature_test.go @@ -8,7 +8,7 @@ import ( ) func TestDeprecatedWithout(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ without .V "a" }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ without .V "a" }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"b", "c", "d", "e"}}}, {Input: `{{ without .V "a" }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"b", "c", "d", "e", "a"}}}, @@ -22,7 +22,7 @@ func TestDeprecatedWithout(t *testing.T) { } func TestDeprecatedAppend(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ append .V "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string{}}}, {Input: `{{ append .V "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string(nil)}}, {Input: `{{ append .V "a" }}`, ExpectedOutput: "[x a]", Data: map[string]any{"V": []string{"x"}}}, @@ -35,7 +35,7 @@ func TestDeprecatedAppend(t *testing.T) { } func TestDeprecatedPrepend(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ prepend .V "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string{}}}, {Input: `{{ prepend .V "a" }}`, ExpectedOutput: "[a]", Data: map[string]any{"V": []string(nil)}}, {Input: `{{ prepend .V "a" }}`, ExpectedOutput: "[a x]", Data: map[string]any{"V": []string{"x"}}}, @@ -48,7 +48,7 @@ func TestDeprecatedPrepend(t *testing.T) { } func TestDeprecatedSlice(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{ slice .V }}`, ExpectedOutput: "[a b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ slice .V 1 }}`, ExpectedOutput: "[b c d e]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, {Input: `{{ slice .V 1 3 }}`, ExpectedOutput: "[b c]", Data: map[string]any{"V": []string{"a", "b", "c", "d", "e"}}}, diff --git a/registry/std/functions_test.go b/registry/std/functions_test.go index 4f81d24..1858771 100644 --- a/registry/std/functions_test.go +++ b/registry/std/functions_test.go @@ -9,14 +9,14 @@ import ( // TestHello asserts the Hello method returns the expected greeting. func TestHello(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestHello", Input: `{{hello}}`, ExpectedOutput: "Hello!"}, } pesticide.RunTestCases(t, std.NewRegistry(), tc) } func TestDefault(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestDefaultEmptyInput", Input: `{{default "default" ""}}`, ExpectedOutput: "default"}, {Name: "TestDefaultGivenInput", Input: `{{default "default" "given"}}`, ExpectedOutput: "given"}, {Name: "TestDefaultIntInput", Input: `{{default "default" 42}}`, ExpectedOutput: "42"}, @@ -33,7 +33,7 @@ func TestDefault(t *testing.T) { } func TestEmpty(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestEmptyEmptyInput", Input: `{{if empty ""}}1{{else}}0{{end}}`, ExpectedOutput: "1"}, {Name: "TestEmptyGivenInput", Input: `{{if empty "given"}}1{{else}}0{{end}}`, ExpectedOutput: "0"}, {Name: "TestEmptyIntInput", Input: `{{if empty 42}}1{{else}}0{{end}}`, ExpectedOutput: "0"}, @@ -54,7 +54,7 @@ func TestEmpty(t *testing.T) { } func TestAll(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestAllEmptyInput", Input: `{{if all ""}}1{{else}}0{{end}}`, ExpectedOutput: "0"}, {Name: "TestAllGivenInput", Input: `{{if all "given"}}1{{else}}0{{end}}`, ExpectedOutput: "1"}, {Name: "TestAllIntInput", Input: `{{if all 42 0 1}}1{{else}}0{{end}}`, ExpectedOutput: "0"}, @@ -68,7 +68,7 @@ func TestAll(t *testing.T) { } func TestAny(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestAnyEmptyInput", Input: `{{if any ""}}1{{else}}0{{end}}`, ExpectedOutput: "0"}, {Name: "TestAnyGivenInput", Input: `{{if any "given"}}1{{else}}0{{end}}`, ExpectedOutput: "1"}, {Name: "TestAnyIntInput", Input: `{{if any 42 0 1}}1{{else}}0{{end}}`, ExpectedOutput: "1"}, @@ -82,7 +82,7 @@ func TestAny(t *testing.T) { } func TestCoalesce(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestCoalesceEmptyInput", Input: `{{coalesce ""}}`, ExpectedOutput: ""}, {Name: "TestCoalesceGivenInput", Input: `{{coalesce "given"}}`, ExpectedOutput: "given"}, {Name: "TestCoalesceIntInput", Input: `{{ coalesce "" 0 nil 42 }}`, ExpectedOutput: "42"}, @@ -96,7 +96,7 @@ func TestCoalesce(t *testing.T) { } func TestTernary(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Input: `{{true | ternary "foo" "bar"}}`, ExpectedOutput: "foo"}, {Input: `{{ternary "foo" "bar" true}}`, ExpectedOutput: "foo"}, {Input: `{{false | ternary "foo" "bar"}}`, ExpectedOutput: "bar"}, @@ -107,7 +107,7 @@ func TestTernary(t *testing.T) { } func TestCat(t *testing.T) { - var tc = []pesticide.TestCase{ + tc := []pesticide.TestCase{ {Name: "TestCatEmptyInput", Input: `{{cat ""}}`, ExpectedOutput: ""}, {Name: "TestCatGivenInput", Input: `{{cat "given"}}`, ExpectedOutput: "given"}, {Name: "TestCatIntInput", Input: `{{cat 42}}`, ExpectedOutput: "42"}, diff --git a/registry/uniqueid/functions_test.go b/registry/uniqueid/functions_test.go index fcc1470..a66a5cc 100644 --- a/registry/uniqueid/functions_test.go +++ b/registry/uniqueid/functions_test.go @@ -8,7 +8,7 @@ import ( ) func TestUuidv4(t *testing.T) { - var tc = []pesticide.RegexpTestCase{ + tc := []pesticide.RegexpTestCase{ {Name: "TestUuidv4", Template: `{{uuidv4}}`, Regexp: `^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[\da-f]{4}-[\da-f]{12}$`, Length: 36}, }