...

Source file src/github.com/go-task/slim-sprig/v3/strings_test.go

Documentation: github.com/go-task/slim-sprig/v3

     1  package sprig
     2  
     3  import (
     4  	"encoding/base32"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestSubstr(t *testing.T) {
    13  	tpl := `{{"fooo" | substr 0 3 }}`
    14  	if err := runt(tpl, "foo"); err != nil {
    15  		t.Error(err)
    16  	}
    17  }
    18  
    19  func TestSubstr_shorterString(t *testing.T) {
    20  	tpl := `{{"foo" | substr 0 10 }}`
    21  	if err := runt(tpl, "foo"); err != nil {
    22  		t.Error(err)
    23  	}
    24  }
    25  
    26  func TestTrunc(t *testing.T) {
    27  	tpl := `{{ "foooooo" | trunc 3 }}`
    28  	if err := runt(tpl, "foo"); err != nil {
    29  		t.Error(err)
    30  	}
    31  	tpl = `{{ "baaaaaar" | trunc -3 }}`
    32  	if err := runt(tpl, "aar"); err != nil {
    33  		t.Error(err)
    34  	}
    35  	tpl = `{{ "baaaaaar" | trunc -999 }}`
    36  	if err := runt(tpl, "baaaaaar"); err != nil {
    37  		t.Error(err)
    38  	}
    39  	tpl = `{{ "baaaaaz" | trunc 0 }}`
    40  	if err := runt(tpl, ""); err != nil {
    41  		t.Error(err)
    42  	}
    43  }
    44  
    45  func TestQuote(t *testing.T) {
    46  	tpl := `{{quote "a" "b" "c"}}`
    47  	if err := runt(tpl, `"a" "b" "c"`); err != nil {
    48  		t.Error(err)
    49  	}
    50  	tpl = `{{quote "\"a\"" "b" "c"}}`
    51  	if err := runt(tpl, `"\"a\"" "b" "c"`); err != nil {
    52  		t.Error(err)
    53  	}
    54  	tpl = `{{quote 1 2 3 }}`
    55  	if err := runt(tpl, `"1" "2" "3"`); err != nil {
    56  		t.Error(err)
    57  	}
    58  	tpl = `{{ .value | quote }}`
    59  	values := map[string]interface{}{"value": nil}
    60  	if err := runtv(tpl, ``, values); err != nil {
    61  		t.Error(err)
    62  	}
    63  }
    64  func TestSquote(t *testing.T) {
    65  	tpl := `{{squote "a" "b" "c"}}`
    66  	if err := runt(tpl, `'a' 'b' 'c'`); err != nil {
    67  		t.Error(err)
    68  	}
    69  	tpl = `{{squote 1 2 3 }}`
    70  	if err := runt(tpl, `'1' '2' '3'`); err != nil {
    71  		t.Error(err)
    72  	}
    73  	tpl = `{{ .value | squote }}`
    74  	values := map[string]interface{}{"value": nil}
    75  	if err := runtv(tpl, ``, values); err != nil {
    76  		t.Error(err)
    77  	}
    78  }
    79  
    80  func TestContains(t *testing.T) {
    81  	// Mainly, we're just verifying the paramater order swap.
    82  	tests := []string{
    83  		`{{if contains "cat" "fair catch"}}1{{end}}`,
    84  		`{{if hasPrefix "cat" "catch"}}1{{end}}`,
    85  		`{{if hasSuffix "cat" "ducat"}}1{{end}}`,
    86  	}
    87  	for _, tt := range tests {
    88  		if err := runt(tt, "1"); err != nil {
    89  			t.Error(err)
    90  		}
    91  	}
    92  }
    93  
    94  func TestTrim(t *testing.T) {
    95  	tests := []string{
    96  		`{{trim "   5.00   "}}`,
    97  		`{{trimAll "$" "$5.00$"}}`,
    98  		`{{trimPrefix "$" "$5.00"}}`,
    99  		`{{trimSuffix "$" "5.00$"}}`,
   100  	}
   101  	for _, tt := range tests {
   102  		if err := runt(tt, "5.00"); err != nil {
   103  			t.Error(err)
   104  		}
   105  	}
   106  }
   107  
   108  func TestSplit(t *testing.T) {
   109  	tpl := `{{$v := "foo$bar$baz" | split "$"}}{{$v._0}}`
   110  	if err := runt(tpl, "foo"); err != nil {
   111  		t.Error(err)
   112  	}
   113  }
   114  
   115  func TestSplitn(t *testing.T) {
   116  	tpl := `{{$v := "foo$bar$baz" | splitn "$" 2}}{{$v._0}}`
   117  	if err := runt(tpl, "foo"); err != nil {
   118  		t.Error(err)
   119  	}
   120  }
   121  
   122  func TestToString(t *testing.T) {
   123  	tpl := `{{ toString 1 | kindOf }}`
   124  	assert.NoError(t, runt(tpl, "string"))
   125  }
   126  
   127  func TestToStrings(t *testing.T) {
   128  	tpl := `{{ $s := list 1 2 3 | toStrings }}{{ index $s 1 | kindOf }}`
   129  	assert.NoError(t, runt(tpl, "string"))
   130  	tpl = `{{ list 1 .value 2 | toStrings }}`
   131  	values := map[string]interface{}{"value": nil}
   132  	if err := runtv(tpl, `[1 2]`, values); err != nil {
   133  		t.Error(err)
   134  	}
   135  }
   136  
   137  func TestJoin(t *testing.T) {
   138  	assert.NoError(t, runt(`{{ tuple "a" "b" "c" | join "-" }}`, "a-b-c"))
   139  	assert.NoError(t, runt(`{{ tuple 1 2 3 | join "-" }}`, "1-2-3"))
   140  	assert.NoError(t, runtv(`{{ join "-" .V }}`, "a-b-c", map[string]interface{}{"V": []string{"a", "b", "c"}}))
   141  	assert.NoError(t, runtv(`{{ join "-" .V }}`, "abc", map[string]interface{}{"V": "abc"}))
   142  	assert.NoError(t, runtv(`{{ join "-" .V }}`, "1-2-3", map[string]interface{}{"V": []int{1, 2, 3}}))
   143  	assert.NoError(t, runtv(`{{ join "-" .value }}`, "1-2", map[string]interface{}{"value": []interface{}{"1", nil, "2"}}))
   144  }
   145  
   146  func TestSortAlpha(t *testing.T) {
   147  	// Named `append` in the function map
   148  	tests := map[string]string{
   149  		`{{ list "c" "a" "b" | sortAlpha | join "" }}`: "abc",
   150  		`{{ list 2 1 4 3 | sortAlpha | join "" }}`:     "1234",
   151  	}
   152  	for tpl, expect := range tests {
   153  		assert.NoError(t, runt(tpl, expect))
   154  	}
   155  }
   156  func TestBase64EncodeDecode(t *testing.T) {
   157  	magicWord := "coffee"
   158  	expect := base64.StdEncoding.EncodeToString([]byte(magicWord))
   159  
   160  	if expect == magicWord {
   161  		t.Fatal("Encoder doesn't work.")
   162  	}
   163  
   164  	tpl := `{{b64enc "coffee"}}`
   165  	if err := runt(tpl, expect); err != nil {
   166  		t.Error(err)
   167  	}
   168  	tpl = fmt.Sprintf("{{b64dec %q}}", expect)
   169  	if err := runt(tpl, magicWord); err != nil {
   170  		t.Error(err)
   171  	}
   172  }
   173  func TestBase32EncodeDecode(t *testing.T) {
   174  	magicWord := "coffee"
   175  	expect := base32.StdEncoding.EncodeToString([]byte(magicWord))
   176  
   177  	if expect == magicWord {
   178  		t.Fatal("Encoder doesn't work.")
   179  	}
   180  
   181  	tpl := `{{b32enc "coffee"}}`
   182  	if err := runt(tpl, expect); err != nil {
   183  		t.Error(err)
   184  	}
   185  	tpl = fmt.Sprintf("{{b32dec %q}}", expect)
   186  	if err := runt(tpl, magicWord); err != nil {
   187  		t.Error(err)
   188  	}
   189  }
   190  
   191  func TestCat(t *testing.T) {
   192  	tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
   193  	if err := runt(tpl, "a b c"); err != nil {
   194  		t.Error(err)
   195  	}
   196  	tpl = `{{ .value | cat "a" "b"}}`
   197  	values := map[string]interface{}{"value": nil}
   198  	if err := runtv(tpl, "a b", values); err != nil {
   199  		t.Error(err)
   200  	}
   201  }
   202  
   203  func TestIndent(t *testing.T) {
   204  	tpl := `{{indent 4 "a\nb\nc"}}`
   205  	if err := runt(tpl, "    a\n    b\n    c"); err != nil {
   206  		t.Error(err)
   207  	}
   208  }
   209  
   210  func TestNindent(t *testing.T) {
   211  	tpl := `{{nindent 4 "a\nb\nc"}}`
   212  	if err := runt(tpl, "\n    a\n    b\n    c"); err != nil {
   213  		t.Error(err)
   214  	}
   215  }
   216  
   217  func TestReplace(t *testing.T) {
   218  	tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
   219  	if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {
   220  		t.Error(err)
   221  	}
   222  }
   223  
   224  func TestPlural(t *testing.T) {
   225  	tpl := `{{$num := len "two"}}{{$num}} {{$num | plural "1 char" "chars"}}`
   226  	if err := runt(tpl, "3 chars"); err != nil {
   227  		t.Error(err)
   228  	}
   229  	tpl = `{{len "t" | plural "cheese" "%d chars"}}`
   230  	if err := runt(tpl, "cheese"); err != nil {
   231  		t.Error(err)
   232  	}
   233  }
   234  

View as plain text