chromium/tools/json_schema_compiler/test/simple_api_unittest.cc

// 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 "tools/json_schema_compiler/test/simple_api.h"

#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.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/enums.h"

namespace simple_api = test::api::simple_api;
namespace enums = test::api::enums;

namespace {

static base::Value::Dict CreateTestTypeDictionary() {}

void GetManifestParseError(std::string_view manifest_json, std::string* error) {}

void PopulateManifestKeys(std::string_view manifest_json,
                          simple_api::ManifestKeys* manifest_keys) {}

}  // namespace

TEST() {
  base::Value results(simple_api::IncrementInteger::Results::Create(5));
  base::Value::List expected;
  expected.Append(5);
  EXPECT_EQ();
}

TEST() {
  base::Value::List params_value;
  params_value.Append(6);
  std::optional<simple_api::IncrementInteger::Params> params(
      simple_api::IncrementInteger::Params::Create(params_value));
  EXPECT_TRUE();
  EXPECT_EQ();
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append("text");
    params_value.Append("text");
    std::optional<simple_api::OptionalString::Params> params(
        simple_api::OptionalString::Params::Create(params_value));
    EXPECT_FALSE();
  }
  {
    base::Value::List params_value;
    std::optional<simple_api::IncrementInteger::Params> params(
        simple_api::IncrementInteger::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    std::optional<simple_api::OptionalString::Params> params(
        simple_api::OptionalString::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_FALSE();
  }
  {
    base::Value::List params_value;
    params_value.Append("asdf");
    std::optional<simple_api::OptionalString::Params> params(
        simple_api::OptionalString::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_TRUE();
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append(base::Value());
    std::optional<simple_api::OptionalString::Params> params(
        simple_api::OptionalString::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append(5);
    std::optional<simple_api::OptionalString::Params> params(
        simple_api::OptionalString::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append(base::Value());
    params_value.Append("asdf");
    std::optional<simple_api::OptionalBeforeRequired::Params> params(
        simple_api::OptionalBeforeRequired::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_FALSE();
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append(base::Value::Dict());
    params_value.Append("asdf");
    std::optional<simple_api::RequiredFunctionParameter::Params> params(
        simple_api::RequiredFunctionParameter::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_TRUE();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append(5);
    params_value.Append("asdf");
    std::optional<simple_api::RequiredFunctionParameter::Params> params(
        simple_api::RequiredFunctionParameter::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  base::Value results(simple_api::OptionalString::Results::Create());
  base::Value::List expected;
  EXPECT_EQ();
}

TEST() {
  {
    base::Value::Dict value = CreateTestTypeDictionary();
    auto test_type = simple_api::TestType::FromValue(value);
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::Dict value = CreateTestTypeDictionary();
    value.Remove("number");
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::Dict value = CreateTestTypeDictionary();
    auto test_type = simple_api::TestType::FromValue(value.Clone());
    ASSERT_TRUE();
    base::Value::List results =
        simple_api::GetTestType::Results::Create(*test_type);
    ASSERT_EQ();
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value results(simple_api::OnIntegerFired::Create(5));
    base::Value::List expected;
    expected.Append(5);
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value results(simple_api::OnStringFired::Create("yo dawg"));
    base::Value::List expected;
    expected.Append("yo dawg");
    EXPECT_EQ();
  }
}

TEST() {
  {
    simple_api::TestType some_test_type;
    base::Value::Dict expected = CreateTestTypeDictionary();

    std::optional<double> number_value = expected.FindDouble("number");
    ASSERT_TRUE();
    some_test_type.number = *number_value;

    const std::string* string_value = expected.FindString("string");
    ASSERT_TRUE();
    some_test_type.string = *string_value;

    std::optional<int> int_value = expected.FindInt("integer");
    ASSERT_TRUE();
    some_test_type.integer = *int_value;

    std::optional<bool> boolean_value = expected.FindBool("boolean");
    ASSERT_TRUE();
    some_test_type.boolean = *boolean_value;

    base::Value results(simple_api::OnTestTypeFired::Create(some_test_type));
    ASSERT_TRUE();
    ASSERT_EQ();
    EXPECT_EQ();
  }
}

TEST() {
  const char kPartialManifestJson[] = R"({
    "key_string": "abc",
    "key_ref": {
      "string": "ref_string",
      "boolean": true,
      "number": 25.4
    }
  })";

  std::string error;
  ASSERT_NO_FATAL_FAILURE();
  EXPECT_EQ();
}

TEST() {
  const char kPartialManifestJson[] = R"({
    "key_string": "abc",
    "key_ref": {
      "string": "ref_string",
      "boolean": true,
      "number": 25.4,
      "integer": 32,
      "object": {
        "foo": "bar"
      },
      "key_enum": "one",
      "key_enum_array": ["two"]
    }
  })";

  std::string error;
  ASSERT_NO_FATAL_FAILURE();
  EXPECT_EQ();
}

TEST() {
  const char kPartialManifestJson[] = R"({
    "key_string": "abc",
    "key_ref": {
      "string": "ref_string",
      "boolean": true,
      "number": 25.4,
      "integer": 32,
      "array": ["one", "two", 3]
    },
    "key_enum": "one",
    "key_enum_array": ["two"]
  })";

  std::string error;
  ASSERT_NO_FATAL_FAILURE();
  EXPECT_EQ();
}

TEST() {
  {
    const char kPartialManifestJson[] = R"({
      "key_string": "abc",
      "key_ref": {
        "string": "ref_string",
        "boolean": true,
        "number": 25.4,
        "integer": 32,
        "array": ["one", "two"]
      },
      "key_enum": "one",
      "key_enum_array": ["two", false]
    })";

    std::string error;
    ASSERT_NO_FATAL_FAILURE();
    EXPECT_EQ();
  }
  {
    const char kPartialManifestJson[] = R"({
      "key_string": "abc",
      "key_ref": {
        "string": "ref_string",
        "boolean": true,
        "number": 25.4,
        "integer": 32,
        "array": ["one", "two"]
      },
      "key_enum": "one",
      "key_enum_array": [],
      "key_obj": {
        "obj_string": "foo",
        "obj_bool": true,
        "obj_optional_enum_array": ["one", "invalid_value"]
      }
    })";

    std::string error;
    ASSERT_NO_FATAL_FAILURE();
    EXPECT_EQ();
  }
  {
    const char kPartialManifestJson[] = R"({
      "key_string": "abc",
      "key_ref": {
        "string": "ref_string",
        "boolean": true,
        "number": 25.4,
        "integer": 32,
        "array": ["one", "two"]
      },
      "key_enum": "one",
      "key_enum_array": [],
      "key_obj": {
        "obj_string": "foo",
        "obj_bool": true,
        "obj_optional_enum_array": false
      }
    })";

    std::string error;
    ASSERT_NO_FATAL_FAILURE();
    EXPECT_EQ();
  }
}

