File: serialize_host_descriptions_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 4,324 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/devtools/serialize_host_descriptions.h"

#include <array>
#include <optional>
#include <utility>
#include <vector>

#include "base/values.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Optional;
using ::testing::UnorderedElementsAre;

namespace {

HostDescriptionNode GetNodeWithLabel(const char* name, int label) {
  HostDescriptionNode node = {name, std::string(),
                              base::Value(base::Value::Type::DICT)};
  node.representation.GetDict().Set("label", label);
  return node;
}

// Returns the list of children of |arg|.
std::optional<base::Value::List> GetChildren(const base::Value& arg) {
  EXPECT_TRUE(arg.is_dict());
  const base::Value::Dict& dict = arg.GetDict();

  const base::Value* children = dict.Find("children");
  if (!children)
    return std::nullopt;
  EXPECT_EQ(base::Value::Type::LIST, children->type());
  return children->GetList().Clone();
}

// Checks that |arg| is a description of a node with label |l|.
bool CheckLabel(const base::Value& arg, int l) {
  EXPECT_TRUE(arg.is_dict());
  const base::Value::Dict& dict = arg.GetDict();
  std::optional<int> result = dict.FindInt("label");
  if (!result)
    return false;
  return l == *result;
}

// Matches every |arg| with label |label| and checks that it has no children.
MATCHER_P(EmptyNode, label, "") {
  if (!CheckLabel(arg, label))
    return false;
  EXPECT_EQ(GetChildren(arg), std::nullopt);
  return true;
}

}  // namespace

TEST(SerializeHostDescriptionTest, Empty) {
  base::Value::List result =
      SerializeHostDescriptions(std::vector<HostDescriptionNode>(), "123");
  EXPECT_THAT(result, ::testing::IsEmpty());
}

// Test serializing a forest of stubs (no edges).
TEST(SerializeHostDescriptionTest, Stubs) {
  std::vector<HostDescriptionNode> nodes;
  nodes.emplace_back(GetNodeWithLabel("1", 1));
  nodes.emplace_back(GetNodeWithLabel("2", 2));
  nodes.emplace_back(GetNodeWithLabel("3", 3));
  base::Value::List result =
      SerializeHostDescriptions(std::move(nodes), "children");
  EXPECT_THAT(result,
              UnorderedElementsAre(EmptyNode(1), EmptyNode(2), EmptyNode(3)));
}

// Test handling multiple nodes sharing the same name.
TEST(SerializeHostDescriptionTest, SameNames) {
  std::vector<HostDescriptionNode> nodes;
  nodes.emplace_back(GetNodeWithLabel("A", 1));
  nodes.emplace_back(GetNodeWithLabel("A", 2));
  nodes.emplace_back(GetNodeWithLabel("A", 3));
  nodes.emplace_back(GetNodeWithLabel("B", 4));
  nodes.emplace_back(GetNodeWithLabel("C", 5));

  base::Value::List result =
      SerializeHostDescriptions(std::move(nodes), "children");

  // Only the first node called "A", and both nodes "B" and "C" should be
  // returned.
  EXPECT_THAT(result,
              UnorderedElementsAre(EmptyNode(1), EmptyNode(4), EmptyNode(5)));
}

// Test serializing a small forest, of this structure:
// 5 -- 2 -- 4
// 0 -- 6
//   \ 1
//   \ 3

namespace {

// Matchers for non-empty nodes specifically in this test:
MATCHER(Node2, "") {
  if (!CheckLabel(arg, 2))
    return false;
  EXPECT_THAT(GetChildren(arg), Optional(UnorderedElementsAre(EmptyNode(4))));
  return true;
}

MATCHER(Node5, "") {
  if (!CheckLabel(arg, 5))
    return false;
  EXPECT_THAT(GetChildren(arg), Optional(UnorderedElementsAre(Node2())));
  return true;
}

MATCHER(Node0, "") {
  if (!CheckLabel(arg, 0))
    return false;
  EXPECT_THAT(GetChildren(arg), Optional(UnorderedElementsAre(
                                    EmptyNode(1), EmptyNode(3), EmptyNode(6))));
  return true;
}

}  // namespace

TEST(SerializeHostDescriptionTest, Forest) {
  std::vector<HostDescriptionNode> nodes(7);
  auto kNames = std::to_array<const char*>({"0", "1", "2", "3", "4", "5", "6"});
  for (size_t i = 0; i < 7; ++i) {
    nodes[i] = GetNodeWithLabel(kNames[i], i);
  }
  nodes[2].parent_name = "5";
  nodes[4].parent_name = "2";
  nodes[6].parent_name = "0";
  nodes[1].parent_name = "0";
  nodes[3].parent_name = "0";

  base::Value::List result =
      SerializeHostDescriptions(std::move(nodes), "children");

  EXPECT_THAT(result, UnorderedElementsAre(Node0(), Node5()));
}