GeezerVison is showing github.com — Make quote character in @CsvFileSource configurable · junit-team/junit-framework@4181b9c
File treeExpand file treeCollapse file tree Open diff view settings main/java/org/junit/jupiter/params/provider test/java/org/junit/jupiter/params/provider
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: documentation/src/docs/asciidoc/release-notes/release-notes-5.8.2.adoc +7-8Lines changed: 7 additions & 8 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | [[release-notes-5.8.2]] | 2 | 2 | == 5.8.2 | 3 | 3 |
| 4 | | -*Date of Release:* October ❓, 2021 | | 4 | +*Date of Release:* November ❓, 2021 | 5 | 5 |
| 6 | 6 | *Scope:* | 7 | 7 |
| 8 | | -* Text blocks in `@CsvSource` are treated as CSV files | 9 | | -* Custom quote character support in `@CsvSource` | | 8 | +* Text blocks in `@CsvSource` are treated like CSV files | | 9 | +* Custom quote character support in `@CsvSource` and `@CsvFileSource` | 10 | 10 |
| 11 | 11 | For a complete list of all _closed_ issues and pull requests for this release, consult the | 12 | 12 | link:{junit5-repo}+/milestone/60?closed=1+[5.8.2] milestone page in the JUnit repository on | @@ -24,14 +24,13 @@ No changes. | 24 | 24 |
| 25 | 25 | ==== New Features and Improvements | 26 | 26 |
| 27 | | -* Text blocks in `@CsvSource` are now treated as complete CSV files, including support for | 28 | | - comments beginning with a `+++#+++` symbol as well as support for new lines within | | 27 | +* Text blocks in `@CsvSource` are now treated like complete CSV files, including support | | 28 | + for comments beginning with a `+++#+++` symbol as well as support for new lines within | 29 | 29 | quoted strings. See the | 30 | 30 | <<../user-guide/index.adoc#writing-tests-parameterized-tests-sources-CsvSource, User | 31 | 31 | Guide>> for details and examples. | 32 | | -* The quote character for _quoted strings_ in `@CsvSource` is now configurable via the new | 33 | | - `quoteCharacter` attribute, which defaults to a single quote (`'`) for backward | 34 | | - compatibility. | | 32 | +* The quote character for _quoted strings_ in `@CsvSource` and `@CsvFileSource` is now | | 33 | + configurable via new `quoteCharacter` attributes in each annotation. | 35 | 34 |
| 36 | 35 |
| 37 | 36 | [[release-notes-5.8.2-junit-vintage]] | |
Collapse file Copy file name to clipboardExpand all lines: documentation/src/docs/asciidoc/user-guide/writing-tests.adoc +8-7Lines changed: 8 additions & 7 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1344,9 +1344,9 @@ The default delimiter is a comma (`,`), but you can use another character by set | 1344 | 1344 | `String` delimiter instead of a single character. However, both delimiter attributes | 1345 | 1345 | cannot be set simultaneously. | 1346 | 1346 |
| 1347 | | -By default, `@CsvSource` uses a single quote `'` as its quote character, but this can be | | 1347 | +By default, `@CsvSource` uses a single quote (`'`) as its quote character, but this can be | 1348 | 1348 | changed via the `quoteCharacter` attribute. See the `'lemon, lime'` value in the example | 1349 | | -above and in the table below. An empty, quoted value `''` results in an empty `String` | | 1349 | +above and in the table below. An empty, quoted value (`''`) results in an empty `String` | 1350 | 1350 | unless the `emptyValue` attribute is set; whereas, an entirely _empty_ value is | 1351 | 1351 | interpreted as a `null` reference. By specifying one or more `nullValues`, a custom value | 1352 | 1352 | can be interpreted as a `null` reference (see the `NIL` example in the table below). An | @@ -1395,9 +1395,9 @@ In contrast to CSV records supplied via the `value` attribute, a text block can | 1395 | 1395 | comments. Any line beginning with a `+++#+++` symbol will be treated as a comment and | 1396 | 1396 | ignored. Note, however, that the `+++#+++` symbol must be the first character on the line | 1397 | 1397 | without any leading whitespace. It is therefore recommended that the closing text block | 1398 | | -delimiter `"""` be placed either at the end of the last line of input or on the following | 1399 | | -line, left aligned with the rest of the input (as can be seen in the example below which | 1400 | | -demonstrates formatting similar to a table). | | 1398 | +delimiter (`"""`) be placed either at the end of the last line of input or on the | | 1399 | +following line, left aligned with the rest of the input (as can be seen in the example | | 1400 | +below which demonstrates formatting similar to a table). | 1401 | 1401 |
| 1402 | 1402 | [source,java,indent=0] | 1403 | 1403 | ---- | @@ -1458,8 +1458,9 @@ include::{testResourcesDir}/two-column.csv[] | 1458 | 1458 | ---- | 1459 | 1459 |
| 1460 | 1460 | In contrast to the default syntax used in `@CsvSource`, `@CsvFileSource` uses a double | 1461 | | -quote `"` as the quote character. See the `"United States of America"` value in the | 1462 | | -example above. An empty, quoted value `""` results in an empty `String` unless the | | 1461 | +quote (`+++"+++`) as the quote character by default, but this can be changed via the | | 1462 | +`quoteCharacter` attribute. See the `"United States of America"` value in the example | | 1463 | +above. An empty, quoted value (`+++""+++`) results in an empty `String` unless the | 1463 | 1464 | `emptyValue` attribute is set; whereas, an entirely _empty_ value is interpreted as a | 1464 | 1465 | `null` reference. By specifying one or more `nullValues`, a custom value can be | 1465 | 1466 | interpreted as a `null` reference. An `ArgumentConversionException` is thrown if the | |
Collapse file Copy file name to clipboardExpand all lines: documentation/src/test/resources/two-column.csv +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -Country, reference | | 1 | +Country, Reference | 2 | 2 | Sweden, 1 | 3 | 3 | Poland, 2 | 4 | 4 | "United States of America", 3 | |
Collapse file Copy file name to clipboardExpand all lines: junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/CsvFileSource.java +19-6Lines changed: 19 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | * via either {@link #delimiter} or {@link #delimiterString}. | 37 | 37 | * | 38 | 38 | * <p>In contrast to the default syntax used in {@code @CsvSource}, {@code @CsvFileSource} | 39 | | - * uses a double quote ({@code "}) as its quote character (see the User Guide for | 40 | | - * examples). An empty, quoted value ({@code ""}) results in an empty {@link String} | 41 | | - * unless the {@link #emptyValue} attribute is set; whereas, an entirely <em>empty</em> | 42 | | - * value is interpreted as a {@code null} reference. By specifying one or more | 43 | | - * {@link #nullValues} a custom value can be interpreted as a {@code null} reference | 44 | | - * (see the User Guide for an example). An | | 39 | + * uses a double quote ({@code "}) as its quote character by default, but this can | | 40 | + * be changed via {@link #quoteCharacter}. An empty, quoted value ({@code ""}) | | 41 | + * results in an empty {@link String} unless the {@link #emptyValue} attribute is | | 42 | + * set; whereas, an entirely <em>empty</em> value is interpreted as a {@code null} | | 43 | + * reference. By specifying one or more {@link #nullValues} a custom value can be | | 44 | + * interpreted as a {@code null} reference (see the User Guide for an example). An | 45 | 45 | * {@link org.junit.jupiter.params.converter.ArgumentConversionException | 46 | 46 | * ArgumentConversionException} is thrown if the target type of a {@code null} | 47 | 47 | * reference is a primitive type. | | 95 | 95 | */ | 96 | 96 | String lineSeparator() default "\n"; | 97 | 97 |
| | 98 | + /** | | 99 | + * The quote character to use for <em>quoted strings</em>. | | 100 | + * | | 101 | + * <p>Defaults to a double quote ({@code "}). | | 102 | + * | | 103 | + * <p>You may change the quote character to anything that makes sense for | | 104 | + * your use case. | | 105 | + * | | 106 | + * @since 5.8.2 | | 107 | + */ | | 108 | + @API(status = EXPERIMENTAL, since = "5.8.2") | | 109 | + char quoteCharacter() default '"'; | | 110 | + | 98 | 111 | /** | 99 | 112 | * The column delimiter character to use when reading the CSV files. | 100 | 113 | * | |
Collapse file Copy file name to clipboardExpand all lines: junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/CsvParserFactory.java +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,6 @@ class CsvParserFactory { | 24 | 24 |
| 25 | 25 | private static final String DEFAULT_DELIMITER = ","; | 26 | 26 | private static final String LINE_SEPARATOR = "\n"; | 27 | | - private static final char DOUBLE_QUOTE = '"'; | 28 | 27 | private static final char EMPTY_CHAR = '\0'; | 29 | 28 | private static final boolean COMMENT_PROCESSING_FOR_CSV_FILE_SOURCE = true; | 30 | 29 |
| @@ -37,7 +36,7 @@ static CsvParser createParserFor(CsvSource annotation) { | 37 | 36 |
| 38 | 37 | static CsvParser createParserFor(CsvFileSource annotation) { | 39 | 38 | String delimiter = selectDelimiter(annotation, annotation.delimiter(), annotation.delimiterString()); | 40 | | - return createParser(delimiter, annotation.lineSeparator(), DOUBLE_QUOTE, annotation.emptyValue(), | | 39 | + return createParser(delimiter, annotation.lineSeparator(), annotation.quoteCharacter(), annotation.emptyValue(), | 41 | 40 | annotation.maxCharsPerColumn(), COMMENT_PROCESSING_FOR_CSV_FILE_SOURCE, | 42 | 41 | annotation.ignoreLeadingAndTrailingWhitespace()); | 43 | 42 | } | |
Collapse file Copy file name to clipboardExpand all lines: junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/CsvFileArgumentsProviderTests.java +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,6 +65,19 @@ void providesArgumentsForCarriageReturnAndSemicolon() { | 65 | 65 | assertThat(arguments).containsExactly(array("foo", "bar"), array("baz", "qux")); | 66 | 66 | } | 67 | 67 |
| | 68 | + @Test | | 69 | + void providesArgumentsWithCustomQuoteCharacter() { | | 70 | + var annotation = csvFileSource()// | | 71 | + .resources("test.csv")// | | 72 | + .quoteCharacter('\'')// | | 73 | + .build(); | | 74 | + | | 75 | + var arguments = provideArguments(annotation, "foo, 'bar \"and\" baz', qux \n 'lemon lime', banana, apple"); | | 76 | + | | 77 | + assertThat(arguments).containsExactly(array("foo", "bar \"and\" baz", "qux"), | | 78 | + array("lemon lime", "banana", "apple")); | | 79 | + } | | 80 | + | 68 | 81 | @Test | 69 | 82 | void providesArgumentsWithStringDelimiter() { | 70 | 83 | var annotation = csvFileSource()// | |
Collapse file Copy file name to clipboardExpand all lines: junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MockCsvAnnotationBuilder.java +16-7Lines changed: 16 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,7 @@ static MockCsvFileSourceBuilder csvFileSource() { | 34 | 34 |
| 35 | 35 | // ------------------------------------------------------------------------- | 36 | 36 |
| | 37 | + private char quoteCharacter = '\0'; | 37 | 38 | protected char delimiter = '\0'; | 38 | 39 | protected String delimiterString = ""; | 39 | 40 | protected String emptyValue = ""; | @@ -46,6 +47,11 @@ private MockCsvAnnotationBuilder() { | 46 | 47 |
| 47 | 48 | protected abstract B getSelf(); | 48 | 49 |
| | 50 | + B quoteCharacter(char quoteCharacter) { | | 51 | + this.quoteCharacter = quoteCharacter; | | 52 | + return getSelf(); | | 53 | + } | | 54 | + | 49 | 55 | B delimiter(char delimiter) { | 50 | 56 | this.delimiter = delimiter; | 51 | 57 | return getSelf(); | @@ -84,7 +90,10 @@ static class MockCsvSourceBuilder extends MockCsvAnnotationBuilder<CsvSource, Mo | 84 | 90 |
| 85 | 91 | private String[] lines = new String[0]; | 86 | 92 | private String textBlock = ""; | 87 | | - private char quoteCharacter = '\''; | | 93 | + | | 94 | + private MockCsvSourceBuilder() { | | 95 | + super.quoteCharacter = '\''; | | 96 | + } | 88 | 97 |
| 89 | 98 | @Override | 90 | 99 | protected MockCsvSourceBuilder getSelf() { | @@ -101,16 +110,12 @@ MockCsvSourceBuilder textBlock(String textBlock) { | 101 | 110 | return this; | 102 | 111 | } | 103 | 112 |
| 104 | | - MockCsvSourceBuilder quoteCharacter(char quoteCharacter) { | 105 | | - this.quoteCharacter = quoteCharacter; | 106 | | - return this; | 107 | | - } | 108 | | - | 109 | 113 | @Override | 110 | 114 | CsvSource build() { | 111 | 115 | var annotation = mock(CsvSource.class); | 112 | 116 |
| 113 | 117 | // Common | | 118 | + when(annotation.quoteCharacter()).thenReturn(super.quoteCharacter); | 114 | 119 | when(annotation.delimiter()).thenReturn(super.delimiter); | 115 | 120 | when(annotation.delimiterString()).thenReturn(super.delimiterString); | 116 | 121 | when(annotation.emptyValue()).thenReturn(super.emptyValue); | @@ -121,7 +126,6 @@ CsvSource build() { | 121 | 126 | // @CsvSource | 122 | 127 | when(annotation.value()).thenReturn(this.lines); | 123 | 128 | when(annotation.textBlock()).thenReturn(this.textBlock); | 124 | | - when(annotation.quoteCharacter()).thenReturn(this.quoteCharacter); | 125 | 129 |
| 126 | 130 | return annotation; | 127 | 131 | } | @@ -136,6 +140,10 @@ static class MockCsvFileSourceBuilder extends MockCsvAnnotationBuilder<CsvFileSo | 136 | 140 | private String lineSeparator = "\n"; | 137 | 141 | private int numLinesToSkip = 0; | 138 | 142 |
| | 143 | + private MockCsvFileSourceBuilder() { | | 144 | + super.quoteCharacter = '"'; | | 145 | + } | | 146 | + | 139 | 147 | @Override | 140 | 148 | protected MockCsvFileSourceBuilder getSelf() { | 141 | 149 | return this; | @@ -171,6 +179,7 @@ CsvFileSource build() { | 171 | 179 | var annotation = mock(CsvFileSource.class); | 172 | 180 |
| 173 | 181 | // Common | | 182 | + when(annotation.quoteCharacter()).thenReturn(super.quoteCharacter); | 174 | 183 | when(annotation.delimiter()).thenReturn(super.delimiter); | 175 | 184 | when(annotation.delimiterString()).thenReturn(super.delimiterString); | 176 | 185 | when(annotation.emptyValue()).thenReturn(super.emptyValue); | |
|