File: if_test.go

package info (click to toggle)
golang-github-icza-gox 0.0~git20210726.cd40a3f-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 332 kB
  • sloc: makefile: 2
file content (72 lines) | stat: -rw-r--r-- 2,102 bytes parent folder | download
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
package gox

import (
	"reflect"
	"testing"
)

func TestIfFuncs(t *testing.T) {
	cases := []struct {
		name string      // Name of the test case
		f    interface{} // Function to call
		a, b interface{} // a and b params to pass in
	}{
		{"IfIf", IfIf, "one", 2.0},
		{"IfBool", IfBool, false, true},
		{"IfString", IfString, "a", "b"},
		{"IfInt", IfInt, 1, 2},
		{"IfInt8", IfInt8, int8(1), int8(2)},
		{"IfInt16", IfInt16, int16(1), int16(2)},
		{"IfInt32", IfInt32, int32(1), int32(2)},
		{"IfInt64", IfInt64, int64(1), int64(2)},
		{"IfUint", IfUint, uint(1), uint(2)},
		{"IfUint8", IfUint8, uint8(1), uint8(2)},
		{"IfUint16", IfUint16, uint16(1), uint16(2)},
		{"IfUint32", IfUint32, uint32(1), uint32(2)},
		{"IfUint64", IfUint64, uint64(1), uint64(2)},
		{"IfFloat32", IfFloat32, float32(1), float32(2)},
		{"IfFloat64", IfFloat64, float64(1), float64(2)},
		{"IfByte", IfByte, byte(1), byte(2)},
		{"IfRune", IfRune, rune(1), rune(2)},
	}

	for _, c := range cases {
		for _, cond := range []bool{false, true} {
			var exp interface{}
			if cond {
				exp = c.a
			} else {
				exp = c.b
			}

			// First test the standalone function:
			fv := reflect.ValueOf(c.f)
			params := []reflect.Value{reflect.ValueOf(cond), reflect.ValueOf(c.a), reflect.ValueOf(c.b)}

			results := fv.Call(params)
			if len(results) != 1 {
				t.Errorf("[%s(%v, %v, %v)] Expected 1 result(s), got: %d",
					c.name, cond, c.a, c.b, len(results))
			}
			if got := results[0].Interface(); got != exp {
				t.Errorf("[%s(%v, %v, %v)] Expected: %v, got: %v",
					c.name, cond, c.a, c.b, exp, got)
			}

			// Next test the similar method of If:
			name := c.name[2:] // Trim leading "If"
			fv = reflect.ValueOf(If(cond)).MethodByName(name)
			params = params[1:] // cond is not a param

			results = fv.Call(params)
			if len(results) != 1 {
				t.Errorf("[If(%v).%s(%v, %v)] Expected 1 result(s), got: %d",
					cond, name, c.a, c.b, len(results))
			}
			if got := results[0].Interface(); got != exp {
				t.Errorf("[If(%v).%s(%v, %v)] Expected: %v, got: %v",
					cond, name, c.a, c.b, exp, got)
			}
		}
	}
}