github.com/coveo/gotemplate@v2.7.7+incompatible/collections/string_test.go (about)

     1  package collections
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestUnIndent(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	type args struct {
    13  		s string
    14  	}
    15  	tests := []struct {
    16  		name string
    17  		args args
    18  		want string
    19  	}{
    20  		{"Indented with tab", args{`
    21  			Hello
    22  
    23  			World
    24  			end!
    25  			`}, "\nHello\n\nWorld\nend!\n"},
    26  		{"Indented with spaces", args{`
    27                  Hello
    28  
    29                  World
    30                  end!
    31                  `}, "\nHello\n\nWorld\nend!\n"},
    32  		{"Normal string", args{"Hello World!"}, "Hello World!"},
    33  		{"Normal string prefixed with spaces", args{"  Hello World!"}, "  Hello World!"},
    34  		{"Indented with mixed spaces", args{`
    35  			Hello
    36  
    37  	        World
    38  			end!
    39  			`}, "\n\t\t\tHello\n\n\t        World\n\t\t\tend!\n\t\t\t"},
    40  		{"One line indented", args{"\nHello\n   World\n"}, "\nHello\n   World\n"},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			if got := UnIndent(tt.args.s); got != tt.want {
    45  				t.Errorf("UnIndent() = %v, want %v", got, tt.want)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestString_ToTitle(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	tests := []struct {
    55  		s    String
    56  		want string
    57  	}{
    58  		{"Hello world", "HELLO WORLD"},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(string(tt.s), func(t *testing.T) {
    62  			if got := tt.s.ToTitle(); string(got) != tt.want {
    63  				t.Errorf("String.ToTitle() = %v, want %v", got, tt.want)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func TestWrapString(t *testing.T) {
    70  	t.Parallel()
    71  
    72  	sample := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    73  	tests := []struct {
    74  		s          string
    75  		width      int
    76  		wantResult string
    77  	}{
    78  		{sample, 1, "Lorem\nipsum\ndolor\nsit\namet,\nconsectetur\nadipiscing\nelit,\nsed\ndo\neiusmod\ntempor\nincididunt\nut\nlabore\net\ndolore\nmagna\naliqua."},
    79  		{sample, 5, "Lorem\nipsum\ndolor\nsit\namet,\nconsectetur\nadipiscing\nelit,\nsed do\neiusmod\ntempor\nincididunt\nut\nlabore\net\ndolore\nmagna\naliqua."},
    80  		{sample, 10, "Lorem ipsum\ndolor sit\namet,\nconsectetur\nadipiscing\nelit, sed\ndo eiusmod\ntempor\nincididunt\nut labore\net dolore\nmagna\naliqua."},
    81  		{sample, 20, "Lorem ipsum dolor sit\namet, consectetur\nadipiscing elit, sed\ndo eiusmod tempor\nincididunt ut labore\net dolore magna\naliqua."},
    82  		{sample, 30, "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor\nincididunt ut labore et dolore\nmagna aliqua."},
    83  		{sample, 40, "Lorem ipsum dolor sit amet, consectetur\nadipiscing elit, sed do eiusmod tempor\nincididunt ut labore et dolore magna\naliqua."},
    84  		{sample, 50, "Lorem ipsum dolor sit amet, consectetur adipiscing\nelit, sed do eiusmod tempor incididunt ut labore et\ndolore magna aliqua."},
    85  		{sample, 75, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua."},
    86  		{sample, 100, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore\net dolore magna aliqua."},
    87  		{sample, 125, sample},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(fmt.Sprintf("Test with %d", tt.width), func(t *testing.T) {
    91  			if gotResult := WrapString(tt.s, tt.width); !reflect.DeepEqual(gotResult, tt.wantResult) {
    92  				t.Errorf("WrapString() =\n%q, want\n%q", gotResult, tt.wantResult)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestString_GetWordAtPosition(t *testing.T) {
    99  	t.Parallel()
   100  
   101  	type args struct {
   102  		pos    int
   103  		accept []string
   104  	}
   105  	tests := []struct {
   106  		s       String
   107  		args    args
   108  		want    String
   109  		wantPos int
   110  	}{
   111  		{"", args{0, nil}, "", -1},
   112  		{"A single character", args{0, nil}, "A", 0},
   113  		{"This a test", args{0, nil}, "This", 0},
   114  		{"This is a secode test", args{3, nil}, "This", 0},
   115  		{"Over", args{20, nil}, "", -1},
   116  		{"Find the second word", args{5, nil}, "the", 5},
   117  		{"Find the $third word", args{10, nil}, "$third", 9},
   118  		{"Find the ($a.value) word", args{10, []string{"."}}, "$a.value", 10},
   119  		{"Find the ($a.value[0]) word", args{10, []string{"."}}, "$a.value", 10},
   120  		{"Find the ($a.value[10]) word", args{10, []string{".", "[]"}}, "$a.value[10]", 10},
   121  		{"Match a space", args{5, nil}, "", 5},
   122  	}
   123  	for _, tt := range tests {
   124  		t.Run(tt.s.Str(), func(t *testing.T) {
   125  			got, pos := tt.s.GetWordAtPosition(tt.args.pos, tt.args.accept...)
   126  			if got != tt.want {
   127  				t.Errorf("String.GetWordAtPosition() got = %v, want %v", got, tt.want)
   128  			}
   129  			if pos != tt.wantPos {
   130  				t.Errorf("String.GetWordAtPosition() pos = %d, want %d", pos, tt.wantPos)
   131  			}
   132  			if got2 := tt.s.SelectWord(tt.args.pos, tt.args.accept...); got != got2 {
   133  				t.Errorf("String.SelectWord() returns %v while String.GetWordAtPosition() returns %v", got, got2)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestString_GetContextAtPosition(t *testing.T) {
   140  	t.Parallel()
   141  
   142  	type args struct {
   143  		pos   int
   144  		left  string
   145  		right string
   146  	}
   147  	tests := []struct {
   148  		s       String
   149  		args    args
   150  		want    String
   151  		wantPos int
   152  	}{
   153  		{"", args{0, "", ""}, "", -1},
   154  		{"", args{5, "", ""}, "", -1},
   155  		{"Before ()", args{-1, "(", ")"}, "", -1},
   156  		{"After ()", args{100, "(", ")"}, "", -1},
   157  		{"Function()", args{9, "(", ")"}, "()", 8},
   158  		{"A context (within parenthesis) should be returned", args{15, "(", ")"}, "(within parenthesis)", 10},
   159  		{"A context [[within double bracket]] should be returned", args{15, "[[", "]]"}, "[[within double bracket]]", 10},
   160  		{"A context [[from double bracket]] should be returned", args{24, "[[", ""}, "[[from double b", 10},
   161  		{"A context [[to double bracket]] should be returned", args{22, "", "]]"}, "bracket]]", 22},
   162  		{"A context (with (double level parenthesis))", args{22, "(", ")"}, "(double level parenthesis)", 16},
   163  		{"A context (with no bracket)", args{19, "[", "]"}, "", -1},
   164  		{"A context (with no enclosing context)", args{15, "", ""}, " ", 15},
   165  		{"A context (outside of context)", args{1, "(", ")"}, "", -1},
   166  		{"(context) after", args{12, "(", ")"}, "", -1},
   167  		{"Test (with (parenthesis inside) of the context)", args{7, "(", ")"}, "(with (parenthesis inside) of the context)", 5},
   168  		{"Test (with (parenthesis inside) unclosed", args{7, "(", ")"}, "", -1},
   169  		{"Test (with (parenthesis inside) (closed) many time)))", args{7, "(", ")"}, "(with (parenthesis inside) (closed) many time)", 5},
   170  		{"Test (with ((((((a lot of non closed)", args{7, "(", ")"}, "", -1},
   171  		{"Test (with (several) (parenthesis (inside)) of the context) (excluded)", args{7, "(", ")"}, "(with (several) (parenthesis (inside)) of the context)", 5},
   172  		{"Test | with same | left and | right", args{7, "|", "|"}, "| with same |", 5},
   173  		{"A context [[from [[double]] bracket]] [[with a little extra]]", args{12, "[[", "]]"}, "[[from [[double]] bracket]]", 10},
   174  		{"A context [[from [[double]] bracket [[unclosed]]", args{12, "[[", "]]"}, "", -1},
   175  		{"A context [[from [[double]] bracket [[extra]] closed]] many times]]]]", args{12, "[[", "]]"}, "[[from [[double]] bracket [[extra]] closed]]", 10},
   176  	}
   177  	for _, tt := range tests {
   178  		t.Run(tt.s.Str(), func(t *testing.T) {
   179  			got, pos := tt.s.GetContextAtPosition(tt.args.pos, tt.args.left, tt.args.right)
   180  			if got != tt.want {
   181  				t.Errorf("String.GetContextAtPosition() got = %v, want %v", got, tt.want)
   182  			}
   183  			if pos != tt.wantPos {
   184  				t.Errorf("String.GetContextAtPosition() pos = %v, want %v", pos, tt.wantPos)
   185  			}
   186  			if got2 := tt.s.SelectContext(tt.args.pos, tt.args.left, tt.args.right); got != got2 {
   187  				t.Errorf("String.SelectContext() returns %v while String.SelectWord() returns %v", got, got2)
   188  			}
   189  		})
   190  	}
   191  }
   192  
   193  func TestString_Protect(t *testing.T) {
   194  	t.Parallel()
   195  
   196  	tests := []struct {
   197  		name      String
   198  		want      String
   199  		wantArray StringArray
   200  	}{
   201  		{"", "", nil},
   202  		{`"This is a string"`, `"♠0"`, StringArray{`"This is a string"`}},
   203  		{`A test with a "single string"`, `A test with a "♠0"`, StringArray{`"single string"`}},
   204  		{"A test with `backtick string`", `A test with "♠0"`, StringArray{"`backtick string`"}},
   205  		{`Non closed "string`, `Non closed "string`, nil},
   206  		{`Non closed "with" escape "\"`, `Non closed "♠0" escape "\"`, StringArray{`"with"`}},
   207  		{`This contains two "string1" and "string2"`, `This contains two "♠0" and "♠1"`, StringArray{`"string1"`, `"string2"`}},
   208  		{"A mix of `backtick` and \"regular\" string", `A mix of "♠0" and "♠1" string`, StringArray{"`backtick`", `"regular"`}},
   209  		{"A confused one of `backtick with \"` and \"regular with \\\" quoted and ` inside\" string", "A confused one of \"♠0\" and \"♠1\" string", StringArray{"`backtick with \"`", "\"regular with \\\" quoted and ` inside\""}},
   210  		{`A string with "false \\\\\\" inside"`, `A string with "♠0" inside"`, StringArray{`"false \\\\\\"`}},
   211  		{`A string with "true \\\\\\\" inside"`, `A string with "♠0"`, StringArray{`"true \\\\\\\" inside"`}},
   212  	}
   213  	for _, tt := range tests {
   214  		t.Run(tt.name.Str(), func(t *testing.T) {
   215  			got, array := tt.name.Protect()
   216  			if got != tt.want {
   217  				t.Errorf("String.Protect() got = %v, want %v", got, tt.want)
   218  			}
   219  			if !reflect.DeepEqual(array, tt.wantArray) {
   220  				t.Errorf("String.Protect() array = %v, want %v", array, tt.wantArray)
   221  			}
   222  
   223  			restored := got.RestoreProtected(array)
   224  			if tt.name != restored {
   225  				t.Errorf("String.RestoreProtected() got %v, want %v", restored, tt.name)
   226  			}
   227  		})
   228  	}
   229  }
   230  
   231  func TestString_ParseBool(t *testing.T) {
   232  	t.Parallel()
   233  
   234  	tests := []struct {
   235  		value String
   236  		want  bool
   237  	}{
   238  		{"", false},
   239  		{"1", true},
   240  		{"0", false},
   241  		{"F", false},
   242  		{"False", false},
   243  		{"FALSE", false},
   244  		{"No", false},
   245  		{"N", false},
   246  		{"T", true},
   247  		{"true", true},
   248  		{"on", true},
   249  		{"OFF", false},
   250  		{"Whatever", true},
   251  		{"YES", true},
   252  	}
   253  	for _, tt := range tests {
   254  		t.Run(tt.value.Str(), func(t *testing.T) {
   255  			if got := tt.value.ParseBool(); got != tt.want {
   256  				t.Errorf("ParseBoolFromEnv() = %v, want %v", got, tt.want)
   257  			}
   258  		})
   259  	}
   260  }
   261  
   262  func TestString_IndexAll(t *testing.T) {
   263  	tests := []struct {
   264  		name       string
   265  		s          String
   266  		substr     string
   267  		wantResult []int
   268  	}{
   269  		{"Both empty", "", "", nil},
   270  		{"Empty substr", "aaa", "", nil},
   271  		{"Empty source", "", "aa", nil},
   272  		{"Single", "abaabaaa", "a", []int{0, 2, 3, 5, 6, 7}},
   273  		{"Double", "abaabaaabaaaa", "aa", []int{2, 5, 9, 11}},
   274  	}
   275  	for _, tt := range tests {
   276  		t.Run(tt.name, func(t *testing.T) {
   277  			if gotResult := tt.s.IndexAll(tt.substr); !reflect.DeepEqual(gotResult, tt.wantResult) {
   278  				t.Errorf("String.IndexAll() = %v, want %v", gotResult, tt.wantResult)
   279  			}
   280  		})
   281  	}
   282  }
   283  
   284  func TestString_AddLineNumber(t *testing.T) {
   285  	tests := []struct {
   286  		name  string
   287  		s     String
   288  		space int
   289  		want  String
   290  	}{
   291  		{"Empty", "", 0, "1 "},
   292  		{"Just a newline", "\n", 0, "1 \n2 "},
   293  		{"Several lines", "Line 1\nLine 2\nLine 3\n", 0, "1 Line 1\n2 Line 2\n3 Line 3\n4 "},
   294  		{"Several lines", "Line 1\nLine 2\nLine 3\n", 4, "   1 Line 1\n   2 Line 2\n   3 Line 3\n   4 "},
   295  	}
   296  	for _, tt := range tests {
   297  		t.Run(tt.name, func(t *testing.T) {
   298  			if got := tt.s.AddLineNumber(tt.space); got != tt.want {
   299  				t.Errorf("String.AddLineNumber() = %v, want %v", got, tt.want)
   300  			}
   301  		})
   302  	}
   303  }