Skip to content

Commit

Permalink
Adds will_return_by_reference constraint.
Browse files Browse the repository at this point in the history
  • Loading branch information
oniboni committed May 10, 2022
1 parent a67eb4a commit 8fdbf1d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/cgreen/cgreen_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ typedef enum {
CGREEN_STRING,
CGREEN_DOUBLE,
CGREEN_POINTER,
CGREEN_BYVALUE
CGREEN_BYVALUE,
CGREEN_BYREFERENCE
} CgreenValueType;

typedef struct {
Expand Down
2 changes: 2 additions & 0 deletions include/cgreen/constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef enum {
CGREEN_CALL_CONSTRAINT,
CGREEN_CALL_COUNTER_CONSTRAINT,
CGREEN_RETURN_BY_VALUE_CONSTRAINT,
CGREEN_RETURN_BY_REFERENCE_CONSTRAINT,
CGREEN_CAPTURE_PARAMETER_CONSTRAINT
} ConstraintType;

Expand Down Expand Up @@ -85,6 +86,7 @@ Constraint *create_less_than_double_constraint(double expected_value, const char
Constraint *create_greater_than_double_constraint(double expected_value, const char *expected_value_name);
Constraint *create_return_value_constraint(intptr_t value_to_return);
Constraint *create_return_by_value_constraint(intptr_t value_to_return, size_t size);
Constraint *create_return_by_reference_constraint(void *ref_to_value_to_return);
Constraint *create_return_double_value_constraint(double value_to_return);
Constraint *create_set_parameter_value_constraint(const char *parameter_name, intptr_t value_to_set, size_t size_to_set);
Constraint *create_with_side_effect_constraint(void (*callback)(void *), void *data);
Expand Down
1 change: 1 addition & 0 deletions include/cgreen/constraint_syntax_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#define with_side_effect(callback, data) create_with_side_effect_constraint(callback, data)
#define will_return(value) create_return_value_constraint((intptr_t)value)
#define will_return_by_value(value, size) create_return_by_value_constraint((intptr_t)&value, size)
#define will_return_by_reference(value) create_return_by_reference_constraint(&value)
#define will_return_double(value) create_return_double_value_constraint(value)
#define will_set_contents_of_parameter(parameter_name, pointer_to_value, size) create_set_parameter_value_constraint(#parameter_name, (intptr_t)pointer_to_value, (size_t)size)
#define will_capture_parameter(parameter_name, local_variable) create_capture_parameter_constraint(#parameter_name, &local_variable, sizeof(local_variable))
Expand Down
13 changes: 13 additions & 0 deletions src/constraint.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ Constraint *create_return_by_value_constraint(intptr_t value_to_return, size_t s
return constraint;
}

Constraint *create_return_by_reference_constraint(void *ref_to_value_to_return) {
Constraint* constraint = create_constraint();
constraint->type = CGREEN_RETURN_BY_REFERENCE_CONSTRAINT;

constraint->compare = &compare_true;
constraint->execute = &test_true;
constraint->name = "return by reference";
constraint->expected_value = make_cgreen_pointer_value(ref_to_value_to_return);
constraint->expected_value.type = CGREEN_BYREFERENCE;

return constraint;
}

Constraint *create_return_double_value_constraint(double value_to_return) {
Constraint* constraint = create_constraint();
constraint->type = CGREEN_RETURN_VALUE_CONSTRAINT;
Expand Down
3 changes: 3 additions & 0 deletions src/mocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ intptr_t mock_(TestReporter* test_reporter, const char *function, const char *mo
*/
return box_double(stored_result.value.double_value);
#endif
} else if (stored_result.type == CGREEN_BYREFERENCE) {
return (*((intptr_t *)(stored_result.value.pointer_value)));
} else
return stored_result.value.integer_value;
}
Expand Down Expand Up @@ -898,6 +900,7 @@ static CgreenValue stored_result_or_default_for(CgreenVector* constraints) {
switch (constraint->type) {
case CGREEN_RETURN_VALUE_CONSTRAINT:
case CGREEN_RETURN_POINTER_CONSTRAINT:
case CGREEN_RETURN_BY_REFERENCE_CONSTRAINT:
return constraint->expected_value;
case CGREEN_RETURN_BY_VALUE_CONSTRAINT: {
/* When returning a struct by value we need to copy the struct
Expand Down

0 comments on commit 8fdbf1d

Please sign in to comment.