Skip to content

Fix/742 migrate doctest to jest #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b1ad456
Remove QuickSelect doctest
defaude Oct 5, 2021
4b1799e
Remove AverageMedian doctest
defaude Oct 5, 2021
852e16f
Migrate doctest for BinaryExponentiationRecursive.js
defaude Oct 5, 2021
25c29b6
Migrate doctest for EulersTotient.js
defaude Oct 5, 2021
7b8ec07
Migrate doctest for PrimeFactors.js
defaude Oct 5, 2021
e7836e4
Migrate doctest for BogoSort.js
defaude Oct 5, 2021
402ab5c
Migrate doctest for BeadSort.js
defaude Oct 5, 2021
3eff8fd
Migrate doctest for BucketSort.js
defaude Oct 5, 2021
9627cfb
Migrate doctest for CocktailShakerSort.js
defaude Oct 5, 2021
e0e3c19
Migrate doctest for MergeSort.js
defaude Oct 5, 2021
942f9fb
Migrate doctest for QuickSort.js
defaude Oct 5, 2021
3d48cbf
Migrate doctest for ReverseString.js
defaude Oct 5, 2021
dd619c9
Migrate doctest for ReverseString.js
defaude Oct 5, 2021
6b820f3
Migrate doctest for ValidateEmail.js
defaude Oct 5, 2021
db7f626
Migrate doctest for ConwaysGameOfLife.js
defaude Oct 5, 2021
f2aa40c
Remove TernarySearch doctest
defaude Oct 5, 2021
7f5d7fe
Migrate doctest for BubbleSort.js
defaude Oct 5, 2021
c3747e9
Remove doctest from CI and from dependencies
defaude Oct 5, 2021
8274f40
Migrate doctest for RgbHsvConversion.js
defaude Oct 6, 2021
a383356
Add --fix option to "standard" npm script
defaude Oct 6, 2021
bbeeefb
Migrate doctest for BreadthFirstSearch.js
defaude Oct 6, 2021
1244f3c
Migrate doctest for BreadthFirstShortestPath.js
defaude Oct 6, 2021
d019e85
Migrate doctest for EulerMethod.js
defaude Oct 6, 2021
bf68fdd
Migrate doctest for Mandelbrot.js
defaude Oct 6, 2021
cbece80
Migrate doctest for FloodFill.js
defaude Oct 6, 2021
59796a2
Migrate doctest for KochSnowflake.js
defaude Oct 6, 2021
a8ad3a3
Update npm lockfile
defaude Oct 6, 2021
78f5dc4
Update README and COMMITTING with a few bits & bobs regarding testing…
defaude Oct 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/UpdateDirectory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ function pathsToMarkdown (filePaths) {
}

// get paths of all .js files - excluding node_modules, the .github folder, tests and config stuff
globby(['**/*.js', '!(node_modules|.github)/**/*', '!**/*.test.js', '!babel.config.js'])
globby([
'**/*.js',
'!(node_modules|.github)/**/*',
'!**/*.test.js',
'!**/*.manual-test.js',
'!babel.config.js'
])
// create markdown content
.then(pathsToMarkdown)
// write markdown to file
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ jobs:
run: npm ci

- name: 🧪 Run tests
run: |
npm run doctest || true # TODO: Add all doctests
npm test
run: npm test

- name: 💄 Code style
run: npm run style
143 changes: 90 additions & 53 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@

## Before contributing

