emqx_variform_bif_tests.erl 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 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. %% Most of the functions are tested as rule-engine string funcs
  17. -module(emqx_variform_bif_tests).
  18. -compile(export_all).
  19. -compile(nowarn_export_all).
  20. -include_lib("eunit/include/eunit.hrl").
  21. regex_extract_test_() ->
  22. [
  23. ?_assertEqual([<<"12345">>], regex_extract("Order number: 12345", "(\\d+)")),
  24. ?_assertEqual(
  25. [<<"Hello">>, <<"world">>], regex_extract("Hello, world!", "(\\w+).*\s(\\w+)")
  26. ),
  27. ?_assertEqual([], regex_extract("No numbers here!", "(\\d+)")),
  28. ?_assertEqual(
  29. [<<"2021">>, <<"05">>, <<"20">>],
  30. regex_extract("Date: 2021-05-20", "(\\d{4})-(\\d{2})-(\\d{2})")
  31. ),
  32. ?_assertEqual([<<"Hello">>], regex_extract("Hello, world!", "(Hello)")),
  33. ?_assertEqual(
  34. [<<"12">>, <<"34">>], regex_extract("Items: 12, Price: 34", "(\\d+).*\s(\\d+)")
  35. ),
  36. ?_assertEqual(
  37. [<<"john.doe@example.com">>],
  38. regex_extract("Contact: john.doe@example.com", "([\\w\\.]+@[\\w\\.]+)")
  39. ),
  40. ?_assertEqual([], regex_extract("Just some text, nothing more.", "([A-Z]\\d{3})")),
  41. ?_assertEqual(
  42. [<<"admin">>, <<"1234">>],
  43. regex_extract("User: admin, Pass: 1234", "User: (\\w+), Pass: (\\d+)")
  44. ),
  45. ?_assertEqual([], regex_extract("", "(\\d+)")),
  46. ?_assertEqual([], regex_extract("$$$###!!!", "(\\d+)")),
  47. ?_assertEqual([<<"23.1">>], regex_extract("Erlang 23.1 version", "(\\d+\\.\\d+)")),
  48. ?_assertEqual(
  49. [<<"192.168.1.1">>],
  50. regex_extract("Server IP: 192.168.1.1 at port 8080", "(\\d+\\.\\d+\\.\\d+\\.\\d+)")
  51. )
  52. ].
  53. regex_extract(Str, RegEx) ->
  54. emqx_variform_bif:regex_extract(Str, RegEx).
  55. rand_str_test() ->
  56. ?assertEqual(3, size(emqx_variform_bif:rand_str(3))),
  57. ?assertThrow(#{reason := badarg}, size(emqx_variform_bif:rand_str(0))).
  58. rand_int_test() ->
  59. N = emqx_variform_bif:rand_int(10),
  60. ?assert(N =< 10 andalso N >= 1),
  61. ?assertThrow(#{reason := badarg}, emqx_variform_bif:rand_int(0)),
  62. ?assertThrow(#{reason := badarg}, emqx_variform_bif:rand_int(-1)).
  63. base64_encode_decode_test() ->
  64. RandBytes = crypto:strong_rand_bytes(100),
  65. Encoded = emqx_variform_bif:base64_encode(RandBytes),
  66. ?assertEqual(RandBytes, emqx_variform_bif:base64_decode(Encoded)).