import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.List;
import org.junit.jupiter.api.Test;

class CsvTest {

    @Test
    void plainFieldsAreJoinedWithCommas() {
        assertEquals("a,b,3", Csv.join(List.of("a", "b", "3")));
    }

    @Test
    void fieldsWithCommasOrQuotesAreQuoted() {
        assertEquals("\"Lima, de noche\",\"Arturo \"\"Zambo\"\" Cavero\"",
                Csv.join(List.of("Lima, de noche", "Arturo \"Zambo\" Cavero")));
    }

    @Test
    void splitUndoesJoin() {
        List<String> fields = List.of("Lima, de noche", "x \"y\" z", "", "7");
        assertEquals(fields, Csv.split(Csv.join(fields)));
    }

    @Test
    void emptyFieldsAreKept() {
        assertEquals(List.of("a", "", ""), Csv.split("a,,"));
    }

    @Test
    void newlinesInsideAFieldAreRejected() {
        assertThrows(IllegalArgumentException.class, () -> Csv.join(List.of("dos\nlíneas")));
    }

    @Test
    void unclosedQuotesAreRejected() {
        assertThrows(IllegalArgumentException.class, () -> Csv.split("\"abierto,1"));
    }
}
