Home | History | Annotate | Download | only in gn
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "testing/gtest/include/gtest/gtest.h"
      6 #include "tools/gn/functions.h"
      7 #include "tools/gn/test_with_scope.h"
      8 
      9 namespace {
     10 
     11 class GetPathInfoTest : public testing::Test {
     12  public:
     13   GetPathInfoTest() : testing::Test() {
     14     setup_.scope()->set_source_dir(SourceDir("//src/foo/"));
     15   }
     16 
     17   // Convenience wrapper to call GetLabelInfo.
     18   std::string Call(const std::string& input, const std::string& what) {
     19     FunctionCallNode function;
     20 
     21     std::vector<Value> args;
     22     args.push_back(Value(NULL, input));
     23     args.push_back(Value(NULL, what));
     24 
     25     Err err;
     26     Value result = functions::RunGetPathInfo(setup_.scope(), &function,
     27                                              args, &err);
     28     if (err.has_error()) {
     29       EXPECT_TRUE(result.type() == Value::NONE);
     30       return std::string();
     31     }
     32     return result.string_value();
     33   }
     34 
     35  protected:
     36   TestWithScope setup_;
     37 };
     38 
     39 }  // namespace
     40 
     41 TEST_F(GetPathInfoTest, File) {
     42   EXPECT_EQ("bar.txt", Call("foo/bar.txt", "file"));
     43   EXPECT_EQ("bar.txt", Call("bar.txt", "file"));
     44   EXPECT_EQ("bar.txt", Call("/bar.txt", "file"));
     45   EXPECT_EQ("", Call("foo/", "file"));
     46   EXPECT_EQ("", Call("//", "file"));
     47   EXPECT_EQ("", Call("/", "file"));
     48 }
     49 
     50 TEST_F(GetPathInfoTest, Name) {
     51   EXPECT_EQ("bar", Call("foo/bar.txt", "name"));
     52   EXPECT_EQ("bar", Call("bar.", "name"));
     53   EXPECT_EQ("", Call("/.txt", "name"));
     54   EXPECT_EQ("", Call("foo/", "name"));
     55   EXPECT_EQ("", Call("//", "name"));
     56   EXPECT_EQ("", Call("/", "name"));
     57 }
     58 
     59 TEST_F(GetPathInfoTest, Extension) {
     60   EXPECT_EQ("txt", Call("foo/bar.txt", "extension"));
     61   EXPECT_EQ("", Call("bar.", "extension"));
     62   EXPECT_EQ("txt", Call("/.txt", "extension"));
     63   EXPECT_EQ("", Call("f.oo/", "extension"));
     64   EXPECT_EQ("", Call("//", "extension"));
     65   EXPECT_EQ("", Call("/", "extension"));
     66 }
     67 
     68 TEST_F(GetPathInfoTest, Dir) {
     69   EXPECT_EQ("foo", Call("foo/bar.txt", "dir"));
     70   EXPECT_EQ(".", Call("bar.txt", "dir"));
     71   EXPECT_EQ("foo/bar", Call("foo/bar/baz", "dir"));
     72   EXPECT_EQ("//foo", Call("//foo/", "dir"));
     73   EXPECT_EQ("//.", Call("//", "dir"));
     74   EXPECT_EQ("/foo", Call("/foo/", "dir"));
     75   EXPECT_EQ("/.", Call("/", "dir"));
     76 }
     77 
     78 // Note "current dir" is "//src/foo"
     79 TEST_F(GetPathInfoTest, AbsPath) {
     80   EXPECT_EQ("//src/foo/foo/bar.txt", Call("foo/bar.txt", "abspath"));
     81   EXPECT_EQ("//src/foo/bar.txt", Call("bar.txt", "abspath"));
     82   EXPECT_EQ("//src/foo/bar/", Call("bar/", "abspath"));
     83   EXPECT_EQ("//foo", Call("//foo", "abspath"));
     84   EXPECT_EQ("//foo/", Call("//foo/", "abspath"));
     85   EXPECT_EQ("//", Call("//", "abspath"));
     86   EXPECT_EQ("/foo", Call("/foo", "abspath"));
     87   EXPECT_EQ("/foo/", Call("/foo/", "abspath"));
     88   EXPECT_EQ("/", Call("/", "abspath"));
     89 }
     90 
     91 // Note build dir is "//out/Debug/".
     92 TEST_F(GetPathInfoTest, OutDir) {
     93   EXPECT_EQ("//out/Debug/obj/src/foo/foo", Call("foo/bar.txt", "out_dir"));
     94   EXPECT_EQ("//out/Debug/obj/src/foo/bar", Call("bar/", "out_dir"));
     95   EXPECT_EQ("//out/Debug/obj/src/foo", Call(".", "out_dir"));
     96   EXPECT_EQ("//out/Debug/obj/src/foo", Call("bar", "out_dir"));
     97   EXPECT_EQ("//out/Debug/obj/foo", Call("//foo/bar.txt", "out_dir"));
     98   // System paths go into the root obj directory.
     99   EXPECT_EQ("//out/Debug/obj", Call("/foo/bar.txt", "out_dir"));
    100 }
    101 
    102 // Note build dir is "//out/Debug/".
    103 TEST_F(GetPathInfoTest, GenDir) {
    104   EXPECT_EQ("//out/Debug/gen/src/foo/foo", Call("foo/bar.txt", "gen_dir"));
    105   EXPECT_EQ("//out/Debug/gen/src/foo/bar", Call("bar/", "gen_dir"));
    106   EXPECT_EQ("//out/Debug/gen/src/foo", Call(".", "gen_dir"));
    107   EXPECT_EQ("//out/Debug/gen/src/foo", Call("bar", "gen_dir"));
    108   EXPECT_EQ("//out/Debug/gen/foo", Call("//foo/bar.txt", "gen_dir"));
    109   // System paths go into the root obj directory.
    110   EXPECT_EQ("//out/Debug/gen", Call("/foo/bar.txt", "gen_dir"));
    111 }
    112