TEST() {}

TEST() {
  const char kPartialManifestJson[] = R"({
    "key_string": "abc",
    "key_ref": {
      "string": "ref_string",
      "boolean": true,
      "number": 25.4,
      "integer": 32,
      "opt_external_enum": "four"
    },
    "key_enum": "one",
    "key_enum_array": ["two"]
  })";

  std::string error;
  ASSERT_NO_FATAL_FAILURE();
  EXPECT_EQ();
}

TEST() {
  const char kPartialManifestJson[] = R"({
    "key_string": "abc",
    "key_ref": {
      "string": "ref_string",
      "boolean": true,
      "number": 25.4,
      "integer": 32,
      "object": {
        "foo": 42
      },
      "array": ["one", "two"],
      "opt_external_enum": "two"
    },
    "key_obj": {
      "obj_string": "foo",
      "obj_bool": true,
      "obj_optional_enum_array": ["three"]
    },
    "key_enum": "one",
    "key_enum_array": ["two", "one"],
    "3d_key": "yes"
  })";

  simple_api::ManifestKeys manifest_keys;
  ASSERT_NO_FATAL_FAILURE();

  EXPECT_EQ();

  ASSERT_TRUE();
  EXPECT_EQ();
  EXPECT_TRUE();
  ASSERT_TRUE();
  EXPECT_THAT();

  EXPECT_EQ();

  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_DOUBLE_EQ();
  EXPECT_EQ();
  ASSERT_TRUE();
  EXPECT_EQ();
  ASSERT_TRUE();
  EXPECT_THAT();
  EXPECT_EQ();
  EXPECT_THAT();
  EXPECT_EQ();
}

// Ensure leaving out optional keys is not a manifest parse error.
TEST() {
  const char kPartialManifestJson[] = R"({
    "key_string": "abc",
    "key_ref": {
      "string": "ref_string",
      "boolean": true,
      "number": 25.4,
      "integer": 32
    },
    "key_enum": "two",
    "key_enum_array": ["one"]
  })";

  simple_api::ManifestKeys manifest_keys;
  ASSERT_NO_FATAL_FAILURE();

  EXPECT_EQ();
  EXPECT_FALSE();
  EXPECT_EQ();

  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_DOUBLE_EQ();
  EXPECT_EQ();
  EXPECT_FALSE();
  EXPECT_EQ();
  EXPECT_EQ();
}