-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcsh.test.js
More file actions
91 lines (79 loc) · 2.31 KB
/
csh.test.js
File metadata and controls
91 lines (79 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @overview Contains (additional) unit tests for the escaping functionality for
* the C shell (csh).
* @license MIT
*/
import { TextDecoder } from "node:util";
import { testProp } from "@fast-check/ava";
import * as fc from "fast-check";
import * as old from "../../../node_modules/shescape-previous/src/internal/unix/csh.js";
import * as csh from "../../../src/internal/unix/csh.js";
const numRuns = 5_000_000;
const textDecoder = new TextDecoder("utf-8", { fatal: true });
testProp(
"escape functionality is unchanged",
[fc.string()],
(t, arg) => {
const updFn = csh.getEscapeFunction();
const oldFn = old.getEscapeFunction();
const got = updFn(arg).replace(/\\!$/gu, "!");
const want = oldFn(arg);
t.is(got, want);
},
{ numRuns },
);
testProp(
"quote functionality is unchanged",
[fc.string()],
(t, arg) => {
const updFn = csh.getQuoteFunction();
const oldFn = old.getQuoteFunction();
const got = updFn[0](updFn[1](arg));
const want = oldFn[0](oldFn[1](arg));
t.is(got, want);
},
{ numRuns },
);
testProp(
"flag protection functionality is unchanged",
[fc.string()],
(t, arg) => {
const updFn = csh.getFlagProtectionFunction();
const oldFn = old.getFlagProtectionFunction();
const got = updFn(arg);
const want = oldFn(arg);
t.is(got, want);
},
{ numRuns },
);
testProp(
"characters with 0xA0 when utf-8 encoded",
[
fc.string().chain((str) => fc.tuple(fc.constant(str), fc.nat(str.length))),
fc
.uint8Array({ minLength: 1, maxLength: 2 })
.chain((str) => fc.tuple(fc.constant(str), fc.nat(str.length)))
.map(([uint8Array, insertIndex]) => {
try {
const utf8EncodedCharacter = new Uint8Array([
...uint8Array.slice(0, insertIndex),
0xa0,
...uint8Array.slice(insertIndex),
]);
return textDecoder.decode(utf8EncodedCharacter);
} catch {
return null;
}
})
.filter((str) => str?.length === 1),
],
(t, [baseString, insertIndex], testCharacter) => {
const testStr =
baseString.slice(0, insertIndex) +
testCharacter +
baseString.slice(insertIndex);
const escapeFn = csh.getEscapeFunction();
const result = escapeFn(testStr);
t.assert(result.includes(`'${testCharacter}'`));
},
);