From 9ae5ea710010a3b2388d7e0657d98eeb82a5972b Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Sun, 23 Jun 2024 11:31:11 +0700 Subject: [PATCH] v4.0.0-rc2 - Add more samples - Add more tests --- README.md | 59 +++++++++++++----- deno.json | 3 +- tests/helper_test.ts | 19 ++++++ tests/{mod_test.js => mod_test.ts} | 0 tests/sample_test.ts | 96 ++++++++++++++++++++++++++++++ utils/sample.ts | 30 ++++++++++ 6 files changed, 189 insertions(+), 18 deletions(-) create mode 100644 tests/helper_test.ts rename tests/{mod_test.js => mod_test.ts} (100%) create mode 100644 tests/sample_test.ts diff --git a/README.md b/README.md index 569d6d1..156880d 100644 --- a/README.md +++ b/README.md @@ -25,29 +25,42 @@ npx jsr add @ndaidong/txtgen ``` ```ts -import { sentence } from '@ndaidong/txtgen'; -sentence(); +import { sentence } from "@ndaidong/txtgen"; + +for (let i = 0; i < 5; i++) { + console.log(sentence()); +} ``` -In Deno, you can optionally use JSR packages without an install step using `jsr:` specifiers: +In Deno, you can optionally use JSR packages without an install step using +`jsr:` specifiers: ```ts -import { sentence } from 'jsr:@ndaidong/txtgen'; -sentence(); +import { sentence } from "jsr:@ndaidong/txtgen"; + +for (let i = 0; i < 5; i++) { + console.log(sentence()); +} ``` You can still use `npm:` specifiers as before: ```ts -import { sentence } from 'npm:@ndaidong/txtgen'; -sentence(); +import { sentence } from "npm:@ndaidong/txtgen"; + +for (let i = 0; i < 5; i++) { + console.log(sentence()); +} ``` Or import from esm.sh ```ts -import { sentence } from 'https://esm.sh/@ndaidong/txtgen'; -sentence(); +import { sentence } from "https://esm.sh/@ndaidong/txtgen"; + +for (let i = 0; i < 5; i++) { + console.log(sentence()); +} ``` ### Node.js & Bun @@ -63,20 +76,32 @@ bun add @ndaidong/txtgen ``` ```js -import { sentence } from '@ndaidong/txtgen'; +import { sentence } from "@ndaidong/txtgen"; -// CommonJS environment -// const { sentence } = require('txtgen'); +for (let i = 0; i < 5; i++) { + console.log(sentence()); +} +``` -sentence(); +You can also use CJS style: + +```js +const { sentence } = require("@ndaidong/txtgen"); + +for (let i = 0; i < 5; i++) { + console.log(sentence()); +} ``` ### Browsers: ```html ``` @@ -161,7 +186,9 @@ console.log(phrase); // => nisi blandit feugiat tempus imperdiet etiam eu mus au ## Development -Since v4.x.x, we switched to [Deno](https://docs.deno.com/runtime/manual/) platform, and use [DNT](https://github.com/denoland/dnt) to build Node.js packages. +Since v4.x.x, we switched to [Deno](https://docs.deno.com/runtime/manual/) +platform, and use [DNT](https://github.com/denoland/dnt) to build Node.js +packages. ```bash git clone https://github.com/ndaidong/txtgen.git diff --git a/deno.json b/deno.json index 0b2f03a..bb3f5b4 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@ndaidong/txtgen", - "version": "4.0.0-rc1", + "version": "4.0.0-rc2", "description": "Util for generating random sentences, paragraphs and articles in English", "homepage": "https://github.com/ndaidong/txtgen", "repository": { @@ -10,7 +10,6 @@ "author": "@ndaidong", "license": "MIT", "tasks": { - "watch": "deno run --watch mod.ts", "build": "deno run -A ./scripts/build_npm.ts" }, "imports": { diff --git a/tests/helper_test.ts b/tests/helper_test.ts new file mode 100644 index 0000000..91aad3d --- /dev/null +++ b/tests/helper_test.ts @@ -0,0 +1,19 @@ +import { assertEquals } from "assert"; + +import { adjectives, nouns } from "../utils/sample.ts"; + +import { generator, normalize } from "../utils/helper.ts"; + +Deno.test("check if normalize() works correctly", () => { + assertEquals(normalize("dog"), "a dog"); + assertEquals(normalize("car"), "a car"); + assertEquals(normalize("hour"), "an hour"); + assertEquals(normalize("egg"), "an egg"); +}); + +Deno.test("check if generator() works correctly", () => { + assertEquals(nouns.includes(generator.noun()), true); + assertEquals(generator.a_noun().startsWith("a"), true); + assertEquals(adjectives.includes(generator.adjective()), true); + assertEquals(generator.an_adjective().startsWith("a"), true); +}); diff --git a/tests/mod_test.js b/tests/mod_test.ts similarity index 100% rename from tests/mod_test.js rename to tests/mod_test.ts diff --git a/tests/sample_test.ts b/tests/sample_test.ts new file mode 100644 index 0000000..578e95f --- /dev/null +++ b/tests/sample_test.ts @@ -0,0 +1,96 @@ +import { assertEquals, assertNotEquals } from "assert"; + +import { + addAdjectives, + addNouns, + addTemplates, + adjectives, + getAdjectives, + getNouns, + getTemplates, + nouns, + sentenceTemplates, + setAdjectives, + setNouns, + setTemplates, +} from "../utils/sample.ts"; + +Deno.test("check if getNouns() return same as pre-defined nouns", () => { + const val = getNouns(); + const lastIndex = val.length - 1; + assertEquals(val.length, nouns.length); + assertEquals(val[0], nouns[0]); + assertEquals(val[9], nouns[9]); + assertEquals(val[lastIndex], nouns[lastIndex]); +}); + +Deno.test("check if addNouns()/setNouns() affects to pre-defined nouns", () => { + const preDefined = getNouns(); + setNouns(["one", "two", "three"]); + const valAfterSetCall = getNouns(); + assertNotEquals(valAfterSetCall.length, preDefined.length); + assertEquals(valAfterSetCall.length, 3); + assertEquals(valAfterSetCall[1], "two"); + + addNouns(["four", "five", "six"]); + const valAfterAddCall = getNouns(); + assertEquals(valAfterAddCall.length > valAfterSetCall.length, true); + assertEquals(valAfterAddCall.includes("four"), true); + assertEquals(valAfterAddCall.includes("six"), true); +}); + +Deno.test("check if getAdjectives() return same as pre-defined adjectives", () => { + const val = getAdjectives(); + const lastIndex = val.length - 1; + assertEquals(val.length, adjectives.length); + assertEquals(val[0], adjectives[0]); + assertEquals(val[9], adjectives[9]); + assertEquals(val[lastIndex], adjectives[lastIndex]); +}); + +Deno.test("check if addAdjectives()/setAdjectives() affects to pre-defined adjectives", () => { + const preDefined = getAdjectives(); + setAdjectives(["black", "white", "yellow"]); + const valAfterSetCall = getAdjectives(); + assertNotEquals(valAfterSetCall.length, preDefined.length); + assertEquals(valAfterSetCall.length, 3); + assertEquals(valAfterSetCall[1], "white"); + + addAdjectives(["blue", "red", "green"]); + const valAfterAddCall = getAdjectives(); + assertEquals(valAfterAddCall.length > valAfterSetCall.length, true); + assertEquals(valAfterAddCall.includes("blue"), true); + assertEquals(valAfterAddCall.includes("green"), true); +}); + +Deno.test("check if getTemplates() return same as pre-defined sentenceTemplates", () => { + const val = getTemplates(); + const lastIndex = val.length - 1; + assertEquals(val.length, sentenceTemplates.length); + assertEquals(val[0], sentenceTemplates[0]); + assertEquals(val[9], sentenceTemplates[9]); + assertEquals(val[lastIndex], sentenceTemplates[lastIndex]); +}); + +Deno.test("check if addTemplates()/setTemplates() affects to pre-defined sentenceTemplates", () => { + const preDefined = getTemplates(); + setTemplates([ + "first sentence template", + "second sentence template", + "third sentence template", + ]); + const valAfterSetCall = getTemplates(); + assertNotEquals(valAfterSetCall.length, preDefined.length); + assertEquals(valAfterSetCall.length, 3); + assertEquals(valAfterSetCall[1], "second sentence template"); + + addTemplates([ + "fourth sentence template", + "fifth sentence template", + "sixth sentence template", + ]); + const valAfterAddCall = getTemplates(); + assertEquals(valAfterAddCall.length > valAfterSetCall.length, true); + assertEquals(valAfterAddCall.includes("fourth sentence template"), true); + assertEquals(valAfterAddCall.includes("sixth sentence template"), true); +}); diff --git a/utils/sample.ts b/utils/sample.ts index 09d2c1f..a8b97d2 100644 --- a/utils/sample.ts +++ b/utils/sample.ts @@ -84,6 +84,7 @@ export let nouns: string[] = [ "tangerine", "watermelon", ]; + export let adjectives: string[] = [ "adaptable", "adventurous", @@ -326,6 +327,35 @@ export const vowels: string[] = [ export let sentenceTemplates: string[] = [ "however, {{nouns}} have begun to rent {{nouns}} over the past few months, specifically for {{nouns}} associated with their {{nouns}}", + "the {{noun}} is {{a_noun}}", + "{{a_noun}} is {{an_adjective}} {{noun}}", + "the first {{adjective}} {{noun}} is, in its own way, {{a_noun}}", + "their {{noun}} was, in this moment, {{an_adjective}} {{noun}}", + "{{a_noun}} is {{a_noun}} from the right perspective", + "the literature would have us believe that {{an_adjective}} {{noun}} is not but {{a_noun}}", + "{{an_adjective}} {{noun}} is {{a_noun}} of the mind", + "the {{adjective}} {{noun}} reveals itself as {{an_adjective}} {{noun}} to those who look", + "authors often misinterpret the {{noun}} as {{an_adjective}} {{noun}}, when in actuality it feels more like {{an_adjective}} {{noun}}", + "we can assume that any instance of {{a_noun}} can be construed as {{an_adjective}} {{noun}}", + "they were lost without the {{adjective}} {{noun}} that composed their {{noun}}", + "the {{adjective}} {{noun}} comes from {{an_adjective}} {{noun}}", + "{{a_noun}} can hardly be considered {{an_adjective}} {{noun}} without also being {{a_noun}}", + "few can name {{an_adjective}} {{noun}} that isn't {{an_adjective}} {{noun}}", + "some posit the {{adjective}} {{noun}} to be less than {{adjective}}", + "{{a_noun}} of the {{noun}} is assumed to be {{an_adjective}} {{noun}}", + "{{a_noun}} sees {{a_noun}} as {{an_adjective}} {{noun}}", + "the {{noun}} of {{a_noun}} becomes {{an_adjective}} {{noun}}", + "{{a_noun}} is {{a_noun}}'s {{noun}}", + "{{a_noun}} is the {{noun}} of {{a_noun}}", + "{{an_adjective}} {{noun}}'s {{noun}} comes with it the thought that the {{adjective}} {{noun}} is {{a_noun}}", + "{{nouns}} are {{adjective}} {{nouns}}", + "{{adjective}} {{nouns}} show us how {{nouns}} can be {{nouns}}", + "before {{nouns}}, {{nouns}} were only {{nouns}}", + "those {{nouns}} are nothing more than {{nouns}}", + "some {{adjective}} {{nouns}} are thought of simply as {{nouns}}", + "one cannot separate {{nouns}} from {{adjective}} {{nouns}}", + "the {{nouns}} could be said to resemble {{adjective}} {{nouns}}", + "{{an_adjective}} {{noun}} without {{nouns}} is truly a {{noun}} of {{adjective}} {{nouns}}", ]; export const phrases: string[] = [