• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 "gn/ninja_target_command_util.h"
6 
7 #include <algorithm>
8 #include <sstream>
9 
10 #include "util/build_config.h"
11 #include "util/test/test.h"
12 
13 namespace {
14 
15 // Helper function that uses a "Writer" to format a list of strings and return
16 // the generated output as a string.
17 template <typename Writer, typename Item>
FormatWithWriter(Writer writer,std::vector<Item> items)18 std::string FormatWithWriter(Writer writer, std::vector<Item> items) {
19   std::ostringstream out;
20   for (const Item& item : items) {
21     writer(item, out);
22   }
23   return out.str();
24 }
25 
26 // Helper function running to test "Writer" by formatting N "Items" and
27 // comparing the resulting string to an expected output.
28 template <typename Writer, typename Item>
TestWriter(Writer writer,std::string expected,std::initializer_list<Item> items)29 void TestWriter(Writer writer,
30                 std::string expected,
31                 std::initializer_list<Item> items) {
32   std::string formatted =
33       FormatWithWriter(writer, std::vector<Item>(std::move(items)));
34 
35   // Manually implement the check and the formatting of the error message to
36   // see the difference in the error message (by default the error message
37   // would just be "formatted == expected").
38   if (formatted != expected) {
39     std::ostringstream stream;
40     stream << '"' << expected << "\" == \"" << formatted << '"';
41     std::string message = stream.str();
42 
43     ::testing::TestResult result(false, message.c_str());
44     ::testing::AssertHelper(__FILE__, __LINE__, result) = ::testing::Message();
45   }
46 }
47 
48 }  // anonymous namespace
49 
TEST(NinjaTargetCommandUtil,DefineWriter)50 TEST(NinjaTargetCommandUtil, DefineWriter) {
51   TestWriter(DefineWriter(),
52 // Escaping is different between Windows and Posix.
53 #if defined(OS_WIN)
54              " -DFOO -DBAR=1 \"-DBAZ=\\\"Baz\\\"\"",
55 #else
56              " -DFOO -DBAR=1 -DBAZ=\\\"Baz\\\"",
57 #endif
58              {"FOO", "BAR=1", "BAZ=\"Baz\""});
59 
60   TestWriter(DefineWriter(ESCAPE_NINJA_COMMAND, true),
61 // Escaping is different between Windows and Posix.
62 #if defined(OS_WIN)
63              " -DFOO -DBAR=1 \"-DBAZ=\\\\\\\"Baz\\\\\\\"\"",
64 #else
65              " -DFOO -DBAR=1 -DBAZ=\\\\\\\"Baz\\\\\\\"",
66 #endif
67              {"FOO", "BAR=1", "BAZ=\"Baz\""});
68 }
69 
TEST(NinjaTargetCommandUtil,FrameworkDirsWriter)70 TEST(NinjaTargetCommandUtil, FrameworkDirsWriter) {
71   PathOutput ninja_path_output(SourceDir("//out"), "", ESCAPE_NINJA_COMMAND);
72   TestWriter(FrameworkDirsWriter(ninja_path_output, "-F"),
73 // Escaping is different between Windows and Posix.
74 #if defined(OS_WIN)
75              " -F. \"-FPath$ With$ Spaces\"",
76 #else
77              " -F. -FPath\\$ With\\$ Spaces",
78 #endif
79              {SourceDir("//out"), SourceDir("//out/Path With Spaces")});
80 
81   PathOutput space_path_output(SourceDir("//out"), "", ESCAPE_SPACE);
82   TestWriter(FrameworkDirsWriter(space_path_output, "-F"),
83              " -F. -FPath\\ With\\ Spaces",
84              {SourceDir("//out"), SourceDir("//out/Path With Spaces")});
85 }
86 
TEST(NinjaTargetCommandUtil,FrameworksWriter)87 TEST(NinjaTargetCommandUtil, FrameworksWriter) {
88   TestWriter(FrameworksWriter("-framework "),
89 // Escaping is different between Windows and Posix.
90 #if defined(OS_WIN)
91              " -framework Foundation -framework \"Name$ With$ Spaces\"",
92 #else
93              " -framework Foundation -framework Name\\$ With\\$ Spaces",
94 #endif
95              {"Foundation.framework", "Name With Spaces.framework"});
96 
97   TestWriter(FrameworksWriter(ESCAPE_SPACE, true, "-framework "),
98              " -framework Foundation -framework Name\\ With\\ Spaces",
99              {"Foundation.framework", "Name With Spaces.framework"});
100 }
101