|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
7 | 8 | ) |
8 | 9 |
|
| 10 | +func TestAnyToString_Success(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + input any |
| 16 | + expected string |
| 17 | + }{ |
| 18 | + {name: "string value", input: "hello", expected: "hello"}, |
| 19 | + {name: "empty string", input: "", expected: ""}, |
| 20 | + {name: "int value", input: 42, expected: "42"}, |
| 21 | + {name: "int zero", input: 0, expected: "0"}, |
| 22 | + {name: "int negative", input: -7, expected: "-7"}, |
| 23 | + {name: "int64 value", input: int64(9999999999), expected: "9999999999"}, |
| 24 | + {name: "int32 value", input: int32(123), expected: "123"}, |
| 25 | + {name: "float64 integer", input: float64(3), expected: "3"}, |
| 26 | + {name: "float64 decimal", input: 3.14, expected: "3.14"}, |
| 27 | + {name: "float64 large", input: 1e18, expected: "1E+18"}, |
| 28 | + {name: "float64 small", input: 0.000123, expected: "0.000123"}, |
| 29 | + {name: "float32 value", input: float32(2.5), expected: "2.5"}, |
| 30 | + {name: "bool true", input: true, expected: "true"}, |
| 31 | + {name: "bool false", input: false, expected: "false"}, |
| 32 | + {name: "uint64 value", input: uint64(100), expected: "100"}, |
| 33 | + {name: "uint value", input: uint(50), expected: "50"}, |
| 34 | + {name: "fallback type", input: []int{1, 2}, expected: fmt.Sprintf("%v", []int{1, 2})}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tt := range tests { |
| 38 | + t.Run(tt.name, func(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + assert.Equal(t, tt.expected, AnyToString(tt.input), "AnyToString should match expected output") |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TestAnyToString_MatchesFmtSprintf verifies that AnyToString matches |
| 46 | +// fmt.Sprintf("%v") for common types. Note: float scientific notation uses |
| 47 | +// uppercase 'E' (from 'G' format) vs fmt.Sprintf's lowercase 'e', so floats |
| 48 | +// that produce scientific notation are excluded from this exact-match test. |
| 49 | +func TestAnyToString_MatchesFmtSprintf(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + values := []any{ |
| 53 | + "text", 42, int64(100), int32(10), |
| 54 | + 3.14, float64(0.5), |
| 55 | + float32(1.5), true, false, |
| 56 | + uint64(99), uint(7), |
| 57 | + } |
| 58 | + |
| 59 | + for _, v := range values { |
| 60 | + t.Run(fmt.Sprintf("%T(%v)", v, v), func(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + assert.Equal(t, fmt.Sprintf("%v", v), AnyToString(v), |
| 63 | + "AnyToString should match fmt.Sprintf for %T", v) |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
9 | 68 | func TestBuildAbsoluteReference(t *testing.T) { |
10 | 69 | t.Parallel() |
11 | 70 |
|
|
0 commit comments