chore: merge Fix/742 migrate doctest to jest (#749)

* Remove QuickSelect doctest

There are more Jest test cases already.

* Remove AverageMedian doctest

Already migrated to jest

* Migrate doctest for BinaryExponentiationRecursive.js

(also remove inline "main" test method)

* Migrate doctest for EulersTotient.js

(also remove inline "main" test method)

* Migrate doctest for PrimeFactors.js

(also remove inline "main" test method)

* Migrate doctest for BogoSort.js

Re-write prototype-polluting helper methods, too.

(also remove inline test driver code)

* Migrate doctest for BeadSort.js

(also remove inline test driver code)

* Migrate doctest for BucketSort.js

(also remove inline test driver code)

* Migrate doctest for CocktailShakerSort.js

(also remove inline test driver code)

* Migrate doctest for MergeSort.js

(also remove inline test driver code)

* Migrate doctest for QuickSort.js

(also remove inline test driver code)

* Migrate doctest for ReverseString.js

(also remove inline test driver code)

* Migrate doctest for ReverseString.js

* Migrate doctest for ValidateEmail.js

* Migrate doctest for ConwaysGameOfLife.js

(remove the animate code, too)

* Remove TernarySearch doctest

Already migrated to jest

* Migrate doctest for BubbleSort.js

(also remove inline test driver code)

* Remove doctest from CI and from dependencies

relates to #742
fixes #586

* Migrate doctest for RgbHsvConversion.js

* Add --fix option to "standard" npm script

* Migrate doctest for BreadthFirstSearch.js

(also remove inline test driver code)

* Migrate doctest for BreadthFirstShortestPath.js

(also remove inline test driver code)

* Migrate doctest for EulerMethod.js

(also remove inline test driver code)

Move manual test-code for plotting stuff in the browser in a distinct file, too. Those "*.manual-test.js" files are excluded from the UpdateDirectory.mjs script, as well.

* Migrate doctest for Mandelbrot.js

(also remove inline test driver code & moved manual drawing test into a *.manual-test.js)

* Migrate doctest for FloodFill.js

* Migrate doctest for KochSnowflake.js

(also move manual drawing test into a *.manual-test.js)

* Update npm lockfile

* Update README and COMMITTING with a few bits & bobs regarding testing & code quality
This commit is contained in:
Roland Hummel
2021-10-07 09:03:38 +02:00
committed by GitHub
parent 6eeb989930
commit b13b12e88c
53 changed files with 882 additions and 13514 deletions

View File

@ -1,26 +1,5 @@
/**
* A short example showing how to reverse a string
* @flow
*/
/**
* Create a new string and append
* @complexity O(n)
*
* Doctests
*
* > ReverseStringIterative('some')
* 'emos'
* > ReverseStringIterative('string')
* 'gnirts'
* > ReverseStringIterative('The Algorithms Javascript')
* 'tpircsavaJ smhtiroglA ehT'
* > ReverseStringIterative([])
* ! TypeError
* > ReverseStringIterative({})
* ! TypeError
* > ReverseStringIterative(null)
* ! TypeError
* A short example showing how to reverse a string.
*/
function ReverseStringIterative (string) {
if (typeof string !== 'string') {
@ -40,34 +19,17 @@ function ReverseStringIterative (string) {
* JS disallows string mutation so we're actually a bit slower.
*
* @complexity O(n)
*
* 'some' -> 'eoms' -> 'emos'
*
* Doctests
*
* > ReverseStringIterativeInplace('some')
* 'emos'
* > ReverseStringIterativeInplace('string')
* 'gnirts'
* > ReverseStringIterativeInplace('The Algorithms Javascript')
* 'tpircsavaJ smhtiroglA ehT'
* > ReverseStringIterativeInplace([])
* ! TypeError
* > ReverseStringIterativeInplace({})
* ! TypeError
* > ReverseStringIterativeInplace(null)
* ! TypeError
*/
function ReverseStringIterativeInplace (string) {
if (typeof string !== 'string') {
throw new TypeError('The given value is not a string')
}
const _string = string.split('')
for (let i = 0; i < Math.floor(_string.length / 2); i++) {
const first = _string[i]
const second = _string[_string.length - 1 - i]
_string[i] = second
_string[i] = _string[_string.length - 1 - i]
_string[_string.length - 1 - i] = first
}

View File

@ -1,19 +1,3 @@
/*
* Doctests
*
* > reverseWords('I Love JS')
* 'JS Love I'
* > reverseWords('Hello World')
* 'World Hello'
* > reverseWords('The Algorithms Javascript')
* 'Javascript Algorithms The'
* > reverseWords([])
* ! TypeError
* > reverseWords({})
* ! TypeError
* > reverseWords(null)
* ! TypeError
*/
const reverseWords = (str) => {
if (typeof str !== 'string') {
throw new TypeError('The given value is not a string')

View File

@ -1,22 +1,6 @@
/*
Function that takes a string input and return either true or false
If it is a valid email address
*/
/*
* Doctests
*
* > validateEmail('mahfoudh.arous@gmail.com')
* true
* > validateEmail('mahfoudh.arous@helsinki.edu')
* true
* > validateEmail('mahfoudh.arous.com')
* false
* > validateEmail('')
* ! TypeError
* > validateEmail(null)
* ! TypeError
*/
/**
* Returns whether the given string is a valid email address or not.
*/
const validateEmail = (str) => {
if (str === '' || str === null) {
throw new TypeError('Email Address String Null or Empty.')

View File

@ -1,63 +1,65 @@
import {
ReverseStringIterative,
ReverseStringIterativeInplace
} from '../ReverseString'
import { ReverseStringIterative, ReverseStringIterativeInplace } from '../ReverseString'
describe('ReverseStringIterative', () => {
it('expects to reverse a simple string', () => {
const SUT = ReverseStringIterative('reverse')
expect(SUT).toEqual('esrever')
expect(ReverseStringIterative('reverse')).toEqual('esrever')
expect(ReverseStringIterative('some')).toEqual('emos')
expect(ReverseStringIterative('string')).toEqual('gnirts')
expect(ReverseStringIterative('The Algorithms Javascript')).toEqual('tpircsavaJ smhtiroglA ehT')
})
it('expects to reverse a string with spaces in between', () => {
const SUT = ReverseStringIterative('reverse me')
expect(SUT).toEqual('em esrever')
expect(ReverseStringIterative('reverse me')).toEqual('em esrever')
})
it('expects to reverse a simple string without capitalizing the first letter', () => {
const SUT = ReverseStringIterative('Javascript')
expect(SUT).toEqual('tpircsavaJ')
expect(ReverseStringIterative('Javascript')).toEqual('tpircsavaJ')
})
it.each`
input
${123456}
${[1, 2, 3, 4, 5, 6]}
${{ test: 'test' }}
${null}
`(
'expects to throw a type error given a value that is $input',
({ input }) => {
expect(() => {
ReverseStringIterative(input)
}).toThrow('The given value is not a string')
expect(() => ReverseStringIterative(input)).toThrow('The given value is not a string')
}
)
it('expects to return a empty string with an empty string is given', () => {
const SUT = ReverseStringIterative('')
expect(SUT).toEqual('')
expect(ReverseStringIterative('')).toEqual('')
})
})
describe('ReverseStringIterativeInplace', () => {
it('expects to reverse a simple string', () => {
const SUT = ReverseStringIterativeInplace('reverse')
expect(SUT).toEqual('esrever')
expect(ReverseStringIterativeInplace('reverse')).toEqual('esrever')
expect(ReverseStringIterativeInplace('some')).toEqual('emos')
expect(ReverseStringIterativeInplace('string')).toEqual('gnirts')
expect(ReverseStringIterativeInplace('The Algorithms Javascript')).toEqual('tpircsavaJ smhtiroglA ehT')
})
it('expects to reverse a simple string without capitalizing the first letter', () => {
const SUT = ReverseStringIterativeInplace('Javascript')
expect(SUT).toEqual('tpircsavaJ')
expect(ReverseStringIterativeInplace('Javascript')).toEqual('tpircsavaJ')
})
it('expects to return an empty string given an empty string', () => {
const SUT = ReverseStringIterativeInplace('Javascript')
expect(SUT).toEqual('tpircsavaJ')
expect(ReverseStringIterativeInplace('Javascript')).toEqual('tpircsavaJ')
})
it.each`
input
${123456}
${[1, 2, 3, 4, 5, 6]}
${{ test: 'test' }}
${null}
`(
'expects to throw a type error given a value that is $input',
({ input }) => {
expect(() => {
ReverseStringIterativeInplace(input)
}).toThrow('The given value is not a string')
expect(() => ReverseStringIterativeInplace(input)).toThrow('The given value is not a string')
}
)
})

View File

@ -2,14 +2,17 @@ import { reverseWords } from '../ReverseWords'
describe('reverseWords', () => {
it('expects to reverse words to return a joined word', () => {
const SUT = reverseWords('I Love JS')
expect(SUT).toBe('JS Love I')
expect(reverseWords('I Love JS')).toBe('JS Love I')
expect(reverseWords('Hello World')).toBe('World Hello')
expect(reverseWords('The Algorithms Javascript')).toBe('Javascript Algorithms The')
})
it.each`
input
${123456}
${[1, 2, 3, 4, 5, 6]}
${{ test: 'test' }}
${null}
`(
'expects to throw a type error given a value that is $input',
({ input }) => {

View File

@ -19,5 +19,6 @@ describe('Validation of an Email Address', () => {
it('expects to throw a type error', () => {
expect(() => { validateEmail('') }).toThrow('Email Address String Null or Empty.')
expect(() => { validateEmail(null) }).toThrow('Email Address String Null or Empty.')
})
})