From 271a7bd8f76a0b1f68f1765e8ed18ed4e5e878f8 Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Tue, 2 Apr 2024 17:48:33 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20`toString`=20methods=20to=20a?= =?UTF-8?q?ll=20primitive=20data=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `toString` method is very useful for debugging purposes. Hence, I added it to all the primitive data types. --- src/bool.ts | 12 +++++++++++ src/datetime.ts | 4 ++++ src/double.ts | 4 ++++ src/integer.ts | 12 +++++++++++ src/text.ts | 4 ++++ tests/bool.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ tests/datetime.test.ts | 20 ++++++++++++++++++ tests/double.test.ts | 20 ++++++++++++++++++ tests/integer.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ tests/text.test.ts | 20 ++++++++++++++++++ 10 files changed, 192 insertions(+) create mode 100644 tests/bool.test.ts create mode 100644 tests/datetime.test.ts create mode 100644 tests/double.test.ts create mode 100644 tests/integer.test.ts create mode 100644 tests/text.test.ts diff --git a/src/bool.ts b/src/bool.ts index 42acf50..61003dd 100644 --- a/src/bool.ts +++ b/src/bool.ts @@ -11,6 +11,10 @@ export class Bool implements TotalOrder { return new Bool(value); } + public toString(this: Bool): string { + return `Bool(${this.value})`; + } + public isSame(this: Bool, that: Bool): boolean { return this.value === that.value; } @@ -59,6 +63,10 @@ export class Any extends Bool implements Semigroup { return new Any(value); } + public override toString(this: Any): string { + return `Any(${this.value})`; + } + public append(this: Any, that: Any): Any { return new Any(this.value || that.value); } @@ -69,6 +77,10 @@ export class All extends Bool implements Semigroup { return new All(value); } + public override toString(this: All): string { + return `All(${this.value})`; + } + public append(this: All, that: All): All { return new All(this.value && that.value); } diff --git a/src/datetime.ts b/src/datetime.ts index 68067da..ac252ac 100644 --- a/src/datetime.ts +++ b/src/datetime.ts @@ -10,6 +10,10 @@ export class DateTime implements TotalOrder { return new DateTime(value); } + public toString(this: DateTime): string { + return `DateTime(${this.value.getTime()})`; + } + public isSame(this: DateTime, that: DateTime): boolean { return Object.is(this.value.getTime(), that.value.getTime()); } diff --git a/src/double.ts b/src/double.ts index 7e9facd..aeb8092 100644 --- a/src/double.ts +++ b/src/double.ts @@ -10,6 +10,10 @@ export class Double implements TotalOrder { return new Double(value); } + public toString(this: Double): string { + return `Double(${this.value})`; + } + public isSame(this: Double, that: Double): boolean { return Object.is(this.value, that.value); } diff --git a/src/integer.ts b/src/integer.ts index 0e76a53..34cdf77 100644 --- a/src/integer.ts +++ b/src/integer.ts @@ -11,6 +11,10 @@ export class Integer implements TotalOrder { return new Integer(value); } + public toString(this: Integer): string { + return `Integer(${this.value})`; + } + public isSame(this: Integer, that: Integer): boolean { return this.value === that.value; } @@ -59,6 +63,10 @@ export class Sum extends Integer implements Semigroup { return new Sum(value); } + public override toString(this: Sum): string { + return `Sum(${this.value})`; + } + public append(this: Sum, that: Sum): Sum { return new Sum(this.value + that.value); } @@ -69,6 +77,10 @@ export class Product extends Integer implements Semigroup { return new Product(value); } + public override toString(this: Product): string { + return `Product(${this.value})`; + } + public append(this: Product, that: Product): Product { return new Product(this.value * that.value); } diff --git a/src/text.ts b/src/text.ts index 09b1ace..8fb3b1c 100644 --- a/src/text.ts +++ b/src/text.ts @@ -11,6 +11,10 @@ export class Text implements Semigroup, TotalOrder { return new Text(value); } + public toString(this: Text): string { + return `Text(${JSON.stringify(this.value)})`; + } + public append(this: Text, that: Text): Text { return new Text(`${this.value}${that.value}`); } diff --git a/tests/bool.test.ts b/tests/bool.test.ts new file mode 100644 index 0000000..c2709ed --- /dev/null +++ b/tests/bool.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "@jest/globals"; +import fc from "fast-check"; + +import type { All, Any, Bool } from "../src/bool.js"; + +import { all, any, bool } from "./arbitraries.js"; + +const boolToStringDefinition = (m: Bool): void => { + expect(m.toString()).toStrictEqual(`Bool(${m.value})`); +}; + +const anyToStringDefinition = (m: Any): void => { + expect(m.toString()).toStrictEqual(`Any(${m.value})`); +}; + +const allToStringDefinition = (m: All): void => { + expect(m.toString()).toStrictEqual(`All(${m.value})`); +}; + +describe("Bool", () => { + describe("toString", () => { + it("should convert Bool to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(bool, boolToStringDefinition)); + }); + }); +}); + +describe("Any", () => { + describe("toString", () => { + it("should convert Any to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(any, anyToStringDefinition)); + }); + }); +}); + +describe("All", () => { + describe("toString", () => { + it("should convert All to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(all, allToStringDefinition)); + }); + }); +}); diff --git a/tests/datetime.test.ts b/tests/datetime.test.ts new file mode 100644 index 0000000..338dd78 --- /dev/null +++ b/tests/datetime.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "@jest/globals"; +import fc from "fast-check"; + +import type { DateTime } from "../src/datetime.js"; + +import { datetime } from "./arbitraries.js"; + +const toStringDefinition = (m: DateTime): void => { + expect(m.toString()).toStrictEqual(`DateTime(${m.value.getTime()})`); +}; + +describe("DateTime", () => { + describe("toString", () => { + it("should convert DateTime to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(datetime, toStringDefinition)); + }); + }); +}); diff --git a/tests/double.test.ts b/tests/double.test.ts new file mode 100644 index 0000000..1c3adbd --- /dev/null +++ b/tests/double.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "@jest/globals"; +import fc from "fast-check"; + +import type { Double } from "../src/double.js"; + +import { double } from "./arbitraries.js"; + +const toStringDefinition = (m: Double): void => { + expect(m.toString()).toStrictEqual(`Double(${m.value})`); +}; + +describe("Double", () => { + describe("toString", () => { + it("should convert Double to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(double, toStringDefinition)); + }); + }); +}); diff --git a/tests/integer.test.ts b/tests/integer.test.ts new file mode 100644 index 0000000..a4d4399 --- /dev/null +++ b/tests/integer.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "@jest/globals"; +import fc from "fast-check"; + +import type { Integer, Product, Sum } from "../src/integer.js"; + +import { integer, product, sum } from "./arbitraries.js"; + +const integerToStringDefinition = (m: Integer): void => { + expect(m.toString()).toStrictEqual(`Integer(${m.value})`); +}; + +const sumToStringDefinition = (m: Sum): void => { + expect(m.toString()).toStrictEqual(`Sum(${m.value})`); +}; + +const productToStringDefinition = (m: Product): void => { + expect(m.toString()).toStrictEqual(`Product(${m.value})`); +}; + +describe("Integer", () => { + describe("toString", () => { + it("should convert Integer to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(integer, integerToStringDefinition)); + }); + }); +}); + +describe("Sum", () => { + describe("toString", () => { + it("should convert Sum to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(sum, sumToStringDefinition)); + }); + }); +}); + +describe("Product", () => { + describe("toString", () => { + it("should convert Product to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(product, productToStringDefinition)); + }); + }); +}); diff --git a/tests/text.test.ts b/tests/text.test.ts new file mode 100644 index 0000000..93e2141 --- /dev/null +++ b/tests/text.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "@jest/globals"; +import fc from "fast-check"; + +import type { Text } from "../src/text.js"; + +import { text } from "./arbitraries.js"; + +const toStringDefinition = (m: Text): void => { + expect(m.toString()).toStrictEqual(`Text(${JSON.stringify(m.value)})`); +}; + +describe("Text", () => { + describe("toString", () => { + it("should convert Text to a string", () => { + expect.assertions(100); + + fc.assert(fc.property(text, toStringDefinition)); + }); + }); +});