github.com/seeker-insurance/kit@v0.0.13/runeset/runeset_test.go (about)

     1  package runeset
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"golang.org/x/text/unicode/norm"
     8  )
     9  
    10  const ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"
    11  
    12  var (
    13  	abc = FromString("abc")
    14  	a   = FromString("a")
    15  	ab  = FromString("ab")
    16  	bc  = FromString("bc")
    17  	ac  = FromString("ac")
    18  )
    19  
    20  func TestRuneSet_Contains(t *testing.T) {
    21  	type args struct {
    22  		r rune
    23  	}
    24  	tests := []struct {
    25  		name string
    26  		rs   RuneSet
    27  		args args
    28  		want bool
    29  	}{
    30  		{
    31  			name: "yes",
    32  			rs:   abc,
    33  			args: args{'c'},
    34  			want: true,
    35  		},
    36  		{
    37  			name: "no",
    38  			rs:   abc,
    39  			args: args{'d'},
    40  			want: false,
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			if got := tt.rs.Contains(tt.args.r); got != tt.want {
    46  				t.Errorf("RuneSet.Contains() = %v, want %v", got, tt.want)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestRuneSet_Intersection(t *testing.T) {
    53  	type args struct {
    54  		sets []RuneSet
    55  	}
    56  	tests := []struct {
    57  		name             string
    58  		rs               RuneSet
    59  		args             args
    60  		wantIntersection RuneSet
    61  	}{
    62  		{
    63  
    64  			name:             "ok",
    65  			rs:               abc,
    66  			args:             args{[]RuneSet{ab, a}},
    67  			wantIntersection: a,
    68  		}, {
    69  			name:             "empty",
    70  			rs:               abc,
    71  			args:             args{[]RuneSet{ab, bc, ac}},
    72  			wantIntersection: RuneSet{},
    73  		},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			if gotIntersection := tt.rs.Intersection(tt.args.sets...); !reflect.DeepEqual(gotIntersection, tt.wantIntersection) {
    78  				t.Errorf("RuneSet.Intersection() = %v, want %v", gotIntersection, tt.wantIntersection)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func TestRuneSet_Equal(t *testing.T) {
    85  	type args struct {
    86  		other RuneSet
    87  	}
    88  
    89  	tests := []struct {
    90  		name string
    91  		rs   RuneSet
    92  		args args
    93  		want bool
    94  	}{
    95  
    96  		{name: "ok",
    97  			rs:   FromString("abcabc"),
    98  			args: args{FromString("abc")},
    99  			want: true,
   100  		},
   101  		{
   102  			name: "no - len",
   103  			rs:   FromString(norm.NFC.String("Finé")),
   104  			args: args{FromString(norm.NFD.String("Finé"))},
   105  			want: false,
   106  		},
   107  		{
   108  			name: "no - different chars",
   109  			rs:   FromString("s"),
   110  			args: args{FromString("p")},
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			if got := tt.rs.Equal(tt.args.other); got != tt.want {
   116  				t.Errorf("RuneSet.Equal() = %v, want %v", got, tt.want)
   117  			}
   118  		})
   119  	}
   120  }
   121  
   122  func TestRuneSet_Union(t *testing.T) {
   123  	type args struct {
   124  		sets []RuneSet
   125  	}
   126  	tests := []struct {
   127  		name      string
   128  		rs        RuneSet
   129  		args      args
   130  		wantUnion RuneSet
   131  	}{
   132  		{
   133  			name:      "ok",
   134  			rs:        RuneSet{},
   135  			args:      args{[]RuneSet{bc, a}},
   136  			wantUnion: abc,
   137  		},
   138  	}
   139  	for _, tt := range tests {
   140  		t.Run(tt.name, func(t *testing.T) {
   141  			if gotUnion := tt.rs.Union(tt.args.sets...); !reflect.DeepEqual(gotUnion, tt.wantUnion) {
   142  				t.Errorf("RuneSet.Union() = %v, want %v", gotUnion, tt.wantUnion)
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  func TestRuneSet_Difference(t *testing.T) {
   149  	type args struct {
   150  		sets []RuneSet
   151  	}
   152  
   153  	abc := FromString("abc")
   154  	a := FromString("a")
   155  	bc := FromString("bc")
   156  	tests := []struct {
   157  		name           string
   158  		rs             RuneSet
   159  		args           args
   160  		wantDifference RuneSet
   161  	}{
   162  		{
   163  			name:           "two diff",
   164  			rs:             abc,
   165  			args:           args{[]RuneSet{bc}},
   166  			wantDifference: a,
   167  		},
   168  		{name: "three diff",
   169  			rs:             abc,
   170  			args:           args{[]RuneSet{a, bc}},
   171  			wantDifference: RuneSet{},
   172  		},
   173  	}
   174  	for _, tt := range tests {
   175  		t.Run(tt.name, func(t *testing.T) {
   176  			if gotDifference := tt.rs.Difference(tt.args.sets...); !reflect.DeepEqual(gotDifference, tt.wantDifference) {
   177  				t.Errorf("RuneSet.Difference() = %v, want %v", gotDifference, tt.wantDifference)
   178  			}
   179  		})
   180  	}
   181  }
   182  
   183  func TestFromRunes(t *testing.T) {
   184  	type args struct {
   185  		runes []rune
   186  	}
   187  	const bigRune = rune(0xFFFF)
   188  	tests := []struct {
   189  		name string
   190  		args args
   191  		want RuneSet
   192  	}{
   193  		{
   194  			name: "dedupe",
   195  			args: args{append([]rune("abcabc"), bigRune)},
   196  			want: RuneSet{
   197  				'a':     yes,
   198  				'b':     yes,
   199  				'c':     yes,
   200  				bigRune: yes,
   201  			},
   202  		},
   203  	}
   204  	for _, tt := range tests {
   205  		t.Run(tt.name, func(t *testing.T) {
   206  			if got := FromRunes(tt.args.runes...); !reflect.DeepEqual(got, tt.want) {
   207  				t.Errorf("FromRunes() = %v, want %v", got, tt.want)
   208  			}
   209  		})
   210  	}
   211  }
   212  
   213  func TestFromString(t *testing.T) {
   214  	const bigRune = rune(0xFFFF)
   215  	type args struct {
   216  		s string
   217  	}
   218  	tests := []struct {
   219  		name    string
   220  		args    args
   221  		wantSet RuneSet
   222  	}{
   223  		{name: "ok",
   224  			args: args{"abcabcabc" + string(bigRune)},
   225  			wantSet: RuneSet{
   226  				'a':     yes,
   227  				'b':     yes,
   228  				'c':     yes,
   229  				bigRune: yes,
   230  			},
   231  		},
   232  	}
   233  	for _, tt := range tests {
   234  		t.Run(tt.name, func(t *testing.T) {
   235  			if gotSet := FromString(tt.args.s); !reflect.DeepEqual(gotSet, tt.wantSet) {
   236  				t.Errorf("FromString() = %v, want %v", gotSet, tt.wantSet)
   237  			}
   238  		})
   239  	}
   240  }
   241  
   242  func TestIntersection(t *testing.T) {
   243  	type args struct {
   244  		set  RuneSet
   245  		sets []RuneSet
   246  	}
   247  	tests := []struct {
   248  		name string
   249  		args args
   250  		want RuneSet
   251  	}{
   252  		{
   253  			name: "ok",
   254  			args: args{
   255  				set:  abc,
   256  				sets: []RuneSet{bc},
   257  			},
   258  			want: bc,
   259  		},
   260  	}
   261  
   262  	for _, tt := range tests {
   263  		t.Run(tt.name, func(t *testing.T) {
   264  			if got := Intersection(tt.args.set, tt.args.sets...); !reflect.DeepEqual(got, tt.want) {
   265  				t.Errorf("Intersection() = %v, want %v", got, tt.want)
   266  			}
   267  		})
   268  	}
   269  }
   270  
   271  func TestUnion(t *testing.T) {
   272  	type args struct {
   273  		sets []RuneSet
   274  	}
   275  	tests := []struct {
   276  		name string
   277  		args args
   278  		want RuneSet
   279  	}{
   280  	// TODO: Add test cases.
   281  	}
   282  	for _, tt := range tests {
   283  		t.Run(tt.name, func(t *testing.T) {
   284  			if got := Union(tt.args.sets...); !reflect.DeepEqual(got, tt.want) {
   285  				t.Errorf("Union() = %v, want %v", got, tt.want)
   286  			}
   287  		})
   288  	}
   289  }
   290  
   291  func TestRuneSet_Copy(t *testing.T) {
   292  	tests := []struct {
   293  		name string
   294  		rs   RuneSet
   295  		want RuneSet
   296  	}{
   297  	// TODO: Add test cases.
   298  	}
   299  	for _, tt := range tests {
   300  		t.Run(tt.name, func(t *testing.T) {
   301  			if got := tt.rs.Copy(); !reflect.DeepEqual(got, tt.want) {
   302  				t.Errorf("RuneSet.Copy() = %v, want %v", got, tt.want)
   303  			}
   304  		})
   305  	}
   306  }