-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators_test.go
623 lines (543 loc) · 16.4 KB
/
operators_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package go_policy_enforcer
import (
"reflect"
"testing"
"github.com/kmesiab/go-policy-enforcer/internal/utils"
)
// TestEqualsPolicyCheckOperator tests the equalsPolicyCheckOperator
func TestEqualsPolicyCheckOperator(t *testing.T) {
tests := []struct {
leftVal any
rightVal any
expected bool
}{
{1, 1, true},
{"abc", "abc", true},
{1.0, 1.0, true},
{1, 2, false},
{"abc", "xyz", false},
}
for _, test := range tests {
result := equalsPolicyCheckOperator(test.leftVal, test.rightVal)
if result != test.expected {
t.Errorf("EqualsPolicyCheckOperator(%v, %v) = %v; want %v", test.leftVal, test.rightVal, result, test.expected)
}
}
}
// TestNotEqualsPolicyCheckOperator tests the notEqualsPolicyCheckOperator
func TestNotEqualsPolicyCheckOperator(t *testing.T) {
tests := []struct {
leftVal any
rightVal any
expected bool
}{
{1, 1, false},
{"abc", "abc", false},
{1.0, 2.0, true},
{1, 2, true},
{"abc", "xyz", true},
}
for _, test := range tests {
result := notEqualsPolicyCheckOperator(test.leftVal, test.rightVal)
if result != test.expected {
t.Errorf("NotEqualsPolicyCheckOperator(%v, %v) = %v; want %v", test.leftVal, test.rightVal, result, test.expected)
}
}
}
// TestGreaterThanPolicyCheckOperator tests the greaterThanPolicyCheckOperator
func TestGreaterThanPolicyCheckOperator(t *testing.T) {
tests := []struct {
leftVal any
rightVal any
expected bool
}{
{2, 1, true},
{1, 1, false},
{0, 1, false},
}
for _, test := range tests {
result := greaterThanPolicyCheckOperator(test.leftVal, test.rightVal)
if result != test.expected {
t.Errorf("GreaterThanPolicyCheckOperator(%v, %v) = %v; want %v", test.leftVal, test.rightVal, result, test.expected)
}
}
}
// TestGreaterThanOrEqualsPolicyCheckOperator tests the greaterThanOrEqualsPolicyCheckOperator
func TestGreaterThanOrEqualsPolicyCheckOperator(t *testing.T) {
tests := []struct {
leftVal any
rightVal any
expected bool
}{
{2, 1, true},
{1, 1, true},
{0, 1, false},
}
for _, test := range tests {
result := greaterThanOrEqualsPolicyCheckOperator(test.leftVal, test.rightVal)
if result != test.expected {
t.Errorf("GreaterThanOrEqualsPolicyCheckOperator(%v, %v) = %v; want %v", test.leftVal, test.rightVal, result, test.expected)
}
}
}
// TestLessThanPolicyCheckOperator tests the lessThanPolicyCheckOperator
func TestLessThanPolicyCheckOperator(t *testing.T) {
tests := []struct {
leftVal any
rightVal any
expected bool
}{
{1, 2, true},
{1, 1, false},
{2, 1, false},
}
for _, test := range tests {
result := lessThanPolicyCheckOperator(test.leftVal, test.rightVal)
if result != test.expected {
t.Errorf("LessThanPolicyCheckOperator(%v, %v) = %v; want %v", test.leftVal, test.rightVal, result, test.expected)
}
}
}
// TestEvaluatePolicyCheckOperator tests the evaluatePolicyCheckOperator function
func TestEvaluatePolicyCheckOperator(t *testing.T) {
tests := []struct {
operator string
leftVal any
rightVal any
expected bool
}{
{"==", 1, 1, true},
{"==", "abc", "abc", true},
{"!=", 1, 2, true},
{">", 2, 1, true},
{">", "b", "a", true}, // String comparison
{">=", 2, 1, true},
{"<", 1, 2, true},
{"<", "a", "b", true}, // String comparison
{"<", 1, 1, false},
}
for _, test := range tests {
result, err := evaluatePolicyCheckOperator(test.operator, test.leftVal, test.rightVal)
if err != nil {
t.Errorf("Error evaluating policy check operator: %v", err)
continue
}
if result != test.expected {
t.Errorf("EvaluatePolicyCheckOperator(%v, %v, %v) = %v; want %v", test.operator, test.leftVal, test.rightVal, result, test.expected)
}
}
}
func TestGetPolicyCheckOperator_NonExistingOperator(t *testing.T) {
nonExistingOperator := "non_existing_operator"
opFunc, err := getPolicyCheckOperator(nonExistingOperator)
if err == nil {
t.Errorf("Expected error for non existing operator but got: %v", err)
}
if opFunc != nil {
t.Errorf("Expected nil function for operator '%s', but got nil", nonExistingOperator)
}
}
func TestGetPolicyCheckOperator_CaseSensitivity(t *testing.T) {
tests := []struct {
operator string
expectedFunc func(any, any) bool
expectNil bool
expectError bool
}{
{"==", equalsPolicyCheckOperator, false, false},
{"!=", notEqualsPolicyCheckOperator, false, false},
{">=", greaterThanOrEqualsPolicyCheckOperator, false, false},
{"in", inPolicyCheckOperator, false, false},
{"not in", notInPolicyCheckOperator, false, false},
{"IN", nil, true, true}, // Case-sensitive mismatch should return nil without error
}
for _, tt := range tests {
t.Run(tt.operator, func(t *testing.T) {
opFunc, err := getPolicyCheckOperator(tt.operator)
// Check for unexpected errors
if (err != nil) != tt.expectError {
t.Errorf("Unexpected error for operator '%s': got %v, want error: %v", tt.operator, err, tt.expectError)
}
// Check for expected nil or non-nil function
if (opFunc == nil) != tt.expectNil {
t.Errorf("Expected nil: %v for operator '%s', but got: %v", tt.expectNil, tt.operator, opFunc)
}
// Compare function behavior if a non-nil function was expected
if !tt.expectNil && opFunc != nil && !compareFunctions(opFunc, tt.expectedFunc) {
t.Errorf("Expected function behavior for operator '%s' does not match", tt.operator)
}
})
}
}
func TestEvaluatePolicyCheckOperator_NilValues(t *testing.T) {
operator := "=="
// Test case 1: leftVal is nil, rightVal is a non-nil string
var leftVal *string
rightVal := "abc"
result, err := evaluatePolicyCheckOperator(operator, leftVal, rightVal)
if err != nil {
t.Errorf("Error evaluating policy check operator: %v", err)
}
if result {
t.Errorf("EvaluatePolicyCheckOperator(%s, nil, %v) = %v; want %v", operator, rightVal, result, false)
}
// Test case 2: leftVal is a non-nil string, rightVal is nil
leftValStr := "abc"
leftVal = &leftValStr
var rightValNil *string
result, err = evaluatePolicyCheckOperator(operator, leftVal, rightValNil)
if err != nil {
t.Errorf("Error evaluating policy check operator: %v", err)
}
if result {
t.Errorf("EvaluatePolicyCheckOperator(%s, %v, nil) = %v; want %v", operator, leftValStr, result, false)
}
// Test case 3: both leftVal and rightVal are nil
leftVal = nil
rightValNil = nil
result, err = evaluatePolicyCheckOperator(operator, leftVal, rightValNil)
if err != nil {
t.Errorf("Error evaluating policy check operator: %v", err)
}
if !result {
t.Errorf("EvaluatePolicyCheckOperator(%s, nil, nil) = %v; want %v", operator, result, true)
}
}
// TestNonNumericValues tests the behavior of the comparison operators with non-numeric values
func TestNonNumericValues(t *testing.T) {
tests := []struct {
operator string
leftVal any
rightVal any
expected bool
}{
{">", "abc", "xyz", false},
{">", "abc", "abc", false},
{">=", "abc", "xyz", false},
{">=", "abc", "abc", true},
{"<", "abc", "xyz", true},
{"<", "abc", "abc", false},
{"<=", "abc", "xyz", true},
{"<=", "abc", "abc", true},
}
for _, test := range tests {
result, err := evaluatePolicyCheckOperator(test.operator, test.leftVal, test.rightVal)
if err != nil {
t.Errorf("Error evaluating policy check operator: %v", err)
continue
}
if result != test.expected {
t.Errorf("EvaluatePolicyCheckOperator(%s, %v, %v) = %v; want %v", test.operator, test.leftVal, test.rightVal, result, test.expected)
}
}
}
func TestGetPolicyCheckOperator_NonStringValues(t *testing.T) {
tests := []struct {
operator string
leftVal any
rightVal any
expected bool
}{
{"==", 1, 1, true},
{"==", 1.0, 1.0, true},
{"==", "1", 1, true},
{"==", "1.0", 1.0, true},
{"!=", 1, 2, true},
{"!=", 1.0, 2.0, true},
{"!=", "1", 2, true},
{"!=", "1.0", 2.0, true},
}
for _, test := range tests {
opFunc, err := getPolicyCheckOperator(test.operator)
if err != nil {
t.Errorf("Expected no error for non existing operator but got: %v", err)
}
if opFunc == nil {
t.Errorf("Expected non-nil function for operator '%s', but got nil", test.operator)
} else {
result := opFunc(test.leftVal, test.rightVal)
if result != test.expected {
t.Errorf("GetPolicyCheckOperator(%s, %v, %v) = %v; want %v", test.operator, test.leftVal, test.rightVal, result, test.expected)
}
}
}
}
func TestEvaluatePolicyCheckOperator_UnsupportedOperator(t *testing.T) {
operator := "unsupported_operator"
leftVal := 10
rightVal := 20
result, err := evaluatePolicyCheckOperator(operator, leftVal, rightVal)
if err == nil {
t.Errorf("Expected error for unsupported operator '%s', but got none", operator)
}
if result {
t.Errorf("Expected false result for unsupported operator '%s', but got true", operator)
}
}
func TestEvaluatePolicyCheckOperator_NonNumericValues(t *testing.T) {
tests := []struct {
operator string
leftVal any
rightVal any
expected bool
}{
{">", "abc", "xyz", false},
{">", "abc", "abc", false},
{">=", "abc", "xyz", false},
{">=", "abc", "abc", true},
{"<", "abc", "xyz", true},
{"<", "abc", "abc", false},
{"<=", "abc", "xyz", true},
{"<=", "abc", "abc", true},
}
for _, test := range tests {
result, err := evaluatePolicyCheckOperator(test.operator, test.leftVal, test.rightVal)
if err != nil {
t.Errorf("Error evaluating policy check operator: %v", err)
continue
}
if result != test.expected {
t.Errorf("EvaluatePolicyCheckOperator(%s, %v, %v) = %v; want %v", test.operator, test.leftVal, test.rightVal, result, test.expected)
}
}
}
func TestEvaluatePolicyCheckOperator_DifferentDataTypes(t *testing.T) {
sameSlice := []string{"abc", "xyz"}
tests := []struct {
operator string
leftVal any
rightVal any
expected bool
errorExpected bool
}{
{
operator: "==",
leftVal: []string{"apple", "banana"},
rightVal: []string{"apple", "banana"},
expected: true,
errorExpected: false,
},
{
operator: "==",
leftVal: []string{"apple", "banana"},
rightVal: []string{"banana", "apple"},
expected: true,
errorExpected: false,
},
{
operator: "==",
leftVal: []string{"apple", "banana"},
rightVal: "banana",
expected: false,
errorExpected: true,
},
{
operator: "===",
leftVal: []string{"apple", "banana"},
rightVal: []string{"banana", "apple"},
expected: false,
errorExpected: false,
},
{
operator: "===",
leftVal: sameSlice,
rightVal: sameSlice,
expected: true,
errorExpected: false,
},
{
operator: "!=",
leftVal: []string{"apple", "banana"},
rightVal: []string{"apple", "banana"},
expected: false,
errorExpected: false,
},
{
operator: "!=",
leftVal: []string{"apple", "banana"},
rightVal: []string{"banana", "apple"},
expected: false,
errorExpected: false,
},
{
operator: "!=",
leftVal: []string{"apple", "banana"},
rightVal: "bananas",
expected: false,
errorExpected: true,
},
{
operator: "in",
leftVal: "banana",
rightVal: []string{"apple", "banana"},
expected: true,
errorExpected: false,
},
{
operator: "in",
leftVal: "orange",
rightVal: []string{"apple", "banana"},
expected: false,
errorExpected: false,
},
}
for _, test := range tests {
result, err := evaluatePolicyCheckOperator(test.operator, test.leftVal, test.rightVal)
if err != nil && !test.errorExpected {
t.Errorf("Error evaluating policy check operator: %v", err)
continue
}
if result != test.expected {
t.Errorf("EvaluatePolicyCheckOperator(%s, %v, %v) = %v; want %v", test.operator, test.leftVal, test.rightVal, result, test.expected)
}
}
}
func TestEvaluatePolicyCheckOperator_SliceComparisons(t *testing.T) {
deepSlice := []string{"apple", "banana"}
tests := []struct {
operator string
leftVal any
rightVal any
expected bool
errorExpected bool
}{
{"==", []string{"apple", "banana"}, "banana", false, true}, // Not two slices
{"==", []string{"apple", "banana"}, []string{"apple", "banana"}, true, false}, // Same elements same order
{"==", []string{"apple"}, []string{"orange"}, false, false}, // Not same elements
{"!=", []string{"apple", "banana"}, []string{"apple", "banana"}, false, false}, // Same elements same order (negative)
{"===", deepSlice, deepSlice, true, false}, // Deep equals
{"===", deepSlice, []string{"apple"}, false, false}, // Deep equals not same positive match
{"!==", deepSlice, []string{"apple"}, true, false}, // Deep equals not same (negative)_
{"in", "orange", []string{"apple", "banana"}, false, false}, // Single not in slice
}
for _, test := range tests {
result, err := evaluatePolicyCheckOperator(test.operator, test.leftVal, test.rightVal)
if err == nil && test.errorExpected {
t.Errorf("Error was expected evaluating policy check operator: %v", err)
continue
}
if result != test.expected {
t.Errorf("EvaluatePolicyCheckOperator(%v, %v, %v) = %v; want %v", test.operator, test.leftVal, test.rightVal, result, test.expected)
}
}
}
func TestToStringSlice_NonNumericStringValues(t *testing.T) {
// Test case: non-numeric string values in 'in' operator
input := []interface{}{
"apple",
123,
"banana",
true,
[]int{4, 5, 6},
}
expected := []string{"apple", "123", "banana", "true", "[4 5 6]"}
result, ok := utils.ToStringSlice(input)
if !ok {
t.Errorf("Expected toStringSlice to return true, but got false")
}
if len(result) != len(expected) {
t.Errorf("Expected result length %d, but got %d", len(expected), len(result))
}
for i := range result {
if result[i] != expected[i] {
t.Errorf("Expected result[%d] = %s, but got %s", i, expected[i], result[i])
}
}
}
func TestToStringSlice_EmptySlice(t *testing.T) {
emptySlice := make([]interface{}, 0)
var expectedResult []string
result, ok := utils.ToStringSlice(emptySlice)
if !ok {
t.Errorf("Expected toStringSlice to return true, but got false")
}
if result == nil || len(result) > 0 {
t.Errorf("Expected result to be non-nil, but got nil")
return
}
if len(result) != len(expectedResult) {
t.Errorf("Expected result length %d, but got %d", len(expectedResult), len(result))
}
}
type CustomTestType struct {
value string
}
// Implement the fmt.Stringer interface for CustomType
func (c CustomTestType) String() string {
return c.value
}
func TestToStringSlice_DifferentDataTypes(t *testing.T) {
tests := []struct {
input any
expected []string
isSlice bool
}{
{
input: []CustomTestType{{value: "test"}, {value: "example"}},
expected: []string{"test", "example"},
isSlice: true,
},
{
input: []int{1, 2, 3},
expected: []string{"1", "2", "3"},
isSlice: true,
},
{
input: []float64{1.1, 2.2, 3.3},
expected: []string{"1.1", "2.2", "3.3"},
isSlice: true,
},
{
input: []bool{true, false, true},
expected: []string{"true", "false", "true"},
isSlice: true,
},
{
input: []interface{}{1, 2.2, "three", true},
expected: []string{"1", "2.2", "three", "true"},
isSlice: true,
},
{
input: 123,
expected: []string{"123"},
isSlice: false,
},
{
input: "hello",
expected: []string{"hello"},
isSlice: false,
},
{
input: []interface{}{}, // Test empty slice
expected: []string{},
isSlice: true,
},
{
input: []interface{}{nil, nil}, // Test slice with nil elements
expected: []string{"<nil>", "<nil>"},
isSlice: true,
},
}
for _, test := range tests {
result, isSlice := utils.ToStringSlice(test.input)
if isSlice != test.isSlice {
t.Errorf("Input: %v\nExpected isSlice: %v, but got: %v", test.input, test.isSlice, isSlice)
}
if isSlice {
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("Input: %v\nExpected: %v, but got: %v", test.input, test.expected, result)
}
} else {
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("Input: %v\nExpected result: %v, but got: %v", test.input, test.expected, result)
}
}
}
}
// Helper function to compare two functions by applying them on the same test data
func compareFunctions(f1, f2 PolicyCheckOperator[any]) bool {
// Test with sample values, adjust these according to the operator being tested
testValue1 := 10
testValue2 := 10
return f1(testValue1, testValue2) == f2(testValue1, testValue2)
}