cmetal 0.5.0

A self-contained learning environment for advanced C, inspired by rustlings
#!/usr/bin/env python3
"""Tests for the header checks of check_exercises.py.

    python3 -m unittest discover -s scripts -p "test_*.py"
"""

import os
import sys
import tempfile
import unittest

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import check_exercises as c  # noqa: E402


class ParseClaims(unittest.TestCase):
    def test_known_sentences(self):
        cases = {
            "The file does not compile.": [("compile",)],
            "The file does not compile with these flags.":
                [("compile",)],
            "The file does not compile: main and the tests already "
            "call the new contract.": [("compile",)],
            "The tests do not compile.": [("test compile",)],
            "One test fails.": [("tests", 1)],
            "All four tests fail.": [("tests", 4)],
            "Eleven tests fail.": [("tests", 11)],
            "main prints the right values. Four tests fail.":
                [("main runs",), ("tests", 4)],
            "main may crash. Two tests fail.":
                [("may", frozenset({"main"})), ("tests", 2)],
            "main may appear to work. Some tests may fail.":
                [("may", frozenset({"main"})),
                 ("may", frozenset({"tests"}))],
            "main and the tests may appear to work. The sanitizer "
            "step fails.":
                [("may", frozenset({"main", "tests"})), ("sanitizer",)],
        }
        for text, expected in cases.items():
            with self.subTest(text=text):
                self.assertEqual(c.parse_claims(text), (expected, []))

    def test_unknown_sentences_are_reported(self):
        for text in ["2 tests fail.", "One of the tests fails.",
                     "The program crashes.", "main crashes.",
                     "The sanitizer reports an error.",
                     "The tests might fail.", "main may do anything.",
                     "Thirteen tests fail."]:
            with self.subTest(text=text):
                claims, unknown = c.parse_claims(text)
                self.assertEqual(claims, [])
                self.assertEqual(unknown, [text])

    def test_every_sentence_is_checked(self):
        """A known sentence does not hide an unknown one after it."""
        claims, unknown = c.parse_claims(
            "Two tests fail. The second one crashes.")
        self.assertEqual(claims, [("tests", 2)])
        self.assertEqual(unknown, ["The second one crashes."])


class HeaderFiles(unittest.TestCase):
    def write(self, text):
        handle = tempfile.NamedTemporaryFile(
            "w", suffix=".c", delete=False)
        self.addCleanup(os.unlink, handle.name)
        handle.write(text)
        handle.close()
        return handle.name

    def test_problem_paragraph(self):
        path = self.write("// x.c - Title\n//\n// Context.\n//\n"
                          "// Two tests\n// fail.\n//\n// TODO: fix f.\n"
                          "\n#include <stdio.h>\n")
        self.assertEqual(c.problem_paragraph(path), "Two tests fail.")

    def test_header_without_todo_has_no_problem_paragraph(self):
        path = self.write("// x.c - Title\n//\n// Two tests fail.\n"
                          "\n#include <stdio.h>\n")
        self.assertIsNone(c.problem_paragraph(path))

    def test_text_after_the_todo_is_rejected(self):
        path = self.write("// x.c - Title\n//\n// Two tests fail.\n//\n"
                          "// TODO: fix f.\n//\n// One more thing.\n")
        self.assertIsNone(c.problem_paragraph(path))

    def test_the_word_include_in_the_prose_does_not_cut_the_header(self):
        path = self.write("// x.c - Title\n//\n// The #include is "
                          "given.\n//\n// One test fails.\n//\n"
                          "// TODO: fix f.\n\n#include <stdio.h>\n")
        self.assertEqual(c.problem_paragraph(path), "One test fails.")

    def test_missing_or_unknown_header_is_a_problem(self):
        ex = {"name": "x", "dir": "d"}
        path = self.write("/* a block comment header */\nint x;\n")
        problems = c.header_problems("gcc", path, ex, "tests", "/tmp")
        self.assertEqual(len(problems), 1)
        self.assertIn("no problem paragraph", problems[0])
        path = self.write("// x.c - Title\n//\n// The program "
                          "crashes.\n//\n// TODO: fix f.\n")
        problems = c.header_problems("gcc", path, ex, "tests", "/tmp")
        self.assertEqual(len(problems), 1)
        self.assertIn("cannot check", problems[0])

    def test_only_an_include_line_marks_a_file_with_tests(self):
        self.assertTrue(c.holds_tests(self.write(
            '#ifdef TEST\n  #  include "cmetal_test.h"\n#endif\n')))
        self.assertFalse(c.holds_tests(self.write(
            '// see cmetal_test.h\nconst char *s = "cmetal_test.h";\n')))


if __name__ == "__main__":
    unittest.main()