emqx_utils_conv_tests.erl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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_conv_tests).
  17. -import(emqx_utils_conv, [bin/1, str/1]).
  18. -include_lib("eunit/include/eunit.hrl").
  19. bin_test_() ->
  20. [
  21. ?_assertEqual(<<"abc">>, bin("abc")),
  22. ?_assertEqual(<<"abc">>, bin(abc)),
  23. ?_assertEqual(<<"{\"a\":1}">>, bin(#{a => 1})),
  24. ?_assertEqual(<<"[{\"a\":1}]">>, bin([#{a => 1}])),
  25. ?_assertEqual(<<"1">>, bin(1)),
  26. ?_assertEqual(<<"2.0">>, bin(2.0)),
  27. ?_assertEqual(<<"true">>, bin(true)),
  28. ?_assertError(_, bin({a, v}))
  29. ].
  30. str_test_() ->
  31. [
  32. ?_assertEqual("abc", str("abc")),
  33. ?_assertEqual("abc", str(abc)),
  34. ?_assertEqual("{\"a\":1}", str(#{a => 1})),
  35. ?_assertEqual("1", str(1)),
  36. ?_assertEqual("2.0", str(2.0)),
  37. ?_assertEqual("true", str(true)),
  38. ?_assertError(_, str({a, v}))
  39. ].