"""Test Error Render. The whole of both generated files is checked here, since they are short enough to read and what matters is the exact C that comes out.""" #################################################################################################################################### import os import tempfile from harness.test import * from common.render import COMMENT_BLOCK_BEGIN, COMMENT_BLOCK_END from common.storage import file_read, file_write from error.parse import bld_err_parse from error.render import * # Header every generated file begins with HEADER = """%s Error Type Definition Automatically generated by 'build.py error' -- do not modify directly. %s """ % ( COMMENT_BLOCK_BEGIN, COMMENT_BLOCK_END, ) #################################################################################################################################### def _render(error): """Render an error declaration and return what was written.""" with tempfile.TemporaryDirectory() as path: file_write(os.path.join(path, "build/error.yaml"), error) bld_err_render(path, bld_err_parse(path)) return ( file_read(os.path.join(path, "src/common/error/error.auto.h")), file_read(os.path.join(path, "src/common/error/error.auto.c.inc")), ) #################################################################################################################################### def test_error_render(): """An error is declared, defined, and listed under the C name built from its name.""" auto_h, auto_c = _render("assert:\n code: 25\n fatal: true\nfile-open: 41\n") assert_equal( auto_h, HEADER + """#ifndef COMMON_ERROR_ERROR_AUTO_H #define COMMON_ERROR_ERROR_AUTO_H %s Error type declarations %s ERROR_DECLARE(AssertError); ERROR_DECLARE(FileOpenError); #endif """ % (COMMENT_BLOCK_BEGIN, COMMENT_BLOCK_END), ) # The code is aligned so the list reads as a column, and a fatal error is one the code itself is wrong about assert_equal( auto_c, HEADER + """ %s Error type definitions %s ERROR_DEFINE( 25, AssertError, true, RuntimeError); ERROR_DEFINE( 41, FileOpenError, false, RuntimeError); %s Error type array %s static const ErrorType *const errorTypeList[] = { &AssertError, &FileOpenError, NULL, }; """ % (COMMENT_BLOCK_BEGIN, COMMENT_BLOCK_END, COMMENT_BLOCK_BEGIN, COMMENT_BLOCK_END), ) #################################################################################################################################### def test_error_render_write(): """A generated file is only written when it changes, so an unchanged one does not trigger a rebuild.""" with tempfile.TemporaryDirectory() as path: file_write(os.path.join(path, "build/error.yaml"), "assert: 25\n") bld_err_render(path, bld_err_parse(path)) path_auto_h = os.path.join(path, "src/common/error/error.auto.h") time_before = os.stat(path_auto_h).st_mtime_ns bld_err_render(path, bld_err_parse(path)) assert_equal(os.stat(path_auto_h).st_mtime_ns, time_before)