| // Copyright 2026 The GN Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "gn/compatibility_level.h" | |
| #include <stdint.h> | |
| #include <string> | |
| #include "gn/value.h" | |
| #include "util/test/test.h" | |
| TEST(CompatibilityLevelTest, ParseDefault) { | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(nullptr); | |
| ASSERT_TRUE(result.has_value()); | |
| EXPECT_TRUE(*result >= CompatibilityLevel::kCurrent); | |
| EXPECT_FALSE(*result >= CompatibilityLevel::kCurrent + 1); | |
| } | |
| TEST(CompatibilityLevelTest, ParseInvalidType) { | |
| Value value(nullptr, "0"); | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value); | |
| ASSERT_TRUE(result.has_error()); | |
| EXPECT_EQ("This is not a integer.", result.message()); | |
| } | |
| TEST(CompatibilityLevelTest, ParseNegative) { | |
| Value value(nullptr, int64_t{-5}); | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value); | |
| ASSERT_TRUE(result.has_error()); | |
| EXPECT_EQ("Invalid compatibility_level -5 (must be >= 0).", result.message()); | |
| } | |
| TEST(CompatibilityLevelTest, ParseBelowMin) { | |
| int64_t below_min = CompatibilityLevel::kMin - 1; | |
| Value value(nullptr, below_min); | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value); | |
| ASSERT_TRUE(result.has_error()); | |
| if (below_min < 0) { | |
| EXPECT_EQ("Invalid compatibility_level " + std::to_string(below_min) + | |
| " (must be >= 0).", | |
| result.message()); | |
| } else { | |
| EXPECT_EQ("compatibility_level " + std::to_string(below_min) + | |
| " is no longer supported.", | |
| result.message()); | |
| } | |
| } | |
| TEST(CompatibilityLevelTest, ParseMin) { | |
| Value value(nullptr, CompatibilityLevel::kMin); | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value); | |
| ASSERT_TRUE(result.has_value()); | |
| EXPECT_TRUE(*result >= CompatibilityLevel::kMin); | |
| EXPECT_FALSE(*result >= CompatibilityLevel::kMin + 1); | |
| } | |
| TEST(CompatibilityLevelTest, ParseCurrent) { | |
| Value value(nullptr, CompatibilityLevel::kCurrent); | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value); | |
| ASSERT_TRUE(result.has_value()); | |
| EXPECT_TRUE(*result >= CompatibilityLevel::kCurrent); | |
| EXPECT_FALSE(*result >= CompatibilityLevel::kCurrent + 1); | |
| } | |
| TEST(CompatibilityLevelTest, ParseAboveCurrent) { | |
| int64_t above_current = CompatibilityLevel::kCurrent + 1; | |
| Value value(nullptr, above_current); | |
| Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value); | |
| ASSERT_TRUE(result.has_error()); | |
| EXPECT_EQ( | |
| "Unsupported compatibility_level " + std::to_string(above_current) + ".", | |
| result.message()); | |
| } |