"""
Tests for helper functions.

Andrew DeOrio <awdeorio@umich.edu>
"""
import textwrap
from pathlib import Path
import pytest
from mailmerge.__main__ import enumerate_range, read_csv_database
from mailmerge import MailmergeError

# Every test_enumerate_range_* unit test uses a list comprehension to yield all
# the values from the generator implementation.
# pylint: disable=unnecessary-comprehension


def test_enumerate_range_default():
    """Verify default start and stop."""
    output = [i for i in enumerate_range(["a", "b", "c"])]
    assert output == [(0, "a"), (1, "b"), (2, "c")]


def test_enumerate_range_stop_none():
    """Verify stop=None."""
    output = [i for i in enumerate_range(["a", "b", "c"], stop=None)]
    assert output == [(0, "a"), (1, "b"), (2, "c")]


def test_enumerate_range_stop_value():
    """Verify stop=value."""
    output = [i for i in enumerate_range(["a", "b", "c"], stop=1)]
    assert output == [(0, "a")]


def test_enumerate_range_stop_zero():
    """Verify stop=0."""
    output = [i for i in enumerate_range(["a", "b", "c"], stop=0)]
    assert output == []


def test_enumerate_range_stop_too_big():
    """Verify stop when value is greater than length."""
    output = [i for i in enumerate_range(["a", "b", "c"], stop=10)]
    assert output == [(0, "a"), (1, "b"), (2, "c")]


def test_enumerate_range_start_zero():
    """Verify start=0."""
    output = [i for i in enumerate_range(["a", "b", "c"], start=0)]
    assert output == [(0, "a"), (1, "b"), (2, "c")]


def test_enumerate_range_start_value():
    """Verify start=1."""
    output = [i for i in enumerate_range(["a", "b", "c"], start=1)]
    assert output == [(1, "b"), (2, "c")]


def test_enumerate_range_start_last_one():
    """Verify start=length - 1."""
    output = [i for i in enumerate_range(["a", "b", "c"], start=2)]
    assert output == [(2, "c")]


def test_enumerate_range_start_length():
    """Verify start=length."""
    output = [i for i in enumerate_range(["a", "b", "c"], start=3)]
    assert output == []


def test_enumerate_range_start_too_big():
    """Verify start past the end."""
    output = [i for i in enumerate_range(["a", "b", "c"], start=10)]
    assert output == []


def test_enumerate_range_start_stop():
    """Verify start and stop together."""
    output = [i for i in enumerate_range(["a", "b", "c"], start=1, stop=2)]
    assert output == [(1, "b")]


def test_csv_bad(tmpdir):
    """CSV with unmatched quote."""
    database_path = Path(tmpdir/"database.csv")
    database_path.write_text(textwrap.dedent("""\
        a,b
        1,"2
    """))
    with pytest.raises(MailmergeError):
        next(read_csv_database(database_path))


def test_csv_quotes_commas(tmpdir):
    """CSV with quotes and commas.

    Note that quotes are escaped with double quotes, not backslash.
    https://docs.python.org/3.7/library/csv.html#csv.Dialect.doublequote
    """
    database_path = Path(tmpdir/"database.csv")
    database_path.write_text(textwrap.dedent('''\
        email,message
        one@test.com,"Hello, ""world"""
    '''))
    row = next(read_csv_database(database_path))
    assert row["email"] == "one@test.com"
    assert row["message"] == 'Hello, "world"'


def test_csv_utf8(tmpdir):
    """CSV with quotes and commas."""
    database_path = Path(tmpdir/"database.csv")
    database_path.write_text(textwrap.dedent("""\
        email,message
        Laȝamon <lam@test.com>,Laȝamon emoji \xf0\x9f\x98\x80 klâwen
    """))
    row = next(read_csv_database(database_path))
    assert row["email"] == "Laȝamon <lam@test.com>"
    assert row["message"] == "Laȝamon emoji \xf0\x9f\x98\x80 klâwen"

@@ -228,181 +228,8 @@
 or%0A%0A
-# Every test_enumerate_range_* unit test uses a list comprehension to yield all%0A# the values from the generator implementation.%0A# pylint: disable=unnecessary-comprehension%0A%0A
 %0Adef
@@ -311,36 +311,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -352,17 +352,17 @@
 %22, %22c%22%5D)
-%5D
+)
 %0A    ass
@@ -483,36 +483,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -535,17 +535,17 @@
 op=None)
-%5D
+)
 %0A    ass
@@ -668,36 +668,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -717,17 +717,17 @@
  stop=1)
-%5D
+)
 %0A    ass
@@ -825,36 +825,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -874,17 +874,17 @@
  stop=0)
-%5D
+)
 %0A    ass
@@ -1009,36 +1009,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -1055,25 +1055,25 @@
 %22%5D, stop=10)
-%5D
+)
 %0A    assert 
@@ -1189,36 +1189,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -1239,17 +1239,17 @@
 start=0)
-%5D
+)
 %0A    ass
@@ -1370,36 +1370,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -1420,17 +1420,17 @@
 start=1)
-%5D
+)
 %0A    ass
@@ -1553,36 +1553,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -1603,17 +1603,17 @@
 start=2)
-%5D
+)
 %0A    ass
@@ -1720,36 +1720,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -1770,17 +1770,17 @@
 start=3)
-%5D
+)
 %0A    ass
@@ -1886,36 +1886,29 @@
    output = 
-%5Bi for i in 
+list(
 enumerate_ra
@@ -1937,17 +1937,17 @@
 tart=10)
-%5D
+)
 %0A    ass
@@ -2063,20 +2063,13 @@
 t = 
-%5Bi for i in 
+list(
 enum
@@ -2113,17 +2113,17 @@
  stop=2)
-%5D
+)
 %0A    ass

Kill a pylint exception