Debian Codegraph @

pkg/base1/test-format.ts

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3 import QUnit, { f } from "qunit-tests";
4
5 QUnit.test("format", function (assert) {
6 assert.equal(cockpit.format("My $adj message with ${amount} of things", { adj: "special", amount: "lots" }),
7 "My special message with lots of things", "named keys");
8 assert.equal(cockpit.format("My $0 message with $1 of things", ["special", "lots"]),
9 "My special message with lots of things", "number keys");
10 assert.equal(cockpit.format("My $0 message with $1 of things", "special", "lots"),
11 "My special message with lots of things", "vararg keys");
12 assert.equal(cockpit.format("My $0 message with lots of things", "special"),
13 "My special message with lots of things", "vararg one key");
14 assert.equal(cockpit.format("Undefined $value", { }), "Undefined ", "missing value");
15
16 /* All falsy values except `0` should return the empty string */
17 assert.equal(cockpit.format("$0", 0), "0", "`0` as argument");
18 assert.equal(cockpit.format("$0", 0.0), "0", "`0.0` as argument");
19 assert.equal(cockpit.format("$0", false), "", "`false` as argument");
20 assert.equal(cockpit.format("$0", null), "", "`null` as argument");
21 });
22
23 QUnit.test("format_number", function (assert) {
24 const checks = [
25 [23.4, "23.4", "23,4"],
26 [23.46, "23.5", "23,5"],
27 [23.44, "23.4", "23,4"],
28
29 [-23.4, "-23.4", "-23,4"],
30 [-23.46, "-23.5", "-23,5"],
31 [-23.44, "-23.4", "-23,4"],
32
33 [0, "0", "0"],
34 [0.001, "0.001", "0,001"],
35 [-0.001, "-0.001", "-0,001"],
36 // smaller values get rounded up
37 [0.0003, "0.001", "0,001"],
38 [-0.0003, "-0.001", "-0,001"],
39
40 [123.0, "123", "123"],
41 [123.01, "123", "123"],
42 [-123.0, "-123", "-123"],
43 [-123.01, "-123", "-123"],
44 [null, "", ""],
45 [undefined, "", ""],
46 ] as const;
47
48 const saved_language = cockpit.language;
49
50 assert.expect(checks.length * 3 + 7);
51
52 cockpit.language = 'en';
53 for (let i = 0; i < checks.length; i++) {
54 assert.strictEqual(cockpit.format_number(checks[i][0]), checks[i][1],
55 f`format_number@en(${checks[i][0]})`
56 );
57 }
58
59 cockpit.language = 'de';
60 for (let i = 0; i < checks.length; i++) {
61 assert.strictEqual(cockpit.format_number(checks[i][0]), checks[i][2],
62 f`format_number@de(${checks[i][0]})`
63 );
64 }
65
66 cockpit.language = 'pt_BR';
67 for (let i = 0; i < checks.length; i++) {
68 assert.strictEqual(cockpit.format_number(checks[i][0]), checks[i][2],
69 f`format_number@pt_BR(${checks[i][0]})`
70 );
71 }
72
73 /* restore this as not to break the other tests */
74 cockpit.language = saved_language;
75
76 // custom precision
77 assert.strictEqual(cockpit.format_number(1.23456, 2), "1.2", "format_number@en(precision 2)");
78 assert.strictEqual(cockpit.format_number(-1.23456, 2), "-1.2", "format_number@en(negative, precision 2)");
79 assert.strictEqual(cockpit.format_number(0.23456, 2), "0.23", "format_number@en(precision 2)");
80 assert.strictEqual(cockpit.format_number(1.23456, 4), "1.235", "format_number@en(precision 4)");
81 assert.strictEqual(cockpit.format_number(0.23456, 4), "0.2346", "format_number@en(precision 4)");
82 assert.strictEqual(cockpit.format_number(0.000123, 2), "0.01", "format_number@en(very small, precision 2)");
83 assert.strictEqual(cockpit.format_number(-0.000123, 2), "-0.01", "format_number@en(negative, very small, precision 2)");
84 });
85
86 QUnit.test("format_bytes", function (assert) {
87 const checks = [
88 [0, undefined, "0 B"],
89 [0, 1000, "0 B"],
90 [0, 1024, "0 B"],
91 [5, 1000, "5 B"],
92 [5, 1024, "5 B"],
93 [999, 1000, "999 B"],
94 [999, 1024, "999 B"],
95 [1023, 1024, "1023 B"],
96 [1934, undefined, "1.93 kB"],
97 [1934, 1000, "1.93 kB"],
98 [2000, 1024, "1.95 KiB"],
99 [1999, 1000, "2.00 kB"],
100 [1999, 1024, "1.95 KiB"],
101 [1000000, 1000, "1 MB"],
102 [1000001, 1000, "1.00 MB"],
103 [1000000, 1024, "977 KiB"],
104 [2000000, 1024, "1.91 MiB"],
105 [2000000, 1000, "2 MB"],
106 [2000001, 1000, "2.00 MB"],
107 [2000000, "MB", "2 MB"],
108 [2000000, "MiB", "1.91 MiB"],
109 [2000000, "kB", "2000 kB"],
110 [2000000, "KiB", "1953 KiB"],
111 [1, "kB", "0.001 kB"],
112 [0, "kB", "0 kB"],
113 [undefined, "kB", ""],
114 [null, "kB", ""],
115 ] as const;
116
117 for (let i = 0; i < checks.length; i++) {
118 if (typeof checks[i][1] === 'string') {
119 // these tests are for backwards compatibility only
120 continue;
121 }
122
123 const base2 = checks[i][1] == 1024;
124 assert.strictEqual(cockpit.format_bytes(checks[i][0], { base2 }), checks[i][2],
125 f`format_bytes(${checks[i][0]}, ${{ base2 }})`);
126 }
127
128 // old API style (deprecated)
129 for (let i = 0; i < checks.length; i++) {
130 assert.strictEqual(cockpit.format_bytes(checks[i][0], checks[i][1]), checks[i][2],
131 f`format_bytes(${checks[i][0]}, ${checks[i][1]})`
132 );
133 }
134 for (let i = 0; i < checks.length; i++) {
135 const split = checks[i][2].split(" ");
136 assert.deepEqual(cockpit.format_bytes(checks[i][0], checks[i][1], { separate: true }), split,
137 f`format_bytes(${checks[i][0]}, ${checks[i][1]}, ${{ separate: true }})`
138 );
139 }
140
141 // backwards compatible API: format_bytes with a boolean options (used to be a single "separate" flag)
142 assert.strictEqual(cockpit.format_bytes(2500000, 1000, false), "2.50 MB");
143 assert.deepEqual(cockpit.format_bytes(2500000, 1000, true), ["2.50", "MB"]);
144 });
145
146 QUnit.test("format_bytes_per_sec", function (assert) {
147 const checks = [
148 // default unit
149 [0, undefined, undefined, "0 B/s"],
150 [5, undefined, undefined, "5 B/s"],
151 [2555, undefined, undefined, "2.56 kB/s"],
152 [12345678, undefined, undefined, "12.3 MB/s"],
153 // explicit base-2 unit
154 [0, 1024, undefined, "0 B/s"],
155 [2555, 1024, undefined, "2.50 KiB/s"],
156 // explicit base-10 unit
157 [0, 1000, undefined, "0 B/s"],
158 [2555, 1000, undefined, "2.56 kB/s"],
159 [12345678, 1000, undefined, "12.3 MB/s"],
160 // explicit unit
161 [12345678, "kB/s", undefined, "12346 kB/s"],
162 [12345678, "MiB/s", undefined, "11.8 MiB/s"],
163 // custom precision
164 [2555, 1000, { precision: 2 }, "2.6 kB/s"],
165 [25555, "MB/s", { precision: 2 }, "0.026 MB/s"],
166 // significant integer digits exceed custom precision
167 [25555000, "kB/s", { precision: 2 }, "25555 kB/s"],
168 [25555678, "kB/s", { precision: 2 }, "25556 kB/s"],
169 ] as const;
170
171 for (let i = 0; i < checks.length; i++) {
172 if (typeof checks[i][1] === 'string') {
173 // these tests are for backwards compatibility only
174 continue;
175 }
176
177 const base2 = checks[i][1] == 1024;
178 assert.strictEqual(cockpit.format_bytes_per_sec(checks[i][0], { base2, ...checks[i][2] }), checks[i][3],
179 f`format_bytes_per_sec(${checks[i][0]}, ${{ base2, ...checks[i][2] }})`);
180 }
181
182 // old API style (deprecated)
183 for (let i = 0; i < checks.length; i++) {
184 assert.strictEqual(cockpit.format_bytes_per_sec(checks[i][0], checks[i][1], checks[i][2]), checks[i][3],
185 f`format_bytes_per_sec(${checks[i][0]}, ${checks[i][1]}, ${checks[i][2]})`);
186 }
187 // separate unit (very deprecated)
188 assert.deepEqual(cockpit.format_bytes_per_sec(2555, 1024, { separate: true }),
189 ["2.50", "KiB/s"]);
190 // backwards compatible API for separate flag (oh so very deprecated)
191 assert.deepEqual(cockpit.format_bytes_per_sec(2555, 1024, true),
192 ["2.50", "KiB/s"]);
193 });
194
195 QUnit.test("format_bits_per_sec", function (assert) {
196 const checks = [
197 [55, "55 bps"],
198 [55.23456789, "55.2 bps"],
199 [55.98765432, "56.0 bps"],
200 [2555, "2.56 kbps"],
201 [2000, "2 kbps"],
202 [2003, "2.00 kbps"]
203 ] as const;
204
205 assert.expect(checks.length);
206 for (let i = 0; i < checks.length; i++) {
207 assert.strictEqual(cockpit.format_bits_per_sec(checks[i][0]), checks[i][1],
208 f`format_bits_per_sec(${checks[i][0]})`);
209 }
210 });
211
212 QUnit.start();