// Copyright 2019 Google LLC // // 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 // // https://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 "dap/any.h" #include "dap/typeof.h" #include "dap/types.h" #include "gmock/gmock.h" #include "gtest/gtest.h" namespace dap { struct AnyTestObject { dap::integer i; dap::number n; }; DAP_STRUCT_TYPEINFO(AnyTestObject, "AnyTestObject", DAP_FIELD(i, "i"), DAP_FIELD(n, "n")); } // namespace dap TEST(Any, EmptyConstruct) { dap::any any; ASSERT_TRUE(any.is()); ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is>()); ASSERT_FALSE(any.is()); } TEST(Any, Boolean) { dap::any any(dap::boolean(true)); ASSERT_TRUE(any.is()); ASSERT_EQ(any.get(), dap::boolean(true)); } TEST(Any, Integer) { dap::any any(dap::integer(10)); ASSERT_TRUE(any.is()); ASSERT_EQ(any.get(), dap::integer(10)); } TEST(Any, Number) { dap::any any(dap::number(123.0f)); ASSERT_TRUE(any.is()); ASSERT_EQ(any.get(), dap::number(123.0f)); } TEST(Any, Array) { using array = dap::array; dap::any any(array({10, 20, 30})); ASSERT_TRUE(any.is()); ASSERT_EQ(any.get(), array({10, 20, 30})); } TEST(Any, Object) { dap::object o; o["one"] = dap::integer(1); o["two"] = dap::integer(2); o["three"] = dap::integer(3); dap::any any(o); ASSERT_TRUE(any.is()); if (any.is()) { auto got = any.get(); ASSERT_EQ(got.size(), 3); ASSERT_EQ(got.count("one"), 1); ASSERT_EQ(got.count("two"), 1); ASSERT_EQ(got.count("three"), 1); ASSERT_TRUE(got["one"].is()); ASSERT_TRUE(got["two"].is()); ASSERT_TRUE(got["three"].is()); ASSERT_EQ(got["one"].get(), dap::integer(1)); ASSERT_EQ(got["two"].get(), dap::integer(2)); ASSERT_EQ(got["three"].get(), dap::integer(3)); } } TEST(Any, TestObject) { dap::any any(dap::AnyTestObject{5, 3.0}); ASSERT_TRUE(any.is()); ASSERT_EQ(any.get().i, 5); ASSERT_EQ(any.get().n, 3.0); } TEST(Any, Assign) { dap::any any; any = dap::integer(10); ASSERT_TRUE(any.is()); ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); ASSERT_EQ(any.get(), dap::integer(10)); any = dap::boolean(true); ASSERT_FALSE(any.is()); ASSERT_TRUE(any.is()); ASSERT_FALSE(any.is()); ASSERT_EQ(any.get(), dap::boolean(true)); any = dap::AnyTestObject{5, 3.0}; ASSERT_FALSE(any.is()); ASSERT_FALSE(any.is()); ASSERT_TRUE(any.is()); ASSERT_EQ(any.get().i, 5); ASSERT_EQ(any.get().n, 3.0); } TEST(Any, Reset) { dap::any any(dap::integer(10)); ASSERT_TRUE(any.is()); any.reset(); ASSERT_FALSE(any.is()); }