]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #include "opentelemetry/common/key_value_iterable_view.h" | |
5 | ||
6 | #include <gtest/gtest.h> | |
7 | #include <map> | |
8 | #include "opentelemetry/nostd/type_traits.h" | |
9 | ||
10 | using namespace opentelemetry; | |
11 | ||
12 | static int TakeKeyValues(const common::KeyValueIterable &iterable) | |
13 | { | |
14 | std::map<std::string, common::AttributeValue> result; | |
15 | int count = 0; | |
16 | iterable.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept { | |
17 | ++count; | |
18 | return true; | |
19 | }); | |
20 | return count; | |
21 | } | |
22 | ||
23 | template <class T, nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr> | |
24 | static int TakeKeyValues(const T &iterable) | |
25 | { | |
26 | return TakeKeyValues(common::KeyValueIterableView<T>{iterable}); | |
27 | } | |
28 | ||
29 | TEST(KeyValueIterableViewTest, is_key_value_iterable) | |
30 | { | |
31 | using M1 = std::map<std::string, std::string>; | |
32 | EXPECT_TRUE(bool{common::detail::is_key_value_iterable<M1>::value}); | |
33 | ||
34 | using M2 = std::map<std::string, int>; | |
35 | EXPECT_TRUE(bool{common::detail::is_key_value_iterable<M2>::value}); | |
36 | ||
37 | using M3 = std::map<std::string, common::AttributeValue>; | |
38 | EXPECT_TRUE(bool{common::detail::is_key_value_iterable<M3>::value}); | |
39 | ||
40 | struct A | |
41 | {}; | |
42 | using M4 = std::map<std::string, A>; | |
43 | EXPECT_FALSE(bool{common::detail::is_key_value_iterable<M4>::value}); | |
44 | } | |
45 | ||
46 | TEST(KeyValueIterableViewTest, ForEachKeyValue) | |
47 | { | |
48 | std::map<std::string, std::string> m1 = {{"abc", "123"}, {"xyz", "456"}}; | |
49 | EXPECT_EQ(TakeKeyValues(m1), 2); | |
50 | ||
51 | std::vector<std::pair<std::string, int>> v1 = {{"abc", 123}, {"xyz", 456}}; | |
52 | EXPECT_EQ(TakeKeyValues(v1), 2); | |
53 | } | |
54 | ||
55 | TEST(KeyValueIterableViewTest, ForEachKeyValueWithExit) | |
56 | { | |
57 | using M = std::map<std::string, std::string>; | |
58 | M m1 = {{"abc", "123"}, {"xyz", "456"}}; | |
59 | common::KeyValueIterableView<M> iterable{m1}; | |
60 | int count = 0; | |
61 | auto exit = iterable.ForEachKeyValue( | |
62 | [&count](nostd::string_view /*key*/, common::AttributeValue /*value*/) noexcept { | |
63 | ++count; | |
64 | return false; | |
65 | }); | |
66 | EXPECT_EQ(count, 1); | |
67 | EXPECT_FALSE(exit); | |
68 | } |