From 5d40a5621b7d0609c07a254c967763d1307b8dda Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 29 Oct 2020 19:19:34 +0000 Subject: [PATCH] Add ParserImplTestWithParam to reduce boilerplate There were a number of places where we were declaring classes derived from `testing::TestWithParam` and then adding the same `parser()` helper logic. Move this common logic down to a new `ParserImplTestWithParam` class, and derive from that instead. Removes a whole bunch of copy-pasta. Change-Id: I8f308b77817fd6327c045d2fdee4462b7f32897a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31401 Commit-Queue: dan sinclair Reviewed-by: David Neto Reviewed-by: dan sinclair --- .../wgsl/parser_impl_pipeline_stage_test.cc | 19 +- .../wgsl/parser_impl_storage_class_test.cc | 19 +- .../parser_impl_struct_decoration_test.cc | 19 +- src/reader/wgsl/parser_impl_test_helper.h | 30 +++ src/reader/wgsl/parser_impl_type_decl_test.cc | 185 ++---------------- .../parser_impl_variable_decoration_test.cc | 18 +- ...r_impl_variable_storage_decoration_test.cc | 20 +- 7 files changed, 47 insertions(+), 263 deletions(-) diff --git a/src/reader/wgsl/parser_impl_pipeline_stage_test.cc b/src/reader/wgsl/parser_impl_pipeline_stage_test.cc index 028f4c7ee3..d1c28521ba 100644 --- a/src/reader/wgsl/parser_impl_pipeline_stage_test.cc +++ b/src/reader/wgsl/parser_impl_pipeline_stage_test.cc @@ -31,24 +31,7 @@ inline std::ostream& operator<<(std::ostream& out, PipelineStageData data) { return out; } -class PipelineStageTest : public testing::TestWithParam { - public: - PipelineStageTest() = default; - ~PipelineStageTest() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class PipelineStageTest : public ParserImplTestWithParam {}; TEST_P(PipelineStageTest, Parses) { auto params = GetParam(); diff --git a/src/reader/wgsl/parser_impl_storage_class_test.cc b/src/reader/wgsl/parser_impl_storage_class_test.cc index ed3f3587e6..f595120872 100644 --- a/src/reader/wgsl/parser_impl_storage_class_test.cc +++ b/src/reader/wgsl/parser_impl_storage_class_test.cc @@ -31,24 +31,7 @@ inline std::ostream& operator<<(std::ostream& out, StorageClassData data) { return out; } -class StorageClassTest : public testing::TestWithParam { - public: - StorageClassTest() = default; - ~StorageClassTest() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class StorageClassTest : public ParserImplTestWithParam {}; TEST_P(StorageClassTest, Parses) { auto params = GetParam(); diff --git a/src/reader/wgsl/parser_impl_struct_decoration_test.cc b/src/reader/wgsl/parser_impl_struct_decoration_test.cc index 21102d1a8a..4d07af480e 100644 --- a/src/reader/wgsl/parser_impl_struct_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_struct_decoration_test.cc @@ -32,24 +32,7 @@ inline std::ostream& operator<<(std::ostream& out, StructDecorationData data) { } class StructDecorationTest - : public testing::TestWithParam { - public: - StructDecorationTest() = default; - ~StructDecorationTest() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; + : public ParserImplTestWithParam {}; TEST_P(StructDecorationTest, Parses) { auto params = GetParam(); diff --git a/src/reader/wgsl/parser_impl_test_helper.h b/src/reader/wgsl/parser_impl_test_helper.h index bdb1a2916e..0961b15691 100644 --- a/src/reader/wgsl/parser_impl_test_helper.h +++ b/src/reader/wgsl/parser_impl_test_helper.h @@ -55,6 +55,36 @@ class ParserImplTest : public testing::Test { Context ctx_; }; +/// WGSL Parser test class with param +template +class ParserImplTestWithParam : public testing::TestWithParam { + public: + /// Constructor + ParserImplTestWithParam() = default; + ~ParserImplTestWithParam() override = default; + + /// Sets up the test helper + void SetUp() override { ctx_.Reset(); } + + /// Tears down the test helper + void TearDown() override { impl_ = nullptr; } + + /// Retrieves the parser from the helper + /// @param str the string to parse + /// @returns the parser implementation + ParserImpl* parser(const std::string& str) { + impl_ = std::make_unique(&ctx_, str); + return impl_.get(); + } + + /// @returns the type manager + TypeManager* tm() { return &(ctx_.type_mgr()); } + + private: + std::unique_ptr impl_; + Context ctx_; +}; + } // namespace wgsl } // namespace reader } // namespace tint diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index 58134512b1..7ad9e5650b 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -123,24 +123,8 @@ inline std::ostream& operator<<(std::ostream& out, VecData data) { out << std::string(data.input); return out; } -class VecTest : public testing::TestWithParam { - public: - VecTest() = default; - ~VecTest() override = default; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class VecTest : public ParserImplTestWithParam {}; TEST_P(VecTest, Parse) { auto params = GetParam(); @@ -157,24 +141,7 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecData{"vec3", 3}, VecData{"vec4", 4})); -class VecMissingGreaterThanTest : public testing::TestWithParam { - public: - VecMissingGreaterThanTest() = default; - ~VecMissingGreaterThanTest() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class VecMissingGreaterThanTest : public ParserImplTestWithParam {}; TEST_P(VecMissingGreaterThanTest, Handles_Missing_GreaterThan) { auto params = GetParam(); @@ -190,24 +157,7 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecData{"vec3 { - public: - VecMissingLessThanTest() = default; - ~VecMissingLessThanTest() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class VecMissingLessThanTest : public ParserImplTestWithParam {}; TEST_P(VecMissingLessThanTest, Handles_Missing_GreaterThan) { auto params = GetParam(); @@ -223,24 +173,7 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecData{"vec3", 3}, VecData{"vec4", 4})); -class VecBadType : public testing::TestWithParam { - public: - VecBadType() = default; - ~VecBadType() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class VecBadType : public ParserImplTestWithParam {}; TEST_P(VecBadType, Handles_Unknown_Type) { auto params = GetParam(); @@ -256,24 +189,7 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecData{"vec3 { - public: - VecMissingType() = default; - ~VecMissingType() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class VecMissingType : public ParserImplTestWithParam {}; TEST_P(VecMissingType, Handles_Missing_Type) { auto params = GetParam(); @@ -604,24 +520,8 @@ inline std::ostream& operator<<(std::ostream& out, MatrixData data) { out << std::string(data.input); return out; } -class MatrixTest : public testing::TestWithParam { - public: - MatrixTest() = default; - ~MatrixTest() override = default; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class MatrixTest : public ParserImplTestWithParam {}; TEST_P(MatrixTest, Parse) { auto params = GetParam(); @@ -646,24 +546,9 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, MatrixData{"mat4x3", 4, 3}, MatrixData{"mat4x4", 4, 4})); -class MatrixMissingGreaterThanTest : public testing::TestWithParam { - public: - MatrixMissingGreaterThanTest() = default; - ~MatrixMissingGreaterThanTest() override = default; +class MatrixMissingGreaterThanTest + : public ParserImplTestWithParam {}; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; TEST_P(MatrixMissingGreaterThanTest, Handles_Missing_GreaterThan) { auto params = GetParam(); auto* p = parser(params.input); @@ -684,24 +569,8 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, MatrixData{"mat4x3 { - public: - MatrixMissingLessThanTest() = default; - ~MatrixMissingLessThanTest() override = default; +class MatrixMissingLessThanTest : public ParserImplTestWithParam {}; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; TEST_P(MatrixMissingLessThanTest, Handles_Missing_GreaterThan) { auto params = GetParam(); auto* p = parser(params.input); @@ -722,24 +591,8 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, MatrixData{"mat4x3 f32>", 4, 3}, MatrixData{"mat4x4 f32>", 4, 4})); -class MatrixBadType : public testing::TestWithParam { - public: - MatrixBadType() = default; - ~MatrixBadType() override = default; +class MatrixBadType : public ParserImplTestWithParam {}; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; TEST_P(MatrixBadType, Handles_Unknown_Type) { auto params = GetParam(); auto* p = parser(params.input); @@ -760,24 +613,8 @@ INSTANTIATE_TEST_SUITE_P(ParserImplTest, MatrixData{"mat4x3", 4, 3}, MatrixData{"mat4x4", 4, 4})); -class MatrixMissingType : public testing::TestWithParam { - public: - MatrixMissingType() = default; - ~MatrixMissingType() override = default; +class MatrixMissingType : public ParserImplTestWithParam {}; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; TEST_P(MatrixMissingType, Handles_Missing_Type) { auto params = GetParam(); auto* p = parser(params.input); diff --git a/src/reader/wgsl/parser_impl_variable_decoration_test.cc b/src/reader/wgsl/parser_impl_variable_decoration_test.cc index d410b6dbd0..d2c77554c5 100644 --- a/src/reader/wgsl/parser_impl_variable_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_variable_decoration_test.cc @@ -76,24 +76,8 @@ inline std::ostream& operator<<(std::ostream& out, BuiltinData data) { out << std::string(data.input); return out; } -class BuiltinTest : public testing::TestWithParam { - public: - BuiltinTest() = default; - ~BuiltinTest() override = default; - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class BuiltinTest : public ParserImplTestWithParam {}; TEST_P(BuiltinTest, VariableDecoration_Builtin) { auto params = GetParam(); diff --git a/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc b/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc index 6cbe9cd188..fa2b40faad 100644 --- a/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc @@ -31,24 +31,8 @@ inline std::ostream& operator<<(std::ostream& out, VariableStorageData data) { return out; } -class VariableStorageTest : public testing::TestWithParam { - public: - VariableStorageTest() = default; - ~VariableStorageTest() override = default; - - void SetUp() override { ctx_.Reset(); } - - void TearDown() override { impl_ = nullptr; } - - ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(&ctx_, str); - return impl_.get(); - } - - private: - std::unique_ptr impl_; - Context ctx_; -}; +class VariableStorageTest + : public ParserImplTestWithParam {}; TEST_P(VariableStorageTest, Parses) { auto params = GetParam();