Thomas Preud'homme | e038fa7 | 2019-04-15 10:10:11 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/FileCheckTest.cpp - FileCheck 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 "llvm/Support/FileCheck.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | namespace { |
| 14 | |
| 15 | class FileCheckTest : public ::testing::Test {}; |
| 16 | |
| 17 | TEST_F(FileCheckTest, FileCheckContext) { |
| 18 | FileCheckPatternContext Cxt; |
| 19 | std::vector<std::string> GlobalDefines; |
| 20 | |
| 21 | // Define local and global variables from command-line. |
| 22 | GlobalDefines.emplace_back(std::string("LocalVar=FOO")); |
| 23 | Cxt.defineCmdlineVariables(GlobalDefines); |
| 24 | |
| 25 | // Check defined variables are present and undefined is absent. |
| 26 | StringRef LocalVarStr = "LocalVar"; |
| 27 | StringRef UnknownVarStr = "UnknownVar"; |
| 28 | llvm::Optional<StringRef> LocalVar = Cxt.getVarValue(LocalVarStr); |
| 29 | llvm::Optional<StringRef> UnknownVar = Cxt.getVarValue(UnknownVarStr); |
| 30 | EXPECT_TRUE(LocalVar); |
| 31 | EXPECT_EQ(*LocalVar, "FOO"); |
| 32 | EXPECT_FALSE(UnknownVar); |
| 33 | |
| 34 | // Clear local variables and check they become absent. |
| 35 | Cxt.clearLocalVars(); |
| 36 | LocalVar = Cxt.getVarValue(LocalVarStr); |
| 37 | EXPECT_FALSE(LocalVar); |
| 38 | |
| 39 | // Redefine global variables and check variables are defined again. |
| 40 | GlobalDefines.emplace_back(std::string("$GlobalVar=BAR")); |
| 41 | Cxt.defineCmdlineVariables(GlobalDefines); |
| 42 | StringRef GlobalVarStr = "$GlobalVar"; |
| 43 | llvm::Optional<StringRef> GlobalVar = Cxt.getVarValue(GlobalVarStr); |
| 44 | EXPECT_TRUE(GlobalVar); |
| 45 | EXPECT_EQ(*GlobalVar, "BAR"); |
| 46 | |
| 47 | // Clear local variables and check global variables remain defined. |
| 48 | Cxt.clearLocalVars(); |
| 49 | GlobalVar = Cxt.getVarValue(GlobalVarStr); |
| 50 | EXPECT_TRUE(GlobalVar); |
| 51 | } |
| 52 | } // namespace |