github.com/suntong/easygen@v5.3.0+incompatible/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  			`{{$s := sprintf .StrTest}} {{$s}}`,
   131  			" These rights belong to those people",
   132  		},
   133  		{
   134  			`{{$s := sprintf "%s, %.2f" .StrTest 12.3456}} {{$s}}`,
   135  			" These rights belong to those people, 12.35",
   136  		},
   137  
   138  		// == standard regexp functions
   139  		{
   140  			`{{ regexpFindString "peach punch" "p([a-z]+)ch" }}`,
   141  			"peach",
   142  		},
   143  		{
   144  			`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" -1 }}`,
   145  			"[peach punch]",
   146  		},
   147  		{
   148  			`{{ regexpFindAllString "peach punch" "p([a-z]+)ch" 1 }}`,
   149  			"[peach]",
   150  		},
   151  		{
   152  			`{{ regexpFindStringIndex "peach punch" "p([a-z]+)ch" }}`,
   153  			"[0 5]",
   154  		},
   155  		{
   156  			`{{ regexpFindStringSubmatch "peach punch" "p([a-z]+)ch" }}`,
   157  			"[peach ea]",
   158  		},
   159  		{
   160  			`{{ regexpFindStringSubmatchIndex "peach punch" "p([a-z]+)ch" }}`,
   161  			"[0 5 1 3]",
   162  		},
   163  		//
   164  		{
   165  			`{{ regexpMatchString "HTTPS://site/" "(?i)^http" }}`,
   166  			"true",
   167  		},
   168  		//
   169  		{
   170  			`{{ regexpReplaceAllLiteralString "html HTML Html aa uml bb Uml" "(?i)html|uml" "XML" }}`,
   171  			"XML XML XML aa XML bb XML",
   172  		},
   173  		{
   174  			`{{ regexpReplaceAllString .StrTest "(?i)th[eo]se" "the" }}`,
   175  			"the rights belong to the people",
   176  		},
   177  		{
   178  			`{{ regexpReplaceAllString "This and these are for THOSE people" "(?i)(this|th[eo]se)" "<b>${1}</b>" }}`,
   179  			"<b>This</b> and <b>these</b> are for <b>THOSE</b> people",
   180  		},
   181  		// {
   182  		// 	`{{ regexpReplaceAllStringFunc "a peach" "p([a-z]+)ch" stringsToUpper }}`,
   183  		// 	"a PEACH",
   184  		// },
   185  		// == my added strings functions
   186  		{
   187  			`{{ indent 2 "a" }}`,
   188  			"a",
   189  		},
   190  		{
   191  			`{{ indent 2 "a\nb\nc" }}`,
   192  			"a\n  b\n  c",
   193  		},
   194  		{
   195  			`{{ pindent 2 "a" }}`,
   196  			"  a",
   197  		},
   198  		{
   199  			`{{ pindent 2 "a\nb\nc" }}`,
   200  			"  a\n  b\n  c",
   201  		},
   202  		{
   203  			`{{ coalesce "a" }}`,
   204  			"a",
   205  		},
   206  		{
   207  			`{{ coalesce "" "b" }}`,
   208  			"b",
   209  		},
   210  		{
   211  			`{{ coalesce "" "" "c" }}`,
   212  			"c",
   213  		},
   214  		{
   215  			`{{ coalesce "" "" "" "" }}`,
   216  			"",
   217  		},
   218  		{
   219  			`{{ coalesce "" }}`,
   220  			"",
   221  		},
   222  		{
   223  			`{{ coalesce .StrTest "Something else" }}`,
   224  			"These rights belong to those people",
   225  		},
   226  		{
   227  			`{{ coalesce .StrEmpty "Something else" }}`,
   228  			"Something else",
   229  		},
   230  		// The following failed the template.Execute before but now fixed
   231  		{
   232  			`{{ coalesce .StrNone "Not exist" }}`,
   233  			"Not exist",
   234  		},
   235  	}
   236  
   237  	testStringManipulation(t, testData)
   238  }
   239  
   240  /*
   241  	{
   242  		`{{  }}`,
   243  		"",
   244  	},
   245  
   246  */