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 8 #include <vector> 9 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/token.h" 18 #include "tools/gn/toolchain.h" 19 #include "tools/gn/value.h" 20 21 // A helper class for setting up a Scope that a test can use. It makes a 22 // toolchain and sets up all the build state. 23 class TestWithScope { 24 public: 25 TestWithScope(); 26 ~TestWithScope(); 27 build_settings()28 BuildSettings* build_settings() { return &build_settings_; } settings()29 Settings* settings() { return &settings_; } toolchain()30 Toolchain* toolchain() { return &toolchain_; } scope()31 Scope* scope() { return &scope_; } 32 33 // This buffer accumulates output from any print() commands executed in the 34 // context of this test. Note that the implementation of this is not 35 // threadsafe so don't write tests that call print from multiple threads. print_output()36 std::string& print_output() { return print_output_; } 37 38 // Fills in the tools for the given toolchain with reasonable default values. 39 // The toolchain in this object will be automatically set up with this 40 // function, it is exposed to allow tests to get the same functionality for 41 // other toolchains they make 42 static void SetupToolchain(Toolchain* toolchain); 43 44 private: 45 void AppendPrintOutput(const std::string& str); 46 47 BuildSettings build_settings_; 48 Settings settings_; 49 Toolchain toolchain_; 50 Scope scope_; 51 52 std::string print_output_; 53 54 DISALLOW_COPY_AND_ASSIGN(TestWithScope); 55 }; 56 57 // Helper class to treat some string input as a file. 58 // 59 // Instantiate it with the contents you want, be sure to check for error, and 60 // then you can execute the ParseNode or whatever. 61 class TestParseInput { 62 public: 63 TestParseInput(const std::string& input); 64 ~TestParseInput(); 65 66 // Indicates whether and what error occurred during tokenizing and parsing. has_error()67 bool has_error() const { return parse_err_.has_error(); } parse_err()68 const Err& parse_err() const { return parse_err_; } 69 input_file()70 const InputFile& input_file() const { return input_file_; } tokens()71 const std::vector<Token>& tokens() const { return tokens_; } parsed()72 const ParseNode* parsed() const { return parsed_.get(); } 73 74 private: 75 InputFile input_file_; 76 77 std::vector<Token> tokens_; 78 scoped_ptr<ParseNode> parsed_; 79 80 Err parse_err_; 81 82 DISALLOW_COPY_AND_ASSIGN(TestParseInput); 83 }; 84 85 #endif // TOOLS_GN_TEST_WITH_SCOPE_H_ 86