-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better error handling with unit tests!
- Loading branch information
1 parent
e218fb0
commit 20f4dd3
Showing
12 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#Error Handling | ||
Below you can find a more detailed explanation of some of the errors that can be encountered while using ftcsv. For parsing, examples of these files can be found in /spec/bad_csvs/ | ||
|
||
|
||
|
||
##Parsing | ||
Note: `[row_number]` indicates the row number of the parsed lua table. As such, it will be one off from the line number in the csv. However, for header-less files, the row returned *will* match the csv line number. | ||
|
||
| Error Message | Detailed Explanation | | ||
| ------------- | ------------- | | ||
| ftcsv: Cannot parse an empty file | The file passed in contains no information. It is an empty file. | | ||
| ftcsv: Cannot parse a file which contains empty headers | If a header field contains no information, then it can't be parsed <br> (ex: `Name,City,,Zipcode`) | | ||
| ftcsv: too few columns in row [row_number] | The number of columns is less than the amount in the header after transformations (renaming, keeping certain fields, etc) | | ||
| ftcsv: too many columns in row [row_number] | The number of columns is greater than the amount in the header after transformations. It can't map the field's count with an existing header. | | ||
| ftcsv: File not found at [path] | When loading, lua can't open the file at [path] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
a,b, | ||
herp,derp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a,b,c | ||
failing,hard | ||
man,oh,well... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a,b,c | ||
man,oh,well... | ||
failing,hard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a,b,c | ||
no,one,knows | ||
what,am,i,doing? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
local ftcsv = require('ftcsv') | ||
|
||
local files = { | ||
{"empty_file", "ftcsv: Cannot parse an empty file"}, | ||
{"empty_file_newline", "ftcsv: Cannot parse a file which contains empty headers"}, | ||
{"empty_header", "ftcsv: Cannot parse a file which contains empty headers"}, | ||
{"too_few_cols", "ftcsv: too few columns in row 1"}, | ||
{"too_few_cols_end", "ftcsv: too few columns in row 2"}, | ||
{"too_many_cols", "ftcsv: too many columns in row 2"}, | ||
{"dne", "ftcsv: File not found at spec/bad_csvs/dne.csv"} | ||
} | ||
|
||
describe("csv decode error", function() | ||
for _, value in ipairs(files) do | ||
it("should error out " .. value[1], function() | ||
local test = function() | ||
ftcsv.parse("spec/bad_csvs/" .. value[1] .. ".csv", ",") | ||
end | ||
assert.has_error(test, value[2]) | ||
end) | ||
end | ||
end) | ||
|
||
it("should error out for fieldsToKeep if no headers and no renaming takes place", function() | ||
local test = function() | ||
local options = {loadFromString=true, headers=false, fieldsToKeep={1, 2}} | ||
ftcsv.parse("apple>banana>carrot\ndiamond>emerald>pearl", ">", options) | ||
end | ||
assert.has_error(test, "ftcsv: fieldsToKeep only works with header-less files when using the 'rename' functionality") | ||
end) | ||
|
||
it("should error out when you want to encode a table and specify a field that doesn't exist", function() | ||
local encodeThis = { | ||
{a = 'herp1', b = 'derp1'}, | ||
{a = 'herp2', b = 'derp2'}, | ||
{a = 'herp3', b = 'derp3'}, | ||
} | ||
|
||
local test = function() | ||
ftcsv.encode(encodeThis, ">", {fieldsToKeep={"c"}}) | ||
end | ||
|
||
assert.has_error(test, "ftcsv: the field 'c' doesn't exist in the inputTable") | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters