5th homework assignment for Pirple's NodeJS master class. This project creates unit tests for two simple libraries: math and lang.
- Tests and test runner are within /test folder, libraries are within /app folder.
- lang and math libraries both contain simple functions.
- Tests are contained in test/lang_unit.js and test/math_unit.js. They check if the library functions return the correct values, that they throw or do not throw errors depending on the parameters passed in.
See the tests in action and watch the code being explained line by line
- Download the project.
- Open the command prompt (for Windows, click Start icon and type in
cmd
) and go to the project directory.eg. :
cd C:/NodeJSTests
- Run the test runner:
node test
Optionally, one can set DEBUG variable to print out messages in the console. (for Windows):
set DEBUG=* & node test // Print out all debug messages
set DEBUG=helpers & node test // Print out debug messages coming from helpers module
Function | Parameters | Description |
---|---|---|
findDivisors |
number |
Returns an array of 2 greatest divisors for a given number. If number is invalid, TypeError is thrown. If number is 1, array [1] is returned. |
getANumber |
min , max |
Returns a random number between min and max. If min is invalid, it is set to 1. If max is invalid, it is set to 10. If min is greater than max , then they are set to 1 and 10 respectively. |
Test name | Function parameters | Expected function output |
---|---|---|
math.findDivisors returns [5,6] for 30 |
30 | [5,6] |
math.findDivisors returns [1] for 1 |
1 | [1] |
math.findDivisors throws exception for a non-number |
'cat' | TypeError |
math.getANumber returns a random number between -100 and 100 |
-100,100 | a number between -100 and 100 |
math.getANumber works when min > max |
1000, 1 | a number |
math.getANumber does not throw exception for a non-number |
'cat', null | a number |
Function | Parameters | Description |
---|---|---|
conjugateRegularPreterite |
verb ,person |
Returns a simple past form (preterite) of the verb passed in, eg. for ('cominar','tu') the result is 'caminaste'. If the verb is not in infinitive form (ends with -ar,-er,-ir), a custom VerbNotInfinitiveError is thrown. If verb is not a valid string, a custom LangError is thrown. person argument must be one of: yo , tu , el_ella , nosotros , vosotros , ellos_ellas , otherwise yo form is returned. |
getOrdinalNumber |
number , noun |
Returns an ordinal number as a string in Spanish and the noun passed. Eg. for (4,'gato') result is 'cuatro gato'; for (10,'sandía') the result is 'décima sandía'. If the arguments are not number and noun respectively, a custom LangError is thrown. |
Test name | Function parameters | Expected function output |
---|---|---|
lang.conjugateRegularPreterite should conjugate correctly |
'beber', 'el_ella' | 'bebió' |
lang.conjugateRegularPreterite throws VerbNotInfinitiveError if verb is not an infinitive |
'xxx', 'yo' | VerbNotInfinitiveError |
lang.conjugateRegularPreterite should return yo form if person argument is incorrect |
'cantar', 'not_existing_personal_pronoun' | 'canté' |
lang.conjugateRegularPreterite should return yo form if person argument is missing |
'cantar' | 'canté' |
lang.conjugateRegularPreterite throws LangError if arguments are of incorrect types |
123, 123 | LangError |
lang.getOrdinalNumber works for singular nouns |
2, 'manzana' | 'segunda manzana' |
lang.getOrdinalNumber works for plural nouns |
10, 'aniversarios' | 'décimos aniversarios' |