From 20c12b1294339d7f8f95239ff756a9987a810ed6 Mon Sep 17 00:00:00 2001 From: Guillaume Campagna Date: Wed, 23 May 2018 13:39:53 -0400 Subject: [PATCH] Handle """ + add test" --- helpers/csv.js | 8 +++++++- tests/samples/1/routes.txt | 3 ++- tests/test_generic_table_functions.js | 10 +++++----- tests/test_gtfs_options.js | 6 ++++-- tests/test_routes.js | 23 +++++++++++++---------- tests/tests.js | 3 ++- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/helpers/csv.js b/helpers/csv.js index 475e321..3d70ba1 100644 --- a/helpers/csv.js +++ b/helpers/csv.js @@ -73,7 +73,13 @@ function fromCsvStringToArray(string, tableName) { } if (!reValid.test(string)) { - if (string.match(/""/)) { + if (string.match(/,"""/)) { // Handle the case with 3 " at start/end of string + string = string.replace(/,"""/g, ',"\\"'); + return fromCsvStringToArray(string, tableName); + } else if (string.match(/""",/)) { + string = string.replace(/""",/g, '\\"",'); + return fromCsvStringToArray(string, tableName); + } else if (string.match(/""/)) { string = string.replace(/""/g, '\\"'); return fromCsvStringToArray(string, tableName); } diff --git a/tests/samples/1/routes.txt b/tests/samples/1/routes.txt index 68b771e..5682a2c 100644 --- a/tests/samples/1/routes.txt +++ b/tests/samples/1/routes.txt @@ -1,2 +1,3 @@ agency_id,route_id,route_short_name,route_long_name,route_type -agency_0,route_0,R0,Route 0,3 \ No newline at end of file +agency_0,route_0,R0,Route 0,3 +agency_0,route_x,RX,"""Route X""",3 \ No newline at end of file diff --git a/tests/test_generic_table_functions.js b/tests/test_generic_table_functions.js index 7e7fe9d..b1b0911 100644 --- a/tests/test_generic_table_functions.js +++ b/tests/test_generic_table_functions.js @@ -30,19 +30,19 @@ describe('Tests on GTFS generic table functions', () => { const route0 = gtfs.getRouteWithId('route_0'); gtfs.addItemInTable({ route_id: 'route_1', route_long_name: 'Route 1' }, ROUTE_TABLE_NAME); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_x']); gtfs.addItemsInTable([ { route_id: 'route_2', route_long_name: 'Route 2' }, { route_id: 'route_3', route_long_name: 'Route 3' }, ], ROUTE_TABLE_NAME); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3', 'route_x']); gtfs.removeItemInTable(gtfs.getRouteWithId('route_2'), ROUTE_TABLE_NAME); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3', 'route_x']); gtfs.removeItemsInTable([gtfs.getRouteWithId('route_0'), gtfs.getRouteWithId('route_3')], ROUTE_TABLE_NAME); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1', 'route_x']); gtfs.setIndexedItemsAsTable(new Map([['route_0', route0]]), ROUTE_TABLE_NAME); expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0']); @@ -75,7 +75,7 @@ describe('Tests on GTFS generic table functions', () => { it('Test on gtfs.getNumberOfItemsInTable(tableName)', (done) => { const gtfs = new Gtfs(`${__dirname}/samples/1`); - expect(gtfs.getNumberOfItemsInTable('routes')).to.equal(1); + expect(gtfs.getNumberOfItemsInTable('routes')).to.equal(2); gtfs.resetRoutes(); expect(gtfs.getNumberOfItemsInTable('routes')).to.equal(0); diff --git a/tests/test_gtfs_options.js b/tests/test_gtfs_options.js index 71adaa0..2d9c9c3 100644 --- a/tests/test_gtfs_options.js +++ b/tests/test_gtfs_options.js @@ -54,7 +54,8 @@ describe('Tests on GTFS constructor options', () => { expect(String(routesTxt)).to.equal( 'route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,' + 'route_text_color,route_sort_order,temp\n' + - 'route_0,agency_0,R0,Route 0,,3,,,,,some value\n' + 'route_0,agency_0,R0,Route 0,,3,,,,,some value\n' + + 'route_x,agency_0,RX,"""Route X""",,3,,,,,some value\n' ); fs.remove(outputPath, (removeError) => { @@ -90,7 +91,8 @@ describe('Tests on GTFS constructor options', () => { expect(String(routesTxt)).to.equal( 'route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,' + 'route_text_color,route_sort_order,temp\n' + - 'route_0,agency_0,R0,Route 0,,3,,,,,"{""key"":""value""}"\n' + 'route_0,agency_0,R0,Route 0,,3,,,,,"{""key"":""value""}"\n' + + 'route_x,agency_0,RX,"""Route X""",,3,,,,,"{""key"":""value""}"\n' ); fs.remove(outputPath, (removeError) => { diff --git a/tests/test_routes.js b/tests/test_routes.js index e84bdfc..3a1c5e5 100644 --- a/tests/test_routes.js +++ b/tests/test_routes.js @@ -8,25 +8,28 @@ describe('Tests on GTFS routes', () => { const path = `${__dirname}/samples/1`; const gtfs = new Gtfs(path); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_x']); const route0 = gtfs.getRouteWithId('route_0'); expect(route0.route_long_name).to.equal('Route 0'); - gtfs.addRoute({route_id: 'route_1', route_long_name: 'Route 1'}); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1']); + const routeX = gtfs.getRouteWithId('route_x'); + expect(routeX.route_long_name).to.equal('"Route X"'); + + gtfs.addRoute({ route_id: 'route_1', route_long_name: 'Route 1' }); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_x']); gtfs.addRoutes([ - {route_id: 'route_2', route_long_name: 'Route 2'}, - {route_id: 'route_3', route_long_name: 'Route 3'}, + { route_id: 'route_2', route_long_name: 'Route 2' }, + { route_id: 'route_3', route_long_name: 'Route 3' }, ]); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_2', 'route_3', 'route_x']); gtfs.removeRoute(gtfs.getRouteWithId('route_2')); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0', 'route_1', 'route_3', 'route_x']); gtfs.removeRoutes([gtfs.getRouteWithId('route_0'), gtfs.getRouteWithId('route_3')]); - expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1']); + expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_1', 'route_x']); gtfs.setIndexedRoutes(new Map([['route_0', route0]])); expect(sortedKeys(gtfs.getIndexedRoutes())).to.deep.equal(['route_0']); @@ -51,7 +54,7 @@ describe('Tests on GTFS routes', () => { it('Tests on gtfs.getNumberOfRoutes()', (done) => { const gtfs = new Gtfs(`${__dirname}/samples/1`); - expect(gtfs.getNumberOfRoutes()).to.equal(1); + expect(gtfs.getNumberOfRoutes()).to.equal(2); gtfs.resetRoutes(); @@ -74,7 +77,7 @@ describe('Tests on GTFS routes', () => { it('Tests on gtfs.resetRoutes()', (done) => { const gtfs = new Gtfs(`${__dirname}/samples/1`); - expect(gtfs.getIndexedRoutes().size).to.equal(1); + expect(gtfs.getIndexedRoutes().size).to.equal(2); gtfs.resetRoutes(); diff --git a/tests/tests.js b/tests/tests.js index 172cb01..ff1cbdc 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -41,7 +41,8 @@ describe('Tests on GTFS', () => { expect(String(routesTxt)).to.equal( 'route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,' + 'route_text_color,route_sort_order,some_extra_route_field\n' + - 'route_0,agency_0,R0,Route 0,Some new description,3,,,,,some_extra_route_value\n' + 'route_0,agency_0,R0,Route 0,Some new description,3,,,,,some_extra_route_value\n' + + 'route_x,agency_0,RX,"""Route X""",Some new description,3,,,,,some_extra_route_value\n' ); fs.readFile(`${outputPath}feed_info.txt`, (readFeedInfoError, feedInfoTxt) => {