Skip to content

Commit

Permalink
Merge pull request #52 from ArkhamCookie/random
Browse files Browse the repository at this point in the history
Add tests for `randomWord` & `randomWords`
  • Loading branch information
ArkhamCookie authored Oct 10, 2023
2 parents 925ca54 + aa927c7 commit d9897a3
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
42 changes: 42 additions & 0 deletions lib/tests/randomWord-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env -S deno test --allow-read --allow-env --allow-net

import { assertExists, assertGreaterOrEqual, assertLessOrEqual } from 'assert'

import { randomWord } from '../../lib/mod.js'

Deno.test({
name: 'words/randomWord: default',
permissions: { read: true, env: true, net: true },
async fn() {
const response = await randomWord({})
assertExists(response)
assertExists(response.word)
console.log(response.word)
}
})

Deno.test({
name: 'words/randomWord: minLength',
permissions: { read: true, env: true, net: true },
async fn() {
const minLength = 5
const response = await randomWord({
minLength
})
assertGreaterOrEqual(response.word.length, minLength)
console.log(`${response.word}: ${response.word.length}`)
}
})

Deno.test({
name: 'words/randomWord: maxLength',
permissions: { read: true, env: true, net: true },
async fn() {
const maxLength = 5
const response = await randomWord({
maxLength
})
assertLessOrEqual(response.word.length, maxLength)
console.log(`${response.word}: ${response.word.length}`)
}
})
16 changes: 0 additions & 16 deletions lib/tests/randomWord.js

This file was deleted.

59 changes: 59 additions & 0 deletions lib/tests/randomWords-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env -S deno test --allow-read --allow-env --allow-net

import { assertEquals, assertExists, assertGreaterOrEqual, assertLessOrEqual } from 'assert'

import { randomWords } from '../../lib/mod.js'

Deno.test({
name: 'words/randomWords: default',
permissions: { read: true, env: true, net: true },
async fn() {
const response = await randomWords({})

assertExists(response)
assertExists(response[0])
assertExists(response[0].word)
response.forEach((word) => assertExists(word))
response.forEach((word) => assertExists(word.word))

response.forEach((word) => console.log(word.word))
}
})

Deno.test({
name: 'words/randomWords: minLength',
permissions: { read: true, env: true, net: true },
async fn() {
const minLength = 5
const response = await randomWords({
minLength
})
response.forEach((word) => assertGreaterOrEqual(word.word.length, minLength))
response.forEach((word) => console.log(`${word.word}: ${word.word.length}`))
}
})

Deno.test({
name: 'words/randomWords: maxLength',
permissions: { read: true, env: true, net: true },
async fn() {
const maxLength = 5
const response = await randomWords({
maxLength
})
response.forEach((word) => assertLessOrEqual(word.word.length, maxLength))
response.forEach((word) => console.log(`${word.word}: ${word.word.length}`))
}
})

Deno.test({
name: 'words/randomWords: limit',
permissions: { read: true, env: true, net: true },
async fn() {
const limit = 3
const response = await randomWords({
limit
})
assertEquals(response.length, limit)
}
})
6 changes: 3 additions & 3 deletions lib/words/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function randomWord({
minLength,
maxLength
}) {
const wordnik = new Wordnik('words.json/randomWord?')
const wordnik = new Wordnik('words.json/randomWord')
let endpoint =
wordnik.base + wordnik.version + wordnik.target

Expand Down Expand Up @@ -110,7 +110,7 @@ export async function randomWord({
* @example
* ```js
* const words = await randomWords({})
* console.log(words[0]) // prints first random word
* console.log(words[0].word) // prints first random word
* ```
*/
export async function randomWords({
Expand All @@ -127,7 +127,7 @@ export async function randomWords({
sortOrder,
limit
}) {
const wordnik = new Wordnik('words.json/randomWords?')
const wordnik = new Wordnik('words.json/randomWords')
let endpoint =
wordnik.base + wordnik.version + wordnik.target

Expand Down

0 comments on commit d9897a3

Please sign in to comment.