kunit: mock: implemented nice, strict and naggy mock distinctions

Nice mocks only fail when there is an expectation on a method, but none
match a given call. Strict mocks only pass when there is a matching
expectation for every call. Naggy mocks have the same pass/fail behavior
as nice, but report a warning in any case a strict mock would fail.

Change-Id: I87017601291ba4346b6f16b77b208eeb97b7674f
Signed-off-by: Felix Guo <felixguoxiuping@gmail.com>
Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
diff --git a/include/test/mock.h b/include/test/mock.h
index 004dcf7..8910957 100644
--- a/include/test/mock.h
+++ b/include/test/mock.h
@@ -86,10 +86,17 @@
 	struct list_head expectations;
 };
 
+enum mock_type {
+	MOCK_TYPE_NICE,
+	MOCK_TYPE_NAGGY,
+	MOCK_TYPE_STRICT
+};
+
 struct mock {
 	struct test_post_condition parent;
 	struct test *test;
 	struct list_head methods;
+	enum mock_type type;
 	const void *(*do_expect)(struct mock *mock,
 				 const char *method_name,
 				 const void *method_ptr,
@@ -98,6 +105,8 @@
 				 int len);
 };
 
+#define DEFAULT_MOCK_TYPE MOCK_TYPE_NAGGY
+
 void mock_init_ctrl(struct test *test, struct mock *mock);
 
 void mock_validate_expectations(struct mock *mock);
