From 6b4669b069b85a0619f23bb31cd75d94a05c0f5b Mon Sep 17 00:00:00 2001 From: 42Atomys Date: Thu, 9 May 2024 17:34:33 +0200 Subject: [PATCH] test: clean up the remaining tests for migration --- random_functions_test.go | 15 ++++- to_migrate_test.go | 125 --------------------------------------- 2 files changed, 14 insertions(+), 126 deletions(-) diff --git a/random_functions_test.go b/random_functions_test.go index 61c9133..5fbe019 100644 --- a/random_functions_test.go +++ b/random_functions_test.go @@ -25,7 +25,9 @@ func testRandHelper(t *testing.T, tcs []randTestCase) { assert.NoError(t, err) assert.Regexp(t, test.regexp, result) - assert.Len(t, result, test.length) + if test.length != -1 { + assert.Len(t, result, test.length) + } }) } } @@ -91,6 +93,17 @@ func TestRandBytes(t *testing.T) { } } +func TestRandInt(t *testing.T) { + var tests = []randTestCase{ + {"TestRandIntWithNegativeValue", `{{ randInt -1 10 }}`, "", -1}, + {"BetweenZeroAndTen", `{{ randInt 0 10 }}`, `^[0-9]{1,2}$`, -1}, + {"BetweenTenAndTwenty", `{{ randInt 10 20 }}`, `^[0-9]{1,2}$`, -1}, + {"BetweenNegativeTenAndTwenty", `{{ randInt -10 20 }}`, `^-?[0-9]{1,2}$`, -1}, + } + + testRandHelper(t, tests) +} + func TestRandomString(t *testing.T) { fh := NewFunctionHandler() assert.Regexp(t, "^[0-9]{100}$", fh.randomString(100, &randomOpts{withNumbers: true})) diff --git a/to_migrate_test.go b/to_migrate_test.go index 504b1d5..e56e590 100644 --- a/to_migrate_test.go +++ b/to_migrate_test.go @@ -3,13 +3,10 @@ package sprout import ( "bytes" "crypto/x509" - "encoding/base32" "encoding/base64" "encoding/pem" "fmt" "net" - "os" - "strconv" "strings" "testing" "text/template" @@ -105,51 +102,6 @@ func TestToString(t *testing.T) { assert.NoError(t, runt(tpl, "string")) } -func TestToStrings(t *testing.T) { - tpl := `{{ $s := list 1 2 3 | toStrings }}{{ index $s 1 | kindOf }}` - assert.NoError(t, runt(tpl, "string")) - tpl = `{{ list 1 .value 2 | toStrings }}` - values := map[string]interface{}{"value": nil} - if err := runtv(tpl, `[1 2]`, values); err != nil { - t.Error(err) - } -} - -func TestBase64EncodeDecode(t *testing.T) { - magicWord := "coffee" - expect := base64.StdEncoding.EncodeToString([]byte(magicWord)) - - if expect == magicWord { - t.Fatal("Encoder doesn't work.") - } - - tpl := `{{b64enc "coffee"}}` - if err := runt(tpl, expect); err != nil { - t.Error(err) - } - tpl = fmt.Sprintf("{{b64dec %q}}", expect) - if err := runt(tpl, magicWord); err != nil { - t.Error(err) - } -} -func TestBase32EncodeDecode(t *testing.T) { - magicWord := "coffee" - expect := base32.StdEncoding.EncodeToString([]byte(magicWord)) - - if expect == magicWord { - t.Fatal("Encoder doesn't work.") - } - - tpl := `{{b32enc "coffee"}}` - if err := runt(tpl, expect); err != nil { - t.Error(err) - } - tpl = fmt.Sprintf("{{b32dec %q}}", expect) - if err := runt(tpl, magicWord); err != nil { - t.Error(err) - } -} - func TestSemverCompare(t *testing.T) { tests := map[string]string{ `{{ semverCompare "1.2.3" "1.2.3" }}`: `true`, @@ -174,18 +126,6 @@ func TestSemver(t *testing.T) { } } -func TestBiggest(t *testing.T) { - tpl := `{{ biggest 1 2 3 345 5 6 7}}` - if err := runt(tpl, `345`); err != nil { - t.Error(err) - } - - tpl = `{{ max 345}}` - if err := runt(tpl, `345`); err != nil { - t.Error(err) - } -} - func TestToFloat64(t *testing.T) { fh := NewFunctionHandler() target := float64(102) @@ -315,26 +255,6 @@ func TestToDecimal(t *testing.T) { } } -func TestRandomInt(t *testing.T) { - var tests = []struct { - min int - max int - }{ - {10, 11}, - {10, 13}, - {0, 1}, - {5, 50}, - } - for _, v := range tests { - x, _ := runRaw(fmt.Sprintf(`{{ randInt %d %d }}`, v.min, v.max), nil) - r, err := strconv.Atoi(x) - assert.NoError(t, err) - assert.True(t, func(min, max, r int) bool { - return r >= v.min && r < v.max - }(v.min, v.max, r)) - } -} - func TestGetHostByName(t *testing.T) { tpl := `{{"www.google.com" | getHostByName}}` @@ -345,13 +265,6 @@ func TestGetHostByName(t *testing.T) { assert.NotEmpty(t, ip) } -func TestTuple(t *testing.T) { - tpl := `{{$t := tuple 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}` - if err := runt(tpl, "foo1a"); err != nil { - t.Error(err) - } -} - func TestIssue188(t *testing.T) { tests := map[string]string{ @@ -412,23 +325,6 @@ func runRaw(tpl string, vars interface{}) (string, error) { return b.String(), nil } -func Example() { - // Set up variables and template. - vars := map[string]interface{}{"Name": " John Jacob Jingleheimer Schmidt "} - tpl := `Hello {{.Name | trim | lower}}` - - // Get the sprout function map. - t := template.Must(template.New("test").Funcs(FuncMap()).Parse(tpl)) - - err := t.Execute(os.Stdout, vars) - if err != nil { - fmt.Printf("Error during template execution: %s", err) - return - } - // Output: - // Hello john jacob jingleheimer schmidt -} - func TestToDate(t *testing.T) { tpl := `{{toDate "2006-01-02" "2017-12-31" | date "02/01/2006"}}` if err := runt(tpl, "31/12/2017"); err != nil { @@ -592,27 +488,6 @@ func TestGenPrivateKey(t *testing.T) { } } -func TestUUIDGeneration(t *testing.T) { - tpl := `{{uuidv4}}` - out, err := runRaw(tpl, nil) - if err != nil { - t.Error(err) - } - - if len(out) != 36 { - t.Error("Expected UUID of length 36") - } - - out2, err := runRaw(tpl, nil) - if err != nil { - t.Error(err) - } - - if out == out2 { - t.Error("Expected subsequent UUID generations to be different") - } -} - func TestBuildCustomCert(t *testing.T) { ca, _ := NewFunctionHandler().GenerateCertificateAuthority("example.com", 365) tpl := fmt.Sprintf(