test: move all module tests to the test/ directory
This commit is contained in:
138
test/term/errors.c
Normal file
138
test/term/errors.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <blue/core/error.h>
|
||||
#include <blue/term/print.h>
|
||||
#include <stdio.h>
|
||||
|
||||
enum sample_code {
|
||||
SAMPLE_OK = 0,
|
||||
SAMPLE_ERR_IO_FAILURE,
|
||||
SAMPLE_ERR_FILE_READ_FAILED,
|
||||
};
|
||||
|
||||
enum sample_msg {
|
||||
SAMPLE_MSG_SUCCESS = 0,
|
||||
SAMPLE_MSG_A_TEMPLATED_MSG,
|
||||
};
|
||||
|
||||
static const b_error_definition sample_errors[] = {
|
||||
B_ERROR_DEFINITION(SAMPLE_OK, "OK", "Success"),
|
||||
B_ERROR_DEFINITION(SAMPLE_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"),
|
||||
B_ERROR_DEFINITION_TEMPLATE(
|
||||
SAMPLE_ERR_FILE_READ_FAILED, "FILE_READ_FAILED",
|
||||
"Failed to read file @i[filepath]",
|
||||
B_ERROR_TEMPLATE_PARAM(
|
||||
"filepath", B_ERROR_TEMPLATE_PARAM_STRING, "%s")),
|
||||
};
|
||||
|
||||
static const b_error_msg sample_error_msg[] = {
|
||||
B_ERROR_MSG_TEMPLATE(
|
||||
SAMPLE_MSG_A_TEMPLATED_MSG, "A templated message: @e[param1]",
|
||||
B_ERROR_TEMPLATE_PARAM(
|
||||
"param1", B_ERROR_TEMPLATE_PARAM_STRING, "%s")),
|
||||
};
|
||||
|
||||
static const char *sample_code_to_string(
|
||||
const struct b_error_vendor *vendor, b_error_status_code code)
|
||||
{
|
||||
switch (code) {
|
||||
case SAMPLE_OK:
|
||||
return "OK";
|
||||
case SAMPLE_ERR_IO_FAILURE:
|
||||
return "IO_FAILURE";
|
||||
case SAMPLE_ERR_FILE_READ_FAILED:
|
||||
return "FILE_READ_FAILED";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static b_error_vendor sample_vendor = {
|
||||
.v_name = "Sample",
|
||||
.v_error_definitions = sample_errors,
|
||||
.v_error_definitions_length = sizeof sample_errors,
|
||||
.v_msg = sample_error_msg,
|
||||
.v_msg_length = sizeof sample_error_msg,
|
||||
};
|
||||
|
||||
static b_result error_return_3(void)
|
||||
{
|
||||
b_result err = b_error_with_string(
|
||||
&sample_vendor, SAMPLE_ERR_IO_FAILURE,
|
||||
"I/O failure while reading file");
|
||||
|
||||
b_error_add_submsg_string(
|
||||
err, B_ERROR_SUBMSG_ERROR, "An @e{error} message");
|
||||
b_error_add_submsg_string(
|
||||
err, B_ERROR_SUBMSG_WARNING, "A @w{warning} message");
|
||||
b_error_add_submsg_template(
|
||||
err, B_ERROR_SUBMSG_WARNING, SAMPLE_MSG_A_TEMPLATED_MSG,
|
||||
B_ERROR_PARAM("param1", "Hello!"));
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static b_result error_return_2(void)
|
||||
{
|
||||
return b_result_propagate(error_return_3());
|
||||
}
|
||||
|
||||
static b_result error_return_1(void)
|
||||
{
|
||||
return b_result_propagate(error_return_2());
|
||||
}
|
||||
|
||||
struct param {
|
||||
const char *name;
|
||||
int value;
|
||||
};
|
||||
|
||||
#define PARAM(n, v) \
|
||||
(struct param) \
|
||||
{ \
|
||||
.name = (n), .value = (v) \
|
||||
}
|
||||
|
||||
static void __test(struct param params[])
|
||||
{
|
||||
for (size_t i = 0; params[i].name; i++) {
|
||||
printf("%s = %d\n", params[i].name, params[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
#define test(...) __test((struct param[]) {__VA_ARGS__, {}})
|
||||
|
||||
static b_result some_operation(void)
|
||||
{
|
||||
b_result result = error_return_2();
|
||||
if (b_result_is_error(result)) {
|
||||
b_result err = b_error_with_template(
|
||||
&sample_vendor, SAMPLE_ERR_FILE_READ_FAILED,
|
||||
B_ERROR_PARAM("filepath", "src/Manifest.json"));
|
||||
b_error_add_submsg_string(
|
||||
err, B_ERROR_SUBMSG_INFO,
|
||||
"An @i{informational} message");
|
||||
|
||||
b_error_caused_by_b_status(result, B_ERR_IO_FAILURE);
|
||||
b_result err2 = b_error_caused_by(err, result);
|
||||
|
||||
return err2;
|
||||
}
|
||||
|
||||
return B_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_set_error_report_function(b_enhanced_error_reporter, B_ERROR_REPORT_ALL);
|
||||
|
||||
test(PARAM("Hello", 1), PARAM("Goodbye", 2));
|
||||
|
||||
b_result err;
|
||||
if (B_CATCH(err, some_operation())) {
|
||||
b_throw(err);
|
||||
}
|
||||
|
||||
b_throw_status(B_ERR_INVALID_ARGUMENT);
|
||||
b_throw_status_string(B_ERR_INVALID_ARGUMENT, "Hello!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
79
test/term/printing.c
Normal file
79
test/term/printing.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <blue/term/tty.h>
|
||||
#include <blue/term/print.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define F_GREEN "[green]"
|
||||
#define F_YELLOW "[yellow]"
|
||||
#define F_RESET "[reset]"
|
||||
|
||||
static const char *text = F_YELLOW
|
||||
"But I must " F_GREEN "explain " F_YELLOW "to you " F_GREEN
|
||||
"how all this " F_YELLOW "mistaken idea of" F_RESET
|
||||
" denouncing pleasure " F_GREEN "and praising pain was born " F_YELLOW
|
||||
"and I will give you " F_RESET "a complete account of " F_YELLOW
|
||||
"the system, and " F_RESET
|
||||
"expound the actual teachings of the great explorer of the truth, the "
|
||||
"master-builder of human happiness.\n"
|
||||
"No one rejects, dislikes, or avoids pleasure itself, because it is "
|
||||
"pleasure, but because those who do not know how to pursue pleasure "
|
||||
"rationally encounter consequences that are extremely painful. Nor "
|
||||
"again is there anyone who loves or pursues or desires to obtain pain "
|
||||
"of itself, because it is pain, but because occasionally circumstances "
|
||||
"occur in which toil and pain can procure him some great pleasure.\n"
|
||||
"To take a trivial example, which of us ever undertakes laborious "
|
||||
"physical exercise, except to obtain some advantage from it? But who "
|
||||
"has any right to find fault with a man who chooses to enjoy a "
|
||||
"pleasure that has no annoying consequences, or one who avoids a pain "
|
||||
"that produces no resultant pleasure? On the other hand, we denounce "
|
||||
"with righteous indignation and dislike men who are so beguiled and "
|
||||
"demoralized by the charms of pleasure of the moment, so blinded by "
|
||||
"desire, that they cannot foresee.";
|
||||
|
||||
static const char *text2
|
||||
= "But I must explain to you how all this mistaken idea of denouncing "
|
||||
"pleasure and praising pain was born and I will give you a complete "
|
||||
"account of the system, and expound the actual teachings of the "
|
||||
"great explorer of the truth, the master-builder of human "
|
||||
"happiness.\n"
|
||||
"No one rejects, dislikes, or avoids pleasure itself, because it is "
|
||||
"pleasure, but because those who do not know how to pursue pleasure "
|
||||
"rationally encounter consequences that are extremely painful. Nor "
|
||||
"again is there anyone who loves or pursues or desires to obtain "
|
||||
"pain of itself, because it is pain, but because occasionally "
|
||||
"circumstances occur in which toil and pain can procure him some "
|
||||
"great pleasure.\n"
|
||||
"To take a trivial example, which of us ever undertakes laborious "
|
||||
"physical exercise, except to obtain some advantage from it? But who "
|
||||
"has any right to find fault with a man who chooses to enjoy a "
|
||||
"pleasure that has no annoying consequences, or one who avoids a "
|
||||
"pain that produces no resultant pleasure? On the other hand, we "
|
||||
"denounce with righteous indignation and dislike men who are so "
|
||||
"beguiled and demoralized by the charms of pleasure of the moment, "
|
||||
"so blinded by desire, that they cannot foresee.";
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *s = "[magenta,uline]Hello, [bright_magenta]world![reset]";
|
||||
b_puts(s);
|
||||
b_putc('\n');
|
||||
|
||||
b_string *str = b_string_create_from_cstr(s);
|
||||
size_t len = b_string_get_size(str, B_STRLEN_IGNORE_MOD);
|
||||
printf("length = %zu\n", len);
|
||||
|
||||
b_paragraph_format format = { 0 };
|
||||
format.p_left_margin = 5;
|
||||
format.p_right_margin = 5;
|
||||
format.p_flags = B_PARAGRAPH_DOUBLE_LINE_BREAK;
|
||||
|
||||
b_print_paragraph(text, b_stdtty, &format);
|
||||
|
||||
b_i("An informational message\n\nWith multiple lines");
|
||||
b_warn("A warning message\nWith multiple lines");
|
||||
b_err("An error message\nWith multiple lines");
|
||||
|
||||
b_printf("[red]formatting ignored: '%s'[reset]\n[dark_grey]dark text[reset]\n", "[blue]wow![reset]");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user