File: idl_schemas_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 (396 lines) | stat: -rw-r--r-- 14,437 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <utility>
#include <vector>

#include "base/strings/utf_string_conversions.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/json_schema_compiler/test/idl_basics.h"
#include "tools/json_schema_compiler/test/idl_object_types.h"
#include "tools/json_schema_compiler/test/idl_properties.h"
#include "tools/json_schema_compiler/test/test_util.h"

using test::api::idl_basics::MyType1;
using test::api::idl_object_types::BarType;
using test::api::idl_object_types::FooType;

namespace Function2 = test::api::idl_basics::Function2;
namespace Function3 = test::api::idl_basics::Function3;
namespace Function4 = test::api::idl_basics::Function4;
namespace Function5 = test::api::idl_basics::Function5;
namespace Function6 = test::api::idl_basics::Function6;
namespace Function7 = test::api::idl_basics::Function7;
namespace Function8 = test::api::idl_basics::Function8;
namespace Function9 = test::api::idl_basics::Function9;
namespace Function10 = test::api::idl_basics::Function10;
namespace Function11 = test::api::idl_basics::Function11;
namespace ObjectFunction1 = test::api::idl_object_types::ObjectFunction1;

namespace {

// Parses `idl_basics::ManifestKeys` from the provided `key_values`, using
// stub values for any omitted top-level keys.
bool ParseManifestKeys(const std::string& key_values,
                       test::api::idl_basics::ManifestKeys& manifest_keys,
                       std::u16string& error) {
  // ManifestKeys specify a number of different required keys. In order to allow
  // tests to focus on testing one particular value, rather than all of them,
  // we provide a default "stub" with valid values. Any values passed into
  // `key_values` will overwrite the values in this stub.
  static constexpr char kStubValuesStr[] =
      R"({
           "key_str": "my key",
           "key_ref": {"x": "my ref"},
           "inline_choice": 3,
           "choice_with_arrays": {"entries": "choice"},
           "choice_with_optional": {"entries": "choice"}
         })";
  base::Value::Dict dict_value = base::test::ParseJsonDict(kStubValuesStr);
  base::Value::Dict provided_values = base::test::ParseJsonDict(key_values);

  // Annoying: `base::Value::Dict` values are recursively merged in
  // `base::Value::Dict::Merge()`, rather than overwritten. Remove them from
  // the `dict_value` if a new value was provided for them.
  if (provided_values.contains("choice_with_arrays")) {
    dict_value.Remove("choice_with_arrays");
  }
  if (provided_values.contains("choice_with_optional")) {
    dict_value.Remove("choice_with_optional");
  }
  dict_value.Merge(std::move(provided_values));

  return test::api::idl_basics::ManifestKeys::ParseFromDictionary(
      dict_value, manifest_keys, error);
}

// Parses `idl_basics::ManifestKeys` from the provided `key_values`, expecting
// success and returning the parsed value.
test::api::idl_basics::ManifestKeys ParseManifestKeysAndReturnValue(
    const std::string& key_values) {
  test::api::idl_basics::ManifestKeys manifest_keys;
  std::u16string error;
  bool result = ParseManifestKeys(key_values, manifest_keys, error);
  EXPECT_TRUE(result) << "Parsing failed.\nValue:\n"
                      << key_values << "\nError: " << base::UTF16ToUTF8(error);

  return manifest_keys;
}

// Attempts to parse `idl_basics::ManifestKeys` from the provided `key_values`,
// expecting failure and returning the encountered parse error.
std::string ParseManifestKeysAndReturnError(const std::string& key_values) {
  test::api::idl_basics::ManifestKeys manifest_keys;
  std::u16string error;
  bool result = ParseManifestKeys(key_values, manifest_keys, error);
  EXPECT_FALSE(result) << "Parsing unexpected succeeded";
  return result ? "<no error>" : base::UTF16ToUTF8(error);
}

}  // namespace

