blob: 9acb6be88410509da0396624032e196a35775c02 [file] [log] [blame]
Kevin Lin266f09e2020-08-19 15:18:28 +08001// 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 Chen64e93a32018-12-20 20:07:18 +08004
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
16namespace runtime_probe {
17
18class MockProbeFunction : public ProbeFunction {
19 public:
Kevin Lin61ef14f2020-08-18 18:41:34 +080020 NAME_PROBE_FUNCTION("mock_function");
Ben Chan26ecad72019-09-27 08:52:05 -070021 MOCK_METHOD(DataType, Eval, (), (const, override));
Wei-Han Chen64e93a32018-12-20 20:07:18 +080022};
23
24TEST(ProbeStatementTest, TestEval) {
25 ProbeStatement probe_statement;
26
27 // Set up |expect_|
28 auto expect_string = R"({
29 "expected_field": [true, "str"]
30 })";
Kevin Linaf1ee812020-06-23 16:53:53 +080031 auto expect = base::JSONReader::Read(expect_string);
32 ASSERT_TRUE(expect.has_value());
33 ASSERT_TRUE(expect->is_dict());
Wei-Han Chen64e93a32018-12-20 20:07:18 +080034
Kevin Linaf1ee812020-06-23 16:53:53 +080035 probe_statement.expect_ = ProbeResultChecker::FromValue(*expect);
Wei-Han Chen64e93a32018-12-20 20:07:18 +080036
37 // Set up |eval_|
38 auto mock_eval = std::make_unique<MockProbeFunction>();
39
Kevin Linaf1ee812020-06-23 16:53:53 +080040 base::Value good_result(base::Value::Type::DICTIONARY);
41 good_result.SetStringKey("expected_field", "expected");
42 good_result.SetStringKey("optional_field", "optional");
Wei-Han Chen64e93a32018-12-20 20:07:18 +080043
Kevin Linaf1ee812020-06-23 16:53:53 +080044 auto good_result2 = good_result.Clone();
Qijiang Fan34a5f0e2020-01-14 16:39:03 +090045
Wei-Han Chen64e93a32018-12-20 20:07:18 +080046 // bad_result is empty, which doesn't have expected field
Kevin Linaf1ee812020-06-23 16:53:53 +080047 base::Value bad_result(base::Value::Type::DICTIONARY);
48 bad_result.SetStringKey("optional_field", "optional");
Wei-Han Chen64e93a32018-12-20 20:07:18 +080049
Qijiang Fan34a5f0e2020-01-14 16:39:03 +090050 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 Chen64e93a32018-12-20 20:07:18 +080059 EXPECT_CALL(*mock_eval, Eval())
Qijiang Fan34a5f0e2020-01-14 16:39:03 +090060 .WillOnce(::testing::Return(::testing::ByMove(std::move(val_a))))
61 .WillOnce(::testing::Return(::testing::ByMove(std::move(val_b))));
Wei-Han Chen64e93a32018-12-20 20:07:18 +080062
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 Linaf1ee812020-06-23 16:53:53 +080070 auto* str_value = results[0].FindStringKey("expected_field");
71 ASSERT_NE(str_value, nullptr);
72 ASSERT_EQ(*str_value, "expected");
Wei-Han Chen64e93a32018-12-20 20:07:18 +080073
Kevin Linaf1ee812020-06-23 16:53:53 +080074 str_value = results[0].FindStringKey("optional_field");
75 ASSERT_NE(str_value, nullptr);
76 ASSERT_EQ(*str_value, "optional");
Wei-Han Chen64e93a32018-12-20 20:07:18 +080077 }
78}
79
80} // namespace runtime_probe