-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
152 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import fc from "fast-check"; | ||
import { beforeAll, describe, expect, it } from "vitest"; | ||
|
||
import { escapeField } from "@web-csv-toolbox/common"; | ||
|
||
import { FC } from "#/tests/utils/helper"; | ||
|
||
import { loadWASM, parseStringToArraySync } from "./wasm"; | ||
|
||
describe("parseStringToArraySync", async () => { | ||
beforeAll(async () => { | ||
await loadWASM(); | ||
}); | ||
|
||
it("should parse CSV string to record of arrays", async () => { | ||
await fc.assert( | ||
fc.asyncProperty( | ||
fc.gen().map((g) => { | ||
const header = g(FC.header, { | ||
fieldConstraints: { | ||
unit: "grapheme", | ||
}, | ||
}); | ||
const EOL = g(FC.eol); | ||
const csvData = g(FC.csvData, { | ||
fieldConstraints: { | ||
unit: "grapheme", | ||
}, | ||
columnsConstraints: { | ||
minLength: header.length, | ||
maxLength: header.length, | ||
}, | ||
}); | ||
const EOF = g(fc.boolean); | ||
const csv = [ | ||
header.map((v) => escapeField(v, { quote: true })).join(","), | ||
...csvData.map((row) => | ||
row.map((v) => escapeField(v, { quote: true })).join(","), | ||
), | ||
...(EOF ? [""] : []), | ||
].join(EOL); | ||
const data = | ||
csvData.length >= 1 | ||
? csvData.map((row) => | ||
Object.fromEntries(row.map((v, i) => [header[i], v])), | ||
) | ||
: []; | ||
return { data, csv, header }; | ||
}), | ||
async ({ data, csv }) => { | ||
let i = 0; | ||
const result = parseStringToArraySync(csv); | ||
expect(result.length).toEqual(data.length); | ||
for (const record of result) { | ||
expect(data[i++]).toEqual(record); | ||
} | ||
}, | ||
), | ||
); | ||
}); | ||
|
||
it("should throw error when delimiter is not a single character", async () => { | ||
const csv = "a,b,c\n1,2,3"; | ||
|
||
expect(() => | ||
parseStringToArraySync(csv, { delimiter: "ab" }), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
// biome-ignore lint/style/noUnusedTemplateLiteral: This is a snapshot | ||
`[RangeError: delimiter must be a single character]`, | ||
); | ||
}); | ||
|
||
it("should throw error when quotation is not double quote", async () => { | ||
const csv = "a,b,c\n1,2,3"; | ||
|
||
expect(() => | ||
parseStringToArraySync(csv, { quotation: "'" }), | ||
).toThrowErrorMatchingInlineSnapshot( | ||
// biome-ignore lint/style/noUnusedTemplateLiteral: This is a snapshot | ||
`[RangeError: Invalid quotation, must be double quote on WASM.]`, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.