@@ -128,6 +137,60 @@
 #define MOCK(name) name##_mock
 
 /**
+ * STRICT_MOCK() - sets the mock to be strict and returns the mock
+ * @mock: the mock
+ *
+ * For an example, see ``The Nice, the Strict, and the Naggy`` under
+ * ``Using KUnit``.
+ */
+#define STRICT_MOCK(mock) \
+({ \
+	mock_get_ctrl(mock)->type = MOCK_TYPE_STRICT; \
+	mock; \
+})
+
+static inline bool is_strict_mock(struct mock *mock)
+{
+	return mock->type == MOCK_TYPE_STRICT;
+}
+
+/**
+ * NICE_MOCK() - sets the mock to be nice and returns the mock
+ * @mock: the mock
+ *
+ * For an example, see ``The Nice, the Strict, and the Naggy`` under
+ * ``Using KUnit``.
+ */
+#define NICE_MOCK(mock) \
+({ \
+	mock_get_ctrl(mock)->type = MOCK_TYPE_NICE; \
+	mock; \
+})
+
+static inline bool is_nice_mock(struct mock *mock)
+{
+	return mock->type == MOCK_TYPE_NICE;
+}
+
+/**
+ * NAGGY_MOCK() - sets the mock to be naggy and returns the mock
+ * @mock: the mock
+ *
+ * For an example, see ``The Nice, the Strict, and the Naggy`` under
+ * ``Using KUnit``.
+ */
+#define NAGGY_MOCK(mock) \
+({ \
+	mock_get_ctrl(mock)->type = MOCK_TYPE_NAGGY; \
+	mock; \
+})
+
+static inline bool is_naggy_mock(struct mock *mock)
+{
+	return mock->type == MOCK_TYPE_NAGGY;
+}
+
+/**
  * EXPECT_CALL() - Declares a *call expectation* on a mock method or function.
  * @expectation_call: a mocked method or function with parameters replaced with
  *                    matchers.
diff --git a/test/mock-test.c b/test/mock-test.c
index cf34e1f7..cc7f3e2 100644
--- a/test/mock-test.c
+++ b/test/mock-test.c
@@ -139,7 +139,7 @@
 static void mock_test_do_expect_default_return(struct test *test)
 {
 	struct mock_test_context *ctx = test->priv;
-	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct MOCK(test) *mock_test = NICE_MOCK(ctx->mock_test);
 	struct test *trgt = mock_get_trgt(mock_test);
 	struct mock *mock = ctx->mock;
 	int param0 = 5, param1 = -5;
@@ -176,6 +176,48 @@
 	EXPECT_EQ(test, 0, expectation->times_called);
 }
 
+/**
+ * DOC: Testing the failure condition of different mock types.
+ *
+ * The following tests will test the behaviour of expectations under different
+ * conditions. For example, what happens when an expectation:
+ * - is not satisfied at the end of the test
+ * - is fulfilled but the expected function is called again
+ * - a function is called without expectations set on it
+ *
+ * For each of these conditions, there may be variations between the different
+ * types of mocks: nice mocks, naggy mocks (the default) and strict mocks.
+ *
+ * More information about these mocks can be found in the kernel documentation
+ * under Documentation/test/api/class-and-function-mocking
+ */
+
+/* Method called on strict mock with no expectations will fail */
+static void mock_test_strict_no_expectations_will_fail(struct test *test)
+{
+	struct mock_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test *trgt = mock_get_trgt(mock_test);
+	struct mock *mock = ctx->mock;
+	int param0 = 5, param1 = -5;
+	static const char * const two_param_types[] = {"int", "int"};
+	const void *two_params[] = {&param0, &param1};
+	struct mock_expectation *expectation;
+
+	mock->type = MOCK_TYPE_STRICT;
+
+	mock_set_default_action(mock,
+				"test_printk",
+				test_printk,
+				int_return(trgt, -4));
+
+	expectation = EXPECT_CALL(fail(mock_get_ctrl(mock_test), any(test)));
+
+	mock->do_expect(mock, "test_printk", test_printk, two_param_types,
+		two_params, ARRAY_SIZE(two_params));
+	mock_validate_expectations(mock);
+}
+
 /*
  * Method called on naggy mock with no expectations will not fail, but will show
  * a warning message
@@ -191,6 +233,8 @@
 	const void *two_params[] = {&param0, &param1};
 	struct mock_expectation *expectation;
 
+	mock->type = MOCK_TYPE_NAGGY;
+
 	mock_set_default_action(mock, "test_printk", test_printk,
 		int_return(trgt, -4));
 
@@ -212,6 +256,91 @@
 	mock_validate_expectations(mock);
 }
 
+/* Method called on nice mock with no expectations will do nothing. */
+static void mock_test_nice_no_expectations_do_nothing(struct test *test)
+{
+	struct mock_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test *trgt = mock_get_trgt(mock_test);
+	struct mock *mock = ctx->mock;
+	int param0 = 5, param1 = -5;
+	static const char * const two_param_types[] = {"int", "int"};
+	const void *two_params[] = {&param0, &param1};
+	struct mock_expectation *expectation;
+
+	mock->type = MOCK_TYPE_NICE;
+
+	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 = EXPECT_CALL(mock_vprintk(mock_get_ctrl(mock_test),
+					       any(test),
+					       any(test)));
+	expectation->min_calls_expected = 0;
+	expectation->max_calls_expected = 0;
+
+	mock->do_expect(mock,
+			"test_printk",
+			test_printk,
+			two_param_types,
+			two_params,
+			ARRAY_SIZE(two_params));
+	mock_validate_expectations(mock);
+}
+
+/* Test that method called on a mock (of any type) with no matching expectations
+ * will fail test and print all the tried expectations.
+ */
+static void
+run_method_called_but_no_matching_expectation_test(struct test *test,
+						   enum mock_type mock_type)
+{
+	struct mock_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test *trgt = mock_get_trgt(mock_test);
+	struct mock *mock = ctx->mock;
+	int param0 = 5, param1 = -5;
+	static const char * const two_param_types[] = {"int", "int"};
+	const void *two_params[] = {&param0, &param1};
+	struct mock_expectation *handle;
+	struct mock_param_matcher *two_matchers[] = {
+		int_eq(trgt, 100),
+		int_eq(trgt, 100)
+	};
+	mock_add_matcher(mock, "test_printk", test_printk, two_matchers,
+		ARRAY_SIZE(two_matchers));
+	handle = EXPECT_CALL(fail(mock_get_ctrl(mock_test), any(test)));
+
+	mock->type = mock_type;
+
+	mock->do_expect(mock, "test_printk", test_printk, two_param_types,
+		two_params, ARRAY_SIZE(two_params));
+}
+
+static void mock_test_naggy_no_matching_expectations_fail(struct test *test)
+{
+	run_method_called_but_no_matching_expectation_test(test,
+							   MOCK_TYPE_NAGGY);
+}
+
+static void mock_test_strict_no_matching_expectations_fail(struct test *test)
+{
+	run_method_called_but_no_matching_expectation_test(test,
+							   MOCK_TYPE_STRICT);
+}
+
+static void mock_test_nice_no_matching_expectations_fail(struct test *test)
+{
+	run_method_called_but_no_matching_expectation_test(test,
+							   MOCK_TYPE_NICE);
+}
+
 static void mock_test_mock_validate_expectations(struct test *test)
 {
 	struct mock_test_context *ctx = test->priv;
@@ -240,6 +369,57 @@
 	mock_validate_expectations(mock);
 }
 
