• 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_COMPILATION_DATABASE),
61              // Escaping is different between Windows and Posix.
62              " -DFOO -DBAR=1 \"-DBAZ=\\\"Baz\\\"\"",
63              {"FOO", "BAR=1", "BAZ=\"Baz\""});
64 }
65 
TEST(NinjaTargetCommandUtil,FrameworkDirsWriter)66 TEST(NinjaTargetCommandUtil, FrameworkDirsWriter) {
67   PathOutput ninja_path_output(SourceDir("//out"), "", ESCAPE_NINJA_COMMAND);
68   TestWriter(FrameworkDirsWriter(ninja_path_output, "-F"),
69 // Escaping is different between Windows and Posix.
70 #if defined(OS_WIN)
71              " -F. \"-FPath$ With$ Spaces\"",
72 #else
73              " -F. -FPath\\$ With\\$ Spaces",
74 #endif
75              {SourceDir("//out"), SourceDir("//out/Path With Spaces")});
76 
77   PathOutput space_path_output(SourceDir("//out"), "", ESCAPE_SPACE);
78   TestWriter(FrameworkDirsWriter(space_path_output, "-F"),
79              " -F. -FPath\\ With\\ Spaces",
80              {SourceDir("//out"), SourceDir("//out/Path With Spaces")});
81 }
82 
TEST(NinjaTargetCommandUtil,FrameworksWriter)83 TEST(NinjaTargetCommandUtil, FrameworksWriter) {
84   TestWriter(FrameworksWriter("-framework "),
85 // Escaping is different between Windows and Posix.
86 #if defined(OS_WIN)
87              " -framework Foundation -framework \"Name$ With$ Spaces\"",
88 #else
89              " -framework Foundation -framework Name\\$ With\\$ Spaces",
90 #endif
91              {"Foundation.framework", "Name With Spaces.framework"});
92 
93   TestWriter(FrameworksWriter(ESCAPE_SPACE, "-framework "),
94              " -framework Foundation -framework Name\\ With\\ Spaces",
95              {"Foundation.framework", "Name With Spaces.framework"});
96 }
97