TEST(IdlCompiler, Basics) {
  // Test MyType1.
  static_assert(!std::is_copy_constructible_v<MyType1>);
  static_assert(!std::is_copy_assignable_v<MyType1>);
  static_assert(std::is_move_constructible_v<MyType1>);
  static_assert(std::is_move_assignable_v<MyType1>);

  MyType1 a;
  a.x = 5;
  a.y = std::string("foo");
  auto b = MyType1::FromValue(a.ToValue());
  ASSERT_TRUE(b);
  EXPECT_EQ(a.x, b->x);
  EXPECT_EQ(a.y, b->y);
  EXPECT_EQ(a.Clone().ToValue(), a.ToValue());

  // Test Function2, which takes an integer parameter.
  base::Value::List list;
  list.Append(5);
  std::optional<Function2::Params> f2_params = Function2::Params::Create(list);
  EXPECT_EQ(5, f2_params->x);

  // Test Function3, which takes a MyType1 parameter.
  list.clear();
  base::Value::Dict tmp;
  tmp.Set("x", 17);
  tmp.Set("y", "hello");
  tmp.Set("z", "zstring");
  tmp.Set("a", "astring");
  tmp.Set("b", "bstring");
  tmp.Set("c", "cstring");
  list.Append(base::Value(std::move(tmp)));
  std::optional<Function3::Params> f3_params = Function3::Params::Create(list);
  EXPECT_EQ(17, f3_params->arg.x);
  EXPECT_EQ("hello", f3_params->arg.y);

  // Test functions that take a callback function as a parameter, with varying
  // callback signatures.
  base::Value::List f4_results = Function4::Results::Create();
  base::Value::List expected;
  EXPECT_EQ(expected, f4_results);

  base::Value::List f5_results = Function5::Results::Create(13);
  ASSERT_EQ(1u, f5_results.size());
  EXPECT_TRUE(f5_results[0].is_int());

  base::Value::List f6_results = Function6::Results::Create(a);
  ASSERT_EQ(1u, f6_results.size());
  auto c = MyType1::FromValue(f6_results[0]);
  ASSERT_TRUE(c);
  EXPECT_EQ(a.x, c->x);
  EXPECT_EQ(a.y, c->y);
}

TEST(IdlCompiler, OptionalArguments) {
  // Test a function that takes one optional argument, both without and with
  // that argument.
  base::Value::List list;
  std::optional<Function7::Params> f7_params = Function7::Params::Create(list);
  EXPECT_FALSE(f7_params->arg.has_value());
  list.Append(7);
  f7_params = Function7::Params::Create(list);
  EXPECT_EQ(7, *(f7_params->arg));

  // Similar to above, but a function with one required and one optional
  // argument.
  list.clear();
  list.Append(8);
  std::optional<Function8::Params> f8_params = Function8::Params::Create(list);
  EXPECT_EQ(8, f8_params->arg1);
  EXPECT_FALSE(f8_params->arg2.has_value());
  list.Append("foo");
  f8_params = Function8::Params::Create(list);
  EXPECT_EQ(8, f8_params->arg1);
  EXPECT_EQ("foo", *(f8_params->arg2));

  // Test a function with an optional argument of custom type.
  list.clear();
  std::optional<Function9::Params> f9_params = Function9::Params::Create(list);
  EXPECT_FALSE(f9_params->arg.has_value());
  list.clear();
  base::Value::Dict tmp;
  tmp.Set("x", 17);
  tmp.Set("y", "hello");
  tmp.Set("z", "zstring");
  tmp.Set("a", "astring");
  tmp.Set("b", "bstring");
  tmp.Set("c", "cstring");
  list.Append(base::Value(std::move(tmp)));
  f9_params = Function9::Params::Create(list);
  ASSERT_TRUE(f9_params->arg);
  const MyType1& t1 = *f9_params->arg;
  EXPECT_EQ(17, t1.x);
  EXPECT_EQ("hello", t1.y);
}

