diff --git a/strings/base_collections_vector.h b/strings/base_collections_vector.h index b82ba3853..ff0ad6bec 100644 --- a/strings/base_collections_vector.h +++ b/strings/base_collections_vector.h @@ -115,15 +115,26 @@ namespace winrt::impl bool IndexOf(Windows::Foundation::IInspectable const& value, uint32_t& index) const { - try + if constexpr (is_com_interface_v) { - return IndexOf(unbox_value(value), index); + if (!value) + { + return base_type::IndexOf(nullptr, index); + } + else if (auto as = value.try_as()) + { + return base_type::IndexOf(as, index); + } } - catch (hresult_no_interface const&) + else { - index = 0; - return false; + if (auto as = value.try_as()) + { + return base_type::IndexOf(as.value(), index); + } } + + return false; } using base_type::GetMany; diff --git a/test/test/observable_index_of.cpp b/test/test/observable_index_of.cpp new file mode 100644 index 000000000..14d434d34 --- /dev/null +++ b/test/test/observable_index_of.cpp @@ -0,0 +1,37 @@ +#include "pch.h" + +using namespace winrt; +using namespace Windows::Foundation; +using namespace Windows::Foundation::Collections; + +TEST_CASE("observable_index_of") +{ + { + auto v = single_threaded_observable_vector().as>(); + v.Append(box_value(123)); + uint32_t index = 0; + REQUIRE(v.IndexOf(box_value(123), index)); + REQUIRE(!v.IndexOf(nullptr, index)); + REQUIRE(!v.IndexOf(box_value(456), index)); + REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index)); + } + { + auto value = Uri(L"http://kennykerr.ca"); + + auto v = single_threaded_observable_vector().as>(); + v.Append(value); + uint32_t index = 0; + REQUIRE(v.IndexOf(value, index)); + REQUIRE(!v.IndexOf(nullptr, index)); + REQUIRE(!v.IndexOf(box_value(456), index)); + REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index)); + } + { + auto v = single_threaded_observable_vector().as>(); + v.Append(nullptr); + uint32_t index = 0; + REQUIRE(v.IndexOf(nullptr, index)); + REQUIRE(!v.IndexOf(box_value(456), index)); + REQUIRE(!v.IndexOf(Uri(L"http://kennykerr.ca"), index)); + } +} diff --git a/test/test/test.vcxproj b/test/test/test.vcxproj index ed6e934bd..b6c9c229a 100644 --- a/test/test/test.vcxproj +++ b/test/test/test.vcxproj @@ -417,6 +417,7 @@ +