summaryrefslogtreecommitdiff
path: root/t/unit-tests/generate-clar-suites.sh
blob: d5c712221e46a2eaa288fff5262438e5f04d6f72 (plain)
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
#!/bin/sh

if test $# -lt 2
then
	echo "USAGE: $0 <CLAR_DECLS_H> <OUTPUT>" 2>&1
	exit 1
fi

CLAR_DECLS_H="$1"
OUTPUT="$2"

awk '
	function add_suite(suite, initialize, cleanup, count) {
		if (!suite) return
		suite_count++
		callback_count += count
		suites = suites "    {\n"
		suites = suites "        \"" suite "\",\n"
		suites = suites "        " initialize ",\n"
		suites = suites "        " cleanup ",\n"
		suites = suites "        _clar_cb_" suite ", " count ", 1\n"
		suites = suites "    },\n"
	}

	BEGIN {
		suites = "static struct clar_suite _clar_suites[] = {\n"
	}

	{
		print
		name = $3; sub(/\(.*$/, "", name)
		suite = name; sub(/^test_/, "", suite); sub(/__.*$/, "", suite)
		short_name = name; sub(/^.*__/, "", short_name)
		cb = "{ \"" short_name "\", &" name " }"
		if (suite != prev_suite) {
			add_suite(prev_suite, initialize, cleanup, count)
			if (callbacks) callbacks = callbacks "};\n"
			callbacks = callbacks "static const struct clar_func _clar_cb_" suite "[] = {\n"
			initialize = "{ NULL, NULL }"
			cleanup = "{ NULL, NULL }"
			count = 0
			prev_suite = suite
		}
		if (short_name == "initialize") {
			initialize = cb
		} else if (short_name == "cleanup") {
			cleanup = cb
		} else {
			callbacks = callbacks "    " cb ",\n"
			count++
		}
	}

	END {
		add_suite(suite, initialize, cleanup, count)
		suites = suites "};"
		if (callbacks) callbacks = callbacks "};"
		print callbacks
		print suites
		print "static const size_t _clar_suite_count = " suite_count ";"
		print "static const size_t _clar_callback_count = " callback_count ";"
	}
' "$CLAR_DECLS_H" >"$OUTPUT"