Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(csv): improve stringify testing #5150

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion csv/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ function getValuesFromItem(
let value: unknown = item;

for (const prop of column.prop) {
if (typeof value !== "object" || value === null) continue;
if (typeof value !== "object" || value === null) {
continue;
}
if (Array.isArray(value)) {
if (typeof prop === "number") value = value[prop];
else {
Expand Down
33 changes: 33 additions & 0 deletions csv/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ Deno.test({
},
},
);
await t.step(
{
name: "Column: array string accessor via prop field",
fn() {
const columns = [{ prop: ["msg", "value"] }];
const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }];
const output = `value${CRLF}foo${CRLF}bar${CRLF}`;
assertEquals(stringify(data, { columns }), output);
},
},
);
await t.step(
{
name: "Explicit header",
Expand Down Expand Up @@ -367,6 +378,18 @@ Deno.test({
},
},
);
await t.step(
{
name: "The case when the entire item is null",
fn() {
const columns = [0];
const data = [null, null];
const output = `0${CRLF}${CRLF}${CRLF}`;
// deno-lint-ignore no-explicit-any
assertEquals(stringify(data as any, { columns }), output);
},
},
);
await t.step(
{
name: "Targeted value: hex number",
Expand Down Expand Up @@ -510,6 +533,16 @@ Deno.test({
assertEquals(stringify(data), output);
},
});
await t.step({
name: "The case when each item is single data, no columns",
fn() {
const data = [1, 2, 3, 4, 5, 6];
const output = `1${CRLF}2${CRLF}3${CRLF}4${CRLF}5${CRLF}6${CRLF}`;

// deno-lint-ignore no-explicit-any
assertEquals(stringify(data as any), output);
},
});
await t.step(
{
name: "byte-order mark with bom=true",
Expand Down