File: count_tests.py

package info (click to toggle)
cbmc 6.6.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,852 kB
  • sloc: cpp: 386,459; ansic: 114,466; java: 28,405; python: 6,003; yacc: 4,552; makefile: 4,041; lex: 2,487; xml: 2,388; sh: 2,050; perl: 557; pascal: 184; javascript: 163; ada: 36
file content (80 lines) | stat: -rwxr-xr-x 2,718 bytes parent folder | download
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
#!/usr/bin/env python3
#
# Count number of unit tests.
#

import argparse
import os
import sys


class argument_separator_countert:
    def __init__(self):
        self.bracket_depth = 0
        self.separators = 0

    def read_text(self, text):
        previous_character = None
        in_quotes = False
        for character in text:
            if in_quotes:
                if character == '"' and previous_character != "\\":
                    in_quotes = False
            else:
                if character == '"':
                    in_quotes = True
                elif character == '(' or character == '<':
                    self.bracket_depth += 1
                elif character == ')' or character == '(':
                    self.bracket_depth -= 1
                elif character == ',' and self.bracket_depth == 1:
                    self.separators += 1
            previous_character = character


def tests_in_file(file_path):
    file_test_count = 0
    template_counter = None
    with open(file_path, "rt") as file:
        for line in file:
            if template_counter is None:
                if line.startswith("TEST_CASE"):
                    file_test_count += 1
                if line.startswith("SCENARIO"):
                    file_test_count += 1
                if line.startswith("TEMPLATE_TEST_CASE"):
                    template_counter = argument_separator_countert()
                    template_counter.read_text(line)
            else:
                template_counter.read_text(line)
            if template_counter is not None and template_counter.bracket_depth == 0:
                file_test_count += (template_counter.separators - 1)
                template_counter = None
    return file_test_count


def count_tests_in_directory(directory_path, exclude_files):
    test_count = 0
    for root, sub_directories, files in os.walk("."):
        # Excluded from count as built into separate binary.
        if root.endswith("libcprover-cpp"):
            continue
        for file_name in files:
            if any(file_name == excluded_file for excluded_file in exclude_files):
                continue
            _, extension = os.path.splitext(file_name)
            if extension == ".cpp":
                test_count += tests_in_file(os.path.join(root, file_name))
    return test_count


def main():
    argument_parser = argparse.ArgumentParser()
    argument_parser.add_argument("--exclude-files", dest="exclude_files", nargs=1, default=[""])
    arguments = argument_parser.parse_args()
    excluded_files = arguments.exclude_files[0].split()
    print(count_tests_in_directory(".", exclude_files=excluded_files))


if __name__ == "__main__":
    main()