// Copyright (c) 2017-2022 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 #include "jsg-test.h" namespace workerd::jsg::test { namespace { // Static unit tests for Web IDL type concepts. // DictionaryType concept tests static_assert(webidl::DictionaryType); static_assert(!webidl::DictionaryType); static_assert(!webidl::DictionaryType>); // NonCallbackInterfaceType concept tests static_assert(!webidl::NonCallbackInterfaceType); static_assert(webidl::NonCallbackInterfaceType); static_assert(!webidl::NonCallbackInterfaceType>); static_assert(webidl::NonCallbackInterfaceType>); // Backward-compatible variable template tests (these delegate to concepts) static_assert(webidl::isNonCallbackInterfaceType == false); static_assert(webidl::isNonCallbackInterfaceType == true); static_assert(webidl::isNonCallbackInterfaceType> == false); // Additional type category concept tests static_assert(webidl::StringType); static_assert(webidl::StringType); static_assert(webidl::StringType); static_assert(!webidl::StringType); static_assert(webidl::NumericType); static_assert(webidl::NumericType); static_assert(!webidl::NumericType); static_assert(webidl::BooleanType); static_assert(!webidl::BooleanType); static_assert(webidl::InterfaceLikeType); static_assert(webidl::InterfaceLikeType>); static_assert(!webidl::InterfaceLikeType); static_assert(webidl::DistinguishableType); static_assert(webidl::DistinguishableType); static_assert(webidl::DistinguishableType); static_assert(webidl::DistinguishableType); static_assert(webidl::nullableTypeCount == 0); static_assert(webidl::nullableTypeCount> == 1); static_assert(webidl::nullableTypeCount, kj::Maybe> == 2); static_assert(webidl::nullableTypeCount, kj::Maybe>> == 2); static_assert( webidl::nullableTypeCount, kj::Maybe>>> == 3); static_assert(webidl::nullableTypeCount, kj::Maybe>, kj::OneOf, kj::Maybe>> == 4); static_assert(webidl::nullableTypeCount>>> == 2); static_assert(webidl::hasDuplicateTypes == false); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == false); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::hasDuplicateTypes == true); static_assert(webidl::FlattenedTypeTraits::stringTypeCount == 2); static_assert(webidl::FlattenedTypeTraits::stringTypeCount == 2); // ===================================================================================== // ArgumentIndexes tests (meta.h) // Member function - no magic param. struct Dummy { int noMagic(int, double, bool); int withLock(Lock&, int, double); int withInfo(const v8::FunctionCallbackInfo&, int); int constNoMagic(int) const; int constWithLock(Lock&, int, double) const; int constWithInfo(const v8::FunctionCallbackInfo&) const; }; static_assert( kj::isSameType, kj::_::Indexes<0, 1, 2>>()); static_assert(kj::isSameType, kj::_::Indexes<0, 1>>()); static_assert(kj::isSameType, kj::_::Indexes<0>>()); static_assert(kj::isSameType, kj::_::Indexes<0>>()); static_assert( kj::isSameType, kj::_::Indexes<0, 1>>()); static_assert(kj::isSameType, kj::_::Indexes<>>()); // Free functions. static_assert(kj::isSameType, kj::_::Indexes<0, 1>>()); static_assert(kj::isSameType, kj::_::Indexes<0>>()); static_assert(kj::isSameType, kj::_::Indexes<>>()); // ===================================================================================== // requiredArgumentCount tests (meta.h + type-wrapper.h) // // requiredArgumentCount uses the ValueLessParameter concept to detect // injected parameter types. We use a minimal mock wrapper that provides the right unwrap() // overloads so the concept checks succeed for TypeHandler and a fake injected config type. struct FakeConfig {}; // Simulates an InjectConfiguration injected type. struct MockTypeWrapper { // Makes ValueLessParameter> true. template const TypeHandler& unwrap(Lock&, v8::Local, TypeHandler*); // Makes ValueLessParameter true (simulates InjectConfiguration). FakeConfig unwrap(Lock&, v8::Local, FakeConfig*); }; // Shorthand for the tests below. template constexpr int rac = requiredArgumentCount; // All required - count equals total visible args. static_assert(rac == 3); static_assert(rac == 3); // No args — length is 0. static_assert(rac == 0); static_assert(rac == 0); // Optional args stop the count. static_assert(rac)> == 1); static_assert(rac)> == 2); static_assert(rac)> == 0); static_assert(rac)> == 1); // LenientOptional also stops the count. static_assert(rac)> == 1); // TypeHandler is invisible - does not count and does not stop. static_assert(rac&, int, double)> == 2); static_assert(rac&, double)> == 2); static_assert(rac&, Optional)> == 1); // Arguments following optionals are not counted. static_assert(rac&, Optional, int)> == 1); // Arguments is invisible - does not count and does not stop. static_assert(rac)> == 1); // InjectConfiguration types (e.g. CompatibilityFlags::Reader) are invisible. static_assert(rac == 2); static_assert(rac == 2); static_assert(rac)> == 1); // Member functions. static_assert(rac == 3); static_assert(rac == 2); static_assert(rac == 1); static_assert(rac == 1); static_assert(rac == 2); static_assert(rac == 0); // ===================================================================================== // ValueLessParameter concept tests (type-wrapper.h) static_assert(ValueLessParameter> == true); static_assert(ValueLessParameter == true); static_assert(ValueLessParameter == false); static_assert(ValueLessParameter> == false); static_assert(ValueLessParameter == false); // Arguments is NOT a ValueLessParameter — it has its own handling via isArguments<>(). static_assert(ValueLessParameter> == false); KJ_TEST("web-idl meta") { // Nothing to actually do here; tests are compile-time } } // namespace } // namespace workerd::jsg::test