Kevin Lin | 266f09e | 2020-08-19 15:18:28 +0800 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include <base/json/json_reader.h> |
| 8 | #include <base/values.h> |
| 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "runtime_probe/probe_function.h" |
| 13 | #include "runtime_probe/probe_result_checker.h" |
| 14 | #include "runtime_probe/probe_statement.h" |
| 15 | |
| 16 | namespace runtime_probe { |
| 17 | |
| 18 | class MockProbeFunction : public ProbeFunction { |
| 19 | public: |
Kevin Lin | 61ef14f | 2020-08-18 18:41:34 +0800 | [diff] [blame] | 20 | NAME_PROBE_FUNCTION("mock_function"); |
Ben Chan | 26ecad7 | 2019-09-27 08:52:05 -0700 | [diff] [blame] | 21 | MOCK_METHOD(DataType, Eval, (), (const, override)); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | TEST(ProbeStatementTest, TestEval) { |
| 25 | ProbeStatement probe_statement; |
| 26 | |
| 27 | // Set up |expect_| |
| 28 | auto expect_string = R"({ |
| 29 | "expected_field": [true, "str"] |
| 30 | })"; |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 31 | auto expect = base::JSONReader::Read(expect_string); |
| 32 | ASSERT_TRUE(expect.has_value()); |
| 33 | ASSERT_TRUE(expect->is_dict()); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 34 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 35 | probe_statement.expect_ = ProbeResultChecker::FromValue(*expect); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 36 | |
| 37 | // Set up |eval_| |
| 38 | auto mock_eval = std::make_unique<MockProbeFunction>(); |
| 39 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 40 | base::Value good_result(base::Value::Type::DICTIONARY); |
| 41 | good_result.SetStringKey("expected_field", "expected"); |
| 42 | good_result.SetStringKey("optional_field", "optional"); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 43 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 44 | auto good_result2 = good_result.Clone(); |
Qijiang Fan | 34a5f0e | 2020-01-14 16:39:03 +0900 | [diff] [blame] | 45 | |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 46 | // bad_result is empty, which doesn't have expected field |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 47 | base::Value bad_result(base::Value::Type::DICTIONARY); |
| 48 | bad_result.SetStringKey("optional_field", "optional"); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 49 | |
Qijiang Fan | 34a5f0e | 2020-01-14 16:39:03 +0900 | [diff] [blame] | 50 | ProbeFunction::DataType val_a; |
| 51 | // val_a{std::move(x), std::move(y)} implicitly calls the copy constructor |
| 52 | // which is not possible. |
| 53 | val_a.push_back(std::move(good_result)); |
| 54 | val_a.push_back(std::move(bad_result)); |
| 55 | |
| 56 | ProbeFunction::DataType val_b; |
| 57 | val_b.push_back(std::move(good_result2)); |
| 58 | |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 59 | EXPECT_CALL(*mock_eval, Eval()) |
Qijiang Fan | 34a5f0e | 2020-01-14 16:39:03 +0900 | [diff] [blame] | 60 | .WillOnce(::testing::Return(::testing::ByMove(std::move(val_a)))) |
| 61 | .WillOnce(::testing::Return(::testing::ByMove(std::move(val_b)))); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 62 | |
| 63 | probe_statement.eval_ = std::move(mock_eval); |
| 64 | |
| 65 | // Test twice, both invocations should only return |good_result|. |
| 66 | for (auto i = 0; i < 2; i++) { |
| 67 | auto results = probe_statement.Eval(); |
| 68 | ASSERT_EQ(results.size(), 1); |
| 69 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 70 | auto* str_value = results[0].FindStringKey("expected_field"); |
| 71 | ASSERT_NE(str_value, nullptr); |
| 72 | ASSERT_EQ(*str_value, "expected"); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 73 | |
Kevin Lin | af1ee81 | 2020-06-23 16:53:53 +0800 | [diff] [blame] | 74 | str_value = results[0].FindStringKey("optional_field"); |
| 75 | ASSERT_NE(str_value, nullptr); |
| 76 | ASSERT_EQ(*str_value, "optional"); |
Wei-Han Chen | 64e93a3 | 2018-12-20 20:07:18 +0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | } // namespace runtime_probe |