parse.test.ts
1 import { assert, describe, expect, it } from "vitest"; 2 import { parse, DuperValue } from ".."; 3 4 describe("parse", () => { 5 it("parses an empty object", () => { 6 const duper = parse("{}"); 7 expect(duper.type).toEqual("Object"); 8 }); 9 10 it("parses into a JSON-stringifiable object", () => { 11 const duper = parse(` 12 Product({ 13 product_id: Uuid("1dd7b7aa-515e-405f-85a9-8ac812242609"), 14 name: "Wireless Bluetooth Headphones", 15 brand: "AudioTech", 16 price: Decimal("129.99"), 17 dimensions: (18.5, 15.2, 7.8), // In centimeters 18 weight: Kilograms(0.285), 19 in_stock: true, 20 specifications: { 21 battery_life: Duration("30h"), 22 noise_cancellation: true, 23 connectivity: ["Bluetooth 5.0", "3.5mm Jack"], 24 }, 25 image_thumbnail: Png(b64"iVBORw0KGgoAAAANSUhEUgAAAGQ="), 26 tags: ["electronics", "audio", "wireless"], 27 release_date: Date("2023-11-15"), 28 /* Warranty is optional */ 29 warranty_period: null, 30 customer_ratings: { 31 latest_review: r#"Absolutely ""astounding""!! 😎"#, 32 average: 4.5, 33 count: 127, 34 }, 35 created_at: Instant('2023-11-17T21:50:43+00:00'), 36 }) 37 `); 38 expect(duper.type).toEqual("Object"); 39 expect(duper.identifier).toEqual("Product"); 40 expect(JSON.stringify(duper)).toMatchSnapshot(); 41 }); 42 43 it("doesn't parse invalid Duper values", () => { 44 assert.throws(() => parse(``)); 45 assert.throws(() => parse(`{`)); 46 assert.throws(() => parse(`]`)); 47 assert.throws(() => parse(`tru`)); 48 assert.throws(() => parse(`.618`)); 49 assert.throws(() => parse(`Something(1, 2)`)); 50 assert.throws(() => parse(`Instant('2025-11-16')`)); 51 assert.throws(() => parse(`Iñvalid({})`)); 52 }); 53 });