Skip to content

Commit

Permalink
test(yaml): add invalid represent type test (#5874)
Browse files Browse the repository at this point in the history
* initial commit

* update
  • Loading branch information
timreichen authored Sep 2, 2024
1 parent 9298ea5 commit 23f4738
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 7 additions & 7 deletions yaml/_dumper_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
VERTICAL_LINE,
} from "./_chars.ts";
import { DEFAULT_SCHEMA, type Schema } from "./_schema.ts";
import type { KindType, RepresentFn, StyleVariant, Type } from "./_type.ts";
import type { KindType, StyleVariant, Type } from "./_type.ts";
import { isObject } from "./_utils.ts";

const STYLE_PLAIN = 1;
Expand Down Expand Up @@ -740,13 +740,13 @@ export class DumperState {
if (typeof type.represent === "function") {
return type.represent(value, style);
}
if (Object.hasOwn(type.represent, style)) {
const represent = type.represent[style] as RepresentFn<unknown>;
return represent(value, style);
const represent = type.represent[style];
if (!represent) {
throw new TypeError(
`!<${type.tag}> tag resolver accepts not "${style}" style`,
);
}
throw new TypeError(
`!<${type.tag}> tag resolver accepts not "${style}" style`,
);
return represent(value, style);
}

detectType(value: unknown): { tag: string | null; value: unknown } {
Expand Down
6 changes: 6 additions & 0 deletions yaml/stringify_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ Deno.test({
stringify([true, false], { styles: { "!!bool": "uppercase" } }),
"- TRUE\n- FALSE\n",
);

assertThrows(
() => stringify([true, false], { styles: { "!!bool": "octal" } }),
TypeError,
'!<tag:yaml.org,2002:bool> tag resolver accepts not "octal" style',
);
},
});

Expand Down

0 comments on commit 23f4738

Please sign in to comment.