// Copyright 2022 The Tint Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "src/tint/reflection.h" #include "gtest/gtest.h" namespace tint { namespace { struct S { int i; unsigned u; bool b; TINT_REFLECT(i, u, b); }; static_assert(!HasReflection); static_assert(HasReflection); TEST(ReflectionTest, ForeachFieldConst) { const S s{1, 2, true}; size_t field_idx = 0; ForeachField(s, [&](auto& field) { using T = std::decay_t; switch (field_idx) { case 0: EXPECT_TRUE((std::is_same_v)); EXPECT_EQ(field, static_cast(1)); break; case 1: EXPECT_TRUE((std::is_same_v)); EXPECT_EQ(field, static_cast(2)); break; case 2: EXPECT_TRUE((std::is_same_v)); EXPECT_EQ(field, static_cast(true)); break; default: FAIL() << "unexpected field"; break; } field_idx++; }); } TEST(ReflectionTest, ForeachFieldNonConst) { S s{1, 2, true}; size_t field_idx = 0; ForeachField(s, [&](auto& field) { using T = std::decay_t; switch (field_idx) { case 0: EXPECT_TRUE((std::is_same_v)); EXPECT_EQ(field, static_cast(1)); field = static_cast(10); break; case 1: EXPECT_TRUE((std::is_same_v)); EXPECT_EQ(field, static_cast(2)); field = static_cast(20); break; case 2: EXPECT_TRUE((std::is_same_v)); EXPECT_EQ(field, static_cast(true)); field = static_cast(false); break; default: FAIL() << "unexpected field"; break; } field_idx++; }); EXPECT_EQ(s.i, 10); EXPECT_EQ(s.u, 20u); EXPECT_EQ(s.b, false); } } // namespace } // namespace tint