Skip to content

Commit

Permalink
test(yaml): test handling of omap (#4851)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed May 24, 2024
1 parent 5d7cb3c commit 2a6e912
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
5 changes: 0 additions & 5 deletions yaml/_type/omap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ function resolveYamlOmap(data: Any): boolean {
return true;
}

function constructYamlOmap(data: Any): Any {
return data !== null ? data : [];
}

export const omap = new Type("tag:yaml.org,2002:omap", {
construct: constructYamlOmap,
kind: "sequence",
resolve: resolveYamlOmap,
});
42 changes: 42 additions & 0 deletions yaml/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,45 @@ Deno.test({
]);
},
});

Deno.test({
name: "parse() handles omap type",
fn() {
const yaml = `--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffey: 58
`;
assertEquals(parse(yaml), [
{ "Mark McGwire": 65 },
{ "Sammy Sosa": 63 },
{ "Ken Griffey": 58 },
]);

// Invalid omap
// map entry is not an object
assertThrows(
() => parse("--- !!omap\n- 1"),
YAMLError,
"cannot resolve a node with !<tag:yaml.org,2002:omap> explicit tag",
);
// map entry is empty object
assertThrows(
() => parse("--- !!omap\n- {}"),
YAMLError,
"cannot resolve a node with !<tag:yaml.org,2002:omap> explicit tag",
);
// map entry is an object with multiple keys
assertThrows(
() => parse("--- !!omap\n- foo: 1\n bar: 2"),
YAMLError,
"cannot resolve a node with !<tag:yaml.org,2002:omap> explicit tag",
);
// 2 map entries have the same key
assertThrows(
() => parse("--- !!omap\n- foo: 1\n- foo: 2"),
YAMLError,
"cannot resolve a node with !<tag:yaml.org,2002:omap> explicit tag",
);
},
});

0 comments on commit 2a6e912

Please sign in to comment.