chromium/tools/json_schema_compiler/test/enums_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/enums.h"

#include <vector>

#include "base/types/cxx23_to_underlying.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/json_schema_compiler/test/test_util.h"

namespace enums = test::api::enums;
Dictionary;
List;

TEST() {
  {
    base::Value value = Dictionary("type", base::Value("one"));
    auto enum_type = enums::EnumType::FromValue(value.GetDict());
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value value = Dictionary("type", base::Value("invalid"));
    EXPECT_FALSE();
  }
}

TEST() {
  static_assert(0 == base::to_underlying(enums::Enumeration::kNone));

  {
    base::Value::List args;
    args.Append("one");

    std::optional<enums::TakesEnumAsType::Params> params(
        enums::TakesEnumAsType::Params::Create(args));
    ASSERT_TRUE();
    EXPECT_EQ();

    EXPECT_EQ();
  }
  {
    enums::HasEnumeration enumeration;
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::Dict value;
    auto enumeration = enums::HasEnumeration::FromValue(value);
    ASSERT_FALSE();

    value.Set("enumeration", "one");
    enumeration = enums::HasEnumeration::FromValue(value);
    ASSERT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();

    value.Set("optional_enumeration", "two");
    enumeration = enums::HasEnumeration::FromValue(value);
    ASSERT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::Dict value;
    auto enumeration = enums::ReferenceEnum::FromValue(value);
    ASSERT_FALSE();

    value.Set("reference_enum", "one");
    enumeration = enums::ReferenceEnum::FromValue(value);
    ASSERT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append(List(base::Value("one"), base::Value("two")));
    std::optional<enums::TakesEnumArrayAsType::Params> params(
        enums::TakesEnumArrayAsType::Params::Create(params_value));
    ASSERT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append(List(base::Value("invalid")));
    std::optional<enums::TakesEnumArrayAsType::Params> params(
        enums::TakesEnumArrayAsType::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    enums::Enumeration state = enums::Enumeration::kOne;
    auto result = std::make_unique<base::Value>(ToString(state));
    auto expected = std::make_unique<base::Value>("one");
    EXPECT_EQ();
  }
  {
    enums::Enumeration state = enums::Enumeration::kOne;
    base::Value results(enums::ReturnsEnum::Results::Create(state));
    base::Value::List expected;
    expected.Append("one");
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value results(enums::ReturnsTwoEnums::Results::Create(
        enums::Enumeration::kOne, enums::OtherEnumeration::kHam));
    base::Value::List expected;
    expected.Append("one");
    expected.Append("ham");
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value value = Dictionary("type", base::Value("two"));
    auto enum_type = enums::OptionalEnumType::FromValue(value.GetDict());
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value value(base::Value::Type::DICT);
    auto enum_type = enums::OptionalEnumType::FromValue(value.GetDict());
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value value = Dictionary("type", base::Value("invalid"));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append("two");
    std::optional<enums::TakesEnum::Params> params(
        enums::TakesEnum::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append("invalid");
    std::optional<enums::TakesEnum::Params> params(
        enums::TakesEnum::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append(List(base::Value("one"), base::Value("two")));
    std::optional<enums::TakesEnumArray::Params> params(
        enums::TakesEnumArray::Params::Create(params_value));
    ASSERT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append(List(base::Value("invalid")));
    std::optional<enums::TakesEnumArray::Params> params(
        enums::TakesEnumArray::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append("three");
    std::optional<enums::TakesOptionalEnum::Params> params(
        enums::TakesOptionalEnum::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    std::optional<enums::TakesOptionalEnum::Params> params(
        enums::TakesOptionalEnum::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append("invalid");
    std::optional<enums::TakesOptionalEnum::Params> params(
        enums::TakesOptionalEnum::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    base::Value::List params_value;
    params_value.Append("one");
    params_value.Append("ham");
    std::optional<enums::TakesMultipleOptionalEnums::Params> params(
        enums::TakesMultipleOptionalEnums::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append("one");
    std::optional<enums::TakesMultipleOptionalEnums::Params> params(
        enums::TakesMultipleOptionalEnums::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    std::optional<enums::TakesMultipleOptionalEnums::Params> params(
        enums::TakesMultipleOptionalEnums::Params::Create(params_value));
    EXPECT_TRUE();
    EXPECT_EQ();
    EXPECT_EQ();
  }
  {
    base::Value::List params_value;
    params_value.Append("three");
    params_value.Append("invalid");
    std::optional<enums::TakesMultipleOptionalEnums::Params> params(
        enums::TakesMultipleOptionalEnums::Params::Create(params_value));
    EXPECT_FALSE();
  }
}

TEST() {
  {
    enums::Enumeration some_enum = enums::Enumeration::kOne;
    auto result = std::make_unique<base::Value>(ToString(some_enum));
    auto expected = std::make_unique<base::Value>("one");
    EXPECT_EQ();
  }
  {
    enums::Enumeration some_enum = enums::Enumeration::kOne;
    base::Value results(enums::OnEnumFired::Create(some_enum));
    base::Value::List expected;
    expected.Append("one");
    EXPECT_EQ();
  }
}

TEST() {
  {
    base::Value results(enums::OnTwoEnumsFired::Create(
        enums::Enumeration::kOne, enums::OtherEnumeration::kHam));
    base::Value::List expected;
    expected.Append("one");
    expected.Append("ham");
    EXPECT_EQ();
  }
}

TEST() {
  // This test validates that we keep consistent kCaptalisation for different
  // formats for the value the enum is mapping.
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
  EXPECT_EQ();
}