github.com/hairyhenderson/templater@v3.5.0+incompatible/tests/integration/strings_test.go (about)

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"strings"
     7  
     8  	. "gopkg.in/check.v1"
     9  
    10  	"github.com/gotestyourself/gotestyourself/icmd"
    11  )
    12  
    13  type StringsSuite struct{}
    14  
    15  var _ = Suite(&StringsSuite{})
    16  
    17  func (s *StringsSuite) TestIndent(c *C) {
    18  	result := icmd.RunCommand(GomplateBin, "-i",
    19  		`{{ strings.Indent "   " "hello world" }}
    20  {{ "hello\nmultiline\nworld" | indent 2 "-" }}
    21  {{ "foo\nbar" | strings.Indent 2 }}
    22      {{"hello\nworld" | strings.Indent 5 | strings.TrimSpace }}
    23  `)
    24  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `   hello world
    25  --hello
    26  --multiline
    27  --world
    28    foo
    29    bar
    30      hello
    31       world`})
    32  }
    33  
    34  func (s *StringsSuite) TestRepeat(c *C) {
    35  	result := icmd.RunCommand(GomplateBin, "-i",
    36  		`ba{{ strings.Repeat 2 "na" }}`)
    37  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `banana`})
    38  
    39  	result = icmd.RunCommand(GomplateBin, "-i",
    40  		`ba{{ strings.Repeat 9223372036854775807 "na" }}`)
    41  	result.Assert(c, icmd.Expected{ExitCode: 1, Err: `too long: causes overflow`})
    42  
    43  	result = icmd.RunCommand(GomplateBin, "-i",
    44  		`ba{{ strings.Repeat -1 "na" }}`)
    45  	result.Assert(c, icmd.Expected{ExitCode: 1, Err: `negative count`})
    46  }
    47  
    48  func (s *StringsSuite) TestSlug(c *C) {
    49  	result := icmd.RunCommand(GomplateBin, "-i",
    50  		`{{ strings.Slug "Hellö, Wôrld! Free @ last..." }}`)
    51  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `hello-world-free-at-last`})
    52  }
    53  
    54  func (s *StringsSuite) TestCaseFuncs(c *C) {
    55  	result := icmd.RunCommand(GomplateBin, "-i",
    56  		`{{ strings.CamelCase "Hellö, Wôrld! Free @ last..." }}
    57  {{ strings.SnakeCase "Hellö, Wôrld! Free @ last..." }}
    58  {{ strings.KebabCase "Hellö, Wôrld! Free @ last..." }}`)
    59  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `HellöWôrldFreeLast
    60  Hellö_wôrld_free_last
    61  Hellö-wôrld-free-last`})
    62  }
    63  
    64  func (s *StringsSuite) TestWordWrap(c *C) {
    65  	out := `There shouldn't be any wrapping of long words or URLs because that would break
    66  things very badly. To wit:
    67  https://example.com/a/super-long/url/that-shouldnt-be?wrapped=for+fear+of#the-breaking-of-functionality
    68  should appear on its own line, regardless of the desired word-wrapping width
    69  that has been set.`
    70  	text := strings.Replace(out, "\n", " ", -1)
    71  	in := `{{ print "` + text + `" | strings.WordWrap 80 }}`
    72  	inOutTest(c, in, out)
    73  }