Skip to content

Commit 3685064

Browse files
authored
test(jsnum): add binary/oct/hex BigInt cases including underscores and uppercase prefixes (#1678)
1 parent f500954 commit 3685064

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

internal/jsnum/pseudobigint_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,41 @@ func TestParsePseudoBigInt(t *testing.T) {
3131
}
3232
})
3333

34-
// TODO(jakebailey): tests for other bases
34+
t.Run("parse non-decimal bases (small numbers)", func(t *testing.T) {
35+
t.Parallel()
36+
37+
type tc struct {
38+
lit string
39+
out string
40+
}
41+
cases := []tc{
42+
// binary
43+
{lit: "0b0n", out: "0"},
44+
{lit: "0b1n", out: "1"},
45+
{lit: "0b1010n", out: "10"},
46+
{lit: "0b1010_0101n", out: "165"},
47+
{lit: "0B1101n", out: "13"}, // uppercase prefix
48+
49+
// octal
50+
{lit: "0o0n", out: "0"},
51+
{lit: "0o7n", out: "7"},
52+
{lit: "0o755n", out: "493"},
53+
{lit: "0o7_5_5n", out: "493"},
54+
{lit: "0O12n", out: "10"}, // uppercase prefix
55+
56+
// hex
57+
{lit: "0x0n", out: "0"},
58+
{lit: "0xFn", out: "15"},
59+
{lit: "0xFFn", out: "255"},
60+
{lit: "0xF_Fn", out: "255"},
61+
{lit: "0X1Fn", out: "31"}, // uppercase prefix
62+
}
63+
64+
for _, c := range cases {
65+
got := ParsePseudoBigInt(c.lit)
66+
assert.Equal(t, got, c.out, "literal: %q", c.lit)
67+
}
68+
})
3569

3670
t.Run("can parse large literals", func(t *testing.T) {
3771
t.Parallel()

0 commit comments

Comments
 (0)