|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.catalyst.csv |
19 | 19 |
|
| 20 | +import org.scalatest.prop.TableDrivenPropertyChecks._ |
| 21 | + |
20 | 22 | import org.apache.spark.SparkFunSuite |
21 | 23 |
|
22 | 24 | class CSVExprUtilsSuite extends SparkFunSuite { |
@@ -58,4 +60,40 @@ class CSVExprUtilsSuite extends SparkFunSuite { |
58 | 60 | } |
59 | 61 | assert(exception.getMessage.contains("Delimiter cannot be empty string")) |
60 | 62 | } |
| 63 | + |
| 64 | + val testCases = Table( |
| 65 | + ("input", "separatorStr", "expectedErrorMsg"), |
| 66 | + // normal tab |
| 67 | + ("""\t""", Some("\t"), None), |
| 68 | + // backslash, then tab |
| 69 | + ("""\\t""", Some("""\t"""), None), |
| 70 | + // invalid special character (dot) |
| 71 | + ("""\.""", None, Some("Unsupported special character for delimiter")), |
| 72 | + // backslash, then dot |
| 73 | + ("""\\.""", Some("""\."""), None), |
| 74 | + // nothing special, just straight conversion |
| 75 | + ("""foo""", Some("foo"), None), |
| 76 | + // tab in the middle of some other letters |
| 77 | + ("""ba\tr""", Some("ba\tr"), None), |
| 78 | + // null character, expressed in Unicode literal syntax |
| 79 | + ("""\u0000""", Some("\u0000"), None), |
| 80 | + // and specified directly |
| 81 | + ("\0", Some("\u0000"), None) |
| 82 | + ) |
| 83 | + |
| 84 | + test("should correctly produce separator strings, or exceptions, from input") { |
| 85 | + forAll(testCases) { (input, separatorStr, expectedErrorMsg) => |
| 86 | + try { |
| 87 | + val separator = CSVExprUtils.toDelimiterStr(input) |
| 88 | + assert(separatorStr.isDefined) |
| 89 | + assert(expectedErrorMsg.isEmpty) |
| 90 | + assert(separator.equals(separatorStr.get)) |
| 91 | + } catch { |
| 92 | + case e: IllegalArgumentException => |
| 93 | + assert(separatorStr.isEmpty) |
| 94 | + assert(expectedErrorMsg.isDefined) |
| 95 | + assert(e.getMessage.contains(expectedErrorMsg.get)) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
61 | 99 | } |
0 commit comments