| 1 | //===- unittests/InstallAPI/FileList.cpp - File List Tests ---------------===// |
| 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/InstallAPI/FileList.h" |
| 10 | #include "clang/InstallAPI/HeaderFile.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/Support/MemoryBuffer.h" |
| 13 | #include "llvm/Testing/Support/Error.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace clang::installapi; |
| 18 | |
| 19 | namespace FileListTests { |
| 20 | |
| 21 | static inline void (std::string Input, HeaderSeq &Expected) { |
| 22 | auto InputBuf = MemoryBuffer::getMemBuffer(InputData: Input); |
| 23 | HeaderSeq ; |
| 24 | llvm::Error Err = FileListReader::loadHeaders(InputBuffer: std::move(InputBuf), Destination&: Headers); |
| 25 | ASSERT_THAT_ERROR(std::move(Err), Succeeded()); |
| 26 | |
| 27 | EXPECT_EQ(Expected.size(), Headers.size()); |
| 28 | EXPECT_EQ(Headers, Expected); |
| 29 | } |
| 30 | |
| 31 | TEST(FileList, Version3) { |
| 32 | static const char Input[] = R"({ |
| 33 | "version" : "3", |
| 34 | "headers" : [ |
| 35 | { |
| 36 | "type" : "public", |
| 37 | "path" : "/tmp/dst/usr/include/foo.h", |
| 38 | "language" : "objective-c" |
| 39 | }, |
| 40 | { |
| 41 | "type" : "private", |
| 42 | "path" : "/tmp/dst/usr/local/include/bar.h", |
| 43 | "language" : "objective-c++" |
| 44 | }, |
| 45 | { |
| 46 | "type" : "project", |
| 47 | "path" : "/tmp/src/baz.h" |
| 48 | } |
| 49 | ] |
| 50 | })" ; |
| 51 | |
| 52 | HeaderSeq Expected = { |
| 53 | {"/tmp/dst/usr/include/foo.h" , HeaderType::Public, "foo.h" , |
| 54 | clang::Language::ObjC}, |
| 55 | {"/tmp/dst/usr/local/include/bar.h" , HeaderType::Private, "bar.h" , |
| 56 | clang::Language::ObjCXX}, |
| 57 | {"/tmp/src/baz.h" , HeaderType::Project, "" , std::nullopt}}; |
| 58 | |
| 59 | testValidFileList(Input, Expected); |
| 60 | } |
| 61 | |
| 62 | TEST(FileList, Version1) { |
| 63 | static const char Input[] = R"({ |
| 64 | "version" : "1", |
| 65 | "headers" : [ |
| 66 | { |
| 67 | "type" : "public", |
| 68 | "path" : "/usr/include/foo.h" |
| 69 | }, |
| 70 | { |
| 71 | "type" : "private", |
| 72 | "path" : "/usr/local/include/bar.h" |
| 73 | } |
| 74 | ] |
| 75 | })" ; |
| 76 | |
| 77 | HeaderSeq Expected = { |
| 78 | {"/usr/include/foo.h" , HeaderType::Public, "foo.h" , std::nullopt}, |
| 79 | {"/usr/local/include/bar.h" , HeaderType::Private, "bar.h" , std::nullopt}, |
| 80 | }; |
| 81 | |
| 82 | testValidFileList(Input, Expected); |
| 83 | } |
| 84 | |
| 85 | TEST(FileList, Version2) { |
| 86 | static const auto Input = R"({ |
| 87 | "version" : "2", |
| 88 | "headers" : [ |
| 89 | { |
| 90 | "type" : "public", |
| 91 | "path" : "/usr/include/foo.h" |
| 92 | }, |
| 93 | { |
| 94 | "type" : "project", |
| 95 | "path" : "src/bar.h" |
| 96 | } |
| 97 | ] |
| 98 | })" ; |
| 99 | HeaderSeq Expected = { |
| 100 | {"/usr/include/foo.h" , HeaderType::Public, "foo.h" , std::nullopt}, |
| 101 | {"src/bar.h" , HeaderType::Project, "" , std::nullopt}, |
| 102 | }; |
| 103 | |
| 104 | testValidFileList(Input, Expected); |
| 105 | } |
| 106 | |
| 107 | TEST(FileList, MissingVersion) { |
| 108 | static const char Input[] = R"({ |
| 109 | "headers" : [ |
| 110 | { |
| 111 | "type" : "public", |
| 112 | "path" : "/usr/include/foo.h" |
| 113 | }, |
| 114 | { |
| 115 | "type" : "private", |
| 116 | "path" : "/usr/local/include/bar.h" |
| 117 | } |
| 118 | ] |
| 119 | })" ; |
| 120 | auto InputBuf = MemoryBuffer::getMemBuffer(InputData: Input); |
| 121 | HeaderSeq ; |
| 122 | llvm::Error Err = FileListReader::loadHeaders(InputBuffer: std::move(InputBuf), Destination&: Headers); |
| 123 | EXPECT_THAT_ERROR( |
| 124 | std::move(Err), |
| 125 | FailedWithMessage( |
| 126 | "invalid input format: required field 'version' not specified\n" )); |
| 127 | } |
| 128 | |
| 129 | TEST(FileList, InvalidTypes) { |
| 130 | static const char Input[] = R"({ |
| 131 | "version" : "1", |
| 132 | "headers" : [ |
| 133 | { |
| 134 | "type" : "project", |
| 135 | "path" : "/usr/include/foo.h" |
| 136 | } |
| 137 | ] |
| 138 | })" ; |
| 139 | auto InputBuf = MemoryBuffer::getMemBuffer(InputData: Input); |
| 140 | HeaderSeq ; |
| 141 | llvm::Error Err = FileListReader::loadHeaders(InputBuffer: std::move(InputBuf), Destination&: Headers); |
| 142 | EXPECT_THAT_ERROR( |
| 143 | std::move(Err), |
| 144 | FailedWithMessage("invalid input format: unsupported header type\n" )); |
| 145 | } |
| 146 | } // namespace FileListTests |
| 147 | |