1 // Copyright 2021 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_GLSL_TEST_HELPER_H_ 16 #define SRC_WRITER_GLSL_TEST_HELPER_H_ 17 18 #include <memory> 19 #include <string> 20 #include <utility> 21 22 #include "gtest/gtest.h" 23 #include "src/transform/glsl.h" 24 #include "src/transform/manager.h" 25 #include "src/transform/renamer.h" 26 #include "src/writer/glsl/generator_impl.h" 27 28 namespace tint { 29 namespace writer { 30 namespace glsl { 31 32 /// Helper class for testing 33 template <typename BODY> 34 class TestHelperBase : public BODY, public ProgramBuilder { 35 public: 36 TestHelperBase() = default; 37 ~TestHelperBase() override = default; 38 39 /// Builds the program and returns a GeneratorImpl from the program. 40 /// @note The generator is only built once. Multiple calls to Build() will 41 /// return the same GeneratorImpl without rebuilding. 42 /// @return the built generator Build()43 GeneratorImpl& Build() { 44 if (gen_) { 45 return *gen_; 46 } 47 // Fake that the GLSL sanitizer has been applied, so that we can unit test 48 // the writer without it erroring. 49 SetTransformApplied<transform::Glsl>(); 50 [&]() { 51 ASSERT_TRUE(IsValid()) << "Builder program is not valid\n" 52 << diag::Formatter().format(Diagnostics()); 53 }(); 54 program = std::make_unique<Program>(std::move(*this)); 55 [&]() { 56 ASSERT_TRUE(program->IsValid()) 57 << diag::Formatter().format(program->Diagnostics()); 58 }(); 59 gen_ = std::make_unique<GeneratorImpl>(program.get()); 60 return *gen_; 61 } 62 63 /// Builds the program, runs the program through the transform::Glsl sanitizer 64 /// and returns a GeneratorImpl from the sanitized program. 65 /// @note The generator is only built once. Multiple calls to Build() will 66 /// return the same GeneratorImpl without rebuilding. 67 /// @return the built generator SanitizeAndBuild()68 GeneratorImpl& SanitizeAndBuild() { 69 if (gen_) { 70 return *gen_; 71 } 72 diag::Formatter formatter; 73 [&]() { 74 ASSERT_TRUE(IsValid()) << "Builder program is not valid\n" 75 << formatter.format(Diagnostics()); 76 }(); 77 program = std::make_unique<Program>(std::move(*this)); 78 [&]() { 79 ASSERT_TRUE(program->IsValid()) 80 << formatter.format(program->Diagnostics()); 81 }(); 82 83 transform::Manager transform_manager; 84 transform::DataMap transform_data; 85 transform_data.Add<transform::Renamer::Config>( 86 transform::Renamer::Target::kGlslKeywords); 87 transform_manager.Add<tint::transform::Renamer>(); 88 transform_manager.Add<tint::transform::Glsl>(); 89 auto result = transform_manager.Run(program.get(), transform_data); 90 [&]() { 91 ASSERT_TRUE(result.program.IsValid()) 92 << formatter.format(result.program.Diagnostics()); 93 }(); 94 *program = std::move(result.program); 95 gen_ = std::make_unique<GeneratorImpl>(program.get()); 96 return *gen_; 97 } 98 99 /// The program built with a call to Build() 100 std::unique_ptr<Program> program; 101 102 private: 103 std::unique_ptr<GeneratorImpl> gen_; 104 }; 105 using TestHelper = TestHelperBase<testing::Test>; 106 107 template <typename T> 108 using TestParamHelper = TestHelperBase<testing::TestWithParam<T>>; 109 110 } // namespace glsl 111 } // namespace writer 112 } // namespace tint 113 114 #endif // SRC_WRITER_GLSL_TEST_HELPER_H_ 115