Skip to content

Commit 0f1897d

Browse files
Merge pull request #624 from GuillaumeGomez/more-bools
Allow to use `boolean` values in more commands
2 parents 7289128 + 769aed9 commit 0f1897d

File tree

16 files changed

+180
-17
lines changed

16 files changed

+180
-17
lines changed

src/commands/assert.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,13 @@ function parseAssertObjPropertyInner(parser, assertFalse, objName) {
171171
'object-path': [],
172172
},
173173
valueTypes: {
174-
'string': {},
175-
'number': {
174+
string: {},
175+
number: {
176176
allowNegative: true,
177177
allowFloat: true,
178178
},
179-
'ident': {
179+
boolean: {},
180+
ident: {
180181
allowed: ['null'],
181182
},
182183
},

src/commands/general.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ function parseObjPropertyInner(parser, objName) {
125125
},
126126
valueTypes: {
127127
// In case it's an ident, we only want to allow `null`.
128+
ident: {
129+
allowed: ['null'],
130+
},
128131
string: {},
132+
boolean: {},
129133
number: {
130134
allowFloat: true,
131135
allowNegative: true,
@@ -142,7 +146,13 @@ function parseObjPropertyInner(parser, objName) {
142146

143147
for (const [key, value] of json) {
144148
const k_s = value.key.kind === 'object-path' ? key : `["${key}"]`;
145-
content.push(`[${k_s}, "${value.value}"]`);
149+
if (value.kind === 'string') {
150+
content.push(`[${k_s}, "${value.value}"]`);
151+
} else if (value.kind === 'ident') {
152+
content.push(`[${k_s}, undefined]`);
153+
} else {
154+
content.push(`[${k_s}, ${value.value}]`);
155+
}
146156
}
147157

148158
if (content.length === 0) {
@@ -169,7 +179,11 @@ await page.evaluate(() => {
169179
}
170180
object = object[subPath];
171181
}
172-
object[path[path.length - 1]] = value;
182+
if (value === undefined) {
183+
delete object[path[path.length - 1]];
184+
} else {
185+
object[path[path.length - 1]] = value;
186+
}
173187
}
174188
const ${varDict} = [
175189
${indentString(content.join(',\n'), 2)}

tests/api-output/parseSetDocumentProperty/basic-1.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ instructions = [
88
}
99
object = object[subPath];
1010
}
11-
object[path[path.length - 1]] = value;
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
1216
}
1317
const parsePropDict = [
14-
[[\"a\"], \"2\"]
18+
[[\"a\"], 2]
1519
];
1620
for (const [parsePropKey, parsePropValue] of parsePropDict) {
1721
setObjValue(document, parsePropKey, parsePropValue);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
instructions = [
2+
"""await page.evaluate(() => {
3+
function setObjValue(object, path, value) {
4+
for (let i = 0; i < path.length - 1; ++i) {
5+
const subPath = path[i];
6+
if (object[subPath] === undefined || object[subPath] === null) {
7+
object[subPath] = {};
8+
}
9+
object = object[subPath];
10+
}
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
16+
}
17+
const parsePropDict = [
18+
[[\"a\"], true]
19+
];
20+
for (const [parsePropKey, parsePropValue] of parsePropDict) {
21+
setObjValue(document, parsePropKey, parsePropValue);
22+
}
23+
});""",
24+
]
25+
wait = false
26+
checkResult = true
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
instructions = [
2+
"""await page.evaluate(() => {
3+
function setObjValue(object, path, value) {
4+
for (let i = 0; i < path.length - 1; ++i) {
5+
const subPath = path[i];
6+
if (object[subPath] === undefined || object[subPath] === null) {
7+
object[subPath] = {};
8+
}
9+
object = object[subPath];
10+
}
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
16+
}
17+
const parsePropDict = [
18+
[[\"a\"], \"b\"]
19+
];
20+
for (const [parsePropKey, parsePropValue] of parsePropDict) {
21+
setObjValue(document, parsePropKey, parsePropValue);
22+
}
23+
});""",
24+
]
25+
wait = false
26+
checkResult = true

tests/api-output/parseSetDocumentProperty/escape-1.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ instructions = [
88
}
99
object = object[subPath];
1010
}
11-
object[path[path.length - 1]] = value;
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
1216
}
1317
const parsePropDict = [
1418
[[\"a\"], \"2\"],

tests/api-output/parseSetDocumentProperty/object-path-1.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ instructions = [
88
}
99
object = object[subPath];
1010
}
11-
object[path[path.length - 1]] = value;
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
1216
}
1317
const parsePropDict = [
14-
[[\"a\",\"b\"], \"2\"]
18+
[[\"a\",\"b\"], 2]
1519
];
1620
for (const [parsePropKey, parsePropValue] of parsePropDict) {
1721
setObjValue(document, parsePropKey, parsePropValue);

tests/api-output/parseSetDocumentProperty/object-path-2.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ instructions = [
88
}
99
object = object[subPath];
1010
}
11-
object[path[path.length - 1]] = value;
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
1216
}
1317
const parsePropDict = [
1418
[[\"a\"], \"2\"],

tests/api-output/parseSetWindowProperty/basic-1.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ instructions = [
88
}
99
object = object[subPath];
1010
}
11-
object[path[path.length - 1]] = value;
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
1216
}
1317
const parsePropDict = [
14-
[[\"a\"], \"2\"]
18+
[[\"a\"], 2]
1519
];
1620
for (const [parsePropKey, parsePropValue] of parsePropDict) {
1721
setObjValue(window, parsePropKey, parsePropValue);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
instructions = [
2+
"""await page.evaluate(() => {
3+
function setObjValue(object, path, value) {
4+
for (let i = 0; i < path.length - 1; ++i) {
5+
const subPath = path[i];
6+
if (object[subPath] === undefined || object[subPath] === null) {
7+
object[subPath] = {};
8+
}
9+
object = object[subPath];
10+
}
11+
if (value === undefined) {
12+
delete object[path[path.length - 1]];
13+
} else {
14+
object[path[path.length - 1]] = value;
15+
}
16+
}
17+
const parsePropDict = [
18+
[[\"a\"], true]
19+
];
20+
for (const [parsePropKey, parsePropValue] of parsePropDict) {
21+
setObjValue(window, parsePropKey, parsePropValue);
22+
}
23+
});""",
24+
]
25+
wait = false
26+
checkResult = true

0 commit comments

Comments
 (0)