javascript - Testing/validating/evaluating the outcome of every path in a function? -
disclaimer - i've tried finding answer via google/stackoverflow, don't know how define problem (i don't know proper term)
i have many small ai snippets such follows. there ._ai
snippet (like below) per enemy type, 1 function next()
called finite state machine in main game loop (fyi: next
function doesn't called every update iteration, when enemy shifted queue).
the question: how test every case (taking account enemy ai snippets might more complex, having cases may occur 1 in 1000 turns) , ensure code valid?
in example below, if added line blabla/1
under count++, error might not crop long time, javascript interpreter won't catch error until hits particular path. in compiled languages, adding garbage such blabla/1
caught @ compile time.
// ai snippet this._ai = (function(commands){ var count = 0; return { next: function(ondone, goodies, baddies) { // if internal counter reaches // 2, launch super attack , // reset count if(count >= 2) { commands.super(ondone); count = 0; } else { // if not performing super attack // there 50% chance of calling // `attack` command if(chance(50)) { var target = goodies[0]; commands.attack(ondone, target); } // or 50% chance of calling // `charge` command else { commands.charge(ondone); count++; } } } }; })(this._commands);
i rig random generator return table of values 0-n
, run next
1000's of times against each number. don't feel concretely tell me every path error free.
as say, unit tests must test every path sure works well.
but should able decide path method follow before calling on tests, you're able know if method behaviour expected one, , if there error.
so, example, if there path followed in 1 of every 1000 executions, shouldn't need test 0, 1, 2 ... 999 cases. 1 combination of results behave distinctly.
for example, in snippet shown have these cases:
- the counter has reached 2
- the counter has not reached 2 , chance returns true
- the counter has not reached 2 , chance returns false
one way archieve taking control of counter , of chance method mocking them.
if want know happens when counter has reached 2 , next method called, pass counter 2 , call next. don't need reach 2 on counter passing code.
as randomizer, don't need try until randomizer returns value want test. make mock , configure behave need each case.
i hope helps.
Comments
Post a Comment