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/basictypes.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/settings.h"
17 #include "tools/gn/target.h"
18 #include "tools/gn/token.h"
19 #include "tools/gn/toolchain.h"
20 #include "tools/gn/value.h"
22 // A helper class for setting up a Scope that a test can use. It makes a
23 // toolchain and sets up all the build state.
29 BuildSettings
* build_settings() { return &build_settings_
; }
30 Settings
* settings() { return &settings_
; }
31 Toolchain
* toolchain() { return &toolchain_
; }
32 Scope
* scope() { return &scope_
; }
34 // This buffer accumulates output from any print() commands executed in the
35 // context of this test. Note that the implementation of this is not
36 // threadsafe so don't write tests that call print from multiple threads.
37 std::string
& print_output() { return print_output_
; }
39 // Parse the given string into a label in the default toolchain. This will
40 // assert if the label isn't valid (this is intended for hardcoded labels).
41 Label
ParseLabel(const std::string
& str
) const;
43 // Fills in the tools for the given toolchain with reasonable default values.
44 // The toolchain in this object will be automatically set up with this
45 // function, it is exposed to allow tests to get the same functionality for
46 // other toolchains they make.
47 static void SetupToolchain(Toolchain
* toolchain
);
49 // Sets the given text command on the given tool, parsing it as a
50 // substitution pattern. This will assert if the input is malformed. This is
51 // designed to help setting up Tools for tests.
52 static void SetCommandForTool(const std::string
& cmd
, Tool
* tool
);
55 void AppendPrintOutput(const std::string
& str
);
57 BuildSettings build_settings_
;
62 std::string print_output_
;
64 DISALLOW_COPY_AND_ASSIGN(TestWithScope
);
67 // Helper class to treat some string input as a file.
69 // Instantiate it with the contents you want, be sure to check for error, and
70 // then you can execute the ParseNode or whatever.
71 class TestParseInput
{
73 explicit TestParseInput(const std::string
& input
);
76 // Indicates whether and what error occurred during tokenizing and parsing.
77 bool has_error() const { return parse_err_
.has_error(); }
78 const Err
& parse_err() const { return parse_err_
; }
80 const InputFile
& input_file() const { return input_file_
; }
81 const std::vector
<Token
>& tokens() const { return tokens_
; }
82 const ParseNode
* parsed() const { return parsed_
.get(); }
85 InputFile input_file_
;
87 std::vector
<Token
> tokens_
;
88 scoped_ptr
<ParseNode
> parsed_
;
92 DISALLOW_COPY_AND_ASSIGN(TestParseInput
);
95 // Shortcut for creating targets for tests that take the test setup, a pretty-
96 // style label, and a target type and sets everything up. The target will
97 // default to public visibility.
98 class TestTarget
: public Target
{
100 TestTarget(TestWithScope
& setup
,
101 const std::string
& label_string
,
102 Target::OutputType type
);
103 ~TestTarget() override
;
106 #endif // TOOLS_GN_TEST_WITH_SCOPE_H_