1 // Copyright 2020 The Tint Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_WRITER_WGSL_TEST_HELPER_H_ 16 #define SRC_WRITER_WGSL_TEST_HELPER_H_ 17 18 #include <memory> 19 #include <utility> 20 21 #include "gtest/gtest.h" 22 #include "src/program_builder.h" 23 #include "src/writer/wgsl/generator_impl.h" 24 25 namespace tint { 26 namespace writer { 27 namespace wgsl { 28 29 /// Helper class for testing 30 template <typename BASE> 31 class TestHelperBase : public BASE, public ProgramBuilder { 32 public: 33 TestHelperBase() = default; 34 35 ~TestHelperBase() override = default; 36 37 /// Builds and returns a GeneratorImpl from the program. 38 /// @note The generator is only built once. Multiple calls to Build() will 39 /// return the same GeneratorImpl without rebuilding. 40 /// @return the built generator Build()41 GeneratorImpl& Build() { 42 if (gen_) { 43 return *gen_; 44 } 45 program = std::make_unique<Program>(std::move(*this)); 46 diag::Formatter formatter; 47 [&]() { 48 ASSERT_TRUE(program->IsValid()) 49 << formatter.format(program->Diagnostics()); 50 }(); 51 gen_ = std::make_unique<GeneratorImpl>(program.get()); 52 return *gen_; 53 } 54 55 /// The program built with a call to Build() 56 std::unique_ptr<Program> program; 57 58 private: 59 std::unique_ptr<GeneratorImpl> gen_; 60 }; 61 using TestHelper = TestHelperBase<testing::Test>; 62 63 template <typename T> 64 using TestParamHelper = TestHelperBase<testing::TestWithParam<T>>; 65 66 } // namespace wgsl 67 } // namespace writer 68 } // namespace tint 69 70 #endif // SRC_WRITER_WGSL_TEST_HELPER_H_ 71