-
-
Save codytowstik/90ef736965f8d52f6752a62d5435b6ff to your computer and use it in GitHub Desktop.
🤖 Example 3 for 'What is Testing?'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// what_is_testing | |
/** | |
* Calculates the number of the specified pokemon caught given a map representing the current Pokedex state. | |
* | |
* @param {{pokemon: string, count: number}} pokedexState the sample Pokedex state | |
* @param {string} pokemonToCount the list of Pokemon to include in the total count | |
* @returns {number} the total specified Pokemon caught | |
*/ | |
let calculateTotalSpecifiedPokemonCaught = ( pokedexState, ...pokemonToCount ) => { | |
let totalPokemonCaught = 0 | |
for ( let index = 0; index < pokemonToCount.length; index++ ) { | |
let currentPokemon = pokemonToCount[ index ] | |
let currentPokemonCount = pokedexState[ currentPokemon ] | |
// if the pokemon doesn't exist in our Pokedex, it counts as zero | |
currentPokemonCount = isNaN( currentPokemonCount ) ? 0 : currentPokemonCount | |
totalPokemonCaught += currentPokemonCount | |
} | |
return totalPokemonCaught | |
} | |
/** | |
* @param {string} testID a description to match this result to the executed function | |
* @param expected expected result of the test function | |
* @param actual actual result of the test function | |
*/ | |
let checkResults = ( testID, expected, actual ) => { | |
if ( expected === actual ) { | |
console.log( `SUCCESS test '${testID}'` ) | |
} | |
else { | |
// using ES6 template literals to format output string with expected data | |
console.log( `FAILED test '${testID}' -- expected ${expected} but got ${actual}` ) | |
} | |
} | |
//// sample test data | |
let samplePokedexState = { | |
'Pikachu': 4, | |
'JigglyPuff': 2, | |
'MewTwo': 1, | |
'Bulbasaur': 1 | |
} | |
let sampleEmptyPokedexState = {} | |
//// test calls | |
let result; | |
// empty data parameters | |
result = calculateTotalSpecifiedPokemonCaught( sampleEmptyPokedexState ) | |
checkResults( 'empty data', 0, result ) | |
// non-empty Pokedex, looking for Pokemon that exist within the map | |
result = calculateTotalSpecifiedPokemonCaught( samplePokedexState, 'Pikachu', 'Bulbasaur' ) | |
checkResults( 'existing Pokemon', 5, result ) | |
// non-empty Pokedex, looking for Pokemon that don't exist within the map | |
result = calculateTotalSpecifiedPokemonCaught( samplePokedexState, 'Vulpix' ) | |
checkResults( 'missing Pokemon', 0, result ) | |
// non-empty Pokedex, looking for some Pokemon that don't exist within the map, and some that do | |
result = calculateTotalSpecifiedPokemonCaught( samplePokedexState, 'Vulpix', 'Pikachu' ) | |
checkResults( 'existing and missing Pokemon', 4, result ) | |
// SUCCESS test 'empty data' | |
// SUCCESS test 'existing Pokemon' | |
// SUCCESS test 'missing Pokemon' | |
// SUCCESS test 'existing and missing Pokemon' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment