Files
c2/v0/test.h
T
2026-04-29 10:59:06 +02:00

95 lines
2.4 KiB
C

/**
* Contains test assertions routines.
*/
#ifndef TEST_H
#define TEST_H
#include "token.h"
#include "ast.h"
typedef void (*Test)(void);
/**
* Fails a test.
* @param msg The message to print to the console.
*/
void fail(const char* msg);
/**
* Asserts that a pointer is not null.
*
* Calls `fail` if the assertion does not hold.
*
* @param ptr The pointer to test.
* @param msg The message to print if the pointer is null.
*/
void assert_not_null(void* ptr, const char* msg);
/**
* Asserts that a string has the expected value.
*
* Calls `fail` if the assertion does not hold.
*
* @param expected The expected value. This is typically a string literal.
* @param actual The actual value. This is typically an expression.
* @param msg The message to print if these do not match.
*/
void assert_str(const char* expected, const char* actual, const char* msg);
/**
* Asserts that a string has the expected value.
*
* Calls `fail` if the assertion does not hold.
*
* @param expected The expected value. This is typically a string literal.
* @param actual The actual value. This is typically an expression.
* @param msg The message to print if these do not match.
*/
void assert_string(const char* expected, String actual, const char* msg);
/**
* Asserts that the logged output matches the expected value.
*/
void assert_log(const char* expected, const char* msg);
/**
* Asserts that the logged output matches the content of the file `v0/tests/xyz.log`, where xyz is the test name.
* If GENERATE_GOLDEN=1, the file is overwritten with the actual output.
*/
void assert_log_file(const char* msg);
/**
* Asserts that two integers are equal.
*/
void assert_int(int expected, int actual, const char* msg);
/**
* Asserts that a condition is true.
*/
#include "bool.h"
void assert_true(bool condition, const char* msg);
/**
* Asserts that a condition is false.
*/
void assert_false(bool condition, const char* msg);
/**
* Get the token stream used for this test.
* It reads from the `v0/tests/xyz.c2` file, where xyz is the test name.
*
* At the end of the test, the tokenstream will be freed automatically by the test harness.
*/
TokenStream* test_get_tokenstream(void);
/**
* Gets a parsed module for the this test.
* It reads from the `v0/tests/xyz.c2` file, where xyz is the test name.
*
* At the end of the test, the AST will be freed automatically by the test harness.
*/
Module* test_get_ast(void);
#endif