gopkg.in/easygen.v4@v4.1.0/t_strings_test.go (about)

     1  package easygen_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/go-easygen/easygen"
     8  )
     9  
    10  type testItem struct {
    11  	tmplStr, expected string
    12  }
    13  
    14  //var tmpl *template.Template
    15  
    16  func testStringManipulation(t *testing.T, d []testItem) {
    17  	tmpl = easygen.NewTemplate().Funcs(easygen.FuncDefs())
    18  	for _, tc := range d {
    19  		buf := bytes.NewBufferString("")
    20  		easygen.Process0(tmpl, buf, tc.tmplStr, "test/strings")
    21  
    22  		v := buf.String()
    23  		//t.Log(v)
    24  		if v != tc.expected {
    25  			t.Errorf("'%s' expects '%s', got '%s'", tc.tmplStr, tc.expected, v)
    26  		}
    27  	}
    28  }
    29  
    30  // for standalone test, change package to `main` and the next func def to,
    31  // func main() {
    32  func TestStringManipulation(t *testing.T) {
    33  
    34  	testData := []testItem{
    35  		// == standard strings functions
    36  		{
    37  			`{{ stringsContains "seafood" "foo" }}`,
    38  			"true",
    39  		},
    40  		{
    41  			`{{ stringsContains "seafood" "bar" }}`,
    42  			"false",
    43  		},
    44  		{
    45  			`{{ stringsContainsAny "team" "i" }}`,
    46  			"false",
    47  		},
    48  		{
    49  			`{{ stringsContainsAny "failure" "u & i" }}`,
    50  			"true",
    51  		},
    52  		{
    53  			`{{ stringsCount "cheese" "e" }}`,
    54  			"3",
    55  		},
    56  		{
    57  			`{{ stringsEqualFold "Go" "go" }}`,
    58  			"true",
    59  		},
    60  		{
    61  			`{{ stringsFields "  foo bar  baz   " }}`,
    62  			"[foo bar baz]",
    63  		},
    64  		{
    65  			`{{ stringsIndex "go gopher" "go" }}`,
    66  			"0",
    67  		},
    68  		{
    69  			`{{ stringsLastIndex "go gopher" "go" }}`,
    70  			"3",
    71  		},
    72  		{
    73  			`ba{{ stringsRepeat "na" 2 }}`,
    74  			"banana",
    75  		},
    76  		{
    77  			`{{ stringsReplace "oink oink oink" "k" "ky" 2 }}`,
    78  			"oinky oinky oink",
    79  		},
    80  		{
    81  			`{{ stringsReplace "oink oink oink" "oink" "moo" -1 }}`,
    82  			"moo moo moo",
    83  		},
    84  		{
    85  			`{{ stringsSplitN "a,b,c" "," 0 }}`,
    86  			`[]`,
    87  		},
    88  		{
    89  			`{{ stringsSplitAfter "a,b,c" "," }}`,
    90  			`[a, b, c]`,
    91  		},
    92  		{
    93  			`{{ stringsSplitAfterN "a,b,c" "," 2 }}`,
    94  			`[a, b,c]`,
    95  		},
    96  		{
    97  			`{{ stringsTitle "her royal highness" }}`,
    98  			"Her Royal Highness",
    99  		},
   100  		{
   101  			`{{ stringsToLower "Gopher" }}`,
   102  			"gopher",
   103  		},
   104  		{
   105  			`{{ stringsToUpper "Gopher" }}`,
   106  			"GOPHER",
   107  		},
   108  		{
   109  			`{{ stringsTrimSpace " \t\n a lone gopher \n\t\r\n" }}`,
   110  			"a lone gopher",
   111  		},
   112  		{
   113  			`{{ stringsTrim " !!! Achtung !!! " "! " }}`,
   114  			"Achtung",
   115  		},
   116  		{
   117  			`{{ stringsTrimPrefix "Goodbye,, world!" "Goodbye," }}`,
   118  			", world!",
   119  		},
   120  		{
   121  			`{{ stringsTrimSuffix "Hello, goodbye, etc!" "goodbye, etc!" }}`,
   122  			"Hello, ",
   123  		},
   124  		// aliases
   125  		{
   126  			`The {{if eq .StrTest "these rights belong to those people"}}eq says Yea{{else}}eq says Nay{{end}} but {{if eqf .StrTest "these rights belong to those people"}}eqf says Yea{{else}}eqf says Nay{{end}}.`,
   127  			"The eq says Nay but eqf says Yea.",
   128  		},
   129  
   130  		// == standard regexp functions
   131  		{
   132  			`{{ regexpFindString "peach punch" "p([a-z]+)ch" }}`,
   133  			"peach",
   134  		},
   135  		{
   136  			`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" -1 }}`,
   137  			"[peach punch]",
   138  		},
   139  		{
   140  			`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" 1 }}`,
   141  			"[peach]",
   142  		},
   143  		{
   144  			`{{ regexpFindStringIndex "peach punch" "p([a-z]+)ch" }}`,
   145  			"[0 5]",
   146  		},
   147  		{
   148  			`{{ regexpFindStringSubmatch "peach punch" "p([a-z]+)ch" }}`,
   149  			"[peach ea]",
   150  		},
   151  		{
   152  			`{{ regexpFindStringSubmatchIndex "peach punch" "p([a-z]+)ch" }}`,
   153  			"[0 5 1 3]",
   154  		},
   155  		//
   156  		{
   157  			`{{ regexpMatchString "HTTPS://site/" "(?i)^http" }}`,
   158  			"true",
   159  		},
   160  		//
   161  		{
   162  			`{{ regexpReplaceAllLiteralString "html HTML Html aa uml bb Uml" "(?i)html|uml" "XML" }}`,
   163  			"XML XML XML aa XML bb XML",
   164  		},
   165  		{
   166  			`{{ regexpReplaceAllString .StrTest "(?i)th[eo]se" "the" }}`,
   167  			"the rights belong to the people",
   168  		},
   169  		{
   170  			`{{ regexpReplaceAllString "This and these are for THOSE people" "(?i)(this|th[eo]se)" "<b>${1}</b>" }}`,
   171  			"<b>This</b> and <b>these</b> are for <b>THOSE</b> people",
   172  		},
   173  		// {
   174  		// 	`{{ regexpReplaceAllStringFunc "a peach" "p([a-z]+)ch" stringsToUpper }}`,
   175  		// 	"a PEACH",
   176  		// },
   177  		// == my added strings functions
   178  		{
   179  			`{{ coalesce "a" }}`,
   180  			"a",
   181  		},
   182  		{
   183  			`{{ coalesce "" "b" }}`,
   184  			"b",
   185  		},
   186  		{
   187  			`{{ coalesce "" "" "c" }}`,
   188  			"c",
   189  		},
   190  		{
   191  			`{{ coalesce "" "" "" "" }}`,
   192  			"",
   193  		},
   194  		{
   195  			`{{ coalesce "" }}`,
   196  			"",
   197  		},
   198  	}
   199  
   200  	testStringManipulation(t, testData)
   201  }
   202  
   203  /*
   204  	{
   205  		`{{  }}`,
   206  		"",
   207  	},
   208  
   209  */