TEST(IdlCompiler, ArrayTypes) {
  // Tests of a function that takes an integer and an array of integers. First
  // use an empty array.
  base::Value::List list;
  list.Append(33);
  list.Append(base::Value::List());
  std::optional<Function10::Params> f10_params =
      Function10::Params::Create(list);
  ASSERT_TRUE(f10_params != std::nullopt);
  EXPECT_EQ(33, f10_params->x);
  EXPECT_TRUE(f10_params->y.empty());

  // Same function, but this time with 2 values in the array.
  list.clear();
  list.Append(33);
  base::Value::List sublist;
  sublist.Append(34);
  sublist.Append(35);
  list.Append(std::move(sublist));
  f10_params = Function10::Params::Create(list);
  ASSERT_TRUE(f10_params != std::nullopt);
  EXPECT_EQ(33, f10_params->x);
  ASSERT_EQ(2u, f10_params->y.size());
  EXPECT_EQ(34, f10_params->y[0]);
  EXPECT_EQ(35, f10_params->y[1]);

  // Now test a function which takes an array of a defined type.
  list.clear();
  MyType1 a;
  MyType1 b;
  a.x = 5;
  b.x = 6;
  a.y = std::string("foo");
  b.y = std::string("bar");
  base::Value::List sublist2;
  sublist2.Append(base::Value(a.ToValue()));
  sublist2.Append(base::Value(b.ToValue()));
  list.Append(std::move(sublist2));
  std::optional<Function11::Params> f11_params =
      Function11::Params::Create(list);
  ASSERT_TRUE(f11_params != std::nullopt);
  ASSERT_EQ(2u, f11_params->arg.size());
  EXPECT_EQ(5, f11_params->arg[0].x);
  EXPECT_EQ("foo", f11_params->arg[0].y);
  EXPECT_EQ(6, f11_params->arg[1].x);
  EXPECT_EQ("bar", f11_params->arg[1].y);
}

TEST(IdlCompiler, ObjectTypes) {
  // Test the FooType type.
  FooType f1;
  f1.x = 3;
  auto f2 = FooType::FromValue(f1.ToValue());
  ASSERT_TRUE(f2);
  EXPECT_EQ(f1.x, f2->x);

  // Test the BarType type.
  BarType b1;
  b1.x = base::Value(7);
  auto b2 = BarType::FromValue(b1.ToValue());
  ASSERT_TRUE(b2);
  ASSERT_TRUE(b2->x.is_int());
  EXPECT_EQ(7, b2->x.GetInt());
  EXPECT_FALSE(b2->y.has_value());

  // Test the params to the ObjectFunction1 function.
  base::Value::Dict icon_props;
  icon_props.Set("hello", "world");
  ASSERT_TRUE(ObjectFunction1::Params::Icon::FromValue(icon_props));
  base::Value::List list;
  list.Append(std::move(icon_props));
  std::optional<ObjectFunction1::Params> params =
      ObjectFunction1::Params::Create(list);
  ASSERT_TRUE(params != std::nullopt);
  const std::string* tmp =
      params->icon.additional_properties.FindString("hello");
  ASSERT_TRUE(tmp);
  EXPECT_EQ("world", *tmp);
}

