emqx_map_lib_tests.erl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2022 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_map_lib_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. best_effort_recursive_sum_test_() ->
  19. DummyLogger = fun(_) -> ok end,
  20. [
  21. ?_assertEqual(
  22. #{foo => 3},
  23. emqx_map_lib:best_effort_recursive_sum(#{foo => 1}, #{foo => 2}, DummyLogger)
  24. ),
  25. ?_assertEqual(
  26. #{foo => 3, bar => 6.0},
  27. emqx_map_lib:best_effort_recursive_sum(
  28. #{foo => 1, bar => 2.0}, #{foo => 2, bar => 4.0}, DummyLogger
  29. )
  30. ),
  31. ?_assertEqual(
  32. #{foo => 1, bar => 2},
  33. emqx_map_lib:best_effort_recursive_sum(#{foo => 1}, #{bar => 2}, DummyLogger)
  34. ),
  35. ?_assertEqual(
  36. #{foo => #{bar => 42}},
  37. emqx_map_lib:best_effort_recursive_sum(
  38. #{foo => #{bar => 2}}, #{foo => #{bar => 40}}, DummyLogger
  39. )
  40. ),
  41. fun() ->
  42. Self = self(),
  43. Logger = fun(What) -> Self ! {log, What} end,
  44. ?assertEqual(
  45. #{foo => 1, bar => 2},
  46. emqx_map_lib:best_effort_recursive_sum(#{foo => 1, bar => 2}, #{bar => bar}, Logger)
  47. ),
  48. receive
  49. {log, Log} ->
  50. ?assertEqual(#{failed_to_merge => bar, bad_value => bar}, Log)
  51. after 1000 -> error(timeout)
  52. end
  53. end,
  54. ?_assertEqual(
  55. #{},
  56. emqx_map_lib:best_effort_recursive_sum(
  57. #{foo => foo}, #{foo => bar}, DummyLogger
  58. )
  59. ),
  60. ?_assertEqual(
  61. #{foo => 1},
  62. emqx_map_lib:best_effort_recursive_sum(
  63. #{foo => 1}, #{foo => bar}, DummyLogger
  64. )
  65. ),
  66. ?_assertEqual(
  67. #{foo => 1},
  68. emqx_map_lib:best_effort_recursive_sum(
  69. #{foo => bar}, #{foo => 1}, DummyLogger
  70. )
  71. ),
  72. ?_assertEqual(
  73. #{foo => #{bar => 1}},
  74. emqx_map_lib:best_effort_recursive_sum(
  75. #{foo => #{bar => 1}}, #{foo => 1}, DummyLogger
  76. )
  77. ),
  78. ?_assertEqual(
  79. #{foo => #{bar => 1}},
  80. emqx_map_lib:best_effort_recursive_sum(
  81. #{foo => 1}, #{foo => #{bar => 1}}, DummyLogger
  82. )
  83. ),
  84. ?_assertEqual(
  85. #{foo => #{bar => 1}},
  86. emqx_map_lib:best_effort_recursive_sum(
  87. #{foo => 1, bar => ignored}, #{foo => #{bar => 1}}, DummyLogger
  88. )
  89. ),
  90. ?_assertEqual(
  91. #{foo => #{bar => 2}, bar => #{foo => 1}},
  92. emqx_map_lib:best_effort_recursive_sum(
  93. #{foo => 1, bar => #{foo => 1}}, #{foo => #{bar => 2}, bar => 2}, DummyLogger
  94. )
  95. ),
  96. ?_assertEqual(
  97. #{foo => #{bar => 2}, bar => #{foo => 1}},
  98. emqx_map_lib:best_effort_recursive_sum(
  99. #{foo => #{bar => 2}, bar => 2}, #{foo => 1, bar => #{foo => 1}}, DummyLogger
  100. )
  101. ),
  102. ?_assertEqual(
  103. #{foo => #{bar => #{}}},
  104. emqx_map_lib:best_effort_recursive_sum(
  105. #{foo => #{bar => #{foo => []}}}, #{foo => 1}, DummyLogger
  106. )
  107. )
  108. ].