<commit_before>defmodule EncodingTests.EscapedFieldsTest do
  use ExUnit.Case
  alias CSV.Encoding.Encoder

  test "encodes streams to csv strings and escapes them" do
    result = Encoder.encode([["a,", "b\re"], ["c,f\"", "dg"]]) |> Enum.to_list()
    assert result == ["\"a,\",\"b\re\"\r\n", "\"c,f\"\"\",dg\r\n"]
  end

  test "encodes formulas and escapes them" do
    result =
      Encoder.encode(
        [["=1+1", ~S(=1+2";=1+2), ~S(=1+2'" ;,=1+2)], ["-10+7"], ["+10+7"], ["@A1:A10"]],
        escape_formulas: true
      )
      |> Enum.to_list()

    assert result == [
             ~S("'=1+1","'=1+2"";=1+2","'=1+2'"" ;,=1+2") <> "\r\n",
             ~S("'-10+7") <> "\r\n",
             ~S("'+10+7") <> "\r\n",
             ~S("'@A1:A10") <> "\r\n"
           ]
  end

  test "encodes streams of various content to csv strings and escapes them" do
    result = Encoder.encode([[:atom, 1], [["a", "b"], "dg"]]) |> Enum.to_list()
    assert result == ["atom,1\r\n", "ab,dg\r\n"]
  end

  test "allows custom separators and delimiters and escapes them" do
    result =
      Encoder.encode([["a\t", "b\re"], ["c\tf\"", "dg"]], separator: ?\t, delimiter: "\n")
      |> Enum.to_list()

    assert result == ["\"a\t\"\t\"b\re\"\n", "\"c\tf\"\"\"\tdg\n"]
  end

  test "force_quotes does not break escaping" do
    result = Encoder.encode([[:atom, 1], [["a", "b"], "dg"]], force_quotes: true) |> Enum.to_list
    assert result == ["atom,1\r\n", "ab,dg\r\n"]
  end
end<commit_msg>Add tests for escaped fields and force_quote<commit_after>defmodule EncodingTests.EscapedFieldsTest do
  use ExUnit.Case
  alias CSV.Encoding.Encoder

  test "encodes streams to csv strings and escapes them" do
    result = Encoder.encode([["a,", "b\re"], ["c,f\"", "dg"]]) |> Enum.to_list()
    assert result == ["\"a,\",\"b\re\"\r\n", "\"c,f\"\"\",dg\r\n"]
  end

  test "encodes formulas and escapes them" do
    result =
      Encoder.encode(
        [["=1+1", ~S(=1+2";=1+2), ~S(=1+2'" ;,=1+2)], ["-10+7"], ["+10+7"], ["@A1:A10"]],
        escape_formulas: true
      )
      |> Enum.to_list()

    assert result == [
             ~S("'=1+1","'=1+2"";=1+2","'=1+2'"" ;,=1+2") <> "\r\n",
             ~S("'-10+7") <> "\r\n",
             ~S("'+10+7") <> "\r\n",
             ~S("'@A1:A10") <> "\r\n"
           ]
  end

  test "encodes streams of various content to csv strings and escapes them" do
    result = Encoder.encode([[:atom, 1], [["a", "b"], "dg"]]) |> Enum.to_list()
    assert result == ["atom,1\r\n", "ab,dg\r\n"]
  end

  test "allows custom separators and delimiters and escapes them" do
    result =
      Encoder.encode([["a\t", "b\re"], ["c\tf\"", "dg"]], separator: ?\t, delimiter: "\n")
      |> Enum.to_list()

    assert result == ["\"a\t\"\t\"b\re\"\n", "\"c\tf\"\"\"\tdg\n"]
  end

  test "force_quotes works with various content" do
    result = Encoder.encode([[:atom, 1], [["a", "b"], "dg"]], force_quotes: true) |> Enum.to_list
    assert result == ["\"atom\",\"1\"\r\n", "\"ab\",\"dg\"\r\n"]
  end

  test "force_quotes works with content that needs escapes" do
    result = Encoder.encode([["a,", "b\re"], ["c,f\"", "dg"]], force_quotes: true) |> Enum.to_list
    assert result == ["\"a,\",\"b\\re\"\r\n", "\"c,f\"\"\",\"dg\"\r\n"]
  end
end
{'fraction_non_alphanumeric': 0.26556271082490035, 'fraction_numerical': 0.025758969641214352, 'mean_word_length': 2.736540664375716, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 22, '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': '3816247', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1289, 'n_words': 289}