github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/safesql/safesql_test.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package safesql
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"github.com/google/go-cmp/cmp/cmpopts"
    22  )
    23  
    24  func TestTrustedSQLConcat(t *testing.T) {
    25  	var tests = []struct {
    26  		name string
    27  		ss   []TrustedSQLString
    28  		want TrustedSQLString
    29  	}{
    30  		{name: "nothing"},
    31  		{
    32  			name: "one word",
    33  			ss:   []TrustedSQLString{New("foo")},
    34  			want: New("foo"),
    35  		},
    36  		{
    37  			name: "two words",
    38  			ss:   []TrustedSQLString{New("foo"), New("ffa")},
    39  			want: New("fooffa"),
    40  		},
    41  	}
    42  	for _, tt := range tests {
    43  		t.Run(tt.name, func(t *testing.T) {
    44  			got := TrustedSQLStringConcat(tt.ss...)
    45  			if got != tt.want {
    46  				t.Errorf("got: %v, want: %v", got, tt.want)
    47  			}
    48  		})
    49  	}
    50  }
    51  func TestTrustedSQLJoin(t *testing.T) {
    52  	var tests = []struct {
    53  		name string
    54  		ss   []TrustedSQLString
    55  		sep  TrustedSQLString
    56  		want TrustedSQLString
    57  	}{
    58  		{name: "nothing"},
    59  		{
    60  			name: "one word",
    61  			ss:   []TrustedSQLString{New("foo")},
    62  			sep:  New("bar"),
    63  			want: New("foo"),
    64  		},
    65  		{
    66  			name: "two words",
    67  			ss:   []TrustedSQLString{New("foo"), New("ffa")},
    68  			sep:  New("bar"),
    69  			want: New("foobarffa"),
    70  		},
    71  		{
    72  			name: "empty sep",
    73  			ss:   []TrustedSQLString{New("foo"), New("ffa")},
    74  			want: New("fooffa"),
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			got := TrustedSQLStringJoin(tt.ss, tt.sep)
    80  			if got != tt.want {
    81  				t.Errorf("got: %v, want: %v", got, tt.want)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestTrustedSQLStringSplit(t *testing.T) {
    88  	var tests = []struct {
    89  		name string
    90  		in   TrustedSQLString
    91  		sep  TrustedSQLString
    92  		want []TrustedSQLString
    93  	}{
    94  		{name: "nothing"},
    95  		{
    96  			name: "no sep",
    97  			in:   New("foo"),
    98  			sep:  New("bar"),
    99  			want: []TrustedSQLString{New("foo")},
   100  		},
   101  		{
   102  			name: "two words",
   103  			in:   New("foobarffa"),
   104  			sep:  New("bar"),
   105  			want: []TrustedSQLString{New("foo"), New("ffa")},
   106  		},
   107  		{
   108  			name: "empty sep",
   109  			in:   New("foo"),
   110  			want: []TrustedSQLString{New("f"), New("o"), New("o")},
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			got := TrustedSQLStringSplit(tt.in, tt.sep)
   116  			if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty(), cmp.AllowUnexported(TrustedSQLString{})); diff != "" {
   117  				t.Errorf("-want +got: %s", diff)
   118  			}
   119  		})
   120  	}
   121  }