test(yaml): add invalid represent type test (#5874)

* initial commit

* update
This commit is contained in:
Tim Reichen 2024-09-02 09:46:17 +02:00 committed by GitHub
parent 9298ea503f
commit 23f47386ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 7 deletions

View File

@ -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;
@ -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 } {

View File

@ -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',
);
},
});