Skip to content

Commit

Permalink
Merge pull request #14 from la-lala-land/dev
Browse files Browse the repository at this point in the history
feat: Write tests
  • Loading branch information
retraigo committed Oct 15, 2022
2 parents 9fd47ef + d6141a5 commit 42ab6d8
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"deno.enable": true
"deno.enable": true,
"deno.unstable": true
}
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions functions/generator/name/character.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getRandom } from "../../util/mod.ts";

import adjectives from "../../../data/adjectives.ts";
import Adjectives from "../../../data/adjectives.ts";
import Synonyms from "../../../data/synonyms.ts";
import levels from "../../../data/levels.ts";
import thirdperson from "../../../data/characterTypes.ts";
import Levels from "../../../data/levels.ts";
import ThirdPerson from "../../../data/characterTypes.ts";

/**
* Generate a random fantasy character.
Expand All @@ -13,8 +13,8 @@ export const generateCharacter = (): string => {
return `${
Math.random() < 0.9
? Math.random() > 0.3
? `${getRandom(adjectives)} `
: `${getRandom(Synonyms.level)} ${getRandom(levels)} `
? `${getRandom(Adjectives)} `
: `${getRandom(Synonyms.level)} ${getRandom(Levels)} `
: ""
}${getRandom(thirdperson)}`;
}${getRandom(ThirdPerson)}`;
};
13 changes: 8 additions & 5 deletions functions/generator/name/monster.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { getRandom } from "../../util/mod.ts";
import { generateName } from "./normal.ts";

import suffix from "../../../data/suffixesForFantasy.ts"
import Suffix from "../../../data/suffixesForFantasy.ts";
/**
* Generate a fantasy name like the stuff in RPGs.
* @returns Fantasy name
*/
export const generateFantasyName = (): string => {
return `${generateName(3 + Math.floor(Math.random() * 5)).toLowerCase()}${
getRandom(suffix)
}`;
export const generateFantasyName = (
length = 3 + Math.floor(Math.random() * 5),
): string => {
const suffix = getRandom(Suffix);
return `${
generateName(length).slice(0, length - suffix.length).toLowerCase()
}${suffix}`;
};
12 changes: 6 additions & 6 deletions functions/generator/name/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import after from "../../../data/nameRules.ts"

/**
* Generate a random name. The name is just a normal word that can be pronounced.
* @param lenn Length of the name to generate.
* @param length Length of the name to generate.
* @returns Random name that actually makes sense.
*/
export function generateName(lenn?: number): string {
if (!lenn) lenn = 4 + Math.floor(Math.random() * 5);
export function generateName(length?: number): string {
if (!length) length = 4 + Math.floor(Math.random() * 5);
let name: string = getRandom(alphabet);
for (let pointer = 0; pointer < lenn - 1; ++pointer) {
if (pointer === lenn - 2) {
for (let pointer = 0; pointer < length - 1; ++pointer) {
if (pointer === length - 2) {
name += getRandom(
after.find((x) =>
x.letter === name.charAt(name.length - 1).toUpperCase()
Expand All @@ -39,5 +39,5 @@ export function generateName(lenn?: number): string {
}
}
}
return name;
return name.toLowerCase();
}
2 changes: 1 addition & 1 deletion functions/generator/name/race.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fantasy from "../../../db/creatures/fantasy.ts";
import fantasy from "../../../data/creatures/fantasy.ts";
import { capitalize, getRandom } from "../../util/mod.ts";
import { generateFantasyName } from "./monster.ts";

Expand Down
6 changes: 3 additions & 3 deletions functions/generator/story/penance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fantasyCreatures from "../../../db/creatures/fantasy.ts";
import realCreatures from "../../../db/creatures/reality.ts";
import fantasyCreatures from "../../../data/creatures/fantasy.ts";
import realCreatures from "../../../data/creatures/reality.ts";

const creatures = fantasyCreatures.concat(realCreatures);
import { capitalize, getRandom } from "../../util/mod.ts";
Expand Down Expand Up @@ -51,7 +51,7 @@ export const penance = (name: string): string => {
: `${getRandom(fight)}${rand > 0.5 ? `` : `s`}`
} the${Math.random() < 0.5 ? ` ${evil()}` : ``} ${
Math.random() < 0.5 ? generateCharacter() : evilcreature.name
}${Math.random() < 0.5 ? `, ${capitalize(generateFantasyName())},` : ``}${
}${
Math.random() < 0.5
? ` with the help of the ${goodcreature.plural}${
Math.random() < 0.5
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nekooftheabyss/lala",
"version": "3.1.4",
"version": "3.1.5",
"description": "A collection of random useful (probably) javascript classes and functions. No it wasn't named after Lala Satalin Deviluke (or it probably was, idk).",
"main": "lala.js",
"type": "module",
Expand Down
3 changes: 0 additions & 3 deletions test.js

This file was deleted.

210 changes: 210 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import commonEmail from "./data/commonEmail.ts";

import {
capitalize,
generateChainMail,
generateCharacter,
generateEmail,
generateFantasyName,
generateName,
generateRace,
generateStory,
generateString,
getRandom,
owoify,
weebify,
} from "./mod.ts";

import { Race } from "./functions/generator/name/race.ts";

import {
assert,
assertEquals,
assertExists,
assertInstanceOf,
assertStringIncludes,
} from "https://deno.land/std@0.159.0/testing/asserts.ts";

Deno.test({
name: "Generator functions are defined.",
fn() {
assertExists(generateChainMail);
assertExists(generateCharacter);
assertExists(generateEmail);
assertExists(generateFantasyName);
assertExists(generateName);
assertExists(generateRace);
assertExists(generateStory);
assertExists(generateString);
},
});

Deno.test({
name: "Util functions are defined.",
fn() {
assertExists(capitalize);
assertExists(getRandom);
assertExists(weebify);
assertExists(owoify);
},
});

Deno.test({
name: "generateChainMail generates a valid, non-empty string.",
fn() {
const chainMail = generateChainMail();
console.debug(`\t${chainMail}`);
assert(Boolean(chainMail?.length));
},
});

Deno.test({
name:
"generateChainMail, when supplied a custom name, includes that name in the result.",
fn() {
const chainMail = generateChainMail("Lala");
console.debug(`\t${chainMail}`);
assertStringIncludes(chainMail, "Lala");
},
});

Deno.test({
name: "generateCharacter generates a valid, non-empty string",
fn() {
const character = generateCharacter();
console.debug(`\t${character}`);
assert(Boolean(character?.length));
},
});

Deno.test({
name: "generateEmail generates a valid, non-empty string",
fn() {
const email = generateEmail();
console.debug(`\t${email}`);
assert(Boolean(email?.length));
},
});

Deno.test({
name:
"generateEmail generates an email from a popular service when parameter passed is true.",
fn() {
const email = generateEmail(true);
console.debug(`\t${email}`);
assert(commonEmail.some((x) => email.split("@")[1] === x));
},
});

Deno.test({
name: "generateFantasyName generates a valid, non-empty string",
fn() {
const name = generateFantasyName();
console.debug(`\t${name}`);
assert(Boolean(name?.length));
},
});

Deno.test({
name:
"generateFantasyName generates a name equal in length to supplied parameter",
fn() {
const length = 6 + Math.floor(Math.random() * 17);
const name = generateFantasyName(length);
console.debug(
`\tName:\t\t${name}\n\tLength Passed:\t${length}\n\tLength Got:\t${name.length}`,
);
assertEquals(name.length, length);
},
});

Deno.test({
name: "generateName generates a valid, non-empty string",
fn() {
const name = generateName();
console.debug(`\t${name}`);
assert(Boolean(name?.length));
},
});

Deno.test({
name: "generateName generates a name equal in length to supplied parameter",
fn() {
const length = 6 + Math.floor(Math.random() * 17);
const name = generateName(length);
console.debug(
`\tName:\t\t${name}\n\tLength Passed:\t${length}\n\tLength Got:\t${name.length}`,
);
assertEquals(name.length, length);
},
});

Deno.test({
name: "generateRace generates an instance of Race",
fn() {
const race = generateRace();
console.debug(
`\t${race.description}`,
);
assertInstanceOf(race, Race);
},
});

Deno.test({
name: "generateRace, when supplied a custom name, uses that name.",
fn() {
const race = generateRace("Lala");
console.debug(`\t${race.description}`);
assertEquals(race.name, "Lala");
},
});

Deno.test({
name: "generateStory generates a valid, non-empty string.",
fn() {
const story = generateStory();
console.debug(`\t${story}`);
assert(Boolean(story?.length));
},
});

Deno.test({
name:
"generateStory, when supplied a custom name, includes that name in the result.",
fn() {
const story = generateStory("Lala");
console.debug(`\t${story}`);
assertStringIncludes(story, "Lala");
},
});

Deno.test({
name: "generateString generates a valid, non-empty string",
fn() {
const str = generateString();
console.debug(`\t${str}`);
assert(Boolean(str?.length));
},
});

Deno.test({
name:
"generateString generates a string equal in length to supplied parameter",
fn() {
const length = 6 + Math.floor(Math.random() * 17);
const str = generateString(length);
console.debug(
`\String:\t\t${str}\n\tLength Passed:\t${length}\n\tLength Got:\t${str.length}`,
);
assertEquals(str.length, length);
},
});

Deno.test({
name: "generateString generates a purely alphanumeric string.",
fn() {
const str = generateString(99);
console.debug(`\t${str}`);
assert(/^[a-zA-Z0-9]+$/.test(str));
},
});

0 comments on commit 42ab6d8

Please sign in to comment.