| 1 | package util |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | func TestStripNumberPrefix(t *testing.T) { |
| 10 | t.Parallel() |
| 11 | |
| 12 | tests := []struct { |
| 13 | name string |
| 14 | input string |
| 15 | wantNum string |
| 16 | wantBase int |
| 17 | }{ |
| 18 | { |
| 19 | name: "strips hash prefix and returns base 16", |
| 20 | input: "#FF", |
| 21 | wantNum: "FF", |
| 22 | wantBase: 16, |
| 23 | }, |
| 24 | { |
| 25 | name: "strips lowercase 0x prefix and returns base 16", |
| 26 | input: "0xABCD", |
| 27 | wantNum: "ABCD", |
| 28 | wantBase: 16, |
| 29 | }, |
| 30 | { |
| 31 | name: "strips uppercase 0X prefix and returns base 16", |
| 32 | input: "0X1234", |
| 33 | wantNum: "1234", |
| 34 | wantBase: 16, |
| 35 | }, |
| 36 | { |
| 37 | name: "returns input unchanged and base 0 when no prefix", |
| 38 | input: "12345", |
| 39 | wantNum: "12345", |
| 40 | wantBase: 0, |
| 41 | }, |
| 42 | { |
| 43 | name: "handles empty string", |
| 44 | input: "", |
| 45 | wantNum: "", |
| 46 | wantBase: 0, |
| 47 | }, |
| 48 | { |
| 49 | name: "handles single hash character", |
| 50 | input: "#", |
| 51 | wantNum: "", |
| 52 | wantBase: 16, |
| 53 | }, |
| 54 | { |
| 55 | name: "handles single 0x prefix", |
| 56 | input: "0x", |
| 57 | wantNum: "", |
| 58 | wantBase: 16, |
| 59 | }, |
| 60 | { |
| 61 | name: "handles only 0 without x", |
| 62 | input: "0", |
| 63 | wantNum: "0", |
| 64 | wantBase: 0, |
| 65 | }, |
| 66 | { |
| 67 | name: "handles decimal number starting with 0", |
| 68 | input: "0123", |
| 69 | wantNum: "0123", |
| 70 | wantBase: 0, |
| 71 | }, |
| 72 | { |
| 73 | name: "handles lowercase hex with 0x", |
| 74 | input: "0xdeadbeef", |
| 75 | wantNum: "deadbeef", |
| 76 | wantBase: 16, |
| 77 | }, |
| 78 | { |
| 79 | name: "handles uppercase hex with hash", |
| 80 | input: "#DEADBEEF", |
| 81 | wantNum: "DEADBEEF", |
| 82 | wantBase: 16, |
| 83 | }, |
| 84 | { |
| 85 | name: "handles hex with mixed case", |
| 86 | input: "0xDeAdBeEf", |
| 87 | wantNum: "DeAdBeEf", |
| 88 | wantBase: 16, |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | for _, tt := range tests { |
| 93 | t.Run(tt.name, func(t *testing.T) { |
| 94 | t.Parallel() |
| 95 | |
| 96 | gotNum, gotBase := StripNumberPrefix(tt.input) |
| 97 | assert.Equal(t, tt.wantNum, gotNum, "StripNumberPrefix(%q) returned number", tt.input) |
| 98 | assert.Equal(t, tt.wantBase, gotBase, "StripNumberPrefix(%q) returned base", tt.input) |
| 99 | }) |
| 100 | } |
| 101 | } |