| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sstream> | 5 #include <sstream> |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "tools/gn/path_output.h" | 8 #include "tools/gn/path_output.h" |
| 9 #include "tools/gn/source_dir.h" | 9 #include "tools/gn/source_dir.h" |
| 10 #include "tools/gn/source_file.h" | 10 #include "tools/gn/source_file.h" |
| 11 | 11 |
| 12 TEST(PathOutput, Basic) { | 12 TEST(PathOutput, Basic) { |
| 13 SourceDir build_dir("//out/Debug/"); | 13 SourceDir build_dir("//out/Debug/"); |
| 14 PathOutput writer(build_dir, ESCAPE_NONE, false); | 14 PathOutput writer(build_dir, ESCAPE_NONE); |
| 15 { | 15 { |
| 16 // Normal source-root path. | 16 // Normal source-root path. |
| 17 std::ostringstream out; | 17 std::ostringstream out; |
| 18 writer.WriteFile(out, SourceFile("//foo/bar.cc")); | 18 writer.WriteFile(out, SourceFile("//foo/bar.cc")); |
| 19 EXPECT_EQ("../../foo/bar.cc", out.str()); | 19 EXPECT_EQ("../../foo/bar.cc", out.str()); |
| 20 } | 20 } |
| 21 { | 21 { |
| 22 // File in the root dir. | 22 // File in the root dir. |
| 23 std::ostringstream out; | 23 std::ostringstream out; |
| 24 writer.WriteFile(out, SourceFile("//foo.cc")); | 24 writer.WriteFile(out, SourceFile("//foo.cc")); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 std::ostringstream out; | 45 std::ostringstream out; |
| 46 writer.WriteFile(out, SourceFile("/foo/bar.cc")); | 46 writer.WriteFile(out, SourceFile("/foo/bar.cc")); |
| 47 EXPECT_EQ("/foo/bar.cc", out.str()); | 47 EXPECT_EQ("/foo/bar.cc", out.str()); |
| 48 } | 48 } |
| 49 #endif | 49 #endif |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Same as basic but the output dir is the root. | 52 // Same as basic but the output dir is the root. |
| 53 TEST(PathOutput, BasicInRoot) { | 53 TEST(PathOutput, BasicInRoot) { |
| 54 SourceDir build_dir("//"); | 54 SourceDir build_dir("//"); |
| 55 PathOutput writer(build_dir, ESCAPE_NONE, false); | 55 PathOutput writer(build_dir, ESCAPE_NONE); |
| 56 { | 56 { |
| 57 // Normal source-root path. | 57 // Normal source-root path. |
| 58 std::ostringstream out; | 58 std::ostringstream out; |
| 59 writer.WriteFile(out, SourceFile("//foo/bar.cc")); | 59 writer.WriteFile(out, SourceFile("//foo/bar.cc")); |
| 60 EXPECT_EQ("foo/bar.cc", out.str()); | 60 EXPECT_EQ("foo/bar.cc", out.str()); |
| 61 } | 61 } |
| 62 { | 62 { |
| 63 // File in the root dir. | 63 // File in the root dir. |
| 64 std::ostringstream out; | 64 std::ostringstream out; |
| 65 writer.WriteFile(out, SourceFile("//foo.cc")); | 65 writer.WriteFile(out, SourceFile("//foo.cc")); |
| 66 EXPECT_EQ("foo.cc", out.str()); | 66 EXPECT_EQ("foo.cc", out.str()); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 TEST(PathOutput, NinjaEscaping) { | 70 TEST(PathOutput, NinjaEscaping) { |
| 71 SourceDir build_dir("//out/Debug/"); | 71 SourceDir build_dir("//out/Debug/"); |
| 72 PathOutput writer(build_dir, ESCAPE_NINJA, false); | 72 PathOutput writer(build_dir, ESCAPE_NINJA); |
| 73 { | 73 { |
| 74 // Spaces and $ in filenames. | 74 // Spaces and $ in filenames. |
| 75 std::ostringstream out; | 75 std::ostringstream out; |
| 76 writer.WriteFile(out, SourceFile("//foo/foo bar$.cc")); | 76 writer.WriteFile(out, SourceFile("//foo/foo bar$.cc")); |
| 77 EXPECT_EQ("../../foo/foo$ bar$$.cc", out.str()); | 77 EXPECT_EQ("../../foo/foo$ bar$$.cc", out.str()); |
| 78 } | 78 } |
| 79 { | 79 { |
| 80 // Not other weird stuff | 80 // Not other weird stuff |
| 81 std::ostringstream out; | 81 std::ostringstream out; |
| 82 writer.WriteFile(out, SourceFile("//foo/\"foo\\bar\".cc")); | 82 writer.WriteFile(out, SourceFile("//foo/\"foo\\bar\".cc")); |
| 83 EXPECT_EQ("../../foo/\"foo\\bar\".cc", out.str()); | 83 EXPECT_EQ("../../foo/\"foo\\bar\".cc", out.str()); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST(PathOutput, ShellEscaping) { | 87 TEST(PathOutput, ShellEscaping) { |
| 88 SourceDir build_dir("//out/Debug/"); | 88 SourceDir build_dir("//out/Debug/"); |
| 89 PathOutput writer(build_dir, ESCAPE_SHELL, false); | 89 PathOutput writer(build_dir, ESCAPE_SHELL); |
| 90 { | 90 { |
| 91 // Spaces in filenames should get quoted. | 91 // Spaces in filenames should get quoted. |
| 92 std::ostringstream out; | 92 std::ostringstream out; |
| 93 writer.WriteFile(out, SourceFile("//foo/foo bar.cc")); | 93 writer.WriteFile(out, SourceFile("//foo/foo bar.cc")); |
| 94 EXPECT_EQ("\"../../foo/foo bar.cc\"", out.str()); | 94 EXPECT_EQ("\"../../foo/foo bar.cc\"", out.str()); |
| 95 } | 95 } |
| 96 { | 96 { |
| 97 // Quotes should get blackslash-escaped. | 97 // Quotes should get blackslash-escaped. |
| 98 std::ostringstream out; | 98 std::ostringstream out; |
| 99 writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc")); | 99 writer.WriteFile(out, SourceFile("//foo/\"foobar\".cc")); |
| 100 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str()); | 100 EXPECT_EQ("../../foo/\\\"foobar\\\".cc", out.str()); |
| 101 } | 101 } |
| 102 { | 102 { |
| 103 // Backslashes should get escaped on non-Windows and preserved on Windows. | 103 // Backslashes should get escaped on non-Windows and preserved on Windows. |
| 104 std::ostringstream out; | 104 std::ostringstream out; |
| 105 writer.WriteFile(out, SourceFile("//foo\\bar.cc")); | 105 writer.WriteFile(out, SourceFile("//foo\\bar.cc")); |
| 106 #if defined(OS_WIN) | 106 #if defined(OS_WIN) |
| 107 EXPECT_EQ("../../foo\\bar.cc", out.str()); | 107 EXPECT_EQ("../../foo\\bar.cc", out.str()); |
| 108 #else | 108 #else |
| 109 EXPECT_EQ("../../foo\\\\bar.cc", out.str()); | 109 EXPECT_EQ("../../foo\\\\bar.cc", out.str()); |
| 110 #endif | 110 #endif |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 TEST(PathOutput, SlashConversion) { | 114 TEST(PathOutput, SlashConversion) { |
| 115 SourceDir build_dir("//out/Debug/"); | 115 SourceDir build_dir("//out/Debug/"); |
| 116 PathOutput writer(build_dir, ESCAPE_NINJA, true); | 116 PathOutput writer(build_dir, ESCAPE_NINJA); |
| 117 { | 117 { |
| 118 std::ostringstream out; | 118 std::ostringstream out; |
| 119 writer.WriteFile(out, SourceFile("//foo/bar.cc")); | 119 writer.WriteFile(out, SourceFile("//foo/bar.cc")); |
| 120 #if defined(OS_WIN) | |
| 121 EXPECT_EQ("..\\..\\foo\\bar.cc", out.str()); | |
| 122 #else | |
| 123 EXPECT_EQ("../../foo/bar.cc", out.str()); | 120 EXPECT_EQ("../../foo/bar.cc", out.str()); |
| 124 #endif | |
| 125 } | 121 } |
| 126 } | 122 } |
| 127 | 123 |
| 128 TEST(PathOutput, InhibitQuoting) { | 124 TEST(PathOutput, InhibitQuoting) { |
| 129 SourceDir build_dir("//out/Debug/"); | 125 SourceDir build_dir("//out/Debug/"); |
| 130 PathOutput writer(build_dir, ESCAPE_SHELL, false); | 126 PathOutput writer(build_dir, ESCAPE_SHELL); |
| 131 writer.set_inhibit_quoting(true); | 127 writer.set_inhibit_quoting(true); |
| 132 { | 128 { |
| 133 // We should get unescaped spaces in the output with no quotes. | 129 // We should get unescaped spaces in the output with no quotes. |
| 134 std::ostringstream out; | 130 std::ostringstream out; |
| 135 writer.WriteFile(out, SourceFile("//foo/foo bar.cc")); | 131 writer.WriteFile(out, SourceFile("//foo/foo bar.cc")); |
| 136 EXPECT_EQ("../../foo/foo bar.cc", out.str()); | 132 EXPECT_EQ("../../foo/foo bar.cc", out.str()); |
| 137 } | 133 } |
| 138 } | 134 } |
| 139 | 135 |
| 140 TEST(PathOutput, WriteDir) { | 136 TEST(PathOutput, WriteDir) { |
| 141 { | 137 { |
| 142 SourceDir build_dir("//out/Debug/"); | 138 SourceDir build_dir("//out/Debug/"); |
| 143 PathOutput writer(build_dir, ESCAPE_NINJA, false); | 139 PathOutput writer(build_dir, ESCAPE_NINJA); |
| 144 { | 140 { |
| 145 std::ostringstream out; | 141 std::ostringstream out; |
| 146 writer.WriteDir(out, SourceDir("//foo/bar/"), | 142 writer.WriteDir(out, SourceDir("//foo/bar/"), |
| 147 PathOutput::DIR_INCLUDE_LAST_SLASH); | 143 PathOutput::DIR_INCLUDE_LAST_SLASH); |
| 148 EXPECT_EQ("../../foo/bar/", out.str()); | 144 EXPECT_EQ("../../foo/bar/", out.str()); |
| 149 } | 145 } |
| 150 { | 146 { |
| 151 std::ostringstream out; | 147 std::ostringstream out; |
| 152 writer.WriteDir(out, SourceDir("//foo/bar/"), | 148 writer.WriteDir(out, SourceDir("//foo/bar/"), |
| 153 PathOutput::DIR_NO_LAST_SLASH); | 149 PathOutput::DIR_NO_LAST_SLASH); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 205 } |
| 210 { | 206 { |
| 211 std::ostringstream out; | 207 std::ostringstream out; |
| 212 writer.WriteDir(out, SourceDir("//out/Debug/foo/"), | 208 writer.WriteDir(out, SourceDir("//out/Debug/foo/"), |
| 213 PathOutput::DIR_NO_LAST_SLASH); | 209 PathOutput::DIR_NO_LAST_SLASH); |
| 214 EXPECT_EQ("foo", out.str()); | 210 EXPECT_EQ("foo", out.str()); |
| 215 } | 211 } |
| 216 } | 212 } |
| 217 { | 213 { |
| 218 // Empty build dir writer. | 214 // Empty build dir writer. |
| 219 PathOutput root_writer(SourceDir("//"), ESCAPE_NINJA, false); | 215 PathOutput root_writer(SourceDir("//"), ESCAPE_NINJA); |
| 220 { | 216 { |
| 221 std::ostringstream out; | 217 std::ostringstream out; |
| 222 root_writer.WriteDir(out, SourceDir("//"), | 218 root_writer.WriteDir(out, SourceDir("//"), |
| 223 PathOutput::DIR_INCLUDE_LAST_SLASH); | 219 PathOutput::DIR_INCLUDE_LAST_SLASH); |
| 224 EXPECT_EQ("./", out.str()); | 220 EXPECT_EQ("./", out.str()); |
| 225 } | 221 } |
| 226 { | 222 { |
| 227 std::ostringstream out; | 223 std::ostringstream out; |
| 228 root_writer.WriteDir(out, SourceDir("//"), | 224 root_writer.WriteDir(out, SourceDir("//"), |
| 229 PathOutput::DIR_NO_LAST_SLASH); | 225 PathOutput::DIR_NO_LAST_SLASH); |
| 230 EXPECT_EQ(".", out.str()); | 226 EXPECT_EQ(".", out.str()); |
| 231 } | 227 } |
| 232 } | 228 } |
| 233 } | 229 } |
| OLD | NEW |