+static void mock_test_validate_clears_expectations(struct test *test)
+{
+	struct mock_test_context *ctx = test->priv;
+	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct test *trgt = mock_get_trgt(mock_test);
+	struct mock *mock = ctx->mock;
+	struct mock_param_matcher *matchers[] = {
+		int_eq(trgt, 5),
+		int_eq(trgt, -4)
+	};
+	int param0 = 5, param1 = -4;
+	static const char * const two_param_types[] = {"int", "int"};
+	const void *two_params[] = {&param0, &param1};
+
+	struct mock_expectation *expectation;
+
+	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;
+
+	/* Add an arbitrary matcher for 0 calls */
+	expectation = mock_add_matcher(mock, "test_printk", test_printk,
+		matchers, ARRAY_SIZE(matchers));
+	expectation->times_called = 0;
+	expectation->min_calls_expected = 0;
+	expectation->max_calls_expected = 0;
+
+	/* Should have 0 calls and should clear the previous expectation */
+	mock_validate_expectations(mock);
+
+	/* Add a new matcher for 1 call */
+	expectation = mock_add_matcher(mock, "test_printk", test_printk,
+		matchers, ARRAY_SIZE(matchers));
+	expectation->times_called = 0;
+	expectation->min_calls_expected = 1;
+	expectation->max_calls_expected = 1;
+
+	/* Satisfy previous matcher */
+	mock->do_expect(mock, "test_printk", test_printk, two_param_types,
+		two_params, ARRAY_SIZE(two_params));
+
+	/*
+	 * Validate previous satisfy; if we didn't clear the previous
+	 * expectation, it would fail the mock_test.
+	 */
+	mock_validate_expectations(mock);
+}
+
 void *do_mocked_fail(struct mock_action *this, const void **params, int len)
 {
 	static const int ret;
@@ -289,7 +469,13 @@
 	TEST_CASE(mock_test_failed_expect_call_fails_test),
 	TEST_CASE(mock_test_do_expect_default_return),
 	TEST_CASE(mock_test_mock_validate_expectations),
+	TEST_CASE(mock_test_strict_no_expectations_will_fail),
 	TEST_CASE(mock_test_naggy_no_expectations_no_fail),
+	TEST_CASE(mock_test_nice_no_expectations_do_nothing),
+	TEST_CASE(mock_test_strict_no_matching_expectations_fail),
+	TEST_CASE(mock_test_naggy_no_matching_expectations_fail),
+	TEST_CASE(mock_test_nice_no_matching_expectations_fail),
+	TEST_CASE(mock_test_validate_clears_expectations),
 	{},
 };
 
diff --git a/test/mock.c b/test/mock.c
index 0710b7f..f55f488 100644
--- a/test/mock.c
+++ b/test/mock.c
@@ -71,6 +71,7 @@
 	mock->test = test;
 	INIT_LIST_HEAD(&mock->methods);
 	mock->do_expect = mock_do_expect;
+	mock->type = DEFAULT_MOCK_TYPE;
 	mock->parent.validate = mock_validate_wrapper;
 	list_add_tail(&mock->parent.node, &test->post_conditions);
 }
@@ -308,7 +309,12 @@
 		mock_add_method_expectation_error(test, stream,
 			"Method was called with no expectations declared: ",
 			mock, method, type_names, params, len);
-		stream->commit(stream);
+		if (is_strict_mock(mock))
+			test->fail(test, stream);
+		else if (is_naggy_mock(mock))
+			stream->commit(stream);
+		else
+			stream->clear(stream);
 		return NULL;
 	}
 
@@ -338,7 +344,7 @@
 		}
 	}
 
-	if (expectations_all_saturated) {
+	if (expectations_all_saturated && !is_nice_mock(mock)) {
 		mock_add_method_expectation_error(test, stream,
 			"Method was called with fully saturated expectations: ",
 			mock, method, type_names, params, len);
diff --git a/test/test-stream-test.c b/test/test-stream-test.c
index 659df66..db001b7 100644
--- a/test/test-stream-test.c
+++ b/test/test-stream-test.c
@@ -12,7 +12,7 @@
 static void test_stream_test_add(struct test *test)
 {
 	struct test_stream_test_context *ctx = test->priv;
-	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct MOCK(test) *mock_test = NICE_MOCK(ctx->mock_test);
 	struct test_stream *stream = ctx->stream;
 
 	stream->add(stream, "Foo");
@@ -31,7 +31,7 @@
 static void test_stream_test_append(struct test *test)
 {
 	struct test_stream_test_context *ctx = test->priv;
-	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct MOCK(test) *mock_test = NICE_MOCK(ctx->mock_test);
 	struct test_stream *stream = ctx->stream;
 	struct test_stream *other_stream;
 
@@ -53,7 +53,7 @@
 static void test_stream_error_message_when_no_level_set(struct test *test)
 {
 	struct test_stream_test_context *ctx = test->priv;
-	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct MOCK(test) *mock_test = NICE_MOCK(ctx->mock_test);
 	struct test_stream *stream = ctx->stream;
 	struct test_stream *other_stream;
 
@@ -121,7 +121,7 @@
 		struct test *test)
 {
 	struct test_stream_test_context *ctx = test->priv;
-	struct MOCK(test) *mock_test = ctx->mock_test;
+	struct MOCK(test) *mock_test = NICE_MOCK(ctx->mock_test);
 	struct test_stream *stream = ctx->stream;
 
 	stream->add(stream, "Hello World");