emqx_utils_maps_tests.erl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_maps_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. -import(emqx_utils_maps, [indent/3, unindent/2]).
  19. best_effort_recursive_sum_test_() ->
  20. DummyLogger = fun(_) -> ok end,
  21. [
  22. ?_assertEqual(
  23. #{foo => 3},
  24. emqx_utils_maps:best_effort_recursive_sum(#{foo => 1}, #{foo => 2}, DummyLogger)
  25. ),
  26. ?_assertEqual(
  27. #{foo => 3, bar => 6.0},
  28. emqx_utils_maps:best_effort_recursive_sum(
  29. #{foo => 1, bar => 2.0}, #{foo => 2, bar => 4.0}, DummyLogger
  30. )
  31. ),
  32. ?_assertEqual(
  33. #{foo => 1, bar => 2},
  34. emqx_utils_maps:best_effort_recursive_sum(#{foo => 1}, #{bar => 2}, DummyLogger)
  35. ),
  36. ?_assertEqual(
  37. #{foo => #{bar => 42}},
  38. emqx_utils_maps:best_effort_recursive_sum(
  39. #{foo => #{bar => 2}}, #{foo => #{bar => 40}}, DummyLogger
  40. )
  41. ),
  42. fun() ->
  43. Self = self(),
  44. Logger = fun(What) -> Self ! {log, What} end,
  45. ?assertEqual(
  46. #{foo => 1, bar => 2},
  47. emqx_utils_maps:best_effort_recursive_sum(
  48. #{foo => 1, bar => 2}, #{bar => bar}, Logger
  49. )
  50. ),
  51. receive
  52. {log, Log} ->
  53. ?assertEqual(#{failed_to_merge => bar, bad_value => bar}, Log)
  54. after 1000 -> error(timeout)
  55. end
  56. end,
  57. ?_assertEqual(
  58. #{},
  59. emqx_utils_maps:best_effort_recursive_sum(
  60. #{foo => foo}, #{foo => bar}, DummyLogger
  61. )
  62. ),
  63. ?_assertEqual(
  64. #{foo => 1},
  65. emqx_utils_maps:best_effort_recursive_sum(
  66. #{foo => 1}, #{foo => bar}, DummyLogger
  67. )
  68. ),
  69. ?_assertEqual(
  70. #{foo => 1},
  71. emqx_utils_maps:best_effort_recursive_sum(
  72. #{foo => bar}, #{foo => 1}, DummyLogger
  73. )
  74. ),
  75. ?_assertEqual(
  76. #{foo => #{bar => 1}},
  77. emqx_utils_maps:best_effort_recursive_sum(
  78. #{foo => #{bar => 1}}, #{foo => 1}, DummyLogger
  79. )
  80. ),
  81. ?_assertEqual(
  82. #{foo => #{bar => 1}},
  83. emqx_utils_maps:best_effort_recursive_sum(
  84. #{foo => 1}, #{foo => #{bar => 1}}, DummyLogger
  85. )
  86. ),
  87. ?_assertEqual(
  88. #{foo => #{bar => 1}},
  89. emqx_utils_maps:best_effort_recursive_sum(
  90. #{foo => 1, bar => ignored}, #{foo => #{bar => 1}}, DummyLogger
  91. )
  92. ),
  93. ?_assertEqual(
  94. #{foo => #{bar => 2}, bar => #{foo => 1}},
  95. emqx_utils_maps:best_effort_recursive_sum(
  96. #{foo => 1, bar => #{foo => 1}}, #{foo => #{bar => 2}, bar => 2}, DummyLogger
  97. )
  98. ),
  99. ?_assertEqual(
  100. #{foo => #{bar => 2}, bar => #{foo => 1}},
  101. emqx_utils_maps:best_effort_recursive_sum(
  102. #{foo => #{bar => 2}, bar => 2}, #{foo => 1, bar => #{foo => 1}}, DummyLogger
  103. )
  104. ),
  105. ?_assertEqual(
  106. #{foo => #{bar => #{}}},
  107. emqx_utils_maps:best_effort_recursive_sum(
  108. #{foo => #{bar => #{foo => []}}}, #{foo => 1}, DummyLogger
  109. )
  110. )
  111. ].
  112. key_comparer_test() ->
  113. Comp = emqx_utils_maps:key_comparer(foo),
  114. ?assertEqual(
  115. [
  116. #{},
  117. #{baz => 42},
  118. #{foo => 1},
  119. #{foo => 42},
  120. #{foo => bar, baz => 42}
  121. ],
  122. lists:sort(Comp, [
  123. #{foo => 42},
  124. #{baz => 42},
  125. #{foo => bar, baz => 42},
  126. #{foo => 1},
  127. #{}
  128. ])
  129. ).
  130. map_indent_unindent_test_() ->
  131. M = #{a => 1, b => 2},
  132. [
  133. ?_assertEqual(
  134. #{a => 1, c => #{b => 2}},
  135. indent(c, [b], M)
  136. ),
  137. ?_assertEqual(
  138. M,
  139. unindent(c, indent(c, [b], M))
  140. ),
  141. ?_assertEqual(
  142. #{a => 1, b => #{b => 2}},
  143. indent(b, [b], M)
  144. ),
  145. ?_assertEqual(
  146. M,
  147. unindent(b, #{a => 1, b => #{b => 2}})
  148. ),
  149. ?_assertEqual(
  150. #{a => 2},
  151. unindent(b, #{a => 1, b => #{a => 2}})
  152. ),
  153. ?_assertEqual(
  154. #{c => #{a => 1, b => 2}},
  155. indent(c, [a, b], M)
  156. ),
  157. ?_assertEqual(
  158. #{a => 1, b => 2, c => #{}},
  159. indent(c, [], M)
  160. ),
  161. ?_assertEqual(
  162. #{a => 1, b => 2, c => #{}},
  163. indent(c, [d, e, f], M)
  164. ),
  165. ?_assertEqual(
  166. #{a => 1, b => 2},
  167. unindent(c, M)
  168. ),
  169. ?_assertEqual(
  170. #{a => #{c => 3, d => 4}},
  171. unindent(b, #{a => #{c => 3}, b => #{a => #{d => 4}}})
  172. )
  173. ].