Skip to content

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

Open

Description

@iamyojimbo

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.

Activity

  1. changed the title [-]Parser fails to match regex[/-] [+]Parser fails to match regex when not all whitespace before assert_value call[/+] on Mar 27, 2024
  2. changed the title [-]Parser fails to match regex when not all whitespace before assert_value call[/-] [+]Parser fails to match regex when not only whitespace before assert_value call[/+] on Mar 27, 2024
  3. smetana commented on Mar 28, 2024

    @smetana
    Contributor

    Hi,
    Thank you for the report. Will investigate this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions