<commit_before>defmodule ParserTest do
  use ExUnit.Case
  alias CSV.Parser, as: Parser

  setup do
    test_pid = self
    parser_pid = spawn_link(fn ->
      Parser.parse_into(test_pid)
    end)

    {:ok, parser_pid: parser_pid}
  end

  test "turns a sequence of tokens into a csv matrix", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:content, "b"},
      {:delimiter, "\r\n"},
      {:end, 1},
      {:start, 2},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:row, {1, ~w(a b)}}, 10
    assert_receive {:row, {2, ~w(c d)}}, 10
  end

  test "turns a sequence of tokens with escape sequences into a csv matrix", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:row, {1, ["a", "b\r\nc,"]}}, 10
    assert_receive {:row, {2, ["c", "d"]}}, 10
  end

  test "manages escaped double quotes inside double quoted fields according to RFC 4180", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:double_quote, "\""},
      {:double_quote, "\""},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:row, {1, ["a", "b\"c,"]}}, 10
    assert_receive {:row, {2, ["c", "d"]}}, 10
  end

  test "raises a syntax error when given an invalid sequence of tokens", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:double_quote, "\""},
      {:double_quote, "\""},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:double_quote, "\""},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:syntax_error, {2, "Unterminated escape sequence."}}, 10
  end
<commit_msg>Cover stream halted in midst of escape sequence<commit_after>defmodule ParserTest do
  use ExUnit.Case
  alias CSV.Parser, as: Parser

  setup do
    test_pid = self
    parser_pid = spawn_link(fn ->
      Parser.parse_into(test_pid)
    end)

    {:ok, parser_pid: parser_pid}
  end

  test "turns a sequence of tokens into a csv matrix", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:content, "b"},
      {:delimiter, "\r\n"},
      {:end, 1},
      {:start, 2},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:row, {1, ~w(a b)}}, 10
    assert_receive {:row, {2, ~w(c d)}}, 10
  end

  test "turns a sequence of tokens with escape sequences into a csv matrix", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:row, {1, ["a", "b\r\nc,"]}}, 10
    assert_receive {:row, {2, ["c", "d"]}}, 10
  end

  test "manages escaped double quotes inside double quoted fields according to RFC 4180", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:double_quote, "\""},
      {:double_quote, "\""},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:row, {1, ["a", "b\"c,"]}}, 10
    assert_receive {:row, {2, ["c", "d"]}}, 10
  end

  test "raises a syntax error when given an invalid sequence of tokens", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:double_quote, "\""},
      {:double_quote, "\""},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:double_quote, "\""},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:end, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:syntax_error, {2, "Unterminated escape sequence."}}, 10
  end

  test "raises a syntax error when halted in an escape sequence", context do
    Enum.each [
      {:start, 1},
      {:content, "a"},
      {:separator, ","},
      {:double_quote, "\""},
      {:content, "b"},
      {:double_quote, "\""},
      {:double_quote, "\""},
      {:content, "c"},
      {:separator, ","},
      {:double_quote, "\""},
      {:end, 1},
      {:start, 2},
      {:double_quote, "\""},
      {:delimiter, "\r\n"},
      {:content, "c"},
      {:separator, ","},
      {:content, "d"},
      {:halt, 2}
    ], fn(token) ->
      send(context[:parser_pid], token)
    end

    assert_receive {:syntax_error, {2, "Stream halted with unterminated escape sequence."}}, 10
  end

{'fraction_non_alphanumeric': 0.2256524506683641, 'fraction_numerical': 0.014162953532781668, 'mean_word_length': 2.2768508863399375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1509070', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2262, 'n_words': 556}