aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: 269b970c793da993ff25ed8d4023423a073770cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# EZ-Tester

A barebones testing library for C on POSIX compliant systems

## Features

* lightweight
* catch SEGFAULTS and other ungraceful program exits
* no external dependencies ☺️

## Build

Note this has only been tested on my machine

* Void Linux x86_64 glibc

By default `make` builds all targets in `build/(static|dynamic|header)`

## Usage

EZ-Tester can be used as static, dynamic or header only library.
Make sure to copy `eztester.h` somewhere in your include path.
After configuring your project, create a program to run your tests.

<details>
<summary>Example Program</summary>

```c
#include "eztester.h"

int sum_of_integers(const int max){
    return (max*(max+1))/2;
}

eztester_status sample_test(){
    const int max = 100;
    ez_log("Inside of Sample Test");
    ez_log("adding %d consectuive positive integers", max);

    int actual = 0;
    for(int i = 1; i <= max; i++){
        actual += i;
    }

    int expected = sum_of_integers(max);

    if (actual == expected) {
        return TEST_PASS;
    }
    else if (actual < 0) {
        return TEST_ERROR;
    }
    else {
        return TEST_FAIL;
    }
}

ez_status sample_shell_test(){
    // eztester_shell is a wrapper function for `system`
    // see `eztester.h` for more info
    int status = eztester_shell("curl invalid.url");
    if (status == 0){
        return TEST_PASS;
    }
    else if (status == 6){
        return TEST_WARNING;
    }
    else if (status == 127){
        return TEST_ERROR;
    }
    else {
        return TEST_FAIL;
    }
}

int main(int argc, char* argv[]){
    ez_list *tests = ezterster_create_list(2);

    // runners that always return the same status are provided
    ez_register(tests, (ez_test){ez_always_pass, "Always Pass", 0});
    ez_register(tests, (ez_test){sample_test, "Sample Test", 0}); // our test, can be defined in a different translation unit

    // a list will resize on register when it doesn't have capacity
    ez_register(tests, (ez_test){ez_always_fail, "Always Fail", 0});
    ez_register(tests, (ez_test){ez_always_warn, "Always Warn", 0});
    
    ez_register(tests, (ez_test){sample_shell_test, "Check a non existent url");

    ez_run(tests, EXIT_ON_FAIL | EXIT_ON_TIMEOUT );

    ez_destroy_list(tests);
    return 0;
}
```

</details>

More programs are provided in [examples](examples/).

### Static

After building, copy the static libraries into your project

```bash
cp build/static/* $YOUR_PROJECT_DIRECORY/libs
```

When building make sure to add `-Llibs -leztester` to your linker flags

For example:
```bash
gcc -o tests test/test.c src/module.c -Llibs -leztester
```

### Dynamic

### Header

To use the header-only implementation replace the include in your programs entry point to:

```c
#define EZTESTER_IMPLEMENTATION
#include "eztester.h"
#undef EZTESTER_IMPLEMENTATION
```

## TODO

* [x] makefile
    * [x] static library target
    * [x] dynamic library target
    * [x] header-only target
* [x] shell command utility
* [ ] colorized output