Skip to content

Commit 4c5d452

Browse files
committed
test: add test for wrapped with empty name and without name argument
1 parent 1f32358 commit 4c5d452

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

test/composite_parser.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,136 @@ function compositeParserTests(
12731273
answer: 42,
12741274
});
12751275
});
1276+
it("should embed parsed object in current object", () => {
1277+
const parserEmptyName = Parser.start()
1278+
.uint8("messageId")
1279+
.uint8("reportCount")
1280+
.array("reports", {
1281+
length: "reportCount",
1282+
type: Parser.start()
1283+
.uint8("reportId")
1284+
.uint8("reportLength")
1285+
.wrapped("", {
1286+
length: "reportLength",
1287+
wrapper: (buffer) => buffer,
1288+
type: Parser.start()
1289+
.nest("basicReport", {
1290+
type: Parser.start()
1291+
.uint8("dataPoint1")
1292+
.uint8("dataPoint2")
1293+
.uint8("dataPoint3")
1294+
.uint8("dataPoint4"),
1295+
})
1296+
.array("extendedReport", {
1297+
readUntil: "eof",
1298+
type: Parser.start()
1299+
.uint8("dataType")
1300+
.uint8("dataLength")
1301+
.buffer("data", { length: "dataLength" }),
1302+
}),
1303+
}),
1304+
});
1305+
1306+
const parserWithoutName = Parser.start()
1307+
.uint8("messageId")
1308+
.uint8("reportCount")
1309+
.array("reports", {
1310+
length: "reportCount",
1311+
type: Parser.start()
1312+
.uint8("reportId")
1313+
.uint8("reportLength")
1314+
.wrapped({
1315+
length: "reportLength",
1316+
wrapper: (buffer) => buffer,
1317+
type: Parser.start()
1318+
.nest("basicReport", {
1319+
type: Parser.start()
1320+
.uint8("dataPoint1")
1321+
.uint8("dataPoint2")
1322+
.uint8("dataPoint3")
1323+
.uint8("dataPoint4"),
1324+
})
1325+
.array("extendedReport", {
1326+
readUntil: "eof",
1327+
type: Parser.start()
1328+
.uint8("dataType")
1329+
.uint8("dataLength")
1330+
.buffer("data", { length: "dataLength" }),
1331+
}),
1332+
}),
1333+
});
1334+
1335+
const buffer = Buffer.from(
1336+
"1002f11012345678a003303132a101dfa20255aaf21201020304a003343536a202aa55a101eb",
1337+
"hex"
1338+
);
1339+
1340+
deepStrictEqual(parserEmptyName.parse(buffer), {
1341+
messageId: 16,
1342+
reportCount: 2,
1343+
reports: [
1344+
{
1345+
reportId: 241,
1346+
reportLength: 16,
1347+
basicReport: {
1348+
dataPoint1: 18,
1349+
dataPoint2: 52,
1350+
dataPoint3: 86,
1351+
dataPoint4: 120,
1352+
},
1353+
extendedReport: [
1354+
{
1355+
dataType: 160,
1356+
dataLength: 3,
1357+
data: Buffer.from([48, 49, 50]),
1358+
},
1359+
{
1360+
dataType: 161,
1361+
dataLength: 1,
1362+
data: Buffer.from([223]),
1363+
},
1364+
{
1365+
dataType: 162,
1366+
dataLength: 2,
1367+
data: Buffer.from([85, 170]),
1368+
},
1369+
],
1370+
},
1371+
{
1372+
reportId: 242,
1373+
reportLength: 18,
1374+
basicReport: {
1375+
dataPoint1: 1,
1376+
dataPoint2: 2,
1377+
dataPoint3: 3,
1378+
dataPoint4: 4,
1379+
},
1380+
extendedReport: [
1381+
{
1382+
dataType: 160,
1383+
dataLength: 3,
1384+
data: Buffer.from([52, 53, 54]),
1385+
},
1386+
{
1387+
dataType: 162,
1388+
dataLength: 2,
1389+
data: Buffer.from([170, 85]),
1390+
},
1391+
{
1392+
dataType: 161,
1393+
dataLength: 1,
1394+
data: Buffer.from([235]),
1395+
},
1396+
],
1397+
},
1398+
],
1399+
});
1400+
1401+
deepStrictEqual(
1402+
parserEmptyName.parse(buffer),
1403+
parserWithoutName.parse(buffer)
1404+
);
1405+
});
12761406
});
12771407
});
12781408
}

0 commit comments

Comments
 (0)