| 1 | #include <cgreen/cgreen.h> |
| 2 | #include "cgreen_value_internal.h" |
| 3 | #include "utils.h" |
| 4 | |
| 5 | Describe(TestConstraint); |
| 6 | BeforeEach(TestConstraint) {} |
| 7 | AfterEach(TestConstraint) {} |
| 8 | |
| 9 | typedef struct Box { |
| 10 | int id; |
| 11 | int size; |
| 12 | } Box; |
| 13 | |
| 14 | typedef struct Piece { |
| 15 | int id; |
| 16 | int size; |
| 17 | } Piece; |
| 18 | |
| 19 | bool compare_piece_and_box_size(Constraint *constraint, CgreenValue actual) { |
| 20 | return ((Piece *)actual.value.pointer_value)->size |
| 21 | < ((Box*)constraint->expected_value.value.pointer_value)->size ; |
| 22 | } |
| 23 | |
| 24 | static void test_fit_piece(Constraint *constraint, const char *function_name, CgreenValue actual, |
| 25 | const char *test_file, int test_line, TestReporter *reporter) { |
| 26 | (*reporter->assert_true)( |
| 27 | reporter, |
| 28 | test_file, |
| 29 | test_line, |
| 30 | (*constraint->compare)(constraint, actual), |
| 31 | "Piece [%f], does not fit in [%f] in function [%s] parameter [%s]", |
| 32 | ((Piece *)constraint->expected_value.value.pointer_value)->id, |
| 33 | ((Box *)actual.value.pointer_value)->id, |
| 34 | function_name, |
| 35 | constraint->parameter_name); |
| 36 | } |
| 37 | |
| 38 | Constraint *create_piece_fit_in_box_constraint(intptr_t expected_value, const char *expected_value_name) { |
| 39 | Constraint *constraint = create_constraint(); |
| 40 | |
| 41 | constraint->expected_value = make_cgreen_pointer_value((void*)expected_value); |
| 42 | constraint->expected_value_name = string_dup(expected_value_name); |
| 43 | constraint->type = CGREEN_CONTENT_COMPARER_CONSTRAINT; |
| 44 | |
| 45 | constraint->compare = &compare_piece_and_box_size; |
| 46 | constraint->execute = &test_fit_piece; |
| 47 | constraint->name = "fit in box"; |
| 48 | constraint->size_of_expected_value = sizeof(intptr_t); |
| 49 | |
| 50 | return constraint; |
| 51 | } |
| 52 | #define can_fit_in_box(box) create_piece_fit_in_box_constraint((intptr_t)box, #box) |
| 53 | |
| 54 | Ensure(TestConstraint, more_complex_custom_constraint_function) { |
| 55 | Box box1 = {.id = 1, .size = 5}; |
| 56 | Piece piece99 = {.id = 99, .size = 6}; |
| 57 | assert_that(&piece99, can_fit_in_box(&box1)); |
| 58 | } |
| 59 | |
| 60 | int main(int argc, char **argv) { |
| 61 | TestSuite *suite = create_test_suite(); |
| 62 | add_test_with_context(suite, TestConstraint, more_complex_custom_constraint_function); |
| 63 | run_test_suite(suite, create_text_reporter()); |
| 64 | } |