assert-value / assert_value_elixir

ExUnit's assert on steroids that writes and updates tests for you
MIT License
102 stars 9 forks source link

Parser fails to match regex when not only whitespace before assert_value call #16

Open iamyojimbo opened 6 months ago

iamyojimbo commented 6 months ago

Problem

I have code that looks like this:

assert_with_context2(
  context: assert_value(chat_result_to_string(chat_result) == ""),
  value:
    assert_value(
      parse_cpl_result(chat_result.result_message) == [
        "data",
        "data",
        "data"
      ]
    )
)

This throws the error on the context argument as:

21:59:43.581 [error] GenServer AssertValue.Server terminating
** (MatchError) no match of right hand side value: nil
    (assert_value 0.10.4) lib/assert_value/parser.ex:60: AssertValue.Parser.parse_assert_value/5
    (assert_value 0.10.4) lib/assert_value/server.ex:157: AssertValue.Server.prepare_formatted_diff_and_new_code/2
    (assert_value 0.10.4) lib/assert_value/server.ex:86: AssertValue.Server.handle_call/3
    (stdlib 4.3) gen_server.erl:1149: :gen_server.try_handle_call/4
    (stdlib 4.3) gen_server.erl:1178: :gen_server.handle_msg/6
    (stdlib 4.3) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

But it works as expected when I make sure that assert_value call has whitespace up until the margin:

assert_with_context2(
  context:
    assert_value(chat_result_to_string(chat_result) == "--------------------------------"),
  value:
    assert_value(
      parse_cpl_result(chat_result.result_message) == [
        "data",
        "data",
        "data"
      ]
    )
)

Cause

Looks like it's due to this code in lib/assert_value/parser.ex:60:

[_, indentation, assert_value, rest] =
  Regex.run(~r/(^\s*)(assert_value\s*)(.*)/s, suffix)

It assumes that assert_value is preceded by whitespace. Otherwise it won't match anything.

I'd assume that parsing would be more robustly done by first converting the source code to the AST representation and then just looking for the call to :assert_value and manipulating the AST. But without having much more context, I can't comment further.

smetana commented 6 months ago

Hi, Thank you for the report. Will investigate this