Compare commits
5 Commits
3900a39205
...
e9ed464b60
Author | SHA1 | Date | |
---|---|---|---|
e9ed464b60 | |||
9f26846a3d | |||
f2da510a6e | |||
b0a69bfc49 | |||
d58372991f |
18
9unit.c
18
9unit.c
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
|
||||
9unit
|
||||
Copyright (C) 2023 Jonathan Lamothe <jonathan@jlamothe.net>
|
||||
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
|
||||
|
@ -46,8 +46,8 @@ run_test(TestState *s, TestResult (*t)(TestState *))
|
|||
case test_failure:
|
||||
s->failed++;
|
||||
break;
|
||||
case test_postponed:
|
||||
s->postponed++;
|
||||
case test_pending:
|
||||
s->pending++;
|
||||
break;
|
||||
default:
|
||||
exits("test returned an invalid response");
|
||||
|
@ -57,20 +57,21 @@ run_test(TestState *s, TestResult (*t)(TestState *))
|
|||
void
|
||||
run_tests(void (*tests)(TestState *))
|
||||
{
|
||||
if(!tests) return;
|
||||
if (!tests) return;
|
||||
TestState s;
|
||||
init_TestState(&s);
|
||||
(*tests)(&s);
|
||||
print_log(&s);
|
||||
printf("Tests run: %d\n", s.run);
|
||||
printf("Tests passed: %d\n", s.passed);
|
||||
printf("Tests failed: %d\n", s.failed);
|
||||
printf("Tests postponed: %d\n", s.postponed);
|
||||
print_log(&s);
|
||||
printf("Tests pending: %d\n", s.pending);
|
||||
clear_log(&s);
|
||||
if (s.failed) exits("test(s) failed");
|
||||
}
|
||||
|
||||
void
|
||||
append_log(TestState *s, const char *msg)
|
||||
append_test_log(TestState *s, const char *msg)
|
||||
{
|
||||
if (!(s && msg)) return;
|
||||
|
||||
|
@ -108,9 +109,10 @@ init_TestState(TestState *s)
|
|||
s->run = 0;
|
||||
s->passed = 0;
|
||||
s->failed = 0;
|
||||
s->postponed = 0;
|
||||
s->pending = 0;
|
||||
s->first_log = 0;
|
||||
s->last_log = 0;
|
||||
s->ptr = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
9
9unit.h
9
9unit.h
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
|
||||
9unit
|
||||
Copyright (C) 2023 Jonathan Lamothe <jonathan@jlamothe.net>
|
||||
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
|
||||
|
@ -32,9 +32,10 @@ struct TestState
|
|||
int run; // number of tests run
|
||||
int passed; // number of successful tests
|
||||
int failed; // number of failed tests
|
||||
int postponed; // number of postponed tests
|
||||
int pending; // number of pending tests
|
||||
TestLogEntry *first_log; // the first log entry
|
||||
TestLogEntry *last_log; //the last log entry
|
||||
void *ptr; // used for passing data between tests
|
||||
};
|
||||
|
||||
struct TestLogEntry
|
||||
|
@ -48,7 +49,7 @@ typedef enum TestResult
|
|||
{
|
||||
test_success, // the test succeeded
|
||||
test_failure, // the test failed
|
||||
test_postponed // the test was postponed
|
||||
test_pending // the test is pending
|
||||
} TestResult;
|
||||
|
||||
// Runs a single test
|
||||
|
@ -65,7 +66,7 @@ extern void run_tests(
|
|||
|
||||
// Adds an entry to the log that is displayed after the tests have
|
||||
// completed
|
||||
extern void append_log(
|
||||
extern void append_test_log(
|
||||
TestState *, // the current state
|
||||
const char * // the message to append
|
||||
);
|
||||
|
|
12
mkfile
12
mkfile
|
@ -1,5 +1,5 @@
|
|||
# 9unit
|
||||
# Copyright (C) 2023 Jonathan Lamothe <jonathan@jlamothe.net>
|
||||
# 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
|
||||
|
@ -16,6 +16,16 @@
|
|||
|
||||
</$objtype/mkfile
|
||||
|
||||
cleantest:V:
|
||||
cd test && mk $MKFLAGS clean
|
||||
|
||||
nuketest:V:
|
||||
cd test && mk $MKFLAGS clean
|
||||
|
||||
clean:V: cleantest
|
||||
|
||||
nuke:V: nuketest
|
||||
|
||||
LIB=9unit.a
|
||||
OFILES=9unit.$O
|
||||
HFILES=9unit.h
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
# 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/>.
|
||||
|
||||
</$objtype/mkfile
|
||||
|
||||
HFILES=../9unit.h
|
||||
LIB=../9unit.a
|
||||
TEST=tests
|
||||
|
||||
</sys/src/cmd/mktest
|
||||
|
||||
#jl
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
|
||||
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 "../9unit.h"
|
||||
|
||||
#define decl_test(n) static TestResult n(TestState *)
|
||||
#define def_test(n, s) static TestResult n(TestState *s)
|
||||
|
||||
// Internal Prototypes
|
||||
|
||||
static void test(TestState *);
|
||||
static void test_initial_state(TestState *);
|
||||
static void tests(TestState *);
|
||||
decl_test(initial_test_count);
|
||||
decl_test(initial_pass_count);
|
||||
decl_test(initial_fail_count);
|
||||
decl_test(initial_pend_count);
|
||||
decl_test(initial_first_log);
|
||||
decl_test(initial_last_log);
|
||||
decl_test(initial_ptr);
|
||||
|
||||
// Public Functions
|
||||
|
||||
void
|
||||
main()
|
||||
{
|
||||
run_tests(&tests);
|
||||
exits(0);
|
||||
}
|
||||
|
||||
// Internal Functions
|
||||
|
||||
static void
|
||||
tests(TestState *s)
|
||||
{
|
||||
if (!s) exits("ERROR: no TestState");
|
||||
|
||||
// Make a copy of the initial TestState and store it
|
||||
TestState scpy;
|
||||
memcpy(&scpy, s, sizeof(TestState));
|
||||
s->ptr = &scpy;
|
||||
|
||||
test_initial_state(s);
|
||||
}
|
||||
|
||||
static void
|
||||
test_initial_state(TestState *s)
|
||||
{
|
||||
print("Testing initial state\n");
|
||||
run_test(s, &initial_test_count);
|
||||
run_test(s, &initial_pass_count);
|
||||
run_test(s, &initial_fail_count);
|
||||
run_test(s, &initial_pend_count);
|
||||
run_test(s, &initial_first_log);
|
||||
run_test(s, &initial_last_log);
|
||||
run_test(s, &initial_ptr);
|
||||
}
|
||||
|
||||
def_test(initial_test_count, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\trun\n");
|
||||
if (scpy->run == 0) return test_success;
|
||||
append_test_log(s, "initial run was nonzero");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(initial_pass_count, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\tpassed\n");
|
||||
if (scpy->passed == 0) return test_success;
|
||||
append_test_log(s, "initial passed was nonzero");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(initial_fail_count, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\tfailed\n");
|
||||
if (scpy->failed == 0) return test_success;
|
||||
append_test_log(s, "initial failed was nonzero");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(initial_pend_count, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\tpending\n");
|
||||
if (scpy->pending == 0) return test_success;
|
||||
append_test_log(s, "initial pending was nonzero");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(initial_first_log, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\tfirst_log\n");
|
||||
if (scpy->first_log == 0) return test_success;
|
||||
append_test_log(s, "initial first_log was not null");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(initial_last_log, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\tlast_log\n");
|
||||
if (scpy->last_log == 0) return test_success;
|
||||
append_test_log(s, "initial last_log was not null");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(initial_ptr, s)
|
||||
{
|
||||
TestState *scpy = s->ptr;
|
||||
print("\tptr\n");
|
||||
if (scpy->ptr == 0) return test_success;
|
||||
append_test_log(s, "initial ptr was not null");
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
//jl
|
Loading…
Reference in New Issue
Block a user