github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/strings_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"gotest.tools/v3/assert"
     8  )
     9  
    10  func TestStrings_Indent(t *testing.T) {
    11  	inOutTest(t, `{{ strings.Indent "   " "hello world" }}
    12  {{ "hello\nmultiline\nworld" | indent 2 "-" }}
    13  {{ "foo\nbar" | strings.Indent 2 }}
    14      {{"hello\nworld" | strings.Indent 5 | strings.TrimSpace }}
    15  `, `   hello world
    16  --hello
    17  --multiline
    18  --world
    19    foo
    20    bar
    21      hello
    22       world
    23  `)
    24  }
    25  
    26  func TestStrings_Repeat(t *testing.T) {
    27  	inOutTest(t, `ba{{ strings.Repeat 2 "na" }}`, `banana`)
    28  
    29  	_, _, err := cmd(t, "-i", `ba{{ strings.Repeat 9223372036854775807 "na" }}`).run()
    30  	assert.ErrorContains(t, err, `too long: causes overflow`)
    31  
    32  	_, _, err = cmd(t, "-i", `ba{{ strings.Repeat -1 "na" }}`).run()
    33  	assert.ErrorContains(t, err, `negative count`)
    34  }
    35  
    36  func TestStrings_Slug(t *testing.T) {
    37  	inOutTest(t, `{{ strings.Slug "Hellö, Wôrld! Free @ last..." }}`, `hello-world-free-at-last`)
    38  }
    39  
    40  func TestStrings_CaseFuncs(t *testing.T) {
    41  	inOutTest(t, `{{ strings.ToLower "HELLO" }}
    42  {{ strings.ToUpper "hello" }}
    43  {{ strings.Title "is there anybody out there?" }}
    44  {{ strings.Title "foo,bar᳇džaz"}}
    45  `,
    46  		`hello
    47  HELLO
    48  Is There Anybody Out There?
    49  Foo,Bar᳇Džaz
    50  `)
    51  	inOutTest(t, `{{ strings.CamelCase "Hellö, Wôrld! Free @ last..." }}
    52  {{ strings.SnakeCase "Hellö, Wôrld! Free @ last..." }}
    53  {{ strings.KebabCase "Hellö, Wôrld! Free @ last..." }}`, `HellöWôrldFreeLast
    54  Hellö_wôrld_free_last
    55  Hellö-wôrld-free-last`)
    56  }
    57  
    58  func TestStrings_WordWrap(t *testing.T) {
    59  	out := `There shouldn't be any wrapping of long words or URLs because that would break
    60  things very badly. To wit:
    61  https://example.com/a/super-long/url/that-shouldnt-be?wrapped=for+fear+of#the-breaking-of-functionality
    62  should appear on its own line, regardless of the desired word-wrapping width
    63  that has been set.`
    64  	text := strings.ReplaceAll(out, "\n", " ")
    65  	in := `{{ print "` + text + `" | strings.WordWrap 80 }}`
    66  	inOutTest(t, in, out)
    67  }