|
| 1 | +package mold |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + . "github.com/go-playground/assert/v2" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGetPrimitiveValue(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + typ reflect.Kind |
| 15 | + value string |
| 16 | + expected reflect.Value |
| 17 | + expectError bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "string", |
| 21 | + typ: reflect.String, |
| 22 | + value: "test", |
| 23 | + expected: reflect.ValueOf("test"), |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "int", |
| 27 | + typ: reflect.Int, |
| 28 | + value: "123", |
| 29 | + expected: reflect.ValueOf(123), |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "int8", |
| 33 | + typ: reflect.Int8, |
| 34 | + value: "123", |
| 35 | + expected: reflect.ValueOf(int8(123)), |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "int16", |
| 39 | + typ: reflect.Int16, |
| 40 | + value: "123", |
| 41 | + expected: reflect.ValueOf(int16(123)), |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "bool", |
| 45 | + typ: reflect.Bool, |
| 46 | + value: "true", |
| 47 | + expected: reflect.ValueOf(true), |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "error", |
| 51 | + typ: reflect.Int, |
| 52 | + value: "abc", |
| 53 | + expectError: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "unsupported type", |
| 57 | + typ: reflect.Struct, |
| 58 | + value: "abc", |
| 59 | + expectError: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "uint", |
| 63 | + typ: reflect.Uint, |
| 64 | + value: "123", |
| 65 | + expected: reflect.ValueOf(uint(123)), |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "uint8", |
| 69 | + typ: reflect.Uint8, |
| 70 | + value: "123", |
| 71 | + expected: reflect.ValueOf(uint8(123)), |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "uint16", |
| 75 | + typ: reflect.Uint16, |
| 76 | + value: "123", |
| 77 | + expected: reflect.ValueOf(uint16(123)), |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "uint32", |
| 81 | + typ: reflect.Uint32, |
| 82 | + value: "123", |
| 83 | + expected: reflect.ValueOf(uint32(123)), |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "uint64", |
| 87 | + typ: reflect.Uint64, |
| 88 | + value: "123", |
| 89 | + expected: reflect.ValueOf(uint64(123)), |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "float32", |
| 93 | + typ: reflect.Float32, |
| 94 | + value: "123.45", |
| 95 | + expected: reflect.ValueOf(float32(123.45)), |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "float64", |
| 99 | + typ: reflect.Float64, |
| 100 | + value: "123.45", |
| 101 | + expected: reflect.ValueOf(float64(123.45)), |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range tests { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + actual, err := GetPrimitiveValue(tc.typ, tc.value) |
| 108 | + if tc.expectError { |
| 109 | + NotEqual(t, nil, err) |
| 110 | + } else { |
| 111 | + Equal(t, nil, err) |
| 112 | + switch tc.typ { |
| 113 | + case reflect.String: |
| 114 | + Equal(t, tc.expected.String(), actual.String()) |
| 115 | + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 116 | + Equal(t, tc.expected.Int(), actual.Int()) |
| 117 | + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 118 | + Equal(t, tc.expected.Uint(), actual.Uint()) |
| 119 | + case reflect.Float32, reflect.Float64: |
| 120 | + // could not assert equal float because of precision issues |
| 121 | + // so we just check in 4 decimal places |
| 122 | + decimalMask := math.Pow(10, 4) |
| 123 | + Equal(t, math.Round(tc.expected.Float()*decimalMask), math.Round(actual.Float()*decimalMask)) |
| 124 | + case reflect.Bool: |
| 125 | + Equal(t, tc.expected.Bool(), actual.Bool()) |
| 126 | + default: |
| 127 | + Equal(t, tc.expected.Interface(), actual.Interface()) |
| 128 | + } |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments