Debian Codegraph @

test_suite/stdlib.jsonnet

raw
1 /*
2 Copyright 2015 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 // This file tests functions from the standard library (std.jsonnet and builtins).
18
19 // Can capture std from another file.
20 std.assertEqual((import 'lib/capture_std_func.libsonnet')().sqrt(4), 2) &&
21
22 // Each import has its own std.
23 std.assertEqual(
24 local std = { sqrt: function(x) x };
25 local lib = import 'lib/capture_std.libsonnet';
26 lib.sqrt(4),
27 2
28 ) &&
29
30 // Now, test each std library function in turn.
31
32 std.assertEqual(std.makeArray(10, function(i) i + 1), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) &&
33 std.assertEqual(std.makeArray(0, function(i) null), []) &&
34
35 local assertClose(a, b) =
36 // Using 1e-12 as tolerance. Jsonnet uses double-precision floats with machine epsilon of 2**-53 ≈ 1.11e-16.
37 // This tolerance is ~9000x the machine epsilon, which is quite lenient and should work across
38 // different platforms and math libraries.
39 //
40 // We use a combination of absolute and relative error to handle both small and large values:
41 // - For values near zero, we use absolute error to avoid division issues
42 // - For larger values, we use relative error to maintain precision
43 //
44 // This correctly handles cases like:
45 // assertClose(1e-15, 0) - should pass (both are essentially zero)
46 // assertClose(-100, 0) - should fail (significant difference)
47 // assertClose(std.sin(std.pi), 0) - should pass (tiny floating point error)
48 local abs_a = std.abs(a);
49 local abs_b = std.abs(b);
50 local diff = std.abs(a - b);
51 local max_abs = std.max(abs_a, abs_b);
52 local err =
53 if abs_a == 0 && abs_b == 0 then
54 0 // Both are exactly zero, so no error
55 else if max_abs < 1e-12 then
56 diff // For very small values, use absolute error
57 else
58 diff / max_abs; // For larger values, use relative error
59 if err > 1e-12 then
60 error 'Assertion failed (error ' + err + '). ' + a + ' !~ ' + b
61 else
62 true;
63
64 // Regression test for https://github.com/google/jsonnet/issues/511
65 std.assertEqual(
66 std.makeArray(10, std.sqrt),
67 std.makeArray(10, function(n) std.sqrt(n))
68 ) &&
69
70 // Regression test for https://github.com/google/jsonnet/issues/385
71 std.assertEqual(std.makeArray(sz=3, func=function(i) i), [0, 1, 2]) &&
72 std.assertEqual(std.pow(x=3, n=2), 9) &&
73 std.assertEqual(std.pow(n=2, x=3), 9) &&
74
75 std.assertEqual(std.pow(3, 2), 9) &&
76 std.assertEqual(std.floor(10), 10) &&
77 std.assertEqual(std.floor(10.99999), 10) &&
78 std.assertEqual(std.ceil(10), 10) &&
79 std.assertEqual(std.ceil(10.99999), 11) &&
80 std.assertEqual(std.sqrt(0), 0) &&
81 std.assertEqual(std.sqrt(1), 1) &&
82 std.assertEqual(std.sqrt(9), 3) &&
83 std.assertEqual(std.sqrt(16), 4) &&
84 std.assertEqual(std.abs(33), 33) &&
85 std.assertEqual(std.abs(-33), 33) &&
86 std.assertEqual(std.abs(0), 0) &&
87
88 assertClose(std.sin(0.0 * std.pi), 0) &&
89 assertClose(std.sin(0.5 * std.pi), 1) &&
90 assertClose(std.sin(1.0 * std.pi), 0) &&
91 assertClose(std.sin(1.5 * std.pi), -1) &&
92 assertClose(std.sin(2.0 * std.pi), 0) &&
93 assertClose(std.cos(0.0 * std.pi), 1) &&
94 assertClose(std.cos(0.5 * std.pi), 0) &&
95 assertClose(std.cos(1.0 * std.pi), -1) &&
96 assertClose(std.cos(1.5 * std.pi), 0) &&
97 assertClose(std.cos(2.0 * std.pi), 1) &&
98 assertClose(std.tan(0), 0) &&
99 assertClose(std.tan(0.25 * std.pi), 1) &&
100 assertClose(std.asin(0), 0) &&
101 assertClose(std.acos(1), 0) &&
102 assertClose(std.asin(1), 0.5 * std.pi) &&
103 assertClose(std.acos(0), 0.5 * std.pi) &&
104 assertClose(std.atan(0), 0) &&
105 assertClose(std.atan2(1, 1), std.pi / 4) &&
106 assertClose(std.atan2(-1, 1), -std.pi / 4) &&
107 assertClose(std.atan2(1.2, -3.8), 2.835713782184941) && // arbitrary, done on a calculator
108 assertClose(std.deg2rad(0), 0) &&
109 assertClose(std.deg2rad(45), std.pi / 4) &&
110 assertClose(std.deg2rad(90), std.pi / 2) &&
111 assertClose(std.deg2rad(172), 3.0019663134302466) && // arbitrary, done on a calculator
112 assertClose(std.rad2deg(std.pi / 4), 45) &&
113 assertClose(std.rad2deg(std.pi / 2), 90) &&
114 assertClose(std.rad2deg(3.0019663134302466), 172) && // arbitrary, done on a calculator
115 assertClose(std.hypot(3, 4), 5) &&
116 assertClose(std.hypot(5, 12), 13) &&
117 assertClose(std.hypot(1, 1), std.sqrt(2)) &&
118 assertClose(std.log(std.exp(5)), 5) &&
119 assertClose(std.mantissa(1), 0.5) &&
120 assertClose(std.exponent(1), 1) &&
121 assertClose(std.mantissa(128), 0.5) &&
122 assertClose(std.exponent(128), 8) &&
123 assertClose(std.log2(std.pow(2, -5)), -5) &&
124 assertClose(std.log2(std.pow(2, 0)), 0) &&
125 assertClose(std.log2(std.pow(2, std.pi)), std.pi) &&
126 assertClose(std.log10(std.pow(10, -5)), -5) &&
127 assertClose(std.log10(std.pow(10, 0)), 0) &&
128 assertClose(std.log10(std.pow(10, std.pi)), std.pi) &&
129
130 std.assertEqual(std.clamp(-3, 0, 5), 0) &&
131 std.assertEqual(std.clamp(4, 0, 5), 4) &&
132 std.assertEqual(std.clamp(7, 0, 5), 5) &&
133
134 std.assertEqual(std.type(null), 'null') &&
135 std.assertEqual(std.type(true), 'boolean') &&
136 std.assertEqual(std.type(false), 'boolean') &&
137 std.assertEqual(std.type(0), 'number') &&
138 std.assertEqual(std.type(-1e10), 'number') &&
139 std.assertEqual(std.type([1, 2, 3]), 'array') &&
140 std.assertEqual(std.type([]), 'array') &&
141 std.assertEqual(std.type(function(x) x), 'function') &&
142 std.assertEqual(std.type({ x: 1, y: 2 }), 'object') &&
143 std.assertEqual(std.type({}), 'object') &&
144 std.assertEqual(std.type('fail'), 'string') &&
145 std.assertEqual(std.type('' + {}), 'string') &&
146
147 std.assertEqual(std.isString(''), true) &&
148 std.assertEqual(std.isBoolean(true), true) &&
149 std.assertEqual(std.isNumber(0), true) &&
150 std.assertEqual(std.isObject({}), true) &&
151 std.assertEqual(std.isArray([]), true) &&
152 std.assertEqual(std.isFunction(function() 0), true) &&
153 std.assertEqual(std.isNull(null), true) &&
154 std.assertEqual(std.isNull('foo'), false) &&
155
156 std.assertEqual(std.isString(null), false) &&
157 std.assertEqual(std.isBoolean(null), false) &&
158 std.assertEqual(std.isNumber(null), false) &&
159 std.assertEqual(std.isObject(null), false) &&
160 std.assertEqual(std.isArray(null), false) &&
161 std.assertEqual(std.isFunction(null), false) &&
162
163 std.assertEqual(std.member('foo', 'o'), true) &&
164 std.assertEqual(std.member('foo', 'f'), true) &&
165 std.assertEqual(std.member('foo', 'x'), false) &&
166 std.assertEqual(std.member([], 'o'), false) &&
167 std.assertEqual(std.member(['f'], 'o'), false) &&
168 std.assertEqual(std.member(['f', 'o', 'o'], 'o'), true) &&
169 std.assertEqual(std.member(['f', 'o', 'o'], 'f'), true) &&
170 std.assertEqual(std.member(['f', 'o', 'o'], 'g'), false) &&
171
172 std.assertEqual(std.count([true, false, false, true, true, true, false], true), 4) &&
173 std.assertEqual(std.count([true, false, false, true, true, true, false], false), 3) &&
174
175 std.assertEqual(std.filter(function(x) x % 2 == 0, [1, 2, 3, 4]), [2, 4]) &&
176 std.assertEqual(std.filter(function(x) false, [1, 2, 3, 4]), []) &&
177 std.assertEqual(std.filter(function(x) x, []), []) &&
178
179 std.assertEqual(std.objectHas({ x: 1, y: 2 }, 'x'), true) &&
180 std.assertEqual(std.objectHas({ x: 1, y: 2 }, 'z'), false) &&
181 std.assertEqual(std.objectHas({}, 'z'), false) &&
182
183 std.assertEqual(std.length('asdfasdf'), 8) &&
184 std.assertEqual(std.length([1, 4, 9, error 'foo']), 4) &&
185 std.assertEqual(std.length(function(x, y, z) error 'foo'), 3) &&
186 std.assertEqual(std.length({ x: 1, y: 2 }), 2) &&
187 std.assertEqual(std.length({ a: 1, b: 2, c: 0 } + { c: 3, d: error 'foo' }), 4) &&
188 std.assertEqual(std.length(''), 0) &&
189 std.assertEqual(std.length([]), 0) &&
190 std.assertEqual(std.length(function() error 'foo'), 0) &&
191 std.assertEqual(std.length({}), 0) &&
192
193 std.assertEqual(std.objectFields({}), []) &&
194 std.assertEqual(std.objectFields({ x: 1, y: 2 }), ['x', 'y']) &&
195 std.assertEqual(std.objectFields({ a: 1, b: 2, c: null, d: error 'foo' }), ['a', 'b', 'c', 'd']) &&
196 std.assertEqual(std.objectFields({ x: 1 } { x: 1 }), ['x']) &&
197 std.assertEqual(std.objectFields({ x: 1 } { x:: 1 }), []) &&
198 std.assertEqual(std.objectFields({ x: 1 } { x::: 1 }), ['x']) &&
199 std.assertEqual(std.objectFields({ x:: 1 } { x: 1 }), []) &&
200 std.assertEqual(std.objectFields({ x:: 1 } { x:: 1 }), []) &&
201 std.assertEqual(std.objectFields({ x:: 1 } { x::: 1 }), ['x']) &&
202 std.assertEqual(std.objectFields({ x::: 1 } { x: 1 }), ['x']) &&
203 std.assertEqual(std.objectFields({ x::: 1 } { x:: 1 }), []) &&
204 std.assertEqual(std.objectFields({ x::: 1 } { x::: 1 }), ['x']) &&
205
206 std.assertEqual(std.objectValues({}), []) &&
207 std.assertEqual(std.objectValues({ x: 1, y: 2 }), [1, 2]) &&
208 std.assertEqual(std.objectValues({ x: 1 } { x: 1 }), [1]) &&
209 std.assertEqual(std.objectValues({ x: 1 } { x:: 1 }), []) &&
210 std.assertEqual(std.objectValues({ x: 1 } { x::: 1 }), [1]) &&
211 std.assertEqual(std.objectValues({ x:: 1 } { x: 1 }), []) &&
212 std.assertEqual(std.objectValues({ x:: 1 } { x:: 1 }), []) &&
213 std.assertEqual(std.objectValues({ x:: 1 } { x::: 1 }), [1]) &&
214 std.assertEqual(std.objectValues({ x::: 1 } { x: 1 }), [1]) &&
215 std.assertEqual(std.objectValues({ x::: 1 } { x:: 1 }), []) &&
216 std.assertEqual(std.objectValues({ x::: 1 } { x::: 1 }), [1]) &&
217
218 std.assertEqual(std.objectKeysValues({}), []) &&
219 std.assertEqual(std.objectKeysValues({ x: 1, y: 2 }), [{ key: 'x', value: 1 }, { key: 'y', value: 2 }]) &&
220 std.assertEqual(std.objectKeysValues({ x: 1 } { x: 1 }), [{ key: 'x', value: 1 }]) &&
221 std.assertEqual(std.objectKeysValues({ x: 1 } { x:: 1 }), []) &&
222 std.assertEqual(std.objectKeysValues({ x: 1 } { x::: 1 }), [{ key: 'x', value: 1 }]) &&
223 std.assertEqual(std.objectKeysValues({ x:: 1 } { x: 1 }), []) &&
224 std.assertEqual(std.objectKeysValues({ x:: 1 } { x:: 1 }), []) &&
225 std.assertEqual(std.objectKeysValues({ x:: 1 } { x::: 1 }), [{ key: 'x', value: 1 }]) &&
226 std.assertEqual(std.objectKeysValues({ x::: 1 } { x: 1 }), [{ key: 'x', value: 1 }]) &&
227 std.assertEqual(std.objectKeysValues({ x::: 1 } { x:: 1 }), []) &&
228 std.assertEqual(std.objectKeysValues({ x::: 1 } { x::: 1 }), [{ key: 'x', value: 1 }]) &&
229
230 std.assertEqual(std.get({ x: 1, y: 2 }, 'x', 5), 1) &&
231 std.assertEqual(std.get({ x: 1, y: 2 }, 'z', 5), 5) &&
232 std.assertEqual(std.get({ x: 1, y: 2 }, 'z'), null) &&
233 std.assertEqual(std.get({ x::: 1, y::: 2 }, 'x', 5), 1) &&
234 std.assertEqual(std.get({ x::: 1, y::: 2 }, 'z', 5), 5) &&
235 std.assertEqual(std.get({ x::: 1, y::: 2 }, 'z'), null) &&
236 std.assertEqual(std.get({ x:: 1, y:: 2 }, 'x', 5), 1) &&
237 std.assertEqual(std.get({ x:: 1, y:: 2 }, 'x'), 1) &&
238 std.assertEqual(std.get({ x:: 1, y:: 2 }, 'z', 5), 5) &&
239 std.assertEqual(std.get({ x:: 1, y:: 2 }, 'z'), null) &&
240 std.assertEqual(std.get({}, 'z', 5), 5) &&
241 std.assertEqual(std.get({}, 'z'), null) &&
242 std.assertEqual(std.get({ x: 1, y: 2 }, 'x', 5, false), 1) &&
243 std.assertEqual(std.get({ x: 1, y: 2 }, 'z', 5, false), 5) &&
244 std.assertEqual(std.get({ x::: 1, y::: 2 }, 'x', 5, false), 1) &&
245 std.assertEqual(std.get({ x::: 1, y::: 2 }, 'z', 5, false), 5) &&
246 std.assertEqual(std.get({ x:: 1, y:: 2 }, 'x', 5, false), 5) &&
247 std.assertEqual(std.get({ x:: 1, y:: 2 }, 'z', 5, false), 5) &&
248 std.assertEqual(std.get({}, 'z', 5, false), 5) &&
249
250 std.assertEqual(std.toString({ a: 1, b: 2 }), '{"a": 1, "b": 2}') &&
251 std.assertEqual(std.toString({}), '{ }') &&
252 std.assertEqual(std.toString([1, 2]), '[1, 2]') &&
253 std.assertEqual(std.toString([]), '[ ]') &&
254 std.assertEqual(std.toString(null), 'null') &&
255 std.assertEqual(std.toString(true), 'true') &&
256 std.assertEqual(std.toString(false), 'false') &&
257 std.assertEqual(std.toString('str'), 'str') &&
258 std.assertEqual(std.toString(''), '') &&
259 std.assertEqual(std.toString([1, 2, 'foo']), '[1, 2, "foo"]') &&
260
261 std.assertEqual(std.substr('cookie', 1, 3), 'ook') &&
262 std.assertEqual(std.substr('cookie', 1, 0), '') &&
263 std.assertEqual(std.substr('cookie', 1, 15), 'ookie') &&
264 std.assertEqual(std.substr('cookie', 0, 5), 'cooki') &&
265 std.assertEqual(std.substr('cookie', 0, 6), 'cookie') &&
266 std.assertEqual(std.substr('cookie', 0, 15), 'cookie') &&
267 std.assertEqual(std.substr('cookie', 3, 1), 'k') &&
268 std.assertEqual(std.substr('cookie', 20, 1), '') &&
269 std.assertEqual(std.substr('cookie', 6, 1), '') &&
270
271
272 std.assertEqual(std.substr('ąę', 1, 1), 'ę') &&
273
274 std.assertEqual(std.startsWith('food', 'foo'), true) &&
275 std.assertEqual(std.startsWith('food', 'food'), true) &&
276 std.assertEqual(std.startsWith('food', 'foody'), false) &&
277 std.assertEqual(std.startsWith('food', 'wat'), false) &&
278
279 std.assertEqual(std.endsWith('food', 'ood'), true) &&
280 std.assertEqual(std.endsWith('food', 'food'), true) &&
281 std.assertEqual(std.endsWith('food', 'omgfood'), false) &&
282 std.assertEqual(std.endsWith('food', 'wat'), false) &&
283
284 std.assertEqual(std.stripChars(' test test test ', ' '), 'test test test') &&
285 std.assertEqual(std.stripChars('aaabbbbcccc', 'ac'), 'bbbb') &&
286 std.assertEqual(std.stripChars('cacabbbbaacc', 'ac'), 'bbbb') &&
287 std.assertEqual(std.stripChars('', 'ac'), '') &&
288
289 std.assertEqual(std.lstripChars(' test test test ', ' '), 'test test test ') &&
290 std.assertEqual(std.lstripChars('aaabbbbcccc', 'ac'), 'bbbbcccc') &&
291 std.assertEqual(std.lstripChars('cacabbbbaacc', 'ac'), 'bbbbaacc') &&
292 std.assertEqual(std.lstripChars('', 'ac'), '') &&
293
294 std.assertEqual(std.rstripChars(' test test test ', ' '), ' test test test') &&
295 std.assertEqual(std.rstripChars('aaabbbbcccc', 'ac'), 'aaabbbb') &&
296 std.assertEqual(std.rstripChars('cacabbbbaacc', 'ac'), 'cacabbbb') &&
297 std.assertEqual(std.rstripChars('', 'ac'), '') &&
298
299 std.assertEqual(std.codepoint('a'), 97) &&
300 std.assertEqual(std.char(97), 'a') &&
301 std.assertEqual(std.codepoint('\u0000'), 0) &&
302 std.assertEqual(std.char(0), '\u0000') &&
303
304 std.assertEqual(std.strReplace('A cat walked by', 'cat', 'dog'), 'A dog walked by') &&
305 std.assertEqual(std.strReplace('cat', 'cat', 'dog'), 'dog') &&
306 std.assertEqual(std.strReplace('', 'cat', ''), '') &&
307 std.assertEqual(std.strReplace('xoxoxoxox', 'xoxox3xox', 'A'), 'xoxoxoxox') &&
308 std.assertEqual(std.strReplace('xoxoxox3xox', 'xoxox3xox', 'A'), 'xoA') &&
309 std.assertEqual(std.strReplace('A cat is a cat', 'cat', 'dog'), 'A dog is a dog') &&
310 std.assertEqual(std.strReplace('wishyfishyisishy', 'ish', 'and'), 'wandyfandyisandy') &&
311
312 std.assertEqual(std.map(function(x) x * x, []), []) &&
313 std.assertEqual(std.map(function(x) x * x, [1, 2, 3, 4]), [1, 4, 9, 16]) &&
314 std.assertEqual(std.map(function(x) x * x, std.filter(function(x) x > 5, std.range(1, 10))), [36, 49, 64, 81, 100]) &&
315
316 std.assertEqual(std.mapWithIndex(function(i, x) x * i, []), []) &&
317 std.assertEqual(std.mapWithIndex(function(i, x) x * i, [1, 2, 3, 4]), [0, 2, 6, 12]) &&
318 std.assertEqual(std.mapWithIndex(function(i, x) x * i, std.filter(function(x) x > 5, std.range(1, 10))), [0, 7, 16, 27, 40]) &&
319
320 std.assertEqual(std.mapWithKey(function(k, o) k + o, {}), {}) &&
321 std.assertEqual(std.mapWithKey(function(k, o) k + o, { a: 1, b: 2 }), { a: 'a1', b: 'b2' }) &&
322
323 std.assertEqual(std.flatMap(function(x) [x, x], [1, 2, 3]), [1, 1, 2, 2, 3, 3]) &&
324 std.assertEqual(std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3]), [1, 3]) &&
325 std.assertEqual(std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3]), [3, 2, 9, 6]) &&
326
327 std.assertEqual(std.filterMap(function(x) x >= 0, function(x) x * x, [-3, -2, -1, 0, 1, 2, 3]), [0, 1, 4, 9]) &&
328
329 std.assertEqual(std.foldl(function(x, y) [x, y], [], 'foo'), 'foo') &&
330 std.assertEqual(std.foldl(function(x, y) [x, y], [1, 2, 3, 4], []), [[[[[], 1], 2], 3], 4]) &&
331
332 std.assertEqual(std.foldr(function(x, y) [x, y], [], 'bar'), 'bar') &&
333 std.assertEqual(std.foldr(function(x, y) [x, y], [1, 2, 3, 4], []), [1, [2, [3, [4, []]]]]) &&
334
335 std.assertEqual(std.range(2, 6), [2, 3, 4, 5, 6]) &&
336 std.assertEqual(std.range(2, 2), [2]) &&
337 std.assertEqual(std.range(2, 1), []) &&
338
339 std.assertEqual(std.repeat([], 0), []) &&
340 std.assertEqual(std.repeat([1], 1), [1]) &&
341 std.assertEqual(std.repeat([1, 2], 1), [1, 2]) &&
342 std.assertEqual(std.repeat([1], 2), [1, 1]) &&
343 std.assertEqual(std.repeat([1, 2], 2), [1, 2, 1, 2]) &&
344 std.assertEqual(std.repeat('a', 1), 'a') &&
345 std.assertEqual(std.repeat('a', 4), 'aaaa') &&
346 std.assertEqual(std.repeat('ab', 4), 'abababab') &&
347 std.assertEqual(std.repeat('a', 0), '') &&
348
349 std.assertEqual(std.join([], [[1, 2], [3, 4, 5], [6]]), [1, 2, 3, 4, 5, 6]) &&
350 std.assertEqual(std.join(['a', 'b'], [[]]), []) &&
351 std.assertEqual(std.join(['a', 'b'], []), []) &&
352 std.assertEqual(std.join(['a', 'b'], [null, [1, 2], null, [3, 4, 5], [6], null]), [1, 2, 'a', 'b', 3, 4, 5, 'a', 'b', 6]) &&
353 std.assertEqual(std.join(['a', 'b'], [[], [1, 2]]), ['a', 'b', 1, 2]) &&
354 std.assertEqual(std.join('', [null, '12', null, '345', '6', null]), '123456') &&
355 std.assertEqual(std.join('ab', ['']), '') &&
356 std.assertEqual(std.join('ab', []), '') &&
357 std.assertEqual(std.join('ab', [null, '12', null, '345', '6', null]), '12ab345ab6') &&
358 std.assertEqual(std.join('ab', ['', '12']), 'ab12') &&
359 std.assertEqual(std.lines(['a', null, 'b']), 'a\nb\n') &&
360
361 std.assertEqual(std.flattenArrays([[1, 2, 3], [4, 5, 6], []]), [1, 2, 3, 4, 5, 6]) &&
362
363 std.assertEqual(std.flattenDeepArray([]), []) &&
364 std.assertEqual(std.flattenDeepArray([1, 2, 3]), [1, 2, 3]) &&
365 std.assertEqual(std.flattenDeepArray([1, [2, 3]]), [1, 2, 3]) &&
366 std.assertEqual(std.flattenDeepArray([[1], [2, 3], [[null]]]), [1, 2, 3, null]) &&
367
368 std.assertEqual(
369 std.manifestIni({
370 main: { a: '1', b: '2' },
371 sections: {
372 s1: { x: '11', y: '22', z: '33' },
373 s2: { p: 'yes', q: '' },
374 empty: {},
375 },
376 }),
377 'a = 1\nb = 2\n[empty]\n[s1]\nx = 11\ny = 22\nz = 33\n[s2]\np = yes\nq = \n'
378 ) &&
379
380 std.assertEqual(
381 std.manifestIni({
382 sections: {
383 s1: { x: '11', y: '22', z: '33' },
384 s2: { p: 'yes', q: '' },
385 empty: {},
386 },
387 }),
388 '[empty]\n[s1]\nx = 11\ny = 22\nz = 33\n[s2]\np = yes\nq = \n'
389 ) &&
390
391 std.assertEqual(
392 std.manifestIni({
393 main: { a: ['1', '2'] },
394 sections: {
395 s2: { p: ['yes', ''] },
396 },
397 }), 'a = 1\na = 2\n[s2]\np = yes\np = \n'
398 ) &&
399
400
401 std.assertEqual(std.escapeStringJson('hello'), '"hello"') &&
402 std.assertEqual(std.escapeStringJson('he"llo'), '"he\\"llo"') &&
403 std.assertEqual(std.escapeStringJson('he"llo'), '"he\\"llo"') &&
404 std.assertEqual(std.escapeStringBash("he\"l'lo"), "'he\"l'\"'\"'lo'") &&
405 std.assertEqual(std.escapeStringDollars('The path is ${PATH}.'), 'The path is $${PATH}.') &&
406 std.assertEqual(std.escapeStringXML('2 < 3'), '2 &lt; 3') &&
407 std.assertEqual(std.escapeStringXML('3 > 2'), '3 &gt; 2') &&
408 std.assertEqual(std.escapeStringXML('"foo"'), '&quot;foo&quot;') &&
409 std.assertEqual(std.escapeStringXML("don't believe the hype"), 'don&apos;t believe the hype') &&
410 std.assertEqual(std.escapeStringXML('PB&J'), 'PB&amp;J') &&
411 std.assertEqual(std.escapeStringJson('!~'), '"!~"') &&
412
413 std.assertEqual(std.manifestPython({
414 x: 'test',
415 y: [],
416 z: ['foo', 'bar'],
417 n: 1,
418 a: true,
419 b: false,
420 c: null,
421 o: { f1: 'foo', f2: 'bar' },
422 }), '{%s}' % std.join(', ', [
423 '"a": True',
424 '"b": False',
425 '"c": None',
426 '"n": 1',
427 '"o": {"f1": "foo", "f2": "bar"}',
428 '"x": "test"',
429 '"y": []',
430 '"z": ["foo", "bar"]',
431 ])) &&
432
433 std.assertEqual(std.manifestPythonVars({
434 x: 'test',
435 y: [],
436 z: ['foo', 'bar'],
437 n: 1,
438 a: true,
439 b: false,
440 c: null,
441 o: { f1: 'foo', f2: 'bar' },
442 }), std.join('\n', [
443 'a = True',
444 'b = False',
445 'c = None',
446 'n = 1',
447 'o = {"f1": "foo", "f2": "bar"}',
448 'x = "test"',
449 'y = []',
450 'z = ["foo", "bar"]',
451 '',
452 ])) &&
453
454 std.assertEqual(
455 std.manifestXmlJsonml(
456 ['f', {}, ' ', ['g', 'inside'], 'nope', ['h', { attr: 'yolo' }, ['x', { attr: 'otter' }]]]
457 ),
458 '<f> <g>inside</g>nope<h attr="yolo"><x attr="otter"></x></h></f>'
459 ) &&
460
461
462 std.assertEqual(std.base64('Hello World!'), 'SGVsbG8gV29ybGQh') &&
463 std.assertEqual(std.base64('Hello World'), 'SGVsbG8gV29ybGQ=') &&
464 std.assertEqual(std.base64('Hello Worl'), 'SGVsbG8gV29ybA==') &&
465 std.assertEqual(std.base64(''), '') &&
466
467 std.assertEqual(std.base64Decode('SGVsbG8gV29ybGQh'), 'Hello World!') &&
468 std.assertEqual(std.base64Decode('SGVsbG8gV29ybGQ='), 'Hello World') &&
469 std.assertEqual(std.base64Decode('SGVsbG8gV29ybA=='), 'Hello Worl') &&
470 std.assertEqual(std.base64Decode(''), '') &&
471
472 std.assertEqual(std.reverse([]), []) &&
473 std.assertEqual(std.reverse([1]), [1]) &&
474 std.assertEqual(std.reverse([1, 2]), [2, 1]) &&
475 std.assertEqual(std.reverse([1, 2, 3]), [3, 2, 1]) &&
476 std.assertEqual(std.reverse([[1, 2, 3]]), [[1, 2, 3]]) &&
477
478 std.assertEqual(std.sort([]), []) &&
479 std.assertEqual(std.sort([1]), [1]) &&
480 std.assertEqual(std.sort([1, 2]), [1, 2]) &&
481 std.assertEqual(std.sort([2, 1]), [1, 2]) &&
482 std.assertEqual(std.sort(['1', '2']), ['1', '2']) &&
483 std.assertEqual(std.sort(['2', '1']), ['1', '2']) &&
484 std.assertEqual(
485 std.sort(['The', 'rain', 'in', 'spain', 'falls', 'mainly', 'on', 'the', 'plain.']),
486 ['The', 'falls', 'in', 'mainly', 'on', 'plain.', 'rain', 'spain', 'the']
487 ) &&
488
489 std.assertEqual(std.uniq([]), []) &&
490 std.assertEqual(std.uniq([1]), [1]) &&
491 std.assertEqual(std.uniq([1, 2]), [1, 2]) &&
492 std.assertEqual(std.uniq(['1', '2']), ['1', '2']) &&
493 std.assertEqual(
494 std.uniq(['The', 'falls', 'in', 'mainly', 'on', 'plain.', 'rain', 'spain', 'the']),
495 ['The', 'falls', 'in', 'mainly', 'on', 'plain.', 'rain', 'spain', 'the']
496 ) &&
497
498 local animal_set = ['ant', 'bat', 'cat', 'dog', 'elephant', 'fish', 'giraffe'];
499
500 std.assertEqual(
501 std.uniq(['ant', 'bat', 'cat', 'dog', 'dog', 'elephant', 'fish', 'fish', 'giraffe']),
502 animal_set
503 ) &&
504
505 std.assertEqual(
506 std.set(['dog', 'ant', 'bat', 'cat', 'dog', 'elephant', 'fish', 'giraffe', 'fish']),
507 animal_set
508 ) &&
509
510 std.assertEqual(std.setUnion(animal_set, animal_set), animal_set) &&
511 std.assertEqual(std.setUnion(animal_set, []), animal_set) &&
512 std.assertEqual(std.setUnion([], animal_set), animal_set) &&
513 std.assertEqual(std.setUnion([], []), []) &&
514 std.assertEqual(std.setUnion(['a', 'b'], ['b', 'c']), ['a', 'b', 'c']) &&
515
516 std.assertEqual(std.setInter(animal_set, animal_set), animal_set) &&
517 std.assertEqual(std.setInter(animal_set, []), []) &&
518 std.assertEqual(std.setInter([], animal_set), []) &&
519 std.assertEqual(std.setInter([], []), []) &&
520 std.assertEqual(std.setInter(['a', 'b'], ['b', 'c']), ['b']) &&
521
522 std.assertEqual(std.setDiff(animal_set, animal_set), []) &&
523 std.assertEqual(std.setDiff(animal_set, []), animal_set) &&
524 std.assertEqual(std.setDiff([], animal_set), []) &&
525 std.assertEqual(std.setDiff([], []), []) &&
526 std.assertEqual(std.setDiff(['a', 'b'], ['b', 'c']), ['a']) &&
527
528 std.assertEqual(std.setMember('a', ['a', 'b', 'c']), true) &&
529 std.assertEqual(std.setMember('a', []), false) &&
530 std.assertEqual(std.setMember('a', ['b', 'c']), false) &&
531
532 (
533 if std.thisFile == '<stdin>' then
534 // This happens when testing the unparser.
535 true
536 else
537 std.assertEqual(std.thisFile, 'stdlib.jsonnet')
538 ) &&
539
540 std.assertEqual(import 'this_file/a.libsonnet', 'this_file/a.libsonnet') &&
541 std.assertEqual(import 'this_file/b.libsonnet', 'this_file/a.libsonnet') &&
542
543
544 std.assertEqual(std.extVar('var1'), 'test') &&
545
546 std.assertEqual(std.toString(std.extVar('var2')), '{"x": 1, "y": 2}') &&
547 std.assertEqual(std.extVar('var2'), { x: 1, y: 2 }) &&
548 std.assertEqual(std.extVar('var2') { x+: 2 }.x, 3) &&
549
550 std.assertEqual(std.split('foo/bar', '/'), ['foo', 'bar']) &&
551 std.assertEqual(std.split('/foo/', '/'), ['', 'foo', '']) &&
552 std.assertEqual(std.split('foo/_bar', '/_'), ['foo', 'bar']) &&
553 std.assertEqual(std.split('/_foo/_', '/_'), ['', 'foo', '']) &&
554
555 std.assertEqual(std.splitLimit('foo/bar', '/', 1), ['foo', 'bar']) &&
556 std.assertEqual(std.splitLimit('/foo/', '/', 1), ['', 'foo/']) &&
557 std.assertEqual(std.splitLimit('foo/_bar', '/_', 1), ['foo', 'bar']) &&
558 std.assertEqual(std.splitLimit('/_foo/_', '/_', 1), ['', 'foo/_']) &&
559
560 std.assertEqual(std.splitLimitR('foo/bar', '/', 1), ['foo', 'bar']) &&
561 std.assertEqual(std.splitLimitR('/foo/', '/', 1), ['/foo', '']) &&
562 std.assertEqual(std.splitLimitR('/foo/', '/', -1), ['', 'foo', '']) &&
563 std.assertEqual(std.splitLimitR('foo/_bar', '/_', 1), ['foo', 'bar']) &&
564 std.assertEqual(std.splitLimitR('/_foo/_', '/_', 1), ['/_foo', '']) &&
565 std.assertEqual(std.splitLimitR('/_foo/_', '/_', -1), ['', 'foo', '']) &&
566
567 local some_toml = {
568 key: 'value',
569 simple: { t: 5 },
570 section: {
571 a: 1,
572 nested: { b: 2 },
573 'e$caped': { q: 't' },
574 array: [
575 { c: 3 },
576 { d: 4 },
577 ],
578 nestedArray: [{
579 k: 'v',
580 nested: { e: 5 },
581 }],
582 },
583 arraySection: [
584 { q: 1 },
585 { w: 2 },
586 ],
587 'escaped"Section': { z: 'q' },
588 emptySection: {},
589 emptyArraySection: [{}],
590 bool: true,
591 notBool: false,
592 number: 7,
593 array: ['s', 1, [2, 3], { r: 6, a: ['0', 'z'] }],
594 emptyArray: [],
595 '"': 4,
596 };
597
598 std.assertEqual(
599 std.manifestTomlEx(some_toml, ' ') + '\n',
600 |||
601 "\"" = 4
602 array = [
603 "s",
604 1,
605 [ 2, 3 ],
606 { a = [ "0", "z" ], r = 6 }
607 ]
608 bool = true
609 emptyArray = []
610 key = "value"
611 notBool = false
612 number = 7
613
614 [[arraySection]]
615 q = 1
616
617 [[arraySection]]
618 w = 2
619
620 [[emptyArraySection]]
621
622 [emptySection]
623
624 ["escaped\"Section"]
625 z = "q"
626
627 [section]
628 a = 1
629
630 [[section.array]]
631 c = 3
632
633 [[section.array]]
634 d = 4
635
636 [section."e$caped"]
637 q = "t"
638
639 [section.nested]
640 b = 2
641
642 [[section.nestedArray]]
643 k = "v"
644
645 [section.nestedArray.nested]
646 e = 5
647
648 [simple]
649 t = 5
650 |||
651 ) &&
652
653 local some_json = {
654 x: [1, 2, 3, true, false, null, 'string\nstring\n'],
655 arr: [[[]]],
656 y: { a: 1, b: 2, c: [1, 2] },
657 emptyArray: [],
658 emptyObject: {},
659 objectInArray: [{ f: 3 }],
660 '"': null,
661 };
662
663 local bare_yaml_quoted = {
664 '685230': 'canonical',
665 '+685_230': 'decimal',
666 '02472256': 'octal',
667 '-1_0': 'negative integer',
668 '-0.1_0_0': 'negative float',
669 '0x_0A_74_AE': 'hexadecimal',
670 '-0x_0A_74_AE': 'negative hexadecimal',
671 '0b1010_0111_0100_1010_1110': 'binary',
672 '-0b1010_0111_0100_1010_1110': 'binary',
673 '190:20:30': 'sexagesimal',
674 '-190:20:30': 'negative sexagesimal',
675 '6.8523015e+5': 'canonical',
676 '6.8523015e-5': 'canonical',
677 '-6.8523015e+5': 'negative canonical',
678 '685.230_15e+03': 'exponential',
679 '-685.230_15e+03': 'negative exponential',
680 '-685.230_15e-03': 'negative w/ negative exponential',
681 '-685.230_15E-03': 'negative w/ negative exponential',
682 '685_230.15': 'fixed',
683 '-685_230.15': 'negative fixed',
684 '190:20:30.15': 'sexagesimal',
685 '-190:20:30.15': 'negative sexagesimal',
686 '-.inf': 'negative infinity',
687 '.inf': 'positive infinity',
688 '+.inf': 'positive infinity',
689 '.NaN': 'not a number',
690 y: 'boolean true',
691 yes: 'boolean true',
692 Yes: 'boolean true',
693 True: 'boolean true',
694 'true': 'boolean true',
695 on: 'boolean true',
696 On: 'boolean true',
697 NO: 'boolean false',
698 n: 'boolean false',
699 N: 'boolean false',
700 off: 'boolean false',
701 OFF: 'boolean false',
702 'null': 'null word',
703 NULL: 'null word capital',
704 Null: 'null word',
705 '~': 'null key',
706 '': 'empty key',
707 '-': 'invalid bare key',
708 '---': 'triple dash key',
709 '2001-12-15T02:59:43.1Z': 'canonical',
710 '2001-12-14t21:59:43.10-05:00': 'valid iso8601',
711 '2001-12-14 21:59:43.10 -5': 'space separated',
712 '2001-12-15 2:59:43.10': 'no time zone (Z)',
713 '2002-12-14': 'date',
714 };
715 local bare_yaml_unquoted = {
716 '0X_0a_74_ae': 'BARE_KEY',
717 '__-0X_0a_74_ae': 'BARE_KEY',
718 '-0B1010_0111_0100_1010_1110': 'BARE_KEY',
719 '__-0B1010_0111_0100_1010_1110': 'BARE_KEY',
720 x: 'BARE_KEY',
721 b: 'BARE_KEY',
722 just_letters_underscores: 'BARE_KEY',
723 'just-letters-dashes': 'BARE_KEY',
724 'jsonnet.org/k8s-label-like': 'BARE_KEY',
725 '192.168.0.1': 'BARE_KEY',
726 '1-234-567-8901': 'BARE_KEY',
727 };
728 local bare_yaml_test = bare_yaml_quoted + bare_yaml_unquoted;
729
730 std.assertEqual(
731 std.manifestJsonEx(some_json, ' ') + '\n',
732 |||
733 {
734 "\"": null,
735 "arr": [
736 [
737 [
738
739 ]
740 ]
741 ],
742 "emptyArray": [
743
744 ],
745 "emptyObject": {
746
747 },
748 "objectInArray": [
749 {
750 "f": 3
751 }
752 ],
753 "x": [
754 1,
755 2,
756 3,
757 true,
758 false,
759 null,
760 "string\nstring\n"
761 ],
762 "y": {
763 "a": 1,
764 "b": 2,
765 "c": [
766 1,
767 2
768 ]
769 }
770 }
771 |||
772 ) &&
773
774 std.assertEqual(
775 std.manifestJsonEx(some_json, '', ' ', ' : '),
776 '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], '
777 + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], '
778 + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], '
779 + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }'
780 ) &&
781
782 std.assertEqual(
783 std.manifestJsonMinified(some_json),
784 '{"\\"":null,"arr":[[[]]],"emptyArray":[],"emptyObject":{},"objectInArray":[{"f":3}],'
785 + '"x":[1,2,3,true,false,null,"string\\nstring\\n"],"y":{"a":1,"b":2,"c":[1,2]}}'
786 ) &&
787
788 std.assertEqual(
789 std.manifestYamlDoc([{ x: [1, 2, 3] }], quote_keys=false) + '\n',
790 |||
791 - x:
792 - 1
793 - 2
794 - 3
795 |||
796 ) &&
797
798 std.assertEqual(
799 std.manifestYamlDoc([{ x: [1, 2, 3] }]) + '\n',
800 |||
801 - "x":
802 - 1
803 - 2
804 - 3
805 |||
806 ) &&
807
808 std.assertEqual(
809 std.manifestYamlDoc({ x: [1, 2, 3] }) + '\n',
810 |||
811 "x":
812 - 1
813 - 2
814 - 3
815 |||
816 ) &&
817
818 std.assertEqual(
819 std.manifestYamlDoc([[[1, 2], [3, 4]]]) + '\n',
820 |||
821 -
822 -
823 - 1
824 - 2
825 -
826 - 3
827 - 4
828 |||
829 ) &&
830
831 std.assertEqual(
832 std.manifestYamlDoc({ x: [[[1, [1], 1]]] }) + '\n',
833 |||
834 "x":
835 -
836 -
837 - 1
838 -
839 - 1
840 - 1
841 |||
842 ) &&
843
844 std.assertEqual(
845 std.manifestYamlDoc({ x: [[[1, { f: 3, g: [1, 2] }, 1]]] }) + '\n',
846 |||
847 "x":
848 -
849 -
850 - 1
851 - "f": 3
852 "g":
853 - 1
854 - 2
855 - 1
856 |||
857 ) &&
858
859 std.assertEqual(
860 std.manifestYamlDoc({ x: [[[1, { f: 3, g: [1, 2] }, 1]]] }, quote_keys=false) + '\n',
861 |||
862 x:
863 -
864 -
865 - 1
866 - f: 3
867 g:
868 - 1
869 - 2
870 - 1
871 |||
872 ) &&
873
874 std.assertEqual(
875 std.manifestYamlDoc('hello\nworld\n') + '\n',
876 |||
877 |
878 hello
879 world
880 |||
881 ) &&
882
883 std.assertEqual(
884 std.manifestYamlDoc(['hello\nworld\n']) + '\n',
885 |||
886 - |
887 hello
888 world
889 |||
890 ) &&
891
892 std.assertEqual(
893 std.manifestYamlDoc({ f: 'hello\nworld\n' }) + '\n',
894 |||
895 "f": |
896 hello
897 world
898 |||
899 ) &&
900
901 std.assertEqual(
902 std.manifestYamlDoc({ f: 'hello\nworld\n' }, quote_keys=false) + '\n',
903 |||
904 f: |
905 hello
906 world
907 |||
908 ) &&
909
910 std.assertEqual(
911 std.manifestYamlDoc(some_json) + '\n',
912 |||
913 "\"": null
914 "arr":
915 -
916 - []
917 "emptyArray": []
918 "emptyObject": {}
919 "objectInArray":
920 - "f": 3
921 "x":
922 - 1
923 - 2
924 - 3
925 - true
926 - false
927 - null
928 - |
929 string
930 string
931 "y":
932 "a": 1
933 "b": 2
934 "c":
935 - 1
936 - 2
937 |||
938 ) &&
939
940 std.assertEqual(
941 std.manifestYamlDoc(some_json, quote_keys=false) + '\n',
942 |||
943 "\"": null
944 arr:
945 -
946 - []
947 emptyArray: []
948 emptyObject: {}
949 objectInArray:
950 - f: 3
951 x:
952 - 1
953 - 2
954 - 3
955 - true
956 - false
957 - null
958 - |
959 string
960 string
961 "y":
962 a: 1
963 b: 2
964 c:
965 - 1
966 - 2
967 |||
968 ) &&
969
970 std.assertEqual(
971 std.manifestYamlDoc([{ x: [1, 2, 3] }], indent_array_in_object=true) + '\n',
972 |||
973 - "x":
974 - 1
975 - 2
976 - 3
977 |||
978 ) &&
979
980 std.assertEqual(
981 std.manifestYamlDoc([{ x: [1, 2, 3] }], indent_array_in_object=true, quote_keys=false) + '\n',
982 |||
983 - x:
984 - 1
985 - 2
986 - 3
987 |||
988 ) &&
989
990 std.assertEqual(
991 std.manifestYamlDoc({ x: [1, 2, 3] }, indent_array_in_object=true) + '\n',
992 |||
993 "x":
994 - 1
995 - 2
996 - 3
997 |||
998 ) &&
999
1000 std.assertEqual(
1001 std.manifestYamlDoc([[[1, 2], [3, 4]]], indent_array_in_object=true) + '\n',
1002 |||
1003 -
1004 -
1005 - 1
1006 - 2
1007 -
1008 - 3
1009 - 4
1010 |||
1011 ) &&
1012
1013 std.assertEqual(
1014 std.manifestYamlDoc({ x: [[[1, [1], 1]]] }, indent_array_in_object=true) + '\n',
1015 |||
1016 "x":
1017 -
1018 -
1019 - 1
1020 -
1021 - 1
1022 - 1
1023 |||
1024 ) &&
1025
1026 std.assertEqual(
1027 std.manifestYamlDoc({ x: [[[1, { f: 3, g: [1, 2] }, 1]]] }, indent_array_in_object=true) + '\n',
1028 |||
1029 "x":
1030 -
1031 -
1032 - 1
1033 - "f": 3
1034 "g":
1035 - 1
1036 - 2
1037 - 1
1038 |||
1039 ) &&
1040
1041 std.assertEqual(
1042 std.manifestYamlDoc('hello\nworld\n', indent_array_in_object=true) + '\n',
1043 |||
1044 |
1045 hello
1046 world
1047 |||
1048 ) &&
1049
1050 std.assertEqual(
1051 std.manifestYamlDoc(['hello\nworld\n'], indent_array_in_object=true) + '\n',
1052 |||
1053 - |
1054 hello
1055 world
1056 |||
1057 ) &&
1058
1059 std.assertEqual(
1060 std.manifestYamlDoc({ f: 'hello\nworld\n' }, indent_array_in_object=true) + '\n',
1061 |||
1062 "f": |
1063 hello
1064 world
1065 |||
1066 ) &&
1067
1068 std.assertEqual(
1069 std.manifestYamlDoc(some_json, indent_array_in_object=true) + '\n',
1070 |||
1071 "\"": null
1072 "arr":
1073 -
1074 - []
1075 "emptyArray": []
1076 "emptyObject": {}
1077 "objectInArray":
1078 - "f": 3
1079 "x":
1080 - 1
1081 - 2
1082 - 3
1083 - true
1084 - false
1085 - null
1086 - |
1087 string
1088 string
1089 "y":
1090 "a": 1
1091 "b": 2
1092 "c":
1093 - 1
1094 - 2
1095 |||
1096 ) &&
1097
1098 std.assertEqual(
1099 std.manifestYamlDoc(some_json, indent_array_in_object=true, quote_keys=false) + '\n',
1100 |||
1101 "\"": null
1102 arr:
1103 -
1104 - []
1105 emptyArray: []
1106 emptyObject: {}
1107 objectInArray:
1108 - f: 3
1109 x:
1110 - 1
1111 - 2
1112 - 3
1113 - true
1114 - false
1115 - null
1116 - |
1117 string
1118 string
1119 "y":
1120 a: 1
1121 b: 2
1122 c:
1123 - 1
1124 - 2
1125 |||
1126 ) &&
1127
1128 std.assertEqual(
1129 std.manifestYamlDoc(bare_yaml_test, quote_keys=false) + '\n',
1130 |||
1131 "": "empty key"
1132 "+.inf": "positive infinity"
1133 "+685_230": "decimal"
1134 "-": "invalid bare key"
1135 "---": "triple dash key"
1136 "-.inf": "negative infinity"
1137 "-0.1_0_0": "negative float"
1138 -0B1010_0111_0100_1010_1110: "BARE_KEY"
1139 "-0b1010_0111_0100_1010_1110": "binary"
1140 "-0x_0A_74_AE": "negative hexadecimal"
1141 "-190:20:30": "negative sexagesimal"
1142 "-190:20:30.15": "negative sexagesimal"
1143 "-1_0": "negative integer"
1144 "-6.8523015e+5": "negative canonical"
1145 "-685.230_15E-03": "negative w/ negative exponential"
1146 "-685.230_15e+03": "negative exponential"
1147 "-685.230_15e-03": "negative w/ negative exponential"
1148 "-685_230.15": "negative fixed"
1149 ".NaN": "not a number"
1150 ".inf": "positive infinity"
1151 "02472256": "octal"
1152 0X_0a_74_ae: "BARE_KEY"
1153 "0b1010_0111_0100_1010_1110": "binary"
1154 "0x_0A_74_AE": "hexadecimal"
1155 1-234-567-8901: "BARE_KEY"
1156 "190:20:30": "sexagesimal"
1157 "190:20:30.15": "sexagesimal"
1158 192.168.0.1: "BARE_KEY"
1159 "2001-12-14 21:59:43.10 -5": "space separated"
1160 "2001-12-14t21:59:43.10-05:00": "valid iso8601"
1161 "2001-12-15 2:59:43.10": "no time zone (Z)"
1162 "2001-12-15T02:59:43.1Z": "canonical"
1163 "2002-12-14": "date"
1164 "6.8523015e+5": "canonical"
1165 "6.8523015e-5": "canonical"
1166 "685.230_15e+03": "exponential"
1167 "685230": "canonical"
1168 "685_230.15": "fixed"
1169 "N": "boolean false"
1170 "NO": "boolean false"
1171 "NULL": "null word capital"
1172 "Null": "null word"
1173 "OFF": "boolean false"
1174 "On": "boolean true"
1175 "True": "boolean true"
1176 "Yes": "boolean true"
1177 __-0B1010_0111_0100_1010_1110: "BARE_KEY"
1178 __-0X_0a_74_ae: "BARE_KEY"
1179 b: "BARE_KEY"
1180 jsonnet.org/k8s-label-like: "BARE_KEY"
1181 just-letters-dashes: "BARE_KEY"
1182 just_letters_underscores: "BARE_KEY"
1183 "n": "boolean false"
1184 "null": "null word"
1185 "off": "boolean false"
1186 "on": "boolean true"
1187 "true": "boolean true"
1188 x: "BARE_KEY"
1189 "y": "boolean true"
1190 "yes": "boolean true"
1191 "~": "null key"
1192 |||
1193 ) &&
1194
1195 std.assertEqual(
1196 std.manifestYamlStream([bare_yaml_quoted, bare_yaml_unquoted], quote_keys=false),
1197 |||
1198 ---
1199 "": "empty key"
1200 "+.inf": "positive infinity"
1201 "+685_230": "decimal"
1202 "-": "invalid bare key"
1203 "---": "triple dash key"
1204 "-.inf": "negative infinity"
1205 "-0.1_0_0": "negative float"
1206 "-0b1010_0111_0100_1010_1110": "binary"
1207 "-0x_0A_74_AE": "negative hexadecimal"
1208 "-190:20:30": "negative sexagesimal"
1209 "-190:20:30.15": "negative sexagesimal"
1210 "-1_0": "negative integer"
1211 "-6.8523015e+5": "negative canonical"
1212 "-685.230_15E-03": "negative w/ negative exponential"
1213 "-685.230_15e+03": "negative exponential"
1214 "-685.230_15e-03": "negative w/ negative exponential"
1215 "-685_230.15": "negative fixed"
1216 ".NaN": "not a number"
1217 ".inf": "positive infinity"
1218 "02472256": "octal"
1219 "0b1010_0111_0100_1010_1110": "binary"
1220 "0x_0A_74_AE": "hexadecimal"
1221 "190:20:30": "sexagesimal"
1222 "190:20:30.15": "sexagesimal"
1223 "2001-12-14 21:59:43.10 -5": "space separated"
1224 "2001-12-14t21:59:43.10-05:00": "valid iso8601"
1225 "2001-12-15 2:59:43.10": "no time zone (Z)"
1226 "2001-12-15T02:59:43.1Z": "canonical"
1227 "2002-12-14": "date"
1228 "6.8523015e+5": "canonical"
1229 "6.8523015e-5": "canonical"
1230 "685.230_15e+03": "exponential"
1231 "685230": "canonical"
1232 "685_230.15": "fixed"
1233 "N": "boolean false"
1234 "NO": "boolean false"
1235 "NULL": "null word capital"
1236 "Null": "null word"
1237 "OFF": "boolean false"
1238 "On": "boolean true"
1239 "True": "boolean true"
1240 "Yes": "boolean true"
1241 "n": "boolean false"
1242 "null": "null word"
1243 "off": "boolean false"
1244 "on": "boolean true"
1245 "true": "boolean true"
1246 "y": "boolean true"
1247 "yes": "boolean true"
1248 "~": "null key"
1249 ---
1250 -0B1010_0111_0100_1010_1110: "BARE_KEY"
1251 0X_0a_74_ae: "BARE_KEY"
1252 1-234-567-8901: "BARE_KEY"
1253 192.168.0.1: "BARE_KEY"
1254 __-0B1010_0111_0100_1010_1110: "BARE_KEY"
1255 __-0X_0a_74_ae: "BARE_KEY"
1256 b: "BARE_KEY"
1257 jsonnet.org/k8s-label-like: "BARE_KEY"
1258 just-letters-dashes: "BARE_KEY"
1259 just_letters_underscores: "BARE_KEY"
1260 x: "BARE_KEY"
1261 ...
1262 |||
1263 ) &&
1264
1265 std.assertEqual(
1266 std.manifestYamlStream([some_json, some_json, {}, [], 3, '"']),
1267 |||
1268 ---
1269 "\"": null
1270 "arr":
1271 -
1272 - []
1273 "emptyArray": []
1274 "emptyObject": {}
1275 "objectInArray":
1276 - "f": 3
1277 "x":
1278 - 1
1279 - 2
1280 - 3
1281 - true
1282 - false
1283 - null
1284 - |
1285 string
1286 string
1287 "y":
1288 "a": 1
1289 "b": 2
1290 "c":
1291 - 1
1292 - 2
1293 ---
1294 "\"": null
1295 "arr":
1296 -
1297 - []
1298 "emptyArray": []
1299 "emptyObject": {}
1300 "objectInArray":
1301 - "f": 3
1302 "x":
1303 - 1
1304 - 2
1305 - 3
1306 - true
1307 - false
1308 - null
1309 - |
1310 string
1311 string
1312 "y":
1313 "a": 1
1314 "b": 2
1315 "c":
1316 - 1
1317 - 2
1318 ---
1319 {}
1320 ---
1321 []
1322 ---
1323 3
1324 ---
1325 "\""
1326 ...
1327 |||
1328 ) &&
1329
1330 std.assertEqual(
1331 std.manifestYamlStream([some_json, some_json, {}, [], 3, '"'], quote_keys=false),
1332 |||
1333 ---
1334 "\"": null
1335 arr:
1336 -
1337 - []
1338 emptyArray: []
1339 emptyObject: {}
1340 objectInArray:
1341 - f: 3
1342 x:
1343 - 1
1344 - 2
1345 - 3
1346 - true
1347 - false
1348 - null
1349 - |
1350 string
1351 string
1352 "y":
1353 a: 1
1354 b: 2
1355 c:
1356 - 1
1357 - 2
1358 ---
1359 "\"": null
1360 arr:
1361 -
1362 - []
1363 emptyArray: []
1364 emptyObject: {}
1365 objectInArray:
1366 - f: 3
1367 x:
1368 - 1
1369 - 2
1370 - 3
1371 - true
1372 - false
1373 - null
1374 - |
1375 string
1376 string
1377 "y":
1378 a: 1
1379 b: 2
1380 c:
1381 - 1
1382 - 2
1383 ---
1384 {}
1385 ---
1386 []
1387 ---
1388 3
1389 ---
1390 "\""
1391 ...
1392 |||
1393 ) &&
1394
1395 std.assertEqual(
1396 std.manifestYamlStream([some_json, some_json, {}, [], 3, '"'], indent_array_in_object=true),
1397 |||
1398 ---
1399 "\"": null
1400 "arr":
1401 -
1402 - []
1403 "emptyArray": []
1404 "emptyObject": {}
1405 "objectInArray":
1406 - "f": 3
1407 "x":
1408 - 1
1409 - 2
1410 - 3
1411 - true
1412 - false
1413 - null
1414 - |
1415 string
1416 string
1417 "y":
1418 "a": 1
1419 "b": 2
1420 "c":
1421 - 1
1422 - 2
1423 ---
1424 "\"": null
1425 "arr":
1426 -
1427 - []
1428 "emptyArray": []
1429 "emptyObject": {}
1430 "objectInArray":
1431 - "f": 3
1432 "x":
1433 - 1
1434 - 2
1435 - 3
1436 - true
1437 - false
1438 - null
1439 - |
1440 string
1441 string
1442 "y":
1443 "a": 1
1444 "b": 2
1445 "c":
1446 - 1
1447 - 2
1448 ---
1449 {}
1450 ---
1451 []
1452 ---
1453 3
1454 ---
1455 "\""
1456 ...
1457 |||
1458 ) &&
1459
1460 std.assertEqual(
1461 std.manifestYamlStream([{}, [], 3, '"'], c_document_end=false),
1462 |||
1463 ---
1464 {}
1465 ---
1466 []
1467 ---
1468 3
1469 ---
1470 "\""
1471 |||
1472 ) &&
1473
1474 std.assertEqual(std.parseInt('01234567890'), 1234567890) &&
1475 std.assertEqual(std.parseInt('-01234567890'), -1234567890) &&
1476 std.assertEqual(std.parseOctal('755'), 493) &&
1477 std.assertEqual(std.parseOctal('0755'), 493) &&
1478 std.assertEqual(std.parseHex('ff'), 255) &&
1479 std.assertEqual(std.parseHex('FF'), 255) &&
1480 std.assertEqual(std.parseHex('0ff'), 255) &&
1481 std.assertEqual(std.parseHex('0FF'), 255) &&
1482 std.assertEqual(std.parseHex('a'), 10) &&
1483 std.assertEqual(std.parseHex('A'), 10) &&
1484 std.assertEqual(std.parseHex('4a'), 74) &&
1485
1486 // verified by running md5 -s value
1487 std.assertEqual(std.md5(''), 'd41d8cd98f00b204e9800998ecf8427e') &&
1488 std.assertEqual(std.md5('grape'), 'b781cbb29054db12f88f08c6e161c199') &&
1489 std.assertEqual(std.md5("{}[]01234567890\"'+=-_/<>?,.!@#$%^&*|\\:;`~"), 'a680db28332f0c9647376e5b2aeb4b3d') &&
1490 std.assertEqual(std.md5('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. Ut eu diam at pede suscipit sodales. Aenean lectus elit, fermentum non, convallis id, sagittis at, neque. Nullam mauris orci, aliquet et, iaculis et, viverra vitae, ligula. Nulla ut felis in purus aliquam imperdiet. Maecenas aliquet mollis lectus. Vivamus consectetuer risus et tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. Ut eu diam at pede suscipit sodales. Aenean lectus elit, fermentum non, convallis id, sagittis at, neque. Nullam mauris orci, aliquet et, iaculis et, viverra vitae, ligula. Nulla ut felis in purus aliquam imperdiet. Maecenas aliquet mollis lectus. Vivamus consectetuer risus et tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum si.'), '3496bb633e830e7679ce53700d42de1e') &&
1491 std.assertEqual(std.parseInt('-01234567890'), -1234567890) &&
1492
1493 std.assertEqual(std.prune({}), {}) &&
1494 std.assertEqual(std.prune([]), []) &&
1495 std.assertEqual(std.prune(null), null) &&
1496 std.assertEqual(std.prune({ a: [], b: {}, c: null }), {}) &&
1497 std.assertEqual(std.prune([[], {}, null]), []) &&
1498 std.assertEqual(std.prune({ a: [[], {}, null], b: { a: [], b: {}, c: null } }), {}) &&
1499 std.assertEqual(std.prune([[[], {}, null], { a: [], b: {}, c: null }]), []) &&
1500 std.assertEqual(std.prune({ a: [{ b: true }] }), { a: [{ b: true }] }) &&
1501
1502 std.assertEqual(std.parseJson('"foo"'), 'foo') &&
1503 std.assertEqual(std.parseJson('{}'), {}) &&
1504 std.assertEqual(std.parseJson('[]'), []) &&
1505 std.assertEqual(std.parseJson('null'), null) &&
1506 std.assertEqual(std.parseJson('12'), 12) &&
1507 std.assertEqual(std.parseJson('12.123'), 12.123) &&
1508 std.assertEqual(std.parseJson('{"a": {"b": ["c", 42]}}'), { a: { b: ['c', 42] } }) &&
1509
1510 // Empty input evaluates to `null`.
1511 // Regression test for https://github.com/google/jsonnet/issues/1270
1512 std.assertEqual(std.parseYaml(' \n'), null) &&
1513 // Stream input, regression test for https://github.com/google/jsonnet/issues/1148
1514 std.assertEqual(std.parseYaml('1\n---'), [1, null]) &&
1515 std.assertEqual(std.parseYaml('{}'), {}) &&
1516 std.assertEqual(std.parseYaml('[]'), []) &&
1517 std.assertEqual(
1518 std.parseYaml(
1519 |||
1520 foo:
1521 bar:
1522 - true
1523 - 42
1524 - 1.0
1525 |||
1526 ), { foo: { bar: [true, 42, 1] } }
1527 ) &&
1528 std.assertEqual(
1529 std.parseYaml(
1530 |||
1531 ---
1532 foo:
1533 bar:
1534 - true
1535 - 42
1536 - 1.0
1537 ---
1538 wibble:
1539 wobble:
1540 - true
1541 - 42
1542 - 1.0
1543 |||
1544 ), [{ foo: { bar: [true, 42, 1] } }, { wibble: { wobble: [true, 42, 1] } }]
1545 ) &&
1546 std.assertEqual(
1547 std.parseYaml(
1548 |||
1549 - 1
1550 - 2
1551 - 3
1552 |||
1553 ), [1, 2, 3]
1554 ) &&
1555 std.assertEqual(
1556 std.parseYaml(
1557 |||
1558 f1: |
1559 a
1560 b
1561 f2: "a\nb\n"
1562 |||
1563 ), { f1: 'a\nb\n', f2: 'a\nb\n' }
1564 ) &&
1565 // Issue https://github.com/google/jsonnet/issues/1014
1566 std.assertEqual(std.parseYaml('version: 1.2.3'), { version: '1.2.3' }) &&
1567 // Issue https://github.com/google/jsonnet/issues/1050
1568 std.assertEqual(std.type(std.parseYaml('id: "12345"').id), 'string') &&
1569
1570 std.assertEqual(std.asciiUpper('!@#$%&*()asdfghFGHJKL09876 '), '!@#$%&*()ASDFGHFGHJKL09876 ') &&
1571 std.assertEqual(std.asciiLower('!@#$%&*()asdfghFGHJKL09876 '), '!@#$%&*()asdfghfghjkl09876 ') &&
1572
1573 std.assertEqual(std.deepJoin(['a', ['b', 'c', [[], 'd', ['e'], 'f', 'g'], [], []], 'h']),
1574 'abcdefgh') &&
1575
1576 std.assertEqual(std.findSubstr('', 'a'), []) &&
1577 std.assertEqual(std.findSubstr('aa', ''), []) &&
1578 std.assertEqual(std.findSubstr('aa', 'bb'), []) &&
1579 std.assertEqual(std.findSubstr('aa', 'a'), []) &&
1580 std.assertEqual(std.findSubstr('aa', 'aa'), [0]) &&
1581 std.assertEqual(std.findSubstr('aa', 'bbaabaaa'), [2, 5, 6]) &&
1582
1583 std.assertEqual(std.find(null, [null]), [0]) &&
1584 std.assertEqual(std.find([], [[]]), [0]) &&
1585 std.assertEqual(std.find({}, [{}]), [0]) &&
1586 std.assertEqual(std.find('a', []), []) &&
1587 std.assertEqual(std.find('a', ['b']), []) &&
1588 std.assertEqual(std.find('a', ['a']), [0]) &&
1589 std.assertEqual(std.find('a', ['a', ['a'], 'b', 'a']), [0, 3]) &&
1590 std.assertEqual(std.find(['a'], [['a']]), [0]) &&
1591
1592 std.assertEqual(std.encodeUTF8(''), []) &&
1593 std.assertEqual(std.encodeUTF8('A'), [65]) &&
1594 std.assertEqual(std.encodeUTF8('AAA'), [65, 65, 65]) &&
1595 std.assertEqual(std.encodeUTF8('§'), [194, 167]) &&
1596 std.assertEqual(std.encodeUTF8('Zażółć gęślą jaźń'), [90, 97, 197, 188, 195, 179, 197, 130, 196, 135, 32, 103, 196, 153, 197, 155, 108, 196, 133, 32, 106, 97, 197, 186, 197, 132]) &&
1597 std.assertEqual(std.encodeUTF8('😃'), [240, 159, 152, 131]) &&
1598
1599 std.assertEqual(std.decodeUTF8([]), '') &&
1600 std.assertEqual(std.decodeUTF8([65]), 'A') &&
1601 std.assertEqual(std.decodeUTF8([65, 65, 65]), 'AAA') &&
1602 std.assertEqual(std.decodeUTF8([(function(x) 65)(42)]), 'A') &&
1603 std.assertEqual(std.decodeUTF8([65 + 1 - 1]), 'A') &&
1604 std.assertEqual(std.decodeUTF8([90, 97, 197, 188, 195, 179, 197, 130, 196, 135, 32, 103, 196, 153, 197, 155, 108, 196, 133, 32, 106, 97, 197, 186, 197, 132]), 'Zażółć gęślą jaźń') &&
1605 std.assertEqual(std.decodeUTF8([240, 159, 152, 131]), '😃') &&
1606
1607
1608 std.assertEqual(std.any([true, false]), true) &&
1609 std.assertEqual(std.any([false, false]), false) &&
1610 std.assertEqual(std.any([]), false) &&
1611
1612 std.assertEqual(std.all([true, false]), false) &&
1613 std.assertEqual(std.all([true, true]), true) &&
1614 std.assertEqual(std.all([]), true) &&
1615
1616 std.assertEqual(std.sum([1, 2, 3]), 6) &&
1617
1618 std.assertEqual(std.avg([1, 2, 3]), 2) &&
1619 std.assertEqual(std.avg([0, 0, 0]), 0) &&
1620 std.assertEqual(std.avg([1, 1, 2.5]), 1.5) &&
1621
1622 std.assertEqual(std.minArray([3, 1, 2]), 1) &&
1623 std.assertEqual(std.minArray(['3', '1', '2']), '1') &&
1624 std.assertEqual(std.minArray(['a2', 'b1'], keyF=function(x) x[0]), 'a2') &&
1625 std.assertEqual(std.minArray(['a2', 'b1'], keyF=function(x) x[1]), 'b1') &&
1626 std.assertEqual(std.minArray([], onEmpty='default'), 'default') &&
1627
1628 std.assertEqual(std.maxArray([1, 2, 3]), 3) &&
1629 std.assertEqual(std.maxArray(['1', '2', '3']), '3') &&
1630 std.assertEqual(std.maxArray(['a', 'x', 'z']), 'z') &&
1631 std.assertEqual(std.maxArray(['a2', 'b1'], keyF=function(x) x[0]), 'b1') &&
1632 std.assertEqual(std.maxArray(['a2', 'b1'], keyF=function(x) x[1]), 'a2') &&
1633 std.assertEqual(std.maxArray([], onEmpty='default'), 'default') &&
1634
1635
1636 std.assertEqual(std.xor(true, false), true) &&
1637 std.assertEqual(std.xor(true, true), false) &&
1638
1639 std.assertEqual(std.xnor(true, false), false) &&
1640 std.assertEqual(std.xnor(true, true), true) &&
1641
1642 std.assertEqual(std.round(1.2), 1) &&
1643 std.assertEqual(std.round(1.5), 2) &&
1644
1645 std.assertEqual(std.isEmpty(''), true) &&
1646 std.assertEqual(std.isEmpty('non-empty string'), false) &&
1647
1648 std.assertEqual(std.contains([1, 2, 3], 2), true) &&
1649 std.assertEqual(std.contains([1, 2, 3], 'foo'), false) &&
1650
1651 std.assertEqual(std.equalsIgnoreCase('foo', 'FOO'), true) &&
1652 std.assertEqual(std.equalsIgnoreCase('foo', 'bar'), false) &&
1653
1654 std.assertEqual(std.isEven(10), true) &&
1655 std.assertEqual(std.isEven(5), false) &&
1656 std.assertEqual(std.isOdd(5), true) &&
1657 std.assertEqual(std.isOdd(10), false) &&
1658 std.assertEqual(std.isInteger(1), true) &&
1659 std.assertEqual(std.isInteger(1.1), false) &&
1660 std.assertEqual(std.isDecimal(1.1), true) &&
1661 std.assertEqual(std.isDecimal(1), false) &&
1662
1663 std.assertEqual(std.remove([1, 2, 3], 2), [1, 3]) &&
1664 std.assertEqual(std.removeAt([1, 2, 3], 1), [1, 3]) &&
1665
1666 std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz: 3 }, 'foo'), { bar: 2, baz: 3 }) &&
1667 // objectRemoveKey should retain hidden fields as hidden fields.
1668 std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz:: 3 }, 'foo').baz, 3) &&
1669 // objectRemoveKey doesn't break inheritance within the provided object.
1670 std.assertEqual(std.objectRemoveKey({ a: 1 } + { b: super.a }, 'a'), { b: 1 }) &&
1671 // objectRemoveKey works with inheritance outside of the object.
1672 std.assertEqual({ a: 1 } + std.objectRemoveKey({ b: super.a }, 'a'), { a: 1, b: 1 }) &&
1673 // Referential transparency still works.
1674 std.assertEqual(local o1 = { b: super.a }; std.objectRemoveKey({ a: 1 } + o1, 'a'), { b: 1 }) &&
1675 std.assertEqual(local o1 = { b: super.a }; { a: 1 } + std.objectRemoveKey(o1, 'a'), { a: 1, b: 1 }) &&
1676 // Hidden fields still work.
1677 std.assertEqual(std.objectRemoveKey({ a: 1 } + { b:: super.a }, 'a'), {}) &&
1678 std.assertEqual(std.objectRemoveKey({ a: 1 } + { b:: super.a }, 'a').b, 1) &&
1679 std.assertEqual(({ a: 1 } + std.objectRemoveKey({ b:: super.a }, 'a')), { a: 1 }) &&
1680 std.assertEqual(({ a: 1 } + std.objectRemoveKey({ b:: super.a }, 'a')).b, 1) &&
1681
1682 std.assertEqual(std.trim('already trimmed string'), 'already trimmed string') &&
1683 std.assertEqual(std.trim(' string with spaces on both ends '), 'string with spaces on both ends') &&
1684 std.assertEqual(std.trim('string with newline character at end\n'), 'string with newline character at end') &&
1685 std.assertEqual(std.trim('string with tabs at end\t\t'), 'string with tabs at end') &&
1686 // The last character here was previously expressed as \u00A0, but this is rewritten by jsonnetfmt.
1687 // To avoid ambiguity but allow the code to pass unchanged through jsonnetfmt, this now uses a std.char() call.
1688 std.assertEqual(std.trim('string with other special whitespaces at end\f\r\u0085' + std.char(160)), 'string with other special whitespaces at end') &&
1689
1690 true