Home | History | Annotate | Download | only in Support
      1 //===- llvm/unittest/Support/GlobPatternTest.cpp --------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/Support/GlobPattern.h"
     11 #include "gtest/gtest.h"
     12 
     13 using namespace llvm;
     14 namespace {
     15 
     16 class GlobPatternTest : public ::testing::Test {};
     17 
     18 TEST_F(GlobPatternTest, Basics) {
     19   Expected<GlobPattern> Pat1 = GlobPattern::create("");
     20   EXPECT_TRUE((bool)Pat1);
     21   EXPECT_TRUE(Pat1->match(""));
     22   EXPECT_FALSE(Pat1->match("a"));
     23 
     24   Expected<GlobPattern> Pat2 = GlobPattern::create("ab*c*def");
     25   EXPECT_TRUE((bool)Pat2);
     26   EXPECT_TRUE(Pat2->match("abcdef"));
     27   EXPECT_TRUE(Pat2->match("abxcxdef"));
     28   EXPECT_FALSE(Pat2->match(""));
     29   EXPECT_FALSE(Pat2->match("xabcdef"));
     30   EXPECT_FALSE(Pat2->match("abcdefx"));
     31 
     32   Expected<GlobPattern> Pat3 = GlobPattern::create("a??c");
     33   EXPECT_TRUE((bool)Pat3);
     34   EXPECT_TRUE(Pat3->match("axxc"));
     35   EXPECT_FALSE(Pat3->match("axxx"));
     36   EXPECT_FALSE(Pat3->match(""));
     37 
     38   Expected<GlobPattern> Pat4 = GlobPattern::create("[abc-fy-z]");
     39   EXPECT_TRUE((bool)Pat4);
     40   EXPECT_TRUE(Pat4->match("a"));
     41   EXPECT_TRUE(Pat4->match("b"));
     42   EXPECT_TRUE(Pat4->match("c"));
     43   EXPECT_TRUE(Pat4->match("d"));
     44   EXPECT_TRUE(Pat4->match("e"));
     45   EXPECT_TRUE(Pat4->match("f"));
     46   EXPECT_TRUE(Pat4->match("y"));
     47   EXPECT_TRUE(Pat4->match("z"));
     48   EXPECT_FALSE(Pat4->match("g"));
     49   EXPECT_FALSE(Pat4->match(""));
     50 
     51   Expected<GlobPattern> Pat5 = GlobPattern::create("[^abc-fy-z]");
     52   EXPECT_TRUE((bool)Pat5);
     53   EXPECT_TRUE(Pat5->match("g"));
     54   EXPECT_FALSE(Pat5->match("a"));
     55   EXPECT_FALSE(Pat5->match("b"));
     56   EXPECT_FALSE(Pat5->match("c"));
     57   EXPECT_FALSE(Pat5->match("d"));
     58   EXPECT_FALSE(Pat5->match("e"));
     59   EXPECT_FALSE(Pat5->match("f"));
     60   EXPECT_FALSE(Pat5->match("y"));
     61   EXPECT_FALSE(Pat5->match("z"));
     62   EXPECT_FALSE(Pat5->match(""));
     63 }
     64 
     65 TEST_F(GlobPatternTest, Invalid) {
     66   Expected<GlobPattern> Pat1 = GlobPattern::create("[");
     67   EXPECT_FALSE((bool)Pat1);
     68   handleAllErrors(Pat1.takeError(), [&](ErrorInfoBase &EIB) {});
     69 }
     70 
     71 TEST_F(GlobPatternTest, ExtSym) {
     72   Expected<GlobPattern> Pat1 = GlobPattern::create("a*\xFF");
     73   EXPECT_TRUE((bool)Pat1);
     74   EXPECT_TRUE(Pat1->match("axxx\xFF"));
     75   Expected<GlobPattern> Pat2 = GlobPattern::create("[\xFF-\xFF]");
     76   EXPECT_TRUE((bool)Pat2);
     77   EXPECT_TRUE(Pat2->match("\xFF"));
     78 }
     79 }
     80