Skip to content

Instantly share code, notes, and snippets.

@bakerface
Last active April 25, 2022 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakerface/2b0ace994a834edc1faaedef9d52360e to your computer and use it in GitHub Desktop.
Save bakerface/2b0ace994a834edc1faaedef9d52360e to your computer and use it in GitHub Desktop.
A single-file implementation of the mochajs test suite runner in pure C
/**
* Copyright (c) 2022 Chris Baker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
**/
#include <stdio.h>
#ifndef __MOCHA_H__
#define __MOCHA_H__
#define __MOCHA_STATE_BEFORE 0
#define __MOCHA_STATE_IT 1
#define __MOCHA_STATE_AFTER 2
#define __MOCHA_STATE_FINISHED 3
#define __MOCHA_GROUP_IGNORED 0
#define __MOCHA_GROUP_PASSED 1
#define __MOCHA_GROUP_FAILED 2
typedef struct mocha {
unsigned int state;
unsigned int group;
unsigned int current;
unsigned int last;
unsigned int passed;
unsigned int failed;
unsigned int ignored;
unsigned int asserts;
unsigned int line;
const char *file;
const char *reason;
} mocha_t;
#define mocha_init(mocha) do { \
(mocha)->state = __MOCHA_STATE_BEFORE; \
(mocha)->group = __MOCHA_GROUP_IGNORED; \
(mocha)->current = 0; \
(mocha)->last = 0; \
(mocha)->passed = 0; \
(mocha)->failed = 0; \
(mocha)->ignored = 0; \
(mocha)->asserts = 0; \
(mocha)->line = 0; \
(mocha)->file = 0; \
(mocha)->reason = 0; \
} while (0)
#define mocha_describe(mocha, what) \
for ((mocha)->state = __MOCHA_STATE_BEFORE, \
(mocha)->current = 0, \
(mocha)->last = 0, \
puts(what); \
(__MOCHA_STATE_FINISHED == (mocha)->state) ? (puts(""), 0) : 1; )
#define mocha_before_each(mocha) \
for (; __MOCHA_STATE_BEFORE == (mocha)->state; \
(mocha)->state = __MOCHA_STATE_IT)
#define mocha_it(mocha, should) \
for ((mocha)->group = __MOCHA_GROUP_IGNORED, \
(mocha)->last = ((mocha)->last < __LINE__ \
? __LINE__ : (mocha)->last); \
__MOCHA_STATE_IT == (mocha)->state \
&& (mocha)->current < __LINE__ \
&& (__LINE__ == ((mocha)->current = __LINE__)); \
(mocha)->state = __MOCHA_STATE_AFTER, \
((mocha)->group == __MOCHA_GROUP_IGNORED \
? ((mocha)->ignored++, puts(" ! " should), __MOCHA_GROUP_IGNORED) \
: ((mocha)->group == __MOCHA_GROUP_PASSED \
? ((mocha)->passed++, puts(" ✓ " should), __MOCHA_GROUP_PASSED) \
: ((mocha)->failed++, puts(" ✗ " should), \
printf(" Assertion `%s` failed. (%s:%d)\r\n", \
(mocha)->reason, (mocha)->file, (mocha)->line), \
__MOCHA_GROUP_FAILED))))
#define mocha_after_each(mocha) \
for (; __MOCHA_STATE_AFTER == (mocha)->state; \
(mocha)->state = ((mocha)->current == (mocha)->last) \
? __MOCHA_STATE_FINISHED : __MOCHA_STATE_BEFORE)
#define mocha_assert(mocha, condition) \
if (!(condition)) { \
(mocha)->asserts++; \
(mocha)->group = __MOCHA_GROUP_FAILED; \
(mocha)->reason = #condition; \
(mocha)->file = __FILE__; \
(mocha)->line = __LINE__; \
continue; \
} else { \
(mocha)->asserts++; \
(mocha)->group = __MOCHA_GROUP_PASSED; \
}
#define mocha_print(mocha) \
printf("%u passed, %u failed, %u ignored, %u assertions\r\n", \
(mocha)->passed, \
(mocha)->failed, \
(mocha)->ignored, \
(mocha)->asserts \
);
#define mocha_exit_code(mocha) ((mocha)->failed ? 1 : 0)
#endif /* __MOCHA_H__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment