partially testing run_test()

This commit is contained in:
jlamothe 2023-11-09 20:48:51 +00:00
parent 1e5423b564
commit c2f8972d9c
5 changed files with 177 additions and 7 deletions

View File

@ -22,7 +22,7 @@
#include <libc.h> #include <libc.h>
#include "../9unit.h" #include "../9unit.h"
#include "macros.h" #include "util.h"
#include "initial-state.h" #include "initial-state.h"
// Internal Prototypes // Internal Prototypes

View File

@ -16,8 +16,8 @@
</$objtype/mkfile </$objtype/mkfile
HFILES=../9unit.h macros.h initial-state.h run-test.h HFILES=../9unit.h util.h initial-state.h run-test.h
OFILES=initial-state.$O run-test.$O OFILES=util.$O initial-state.$O run-test.$O
LIB=../9unit.a LIB=../9unit.a
TEST=tests TEST=tests

View File

@ -22,11 +22,14 @@
#include <libc.h> #include <libc.h>
#include "../9unit.h" #include "../9unit.h"
#include "util.h"
#include "run-test.h" #include "run-test.h"
// Internal Prototypes // Internal Prototypes
static void test_passing(TestState *); static void test_passing(TestState *);
static void mk_sample_state(TestState *);
decl_test(always_passes);
// Public Functions // Public Functions
@ -39,12 +42,45 @@ test_run_test(TestState *s)
// Internal Functions // Internal Functions
static static void
void test_passing(TestState *s) test_passing(TestState *s)
{ {
void *oldptr = s->ptr; TestState expected, actual;
print("\tpassing\n"); print("\tpassing\n");
s->ptr = oldptr;
// expected result
memset(&expected, 0, sizeof(TestState));
expected.passed = 2;
expected.failed = 2;
expected.pending = 3;
expected.run = 7;
// actual result
mk_sample_state(&actual);
run_test(&actual, always_passes);
compare_states(
s,
"\t\t",
"ERROR: run_test(): passing:",
&expected,
&actual
);
}
static void
mk_sample_state(TestState *s)
{
memset(s, 0, sizeof(TestState));
s->passed = 1;
s->failed = 2;
s->pending = 3;
s->run = 6;
}
decl_test(always_passes)
{
return test_success;
} }
//jl //jl

123
test/util.c Normal file
View File

@ -0,0 +1,123 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "../9unit.h"
#include "util.h"
// Local Types
typedef struct CompareInts CompareInts;
struct CompareInts
{
const char *context;
int expected;
int actual;
};
// Local Prototypes
// compare the run value of two states
static void compare_run(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare two ints
static void compare_ints(
TestState *,
const char *,
int,
int
);
decl_test(compare_ints_test);
// Public Functions
void
compare_states(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
compare_run(s, prefix, context, expected, actual);
}
// Local Functions
static void
compare_run(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("run\n");
snprintf(full_context, STR_BUF_SIZE, "%s run:", context);
compare_ints(s, full_context, expected->run, actual->run);
}
static void
compare_ints(
TestState *s,
const char *context,
int expected,
int actual
)
{
void *old_ptr = s->ptr;
CompareInts ci;
ci.context = context;
ci.expected = expected;
ci.actual = actual;
s->ptr = &ci;
run_test(s, compare_ints_test);
s->ptr = old_ptr;
}
def_test(compare_ints_test, s)
{
const CompareInts *ci = s->ptr;
if (ci->actual == ci->expected) return test_success;
char str[STR_BUF_SIZE];
append_test_log(s, ci->context);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->actual);
append_test_log(s, str);
return test_failure;
}
//jl

View File

@ -18,7 +18,18 @@
*/ */
#define STR_BUF_SIZE 256 // buffer size for constructing arbitrary strings
#define decl_test(n) static TestResult n(TestState *) #define decl_test(n) static TestResult n(TestState *)
#define def_test(n, s) static TestResult n(TestState *s) #define def_test(n, s) static TestResult n(TestState *s)
// compares two TestState values
extern void compare_states(
TestState *, // the state we are *actually* updating ;)
const char *, // prefix for each status line
const char *, // context for errors
const TestState *, // expected state
const TestState * // actual state
);
//jl //jl