diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-02-12 00:44:57 +0200 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-02-12 00:44:57 +0200 |
| commit | 670dbc07239fb860961055eb7af6823786e4febe (patch) | |
| tree | d575fdb4871e23af0220b938ba009d9ea4797523 /src/flatmatter.test.ts | |
| parent | 8ff7c10249b1a9daeb30d47cf5b9266a5a0f7a26 (diff) | |
Refactor to use Effect.
Diffstat (limited to 'src/flatmatter.test.ts')
| -rw-r--r-- | src/flatmatter.test.ts | 173 |
1 files changed, 163 insertions, 10 deletions
diff --git a/src/flatmatter.test.ts b/src/flatmatter.test.ts index 9f00ef9..8e480fc 100644 --- a/src/flatmatter.test.ts +++ b/src/flatmatter.test.ts @@ -1,32 +1,185 @@ import FlatMatter from "./flatmatter.ts"; import { assert } from "vitest"; -import ToObject from "./serializers/to_object.ts"; + +test("Single-level configuration", () => { + const config = FlatMatter.config( + 'a: true\nb: false\nc: 1\nd: 12.5\nf: "some string"', + ); + + expect(config).toStrictEqual({ + a: true, + b: false, + c: 1, + d: 12.5, + f: "some string", + }); +}); + +test("Two-level configuration", () => { + const config = FlatMatter.config( + 'a.a: true\nb.b: false\nc.c: 1\nd.d: 12.5\nf.f: "some string"', + ); + + expect(config).toStrictEqual({ + a: { + a: true, + }, + b: { + b: false, + }, + c: { + c: 1, + }, + d: { + d: 12.5, + }, + f: { + f: "some string", + }, + }); +}); + +test("Simple function usage", () => { + const toUpper = { + name: "to-upper", + compute: (input: string): unknown => { + return input.toUpperCase(); + }, + }; + + const config = FlatMatter.config('a: (to-upper "value")', [toUpper]); + + expect(config).toStrictEqual({ + a: "VALUE", + }); +}); + +test("Piped function by reference usage", () => { + const toUpper = { + name: "to-upper", + compute: (input: string): unknown => { + return input.toUpperCase(); + }, + }; + + const config = FlatMatter.config('a: "value" / to-upper', [toUpper]); + + expect(config).toStrictEqual({ + a: "VALUE", + }); +}); + +test("Piped function by call usage", () => { + const toUpper = { + name: "to-upper", + compute: (input: string, additional: number): unknown => { + return `${input.toUpperCase()}-${additional}`; + }, + }; + + const config = FlatMatter.config('a: "value" / (to-upper 123)', [toUpper]); + + expect(config).toStrictEqual({ + a: "VALUE-123", + }); +}); + +test("Invalid value in pipe", () => { + const config = FlatMatter.config('a: "value" / / asd'); + + expect(config).toStrictEqual({}); +}); + +test("Invalid value in pipe, 2", () => { + const config = FlatMatter.config('a: "value" / asd'); + + expect(config).toStrictEqual({ + a: "value", + }); +}); + +test("Only piped functions", () => { + const firstFn = { + name: "first-fn", + compute: (input: string): unknown => { + return input.toUpperCase(); + }, + }; + + const secondFn = { + name: "second-fn", + compute: (input: string): unknown => { + return `${input}-passed-by-second`; + }, + }; + + const config = FlatMatter.config('a: (first-fn "value / here") / second-fn', [ + firstFn, + secondFn, + ]); + + expect(config).toStrictEqual({ + a: "VALUE / HERE-passed-by-second", + }); +}); + +test("Function call without any args", () => { + const toUpper = { + name: "to-upper", + compute: (input: string): unknown => { + return input.toUpperCase(); + }, + }; + + const config = FlatMatter.config('a: "value" / (to-upper)', [toUpper]); + + expect(config).toStrictEqual({ + a: "VALUE", + }); +}); + +test("Function call using multiple strings with spaces as arg", () => { + const toUpper = { + name: "to-upper", + compute: (input: string): unknown => { + return input.toUpperCase(); + }, + }; + + const config = FlatMatter.config( + 'a: (to-upper "value goes here" "and here")', + [toUpper], + ); + + expect(config).toStrictEqual({ + a: "VALUE GOES HERE", + }); +}); test("Line has no value separator", () => { assert.throws( - () => new FlatMatter("test"), - "Line on index 0 doesn't have a value separator." + () => FlatMatter.config("test"), + "Line on index 0 doesn't have a value separator.", ); }); test("Line can only have one value separator", () => { assert.throws( - () => new FlatMatter("test: this: that"), - "Line on index 0 has multiple value separators." + () => FlatMatter.config("test: this: that"), + "Line on index 0 has multiple value separators.", ); }); test("String values can have colon characters", () => { - assert.doesNotThrow(() => new FlatMatter('test: "this : that"'), Error); + assert.doesNotThrow(() => FlatMatter.config('test: "this : that"'), Error); }); test("FrontMatter creates a new content entry", () => { - const fm = new FlatMatter( - `---\nthis: true\n---\n\nMarkdown goes here.\n\nAnd here.` + const config = FlatMatter.config( + `---\nthis: true\n---\n\nMarkdown goes here.\n\nAnd here.`, ); - const result = fm.serialize(new ToObject()); - assert.deepEqual(result, { + assert.deepEqual(config, { this: true, content: "Markdown goes here.\n\nAnd here.", }); |
