kunit: added expectation modifier macros
Times, Never, RetireOnSaturation, Returns, ActionOnMatch
Change-Id: I3324fc3db28cd230ef5688099c9bcb59fc96782c
Signed-off-by: Felix Guo <felixguo@google.com>
diff --git a/include/test/mock.h b/include/test/mock.h
index 6ac1183..de604a7 100644
--- a/include/test/mock.h
+++ b/include/test/mock.h
@@ -244,6 +244,94 @@ static inline bool is_naggy_mock(struct mock *mock)
*/
#define EXPECT_CALL(expectation_call) mock_master_##expectation_call
+/**
+ * Times() - sets the number of times a method is expected be called with the
+ * matching parameters
+ * @times: the number of times expected
+ * @expectation: the expectation to set
+ *
+ * Return:
+ * the same &struct mock_expectation passed in
+ */
+static inline struct mock_expectation *Times(
+ int times,
+ struct mock_expectation *expectation
+) {
+ expectation->min_calls_expected = times;
+ expectation->max_calls_expected = times;
+ return expectation;
+}
+
+/**
+ * Never() - alias for Times(0)
+ * @expectation: the expectation to set
+ *
+ * Return:
+ * the same &struct mock_expectation passed in
+ */
+static inline struct mock_expectation *Never(
+ struct mock_expectation *expectation
+) {
+ return Times(0, expectation);
+}
+
+/**
+ * RetireOnSaturation() - sets the expectation to retire on saturation
+ * @expectation: the expectation to set
+ *
+ * Return:
+ * the same &struct mock_expectation passed in
+ */
+static inline struct mock_expectation *RetireOnSaturation(
+ struct mock_expectation *expectation
+) {
+ expectation->retire_on_saturation = true;
+ return expectation;
+}
+
+/**
+ * ActionOnMatch() - sets a action of the expectation when matched
+ * @expectation: the expectation to set the action of
+ * @action: action to perform when expectation matches
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ * ActionOnMatch(EXPECT_CALL(...), INVOKE_REAL(test, ...));
+ *
+ * Return:
+ * the same &struct mock_expectation passed in
+ */
+static inline struct mock_expectation *ActionOnMatch(
+ struct mock_expectation *expectation,
+ struct mock_action *action
+) {
+ expectation->action = action;
+ return expectation;
+}
+
+/**
+ * Returns() - sets a action of the expectation to return a value
+ * @expectation: the expectation to set the return value of
+ * @return_action: a return action
+ *
+ * Example:
+ *
+ * .. code-block:: c
+ *
+ * Returns(EXPECT_CALL(...), int_return(test, 10));
+ *
+ * Return:
+ * the same &struct mock_expectation passed in
+ */
+static inline struct mock_expectation *Returns(
+ struct mock_expectation *expectation,
+ struct mock_action *return_action
+) {
+ return ActionOnMatch(expectation, return_action);
+}
+
#define mock_get_ctrl_internal(mock_object) (&(mock_object)->ctrl)
#define mock_get_ctrl(mock_object) mock_get_ctrl_internal(mock_object)
diff --git a/test/mock-test.c b/test/mock-test.c
index cc7f3e2..1281473 100644
--- a/test/mock-test.c
+++ b/test/mock-test.c
@@ -238,9 +238,8 @@ static void mock_test_naggy_no_expectations_no_fail(struct test *test)
mock_set_default_action(mock, "test_printk", test_printk,
int_return(trgt, -4));
- expectation = EXPECT_CALL(fail(mock_get_ctrl(mock_test), any(test)));
- expectation->min_calls_expected = 0;
- expectation->max_calls_expected = 0;
+ expectation = Never(EXPECT_CALL(fail(mock_get_ctrl(mock_test),
+ any(test))));
EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test), any(test),
va_format_cmp(test, str_contains(test,
@@ -275,15 +274,12 @@ static void mock_test_nice_no_expectations_do_nothing(struct test *test)
test_printk,
int_return(trgt, -4));
- expectation = EXPECT_CALL(fail(mock_get_ctrl(mock_test), any(test)));
- expectation->min_calls_expected = 0;
- expectation->max_calls_expected = 0;
+ expectation = Never(EXPECT_CALL(fail(mock_get_ctrl(mock_test),
+ any(test))));
- expectation = EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
- any(test),
- any(test)));
- expectation->min_calls_expected = 0;
- expectation->max_calls_expected = 0;
+ expectation = Never(EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+ any(test),
+ any(test))));
mock->do_expect(mock,
"test_printk",
@@ -388,9 +384,8 @@ static void mock_test_validate_clears_expectations(struct test *test)
mock->type = MOCK_TYPE_STRICT;
/* If all goes well, the mock_test should not fail. */
- expectation = EXPECT_CALL(fail(mock_get_ctrl(mock_test), any(test)));
- expectation->min_calls_expected = 0;
- expectation->max_calls_expected = 0;
+ expectation = Never(EXPECT_CALL(fail(mock_get_ctrl(mock_test),
+ any(test))));
/* Add an arbitrary matcher for 0 calls */
expectation = mock_add_matcher(mock, "test_printk", test_printk,