[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef TOOLS_GN_TEST_WITH_SCOPE_H_ | ||||
6 | #define TOOLS_GN_TEST_WITH_SCOPE_H_ | ||||
7 | |||||
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 8 | #include <vector> |
9 | |||||
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 10 | #include "base/basictypes.h" |
11 | #include "tools/gn/build_settings.h" | ||||
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 12 | #include "tools/gn/err.h" |
13 | #include "tools/gn/input_file.h" | ||||
14 | #include "tools/gn/parse_tree.h" | ||||
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 15 | #include "tools/gn/scope.h" |
brettw | dc957e1a | 2015-08-03 21:54:36 | [diff] [blame] | 16 | #include "tools/gn/scope_per_file_provider.h" |
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 17 | #include "tools/gn/settings.h" |
brettw | 56affab | 2015-06-04 22:01:03 | [diff] [blame] | 18 | #include "tools/gn/target.h" |
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 19 | #include "tools/gn/token.h" |
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 20 | #include "tools/gn/toolchain.h" |
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 21 | #include "tools/gn/value.h" |
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 22 | |
23 | // A helper class for setting up a Scope that a test can use. It makes a | ||||
24 | // toolchain and sets up all the build state. | ||||
25 | class TestWithScope { | ||||
26 | public: | ||||
27 | TestWithScope(); | ||||
28 | ~TestWithScope(); | ||||
29 | |||||
30 | BuildSettings* build_settings() { return &build_settings_; } | ||||
31 | Settings* settings() { return &settings_; } | ||||
[email protected] | 132715e | 2013-11-04 19:19:59 | [diff] [blame] | 32 | Toolchain* toolchain() { return &toolchain_; } |
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 33 | Scope* scope() { return &scope_; } |
34 | |||||
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 35 | // This buffer accumulates output from any print() commands executed in the |
36 | // context of this test. Note that the implementation of this is not | ||||
37 | // threadsafe so don't write tests that call print from multiple threads. | ||||
38 | std::string& print_output() { return print_output_; } | ||||
39 | |||||
brettw | 56affab | 2015-06-04 22:01:03 | [diff] [blame] | 40 | // Parse the given string into a label in the default toolchain. This will |
41 | // assert if the label isn't valid (this is intended for hardcoded labels). | ||||
42 | Label ParseLabel(const std::string& str) const; | ||||
43 | |||||
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 44 | // Fills in the tools for the given toolchain with reasonable default values. |
45 | // The toolchain in this object will be automatically set up with this | ||||
46 | // function, it is exposed to allow tests to get the same functionality for | ||||
brettw | 3dab5fe | 2015-06-29 23:00:15 | [diff] [blame] | 47 | // other toolchains they make. |
[email protected] | 0dfcae7 | 2014-08-19 22:52:16 | [diff] [blame] | 48 | static void SetupToolchain(Toolchain* toolchain); |
49 | |||||
brettw | 3dab5fe | 2015-06-29 23:00:15 | [diff] [blame] | 50 | // Sets the given text command on the given tool, parsing it as a |
51 | // substitution pattern. This will assert if the input is malformed. This is | ||||
52 | // designed to help setting up Tools for tests. | ||||
53 | static void SetCommandForTool(const std::string& cmd, Tool* tool); | ||||
54 | |||||
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 55 | private: |
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 56 | void AppendPrintOutput(const std::string& str); |
57 | |||||
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 58 | BuildSettings build_settings_; |
[email protected] | 404b9863 | 2013-11-03 04:51:00 | [diff] [blame] | 59 | Settings settings_; |
[email protected] | 132715e | 2013-11-04 19:19:59 | [diff] [blame] | 60 | Toolchain toolchain_; |
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 61 | Scope scope_; |
62 | |||||
brettw | dc957e1a | 2015-08-03 21:54:36 | [diff] [blame] | 63 | // Supplies the scope with built-in variables like root_out_dir. |
64 | ScopePerFileProvider scope_progammatic_provider_; | ||||
65 | |||||
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 66 | std::string print_output_; |
67 | |||||
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 68 | DISALLOW_COPY_AND_ASSIGN(TestWithScope); |
69 | }; | ||||
70 | |||||
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 71 | // Helper class to treat some string input as a file. |
72 | // | ||||
73 | // Instantiate it with the contents you want, be sure to check for error, and | ||||
74 | // then you can execute the ParseNode or whatever. | ||||
75 | class TestParseInput { | ||||
76 | public: | ||||
tfarina | dc6279e30 | 2015-03-02 03:07:15 | [diff] [blame] | 77 | explicit TestParseInput(const std::string& input); |
[email protected] | 1e3fbf7b | 2014-04-08 19:15:20 | [diff] [blame] | 78 | ~TestParseInput(); |
79 | |||||
80 | // Indicates whether and what error occurred during tokenizing and parsing. | ||||
81 | bool has_error() const { return parse_err_.has_error(); } | ||||
82 | const Err& parse_err() const { return parse_err_; } | ||||
83 | |||||
84 | const InputFile& input_file() const { return input_file_; } | ||||
85 | const std::vector<Token>& tokens() const { return tokens_; } | ||||
86 | const ParseNode* parsed() const { return parsed_.get(); } | ||||
87 | |||||
88 | private: | ||||
89 | InputFile input_file_; | ||||
90 | |||||
91 | std::vector<Token> tokens_; | ||||
92 | scoped_ptr<ParseNode> parsed_; | ||||
93 | |||||
94 | Err parse_err_; | ||||
95 | |||||
96 | DISALLOW_COPY_AND_ASSIGN(TestParseInput); | ||||
97 | }; | ||||
98 | |||||
brettw | 56affab | 2015-06-04 22:01:03 | [diff] [blame] | 99 | // Shortcut for creating targets for tests that take the test setup, a pretty- |
100 | // style label, and a target type and sets everything up. The target will | ||||
101 | // default to public visibility. | ||||
102 | class TestTarget : public Target { | ||||
103 | public: | ||||
104 | TestTarget(TestWithScope& setup, | ||||
105 | const std::string& label_string, | ||||
106 | Target::OutputType type); | ||||
107 | ~TestTarget() override; | ||||
108 | }; | ||||
109 | |||||
[email protected] | 43bf6a5 | 2013-09-17 16:54:24 | [diff] [blame] | 110 | #endif // TOOLS_GN_TEST_WITH_SCOPE_H_ |