|
| 1 | +// Copyright 2024-2026 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { suite, test } from "node:test"; |
| 16 | +import * as assert from "node:assert/strict"; |
| 17 | +import { codepointLength, printFloat } from "./format.js"; |
| 18 | + |
| 19 | +void suite("codepointLength", () => { |
| 20 | + void test("counts ASCII as one per char", () => { |
| 21 | + assert.strictEqual(codepointLength(""), 0); |
| 22 | + assert.strictEqual(codepointLength("a"), 1); |
| 23 | + assert.strictEqual(codepointLength("abc"), 3); |
| 24 | + }); |
| 25 | + |
| 26 | + void test("counts surrogate pair as one code point", () => { |
| 27 | + // U+1D44E MATHEMATICAL ITALIC SMALL A → surrogate pair "𝑎" |
| 28 | + const s = "𝑎"; |
| 29 | + assert.strictEqual(s.length, 2); |
| 30 | + assert.strictEqual(codepointLength(s), 1); |
| 31 | + }); |
| 32 | + |
| 33 | + void test("counts combining marks separately", () => { |
| 34 | + // "é" as e + combining acute accent: 2 code points. |
| 35 | + const s = "é"; |
| 36 | + assert.strictEqual(codepointLength(s), 2); |
| 37 | + }); |
| 38 | + |
| 39 | + void test("matches CEL size() spread-based count", () => { |
| 40 | + const cases = ["", "x", "𝑎b", "🇺🇸", "héllo"]; |
| 41 | + for (const s of cases) { |
| 42 | + assert.strictEqual(codepointLength(s), [...s].length); |
| 43 | + } |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +void suite("printFloat", () => { |
| 48 | + void test("formats finite numbers via toString", () => { |
| 49 | + assert.strictEqual(printFloat(0), "0"); |
| 50 | + assert.strictEqual(printFloat(1), "1"); |
| 51 | + assert.strictEqual(printFloat(-1.5), "-1.5"); |
| 52 | + assert.strictEqual(printFloat(1e20), "100000000000000000000"); |
| 53 | + }); |
| 54 | + |
| 55 | + void test("formats NaN", () => { |
| 56 | + assert.strictEqual(printFloat(Number.NaN), "NaN"); |
| 57 | + }); |
| 58 | + |
| 59 | + void test("formats +Infinity", () => { |
| 60 | + assert.strictEqual(printFloat(Number.POSITIVE_INFINITY), "Infinity"); |
| 61 | + }); |
| 62 | + |
| 63 | + void test("formats -Infinity", () => { |
| 64 | + assert.strictEqual(printFloat(Number.NEGATIVE_INFINITY), "-Infinity"); |
| 65 | + }); |
| 66 | +}); |
0 commit comments