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.
5 #ifndef TOOLS_GN_TEST_WITH_SCOPE_H_
6 #define TOOLS_GN_TEST_WITH_SCOPE_H_
10 #include "base/macros.h"
11 #include "tools/gn/build_settings.h"
12 #include "tools/gn/err.h"
13 #include "tools/gn/input_file.h"
14 #include "tools/gn/parse_tree.h"
15 #include "tools/gn/scope.h"
16 #include "tools/gn/scope_per_file_provider.h"
17 #include "tools/gn/settings.h"
18 #include "tools/gn/target.h"
19 #include "tools/gn/token.h"
20 #include "tools/gn/toolchain.h"
21 #include "tools/gn/value.h"
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.
30 BuildSettings
* build_settings() { return &build_settings_
; }
31 Settings
* settings() { return &settings_
; }
32 Toolchain
* toolchain() { return &toolchain_
; }
33 Scope
* scope() { return &scope_
; }
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_
; }
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;
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
47 // other toolchains they make.
48 static void SetupToolchain(Toolchain
* toolchain
);
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
);
56 void AppendPrintOutput(const std::string
& str
);
58 BuildSettings build_settings_
;
63 // Supplies the scope with built-in variables like root_out_dir.
64 ScopePerFileProvider scope_progammatic_provider_
;
66 std::string print_output_
;
68 DISALLOW_COPY_AND_ASSIGN(TestWithScope
);
71 // Helper class to treat some string input as a file.
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
{
77 explicit TestParseInput(const std::string
& input
);
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_
; }
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(); }
89 InputFile input_file_
;
91 std::vector
<Token
> tokens_
;
92 scoped_ptr
<ParseNode
> parsed_
;
96 DISALLOW_COPY_AND_ASSIGN(TestParseInput
);
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
{
104 TestTarget(TestWithScope
& setup
,
105 const std::string
& label_string
,
106 Target::OutputType type
);
107 ~TestTarget() override
;
110 #endif // TOOLS_GN_TEST_WITH_SCOPE_H_