11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// This test ensures that assert.CallTracker.verify() works as intended.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst tracker = new assert.CallTracker();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst generic_msg = 'Functions were not called the expected number of times';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction foo() {}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction bar() {}
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst callsfoo = tracker.calls(foo, 1);
161cb0ef41Sopenharmony_ciconst callsbar = tracker.calls(bar, 1);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Expects an error as callsfoo() and callsbar() were called less than one time.
191cb0ef41Sopenharmony_ciassert.throws(
201cb0ef41Sopenharmony_ci  () => tracker.verify(),
211cb0ef41Sopenharmony_ci  { message: generic_msg }
221cb0ef41Sopenharmony_ci);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cicallsfoo();
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Expects an error as callsbar() was called less than one time.
271cb0ef41Sopenharmony_ciassert.throws(
281cb0ef41Sopenharmony_ci  () => tracker.verify(),
291cb0ef41Sopenharmony_ci  { message: 'Expected the bar function to be executed 1 time(s) but was executed 0 time(s).' }
301cb0ef41Sopenharmony_ci);
311cb0ef41Sopenharmony_cicallsbar();
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci// Will throw an error if callsfoo() and callsbar isn't called exactly once.
341cb0ef41Sopenharmony_citracker.verify();
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst callsfoobar = tracker.calls(foo, 1);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cicallsfoo();
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci// Expects an error as callsfoo() was called more than once and callsfoobar() was called less than one time.
411cb0ef41Sopenharmony_ciassert.throws(
421cb0ef41Sopenharmony_ci  () => tracker.verify(),
431cb0ef41Sopenharmony_ci  { message: generic_msg }
441cb0ef41Sopenharmony_ci);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cicallsfoobar();
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// Expects an error as callsfoo() was called more than once
501cb0ef41Sopenharmony_ciassert.throws(
511cb0ef41Sopenharmony_ci  () => tracker.verify(),
521cb0ef41Sopenharmony_ci  { message: 'Expected the foo function to be executed 1 time(s) but was executed 2 time(s).' }
531cb0ef41Sopenharmony_ci);
54