// Tests using IDL "choices" in manifest key-specified types.
TEST(IdlCompiler, ManifestKeys_Choices) {
  {
    // String entry for an inline choice.
    static constexpr char kManifestKeys[] =
        R"({"inline_choice": "string value"})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    EXPECT_EQ("string value", manifest_keys.inline_choice.as_string);
    EXPECT_EQ(std::nullopt, manifest_keys.inline_choice.as_integer);
  }

  {
    // Single entry for non-optional choices.
    static constexpr char kManifestKeys[] =
        R"({"choice_with_arrays": {"entries": "single entry"}})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    EXPECT_EQ("single entry",
              manifest_keys.choice_with_arrays.entries.as_string);
    EXPECT_EQ(std::nullopt,
              manifest_keys.choice_with_arrays.entries.as_strings);
  }

  {
    // Single entry for optional choices.
    static constexpr char kManifestKeys[] =
        R"({"choice_with_optional": {"entries": "single entry"}})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    ASSERT_TRUE(manifest_keys.choice_with_optional.entries);
    EXPECT_EQ("single entry",
              manifest_keys.choice_with_optional.entries->as_string);
    EXPECT_EQ(std::nullopt,
              manifest_keys.choice_with_optional.entries->as_strings);
  }

  {
    // Integer entry for an inline choice.
    static constexpr char kManifestKeys[] = R"({"inline_choice": 42})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    EXPECT_EQ(std::nullopt, manifest_keys.inline_choice.as_string);
    EXPECT_EQ(42, manifest_keys.inline_choice.as_integer);
  }

  {
    // List entry for non-optional choices.
    static constexpr char kManifestKeys[] =
        R"({"choice_with_arrays": {"entries": ["entry1", "entry2"]}})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    EXPECT_EQ(std::nullopt, manifest_keys.choice_with_arrays.entries.as_string);
    EXPECT_THAT(*manifest_keys.choice_with_arrays.entries.as_strings,
                testing::ElementsAre("entry1", "entry2"));
  }

  {
    // List entry for optional choices.
    static constexpr char kManifestKeys[] =
        R"({"choice_with_optional": {"entries": ["entry1", "entry2"]}})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    ASSERT_TRUE(manifest_keys.choice_with_optional.entries);
    EXPECT_EQ(std::nullopt,
              manifest_keys.choice_with_optional.entries->as_string);
    EXPECT_THAT(*manifest_keys.choice_with_optional.entries->as_strings,
                testing::ElementsAre("entry1", "entry2"));
  }

  {
    // Omitted optional choice.
    static constexpr char kManifestKeys[] = R"({"choice_with_optional": { }})";
    test::api::idl_basics::ManifestKeys manifest_keys =
        ParseManifestKeysAndReturnValue(kManifestKeys);
    EXPECT_EQ(std::nullopt, manifest_keys.choice_with_optional.entries);
  }

  {
    // Invalid entry for an inline choice.
    static constexpr char kManifestKeys[] = R"({"inline_choice": ["a", "b"]})";
    EXPECT_EQ(
        "Error at key 'inline_choice'. "
        "Provided value matches none of the allowed options.",
        ParseManifestKeysAndReturnError(kManifestKeys));
  }

  {
    // Error: Invalid entry for `choice_with_arrays`.
    static constexpr char kManifestKeys[] =
        R"({"choice_with_arrays": {"entries": 3}})";

    EXPECT_EQ(
        "Error at key 'choice_with_arrays.entries'. "
        "Provided value matches none of the allowed options.",
        ParseManifestKeysAndReturnError(kManifestKeys));
  }

  {
    // Error: Invalid entry for `choice_with_optional`.
    static constexpr char kManifestKeys[] =
        R"({"choice_with_optional": {"entries": 3}})";

    EXPECT_EQ(
        "Error at key 'choice_with_optional.entries'. "
        "Provided value matches none of the allowed options.",
        ParseManifestKeysAndReturnError(kManifestKeys));
  }

  {
    // Error: Omitted non-optional choices value.
    static constexpr char kManifestKeys[] = R"({"choice_with_arrays": { }})";

    EXPECT_EQ(
        "Error at key 'choice_with_arrays.entries'. Manifest key is required.",
        ParseManifestKeysAndReturnError(kManifestKeys));
  }
}

TEST(IdlCompiler, PropertyValues) {
  EXPECT_EQ(42, test::api::idl_properties::first);
  EXPECT_EQ(42.1, test::api::idl_properties::second);
  EXPECT_STREQ("hello world", test::api::idl_properties::third);
}