Skip to content

Instantly share code, notes, and snippets.

@codytowstik
Created March 10, 2020 18:52
Show Gist options
  • Save codytowstik/9b62548492eb3c4212b9ee6814915eb1 to your computer and use it in GitHub Desktop.
Save codytowstik/9b62548492eb3c4212b9ee6814915eb1 to your computer and use it in GitHub Desktop.
🤖 Example 2 for 'What is Testing?'
// 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 ]
totalPokemonCaught += pokedexState[ currentPokemon ]
}
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'
// FAILED test 'missing Pokemon' -- expected 0 but got NaN
// FAILED test 'existing and missing Pokemon' -- expected 4 but got NaN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment