vunit icon indicating copy to clipboard operation
vunit copied to clipboard

[Feature] Make `check_equal` a generic procedure

Open nselvara opened this issue 8 months ago • 5 comments

Hey there, I thought to make the testing even more powerful. I'd suggest to make check_equal a generic procedure in order to make it easier to define a new one for any other type.

Suggested code:

package example_pkg is
    procedure check_equal_generic
        generic (
            type type_t;
            function to_string(data: type_t) return string
        )
        parameter (
            got: in type_t;
            expected: in type_t;
            msg: in string := check_result_tag;
            level: in log_level_t := null_log_level;
            path_offset: in natural := 1;
            line_num: in natural := 0;
            file_name: in string := ""
        );

    type test_t is (st_1, st_2);
    procedure check_equal is new check_equal_generic generic map (type_t => test_t, to_string => to_string); -- to_string is implicitly defined
end package;

package body example_pkg is
    -- Overloaded generic check_equal procedures for custom checks
    procedure check_equal_generic
        generic (
            type type_t;
            function to_string(data: type_t) return string
        )
        parameter (
            got: in type_t;
            expected: in type_t;
            msg: in string := check_result_tag;
            level: in log_level_t := null_log_level;
            path_offset: in natural := 1;
            line_num: in natural := 0;
            file_name: in string := ""
        )
    is
        constant checker: checker_t := default_checker;
    begin
        -- pragma translate_off
        if got = expected then
            if is_pass_visible(checker) then
                passing_check(
                    checker,
                    p_std_msg(
                        "Equality check passed", msg,
                        "Got " & to_string(got) & "."
                    ),
                    path_offset + 1, line_num, file_name
                );
            else
                passing_check(checker);
            end if;
        else
            failing_check(
                checker,
                p_std_msg(
                    "Equality check failed", msg,
                    "Got " & to_string(got) & ". " &
                    "Expected " & to_string(expected) & "."
                ),
                level, path_offset + 1, line_num, file_name
            );
        end if;
    end;
end package body;

nselvara avatar Jun 17 '24 12:06 nselvara