Skip to content

Instantly share code, notes, and snippets.

@darK-Zi0n-te4am-cr3vv
Created November 28, 2012 22:22
Show Gist options
  • Save darK-Zi0n-te4am-cr3vv/4165119 to your computer and use it in GitHub Desktop.
Save darK-Zi0n-te4am-cr3vv/4165119 to your computer and use it in GitHub Desktop.
FCKING AWESOME UNIT TESTS
STRUCT(KT_TEST_CONTEXT)
{
CHAR *CurrentCase;
INT AssertionsCount;
INT FailCount;
};
static KT_TEST_CONTEXT TestContext;
VOID KtAssert(BOOL Condition, CHAR *FailMessage, INT Line)
{
TestContext.AssertionsCount++;
if (Condition)
{
}
else
{
printf("KT: Assertion failed: %s (line %d)\n", FailMessage, Line);
TestContext.FailCount++;
}
}
BOOL KtSetCase(CHAR *Name, CHAR *File, INT Line)
{
if (TestContext.CurrentCase && strcmp(TestContext.CurrentCase, Name) == 0)
{
printf("KT: exiting case %s (%d assertions, %d failed)\n",
TestContext.CurrentCase,
TestContext.AssertionsCount,
TestContext.FailCount);
printf("KT: case %s %s\n", Name, TestContext.FailCount == 0 ? "passed" : "failed");
return FALSE;
}
printf("KT: Entering case %s (%s:%d)\n", Name, File, Line);
TestContext.AssertionsCount = TestContext.FailCount = 0;
TestContext.CurrentCase = Name;
return TRUE;
}
#define KT_ASSERT(expr) do { KtAssert((expr), #expr, __LINE__); } while (0) ;
#define KT_TEST_FIXTURE(name) void KtTestFixture_##name()
#define KT_TEST_CASE(name) while (KtSetCase(#name, __FILE__, __LINE__))
#define KT_RUN_TESTS(name) do { extern void KtTestFixture_##name(); KtTestFixture_##name(); } while (0) ;
KT_TEST_FIXTURE(String)
{
KT_TEST_CASE(Equals)
{
KT_ASSERT(strcmp("xx", "yy") == 0);
}
}
int main(int argc, char **argv)
{
KT_RUN_TESTS(String);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment