xref: /llvm-project/clang/unittests/Basic/DarwinSDKInfoTest.cpp (revision 5f557616c6b13adbe244b3583459a926628c3466)
1 //===- unittests/Basic/DarwinSDKInfoTest.cpp -- SDKSettings.json test -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Basic/DarwinSDKInfo.h"
10 #include "llvm/Support/JSON.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 using namespace clang;
15 
16 TEST(DarwinSDKInfoTest, ParseAndTestMapping) {
17   llvm::json::Object Obj;
18   Obj["Version"] = "11.0";
19   Obj["MaximumDeploymentTarget"] = "11.99";
20   llvm::json::Object VersionMap;
21   VersionMap["10.15"] = "13.1";
22   VersionMap["11.0"] = "14.0";
23   VersionMap["11.2"] = "14.2";
24   llvm::json::Object MacOS2iOSMac;
25   MacOS2iOSMac["macOS_iOSMac"] = std::move(VersionMap);
26   Obj["VersionMap"] = std::move(MacOS2iOSMac);
27 
28   auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj);
29   ASSERT_TRUE(SDKInfo);
30   EXPECT_EQ(SDKInfo->getVersion(), VersionTuple(11, 0));
31 
32   auto Mapping = SDKInfo->getVersionMapping(
33       DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair());
34   ASSERT_TRUE(Mapping);
35   // Verify that the macOS versions that are present in the map are translated
36   // directly to their corresponding Mac Catalyst versions.
37   EXPECT_EQ(*Mapping->map(VersionTuple(10, 15), VersionTuple(), None),
38             VersionTuple(13, 1));
39   EXPECT_EQ(*Mapping->map(VersionTuple(11, 0), VersionTuple(), None),
40             VersionTuple(14, 0));
41   EXPECT_EQ(*Mapping->map(VersionTuple(11, 2), VersionTuple(), None),
42             VersionTuple(14, 2));
43 
44   // Verify that a macOS version that's not present in the map is translated
45   // like the nearest major OS version.
46   EXPECT_EQ(*Mapping->map(VersionTuple(11, 1), VersionTuple(), None),
47             VersionTuple(14, 0));
48 
49   // Verify that the macOS versions that are outside of the mapped version
50   // range map to the min/max values passed to the `map` call.
51   EXPECT_EQ(*Mapping->map(VersionTuple(10, 14), VersionTuple(99, 99), None),
52             VersionTuple(99, 99));
53   EXPECT_EQ(
54       *Mapping->map(VersionTuple(11, 5), VersionTuple(), VersionTuple(99, 99)),
55       VersionTuple(99, 99));
56   EXPECT_EQ(*Mapping->map(VersionTuple(11, 5), VersionTuple(99, 98),
57                           VersionTuple(99, 99)),
58             VersionTuple(99, 99));
59 }
60 
61 TEST(DarwinSDKInfoTest, MissingKeys) {
62   llvm::json::Object Obj;
63   ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj));
64   Obj["Version"] = "11.0";
65   ASSERT_FALSE(DarwinSDKInfo::parseDarwinSDKSettingsJSON(&Obj));
66 }
67