Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / gn / ninja_target_writer_unittest.cc
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 <sstream>
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/ninja_target_writer.h"
9 #include "tools/gn/test_with_scope.h"
10
11 namespace {
12
13 class TestingNinjaTargetWriter : public NinjaTargetWriter {
14  public:
15   TestingNinjaTargetWriter(const Target* target,
16                            const Toolchain* toolchain,
17                            std::ostream& out)
18       : NinjaTargetWriter(target, toolchain, out) {
19   }
20
21   virtual void Run() OVERRIDE {}
22
23   // Make this public so the test can call it.
24   std::string WriteInputDepsStampAndGetDep(
25       const std::vector<const Target*>& extra_hard_deps) {
26     return NinjaTargetWriter::WriteInputDepsStampAndGetDep(extra_hard_deps);
27   }
28 };
29
30 }  // namespace
31
32 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
33   TestWithScope setup;
34
35   // Make a base target that's a hard dep (action).
36   Target base_target(setup.settings(), Label(SourceDir("//foo/"), "base"));
37   base_target.set_output_type(Target::ACTION);
38
39   // Dependent target that also includes a source prerequisite (should get
40   // included) and a source (should not be included).
41   Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
42   target.set_output_type(Target::EXECUTABLE);
43   target.source_prereqs().push_back(SourceFile("//foo/input.txt"));
44   target.sources().push_back(SourceFile("//foo/source.txt"));
45   target.deps().push_back(LabelTargetPair(&base_target));
46
47   // Dependent action to test that action sources will be treated the same as
48   // source_prereqs.
49   Target action(setup.settings(), Label(SourceDir("//foo/"), "action"));
50   action.set_output_type(Target::ACTION);
51   action.sources().push_back(SourceFile("//foo/action_source.txt"));
52   action.deps().push_back(LabelTargetPair(&target));
53
54   base_target.OnResolved();
55   target.OnResolved();
56   action.OnResolved();
57
58   // Input deps for the base (should be nothing, it has no hard deps).
59   {
60     std::ostringstream stream;
61     TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream);
62     std::string dep =
63         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>());
64
65     EXPECT_TRUE(dep.empty());
66     EXPECT_TRUE(stream.str().empty());
67   }
68
69   // Input deps for the target (should depend on the base).
70   {
71     std::ostringstream stream;
72     TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
73     std::string dep =
74         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>());
75
76     EXPECT_EQ(" | obj/foo/target.inputdeps.stamp", dep);
77     EXPECT_EQ("build obj/foo/target.inputdeps.stamp: stamp "
78                   "../../foo/input.txt obj/foo/base.stamp\n",
79               stream.str());
80   }
81
82   // Input deps for action which should depend on the base since its a hard dep
83   // that is a (indirect) dependency, as well as the the action source.
84   {
85     std::ostringstream stream;
86     TestingNinjaTargetWriter writer(&action, setup.toolchain(), stream);
87     std::string dep =
88         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>());
89
90     EXPECT_EQ(" | obj/foo/action.inputdeps.stamp", dep);
91     EXPECT_EQ("build obj/foo/action.inputdeps.stamp: stamp "
92                   "../../foo/action_source.txt obj/foo/base.stamp\n",
93               stream.str());
94   }
95 }