Skip to content

Commit

Permalink
test: clean up the remaining tests for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed May 9, 2024
1 parent fa611e7 commit 6b4669b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 126 deletions.
15 changes: 14 additions & 1 deletion random_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Expand Down Expand Up @@ -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}))
Expand Down
125 changes: 0 additions & 125 deletions to_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package sprout
import (
"bytes"
"crypto/x509"
"encoding/base32"
"encoding/base64"
"encoding/pem"
"fmt"
"net"
"os"
"strconv"
"strings"
"testing"
"text/template"
Expand Down Expand Up @@ -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`,
Expand All @@ -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)
Expand Down Expand Up @@ -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}}`

Expand All @@ -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{

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 6b4669b

Please sign in to comment.