Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjuanes committed Jul 31, 2024
1 parent f0a7623 commit b557fbd
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,101 @@ describe("{{=function }}", () => {
assert.equal(m(`{{=concat "Hello" "World"}}!`, {}, options), "Hello World!");
});
});

describe("utils", () => {
describe("yaml", () => {
const yaml = lines => m.yaml(lines.join("\n"));

it("should parse simple key-value yaml", () => {
const json = yaml([
`string: "hello world"`,
`boolean: true`,
`number: 123`,
]);
assert.equal(typeof json, "object");
assert.equal(json.string, "hello world");
assert.equal(json.boolean, true);
assert.equal(json.number, 123);
});

it("should parse nested objects", () => {
const json = yaml([
`nested:`,
` key1: "value1"`,
` key2: "value2"`,
` nested:`,
` key1: "value1"`,
`no-nested: "another value"`,
]);
assert.equal(typeof json.nested, "object");
assert.equal(typeof json.nested.nested, "object");
assert.equal(json.nested.key1, "value1");
assert.equal(json.nested.key2, "value2");
assert.equal(json.nested.nested.key1, "value1");
assert.equal(json["no-nested"], "another value");
});

it("should parse an array of simple values", () => {
const json = yaml([
`items:`,
` - "item1"`,
` - "item2"`,
`key: "value"`,
]);
assert.equal(typeof json.items, "object");
assert.equal(json.items.length, 2);
assert.equal(json.items[0], "item1");
assert.equal(json.items[1], "item2");
assert.equal(json.key, "value");
});

it("should parse an array of objects", () => {
const json = yaml([
`items:`,
` - key1: "value1"`,
` key2: "value2"`,
` - key1: "value3"`,
` - items:`,
` - "foo"`,
` - "bar"`,
`foo: "bar"`,
]);
assert.equal(typeof json.items, "object");
assert.equal(json.items.length, 3);
assert.equal(json.items[0].key1, "value1");
assert.equal(json.items[0].key2, "value2");
assert.equal(json.items[1].key1, "value3");
assert.equal(json.items[2].items.length, 2);
assert.equal(json.items[2].items[0], "foo");
assert.equal(json.items[2].items[1], "bar");
assert.equal(json.foo, "bar");
});
});

describe("frontmatter", () => {
const frontmatter = lines => m.frontmatter(lines.join("\n"));

it("should return empty data if no frontmatter is present", () => {
const result = frontmatter([
`Hello world`,
]);
assert.equal(result.body, "Hello world");
assert.equal(Object.keys(result.data).length, 0);
});

it("should return parsed frontmatter", () => {
const result = frontmatter([
`---`,
`key: "value"`,
`items:`,
` - "foo"`,
` - "bar"`,
`---`,
`Hello world`,
]);
assert.equal(result.body, "Hello world");
assert.equal(result.data.key, "value");
assert.equal(result.data.items[1], "bar");
});
});
});

0 comments on commit b557fbd

Please sign in to comment.