Welcome to [TheAlgorithms/Javascript](https://github.com/TheAlgorithms/Javascript)! Before sending your pull requests, make sure that you **read the whole guidelines**. If you have any doubt on the contributing guide, please feel free to [state it clearly in an issue](https://github.com/TheAlgorithms/Javascript/issues/new)
Welcome to [TheAlgorithms/Javascript](https://github.com/TheAlgorithms/Javascript)! Before sending your pull requests,
make sure that you **read the whole guidelines**. If you have any doubt on the contributing guide, please feel free to
[state it clearly in an issue](https://github.com/TheAlgorithms/Javascript/issues/new).

## Contributing

### Contributor

We are very happy that you consider implementing algorithms and data structures for others! This repository is referenced and used by learners from around the globe. Being one of our contributors, you agree and confirm that:
We are very happy that you consider implementing algorithms and data structures for others! This repository is
referenced and used by learners from around the globe. Being one of our contributors, you agree and confirm that:

- You did your work - plagiarism is not allowed.
- Any plagiarized work will not be merged.
- Your work will be distributed under [GNU License](LICENSE) once your pull request is merged
- Your submitted work must fulfill our styles and standards
* You did your work - plagiarism is not allowed.
* Any plagiarized work will not be merged.
* Your work will be distributed under [GNU License](LICENSE) once your pull request is merged.
* Your submitted work must fulfill our styles and standards.

**New implementation** is welcome! For example, new solutions to a problem, different representations of a graph data structure or algorithm designs with different complexity.
**New implementation** is welcome! For example, new solutions to a problem, different representations of a graph data
structure or algorithm designs with different complexity.

**Improving comments** and **writing proper tests** are also highly welcome.

### Contribution

We appreciate any contribution, from fixing grammar mistakes to implementing complex algorithms. Please read this section if you are contributing to your work.
We appreciate any contribution, from fixing grammar mistakes to implementing complex algorithms. Please read this
section if you are contributing to your work.


If you submit a pull request that resolves an open issue, please help us to keep our issue list small by adding `fixes: #{$ISSUE_NO}` to your commit message. GitHub will use this tag to auto-close the issue if your PR is merged.
If you submit a pull request that resolves an open issue, please help us to keep our issue list small by adding
`fixes: #{$ISSUE_NO}` to your commit message. GitHub will use this tag to auto-close the issue if your PR is merged.

#### What is an Algorithm?

An Algorithm is one or more functions (or classes) that:

* take one or more inputs,
* perform some internal calculations or data manipulations,
* return one or more outputs,
Expand All @@ -37,66 +43,97 @@ An Algorithm is one or more functions (or classes) that:
Algorithms should be packaged in a way that would make it easy for readers to put them into larger programs.

Algorithms should:

* have intuitive class and function names that make their purpose clear to readers
* use JavaScript naming conventions and intuitive variable names to ease comprehension
* be flexible to take different input values
* raise JavaScript exceptions (RangeError, etc.) on erroneous input values

Algorithms in this repo should not be how-to examples for existing JavaScript packages. Instead, they should perform internal calculations or manipulations to convert input values into different output values. Those calculations or manipulations can use data types, classes, or functions of existing JavaScript packages but each algorithm in this repo should add unique value.
Algorithms in this repo should not be how-to examples for existing JavaScript packages. Instead, they should perform
internal calculations or manipulations to convert input values into different output values. Those calculations or
manipulations can use data types, classes, or functions of existing JavaScript packages but each algorithm in this repo
should add unique value.

#### File Naming Convention
- filenames should use the UpperCamelCase (PascalCase) style.
- There should be no spaces in filenames.
**Example:**`UserProfile.js` is allowed but `userprofile.js`,`Userprofile.js`,`user-Profile.js`,`userProfile.js` are not

* filenames should use the UpperCamelCase (PascalCase) style.
* There should be no spaces in filenames.
* **Example:**`UserProfile.js` is allowed but `userprofile.js`,`Userprofile.js`,`user-Profile.js`,`userProfile.js` are
not.

#### Testing

Be confident that your code works. When was the last time you committed a code change, your build failed, and half of your app stopped working? Mine was last week. Writing tests for our Algorithms will help us ensure the implementations are air tight even after multiple fixes and code changes.
We use a NPM package [doctest](https://www.npmjs.com/package/doctest) to add basic testing functionality to our code. Doctests are simple structured comments that evaluate a function and ensure a required result.
Be confident that your code works. When was the last time you committed a code change, your build failed, and half of
your app stopped working? Mine was last week. Writing tests for our Algorithms will help us ensure the implementations
are air tight even after multiple fixes and code changes.

We use [Jest](https://jestjs.io/) to run unit tests on our algorithms. It provides a very readable and expressive way to
structure your test code.

The implementation of doctest is quite simple. You can easily learn it [here](https://www.npmjs.com/package/doctest).
It is advised that the algorithm file (module) does not contain any "live" code but rather just exports the function(s)
needed to execute the algorithm. Your test code can import those function(s), call them with the appropriate parameters
and inspect the outcome. Example: [RatInAMaze.test.js](Backtracking/tests/RatInAMaze.test.js).

It is advised that you add the Doctests in a multiline comment just after the Algorithm description.
For Code Structure reference see [this file](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BubbleSort.js).
Please refrain from using `console` in your implementation AND test code.

You can run the doctest by using the command
You can (and should!) run all tests locally before committing your changes:

```shell
npm test
```
$ doctest MyFile.js // if that fails, try: npx doctest MyFile.js

If you want save some time and just run a specific test:

```shell
# this will run any test file where the filename matches "koch"
npm test -- koch
```

You can also start Jest in "watch" mode:

```shell
npm test -- --watchAll
```

This will run all tests and watch source and test files for changes. When a change is made, the tests will run again.

#### Coding Style

To maximize the readability and correctness of our code, we require that new submissions follow [JavaScript Standard Style](https://standardjs.com/)
- Command to install JavaScript Standard Style
```
$ npm install standard --save-dev
```
- Usage
```
$ standard MyFile.js // if that fails, try: npx standard MyFile.js
```

- Use camelCase with the leading character as lowercase for identifier names (variables and functions)
- Names start with a letter
- follow code indentation
- Always use 2 spaces for indentation of code blocks
```
function sumOfArray (arrayOfNumbers) {
let sum = 0
for (let i = 0; i < arrayOfNumbers.length; i++) {
sum += arrayOfNumbers[i]
}
return (sum)
}

```
- Avoid using global variables and avoid '=='
- Please use 'let' over 'var'
- Please use 'console.log()'
- We strongly recommend the use of ECMAScript 6
- Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms.
- Most importantly,
- **Be consistent in the use of these guidelines when submitting.**
- Happy coding!
To maximize the readability and correctness of our code, we require that new submissions follow the
[JavaScript Standard Style](https://standardjs.com/).

Before committing, please run

```shell
npm run style
```

in order to apply the coding style (where it can be done automatically). If an error is shown, please figure out what's
wrong, fix it and run standard again.

A few (but not all) of the things to keep in mind:

* Use camelCase with the leading character as lowercase for identifier names (variables and functions)
* Names start with a letter
* Follow code indentation: Always use 2 spaces for indentation of code blocks
```js
function sumOfArray (arrayOfNumbers) {
let sum = 0
for (let i = 0; i < arrayOfNumbers.length; i++) {
sum += arrayOfNumbers[i]
}
return (sum)
}
```
*
* Avoid using global variables and avoid `==`
* Please use `let` over `var`
* Please refrain from using `console.log` or any other console methods
* **Absolutely** don't use `alert`
* We strongly recommend the use of ECMAScript 6
* Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms
* Most importantly:
* **Be consistent in the use of these guidelines when submitting**
* Happy coding!

Writer [@itsvinayak](https://github.com/itsvinayak), May 2020.
57 changes: 4 additions & 53 deletions Cellular-Automata/ConwaysGameOfLife.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ The Game of Life is a cellular automaton devised by the British mathematician Jo
(example adapted from https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py )
*/

/*
* Doctests
*
* > newGeneration([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
* [ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 0, 0, 0 ] ]
*/

/*
* Generates the next generation for a given state of Conway's Game of Life.
*/
function newGeneration (cells) {
/**
* Generates the next generation for a given state of Conway's Game of Life.
*/
export function newGeneration (cells) {
const nextGeneration = []
for (let i = 0; i < cells.length; i++) {
const nextGenerationRow = []
Expand Down Expand Up @@ -46,45 +39,3 @@ function newGeneration (cells) {
}
return nextGeneration
}

/*
* utility function to display a series of generations in the console
*/
async function animate (cells, steps) {
/*
* utility function to print one frame
*/
function printCells (cells) {
console.clear()
for (let i = 0; i < cells.length; i++) {
let line = ''
for (let j = 0; j < cells[i].length; j++) {
if (cells[i][j] === 1) line += '\u2022'
else line += ' '
}
console.log(line)
}
}

printCells(cells)

for (let i = 0; i < steps; i++) {
await new Promise(resolve => setTimeout(resolve, 250)) // sleep
cells = newGeneration(cells)
printCells(cells)
}
}

// Define glider example
const glider = [
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]

animate(glider, 16)
8 changes: 8 additions & 0 deletions Cellular-Automata/ConwaysGameOfLife.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { newGeneration } from './ConwaysGameOfLife'

describe('newGeneration', () => {
it('should produce the next generation according to the rules', () => {
expect(newGeneration([[0, 1, 0], [0, 1, 0], [0, 1, 0]]))
.toEqual([[0, 0, 0], [1, 1, 1], [0, 0, 0]])
})
})
Loading