Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert all tests to mocha and should

  • Loading branch information...
commit 8fee2680eb5f73fbf6d76e208d1a1073b06de5f9 1 parent a8db9eb
@wdavidw authored
View
5 Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @./node_modules/.bin/mocha --compilers coffee:coffee-script
+
+.PHONY: test
View
6 package.json
@@ -20,12 +20,14 @@
"url": "git://github.com/wdavidw/node-csv-parser.git"
},
"devDependencies": {
- "expresso": "~0.9.2"
+ "coffee-script": "latest",
+ "mocha": "latest",
+ "should": "latest"
},
"dependencies": {},
"optionalDependencies": {},
"scripts": {
- "test": "./node_modules/.bin/expresso ./test"
+ "test": "make test"
},
"homepage": "https://github.com/wdavidw/node-csv-parser"
}
View
2  readme.md
@@ -197,7 +197,7 @@ By extending the Node `EventEmitter` class, the library provide a few useful eve
Columns
-------
-Columns names may be provided or discovered in the first line with the read options `columns`. If defined as an array, the order must match the input source. If set to `true`, the fields are expected to be present in the first line of the input source.
+Columns names may be provided or discovered in the first line with the read options `columns`. If defined as an array, the order must match the one of the input source. If set to `true`, the fields are expected to be present in the first line of the input source.
You can define a different order and even different columns in the read options and in the write options. If the `columns` is not defined in the write options, it will default to the one present in the read options.
View
32 test/buffer.coffee
@@ -2,41 +2,37 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Buffer smaller than in': ->
+describe 'buffer', ->
+ it 'Buffer smaller than in', ->
csv()
.fromPath("#{__dirname}/buffer/smaller.in",
bufferSize: 1024
)
.toPath("#{__dirname}/buffer/smaller.tmp")
- .transform( (data) ->
- assert.ok data instanceof Object
+ .transform (data) ->
+ data.should.be.a 'object'
data
- )
.on('end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/buffer/smaller.out").toString(),
- fs.readFileSync("#{__dirname}/buffer/smaller.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/buffer/smaller.out").toString()
+ result = fs.readFileSync("#{__dirname}/buffer/smaller.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/buffer/smaller.tmp"
)
- 'Buffer same as in': ->
+ it 'Buffer same as in', ->
csv()
.fromPath("#{__dirname}/buffer/same.in",
bufferSize: 1024
)
.toPath("#{__dirname}/buffer/same.tmp")
- .transform( (data) ->
- assert.ok data instanceof Object
+ .transform (data) ->
+ data.should.be.a 'object'
data
- )
.on('end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/buffer/same.out").toString(),
- fs.readFileSync("#{__dirname}/buffer/same.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/buffer/same.out").toString()
+ result = fs.readFileSync("#{__dirname}/buffer/same.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/buffer/same.tmp"
)
View
91 test/columns.coffee
@@ -2,82 +2,71 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test columns in true': ->
+describe 'columns', ->
+ it 'Test columns in true', ->
# Note: if true, columns are expected to be in first line
csv()
- .fromPath("#{__dirname}/columns/in_true.in",
- columns: true
- )
- .toPath("#{__dirname}/columns/in_true.tmp")
+ .fromPath( "#{__dirname}/columns/in_true.in", columns: true )
+ .toPath( "#{__dirname}/columns/in_true.tmp" )
.transform( (data, index) ->
- assert.equal true, data instanceof Object
- assert.equal false, data instanceof Array
+ data.should.be.a 'object'
+ data.should.not.be.an.instanceof Array
if index is 0
- assert.strictEqual '20322051544', data.FIELD_1
+ data.FIELD_1.should.eql '20322051544'
else if index is 1
- assert.strictEqual 'DEF', data.FIELD_4
+ data.FIELD_4.should.eql 'DEF'
data
)
.on('end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync("#{__dirname}/columns/in_true.out").toString(),
- fs.readFileSync("#{__dirname}/columns/in_true.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/columns/in_true.out").toString()
+ result = fs.readFileSync("#{__dirname}/columns/in_true.tmp").toString()
+ result.should.eql expect
fs.unlink("#{__dirname}/columns/in_true.tmp")
)
- 'Test columns in named': ->
+ it 'Test columns in named', ->
# Note: if true, columns are expected to be in first line
csv()
- .fromPath("#{__dirname}/columns/in_named.in",{
+ .fromPath("#{__dirname}/columns/in_named.in", {
columns: ["FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5", "FIELD_6"]
})
.toPath("#{__dirname}/columns/in_named.tmp")
- .transform((data, index) ->
- assert.equal(true, data instanceof Object)
- assert.equal(false, data instanceof Array)
+ .transform (data, index) ->
+ data.should.be.a 'object'
+ data.should.not.be.an.instanceof Array
if index is 0
- assert.strictEqual '20322051544', data.FIELD_1
+ data.FIELD_1.should.eql '20322051544'
else if index is 1
- assert.strictEqual 'DEF', data.FIELD_4
+ data.FIELD_4.should.eql 'DEF'
data
- )
- .on('data',(data, index) ->
- assert.equal(true, data instanceof Object)
- assert.equal(false, data instanceof Array)
- )
- .on('end',(count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync("#{__dirname}/columns/in_named.out").toString(),
- fs.readFileSync("#{__dirname}/columns/in_named.tmp").toString()
- )
- fs.unlink("#{__dirname}/columns/in_named.tmp")
- )
- 'Test columns out named': ->
+ .on 'data',(data, index) ->
+ data.should.be.a 'object'
+ data.should.not.be.an.instanceof Array
+ .on 'end',(count) ->
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/columns/in_named.out").toString()
+ result = fs.readFileSync("#{__dirname}/columns/in_named.tmp").toString()
+ result.should.eql expect
+ fs.unlink "#{__dirname}/columns/in_named.tmp"
+ it 'Test columns out named', ->
# Note: if true, columns are expected to be in first line
csv()
.fromPath("#{__dirname}/columns/out_named.in")
.toPath("#{__dirname}/columns/out_named.tmp",
columns: ["FIELD_1", "FIELD_2"]
)
- .transform( (data, index) ->
- assert.equal(true, data instanceof Array)
+ .transform (data, index) ->
+ data.should.be.an.instanceof Array
return {FIELD_2: data[3], FIELD_1: data[4]}
- )
- .on('data', (data, index) ->
- assert.equal true, data instanceof Object
- assert.equal false, data instanceof Array
- )
- .on('end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync("#{__dirname}/columns/out_named.out").toString(),
- fs.readFileSync("#{__dirname}/columns/out_named.tmp").toString()
- )
+ .on 'data', (data, index) ->
+ data.should.be.a 'object'
+ data.should.not.be.an.instanceof Array
+ .on 'end', (count) ->
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/columns/out_named.out").toString()
+ result = fs.readFileSync("#{__dirname}/columns/out_named.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/columns/out_named.tmp"
- )
View
50 test/delimiter.coffee
@@ -2,50 +2,48 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test empty value': ->
+describe 'delimiter', ->
+ it 'Test empty value', ->
csv()
.fromPath( "#{__dirname}/delimiter/empty_value.in" )
.toPath( "#{__dirname}/delimiter/empty_value.tmp" )
.transform (data, index) ->
- assert.strictEqual 5, data.length
+ data.length.should.eql 5
if index is 0
- assert.strictEqual '', data[1]
- assert.strictEqual '', data[4]
+ data[1].should.eql ''
+ data[4].should.eql ''
else if index is 1
- assert.strictEqual '', data[0]
- assert.strictEqual '', data[3]
- assert.strictEqual '', data[4]
+ data[0].should.eql ''
+ data[3].should.eql ''
+ data[4].should.eql ''
data
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/delimiter/empty_value.out" ).toString(),
- fs.readFileSync( "#{__dirname}/delimiter/empty_value.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/delimiter/empty_value.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/delimiter/empty_value.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/delimiter/empty_value.tmp"
- 'Test tabs to comma': ->
+ it 'Test tabs to comma', ->
csv()
.fromPath( "#{__dirname}/delimiter/tab_to_coma.in", delimiter: '\t' )
.toPath( "#{__dirname}/delimiter/tab_to_coma.tmp", delimiter: ',' )
.transform (data,index) ->
- assert.strictEqual 5, data.length
+ data.length.should.eql 5
if index is 0
- assert.strictEqual '', data[1]
- assert.strictEqual '', data[4]
+ data[1].should.eql ''
+ data[4].should.eql ''
else if index is 1
- assert.strictEqual '', data[0]
- assert.strictEqual '', data[3]
- assert.strictEqual '', data[4]
+ data[0].should.eql ''
+ data[3].should.eql ''
+ data[4].should.eql ''
data
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/delimiter/tab_to_coma.out" ).toString(),
- fs.readFileSync( "#{__dirname}/delimiter/tab_to_coma.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/delimiter/tab_to_coma.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/delimiter/tab_to_coma.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/delimiter/tab_to_coma.tmp"
View
32 test/escape.coffee
@@ -2,37 +2,35 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
+describe 'escape', ->
# Note: we only escape quote and escape character
- 'Test default': ->
+ it 'Test default', ->
csv()
.fromPath( "#{__dirname}/escape/default.in" , escape: '"' )
.toPath( "#{__dirname}/escape/default.tmp" )
.on 'data', (data, index) ->
if index is 0
- assert.equal '19"79.0', data[1]
- assert.equal 'A"B"C', data[3]
+ data[1].should.eql '19"79.0'
+ data[3].should.eql 'A"B"C'
.on 'end', ->
- assert.equal(
- fs.readFileSync( "#{__dirname}/escape/default.out" ).toString(),
- fs.readFileSync( "#{__dirname}/escape/default.tmp" ).toString()
- )
+ result = fs.readFileSync( "#{__dirname}/escape/default.out" ).toString()
+ expect = fs.readFileSync( "#{__dirname}/escape/default.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/escape/default.tmp"
- 'Test backslash': ->
+ it 'Test backslash', ->
csv()
.fromPath( "#{__dirname}/escape/backslash.in" , escape: '\\' )
.toPath( "#{__dirname}/escape/backslash.tmp" )
.on 'data', (data, index) ->
if index is 0
- assert.equal '19"79.0', data[1]
- assert.equal 'A"B"C', data[3]
+ data[1].should.eql '19"79.0'
+ data[3].should.eql 'A"B"C'
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/escape/backslash.out" ).toString(),
- fs.readFileSync( "#{__dirname}/escape/backslash.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/escape/backslash.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/escape/backslash.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/escape/backslash.tmp"
View
80 test/fromto.coffee
@@ -2,50 +2,48 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test fs stream': ->
+describe 'fromto', ->
+ it 'Test fs stream', ->
csv()
.fromStream(fs.createReadStream "#{__dirname}/fromto/sample.in", flags: 'r' )
.toStream(fs.createWriteStream "#{__dirname}/fromto/sample.tmp", flags: 'w' )
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/fromto/sample.out" ).toString(),
- fs.readFileSync( "#{__dirname}/fromto/sample.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/fromto/sample.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/fromto/sample.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/fromto/sample.tmp"
- 'Test string without destination': ->
+ it 'Test string without destination', ->
csv()
.from(fs.readFileSync( "#{__dirname}/fromto/sample.in" ).toString())
.on 'data', (data, index) ->
- assert.ok index < 2
+ index.should.be.below 2
if index is 0
- assert.strictEqual '20322051544', data[0]
+ data[0].should.eql '20322051544'
else if index is 1
- assert.strictEqual '28392898392', data[0]
+ data[0].should.eql '28392898392'
.on 'end', (count) ->
- assert.strictEqual 2, count
- 'Test string to stream': ->
+ count.should.eql 2
+ it 'Test string to stream', ->
csv()
.from(fs.readFileSync( "#{__dirname}/fromto/string_to_stream.in" ).toString())
.toPath( "#{__dirname}/fromto/string_to_stream.tmp" )
.on 'data', (data, index) ->
- assert.ok index < 2
+ index.should.be.below 2
if index is 0
- assert.strictEqual '20322051544', data[0]
+ data[0].should.eql '20322051544'
else if index is 1
- assert.strictEqual '28392898392', data[0]
+ data[0].should.eql '28392898392'
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/fromto/string_to_stream.out" ).toString(),
- fs.readFileSync( "#{__dirname}/fromto/string_to_stream.tmp" ).toString()
- );
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/fromto/string_to_stream.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/fromto/string_to_stream.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/fromto/string_to_stream.tmp"
- 'Test array to stream': ->
+ it 'Test array to stream', ->
# note: destination line breaks is windows styled because we can't guess it
data = [
["20322051544","1979.0","8.8017226E7","ABC","45","2000-01-01"]
@@ -55,19 +53,18 @@ module.exports =
.from(data)
.toPath( "#{__dirname}/fromto/array_to_stream.tmp" )
.on 'data', (data, index) ->
- assert.ok index < 2
+ index.should.be.below 2
if index is 0
- assert.strictEqual '20322051544', data[0]
+ data[0].should.eql '20322051544'
else if index is 1
- assert.strictEqual '28392898392', data[0]
+ data[0].should.eql '28392898392'
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/fromto/array_to_stream.out" ).toString(),
- fs.readFileSync( "#{__dirname}/fromto/array_to_stream.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/fromto/array_to_stream.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/fromto/array_to_stream.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/fromto/array_to_stream.tmp"
- 'Test null': ->
+ it 'Test null', ->
# note: destination line breaks is windows styled because we can't guess it
data = [
["20322051544",null,"8.8017226E7","ABC","45","2000-01-01"]
@@ -82,19 +79,18 @@ module.exports =
)
.toPath( "#{__dirname}/fromto/null.tmp" )
.on 'data', (data, index) ->
- assert.ok index < 2
- assert.strictEqual null, data[0]
- assert.strictEqual null, data[3]
+ index.should.be.below 2
+ should.not.exist data[0]
+ should.not.exist data[3]
if index is 0
- assert.strictEqual null, data[1]
+ should.not.exist data[1]
else if index is 1
- assert.strictEqual null, data[5]
+ should.not.exist data[5]
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/fromto/null.out" ).toString(),
- fs.readFileSync( "#{__dirname}/fromto/null.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/fromto/null.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/fromto/null.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/fromto/null.tmp"
View
59 test/lineBreaks.coffee
@@ -2,64 +2,59 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test line breaks custom': ->
+describe 'lineBreaks', ->
+ it 'Test line breaks custom', ->
csv()
.fromPath( "#{__dirname}/lineBreaks/lineBreaks.in" )
.toPath( "#{__dirname}/lineBreaks/custom.tmp", lineBreaks: '::' )
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/lineBreaks/custom.out").toString(),
- fs.readFileSync( "#{__dirname}/lineBreaks/custom.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/lineBreaks/custom.out").toString()
+ result = fs.readFileSync( "#{__dirname}/lineBreaks/custom.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/lineBreaks/custom.tmp"
- 'Test line breaks unix': ->
+ it 'Test line breaks unix', ->
csv()
.fromPath( "#{__dirname}/lineBreaks/lineBreaks.in" )
.toPath( "#{__dirname}/lineBreaks/unix.tmp", lineBreaks: "unix")
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/lineBreaks/unix.out" ).toString(),
- fs.readFileSync( "#{__dirname}/lineBreaks/unix.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/lineBreaks/unix.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/lineBreaks/unix.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/lineBreaks/unix.tmp"
- 'Test line breaks unicode': ->
+ it 'Test line breaks unicode', ->
csv()
.fromPath( "#{__dirname}/lineBreaks/lineBreaks.in")
.toPath( "#{__dirname}/lineBreaks/unicode.tmp", lineBreaks: 'unicode')
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/lineBreaks/unicode.out" ).toString(),
- fs.readFileSync( "#{__dirname}/lineBreaks/unicode.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/lineBreaks/unicode.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/lineBreaks/unicode.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/lineBreaks/unicode.tmp"
- 'Test line breaks mac': ->
+ it 'Test line breaks mac', ->
csv()
.fromPath( "#{__dirname}/lineBreaks/lineBreaks.in" )
.toPath( "#{__dirname}/lineBreaks/mac.tmp", lineBreaks: 'mac' )
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/lineBreaks/mac.out" ).toString(),
- fs.readFileSync( "#{__dirname}/lineBreaks/mac.tmp" ).toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/lineBreaks/mac.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/lineBreaks/mac.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/lineBreaks/mac.tmp"
- 'Test line breaks windows': ->
+ it 'Test line breaks windows', ->
csv()
.fromPath( "#{__dirname}/lineBreaks/lineBreaks.in" )
.toPath( "#{__dirname}/lineBreaks/windows.tmp", lineBreaks: 'windows' )
.on 'end', (count) ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/lineBreaks/windows.out" ).toString(),
- fs.readFileSync( "#{__dirname}/lineBreaks/windows.tmp" ).toString()
- );
+ count.should.eql 2
+ expect = fs.readFileSync( "#{__dirname}/lineBreaks/windows.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/lineBreaks/windows.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/lineBreaks/windows.tmp"
View
104 test/quotes.coffee
@@ -2,124 +2,110 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test regular quotes': ->
+describe 'quotes', ->
+ it 'Test regular quotes', ->
csv()
.fromPath( "#{__dirname}/quotes/regular.in" )
.toPath( "#{__dirname}/quotes/regular.tmp" )
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/quotes/regular.out").toString(),
- fs.readFileSync("#{__dirname}/quotes/regular.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/quotes/regular.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/regular.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/quotes/regular.tmp"
- 'Test quotes with delimiter': ->
+ it 'Test quotes with delimiter', ->
csv()
.fromPath("#{__dirname}/quotes/delimiter.in")
.toPath("#{__dirname}/quotes/delimiter.tmp")
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/quotes/delimiter.out").toString(),
- fs.readFileSync("#{__dirname}/quotes/delimiter.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/quotes/delimiter.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/delimiter.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/quotes/delimiter.tmp"
- 'Test quotes inside field': ->
+ it 'Test quotes inside field', ->
csv()
.fromPath( "#{__dirname}/quotes/in_field.in" )
.toPath( "#{__dirname}/quotes/in_field.tmp" )
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/quotes/in_field.out").toString(),
- fs.readFileSync("#{__dirname}/quotes/in_field.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/quotes/in_field.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/in_field.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/quotes/in_field.tmp"
- 'Test empty value': ->
+ it 'Test empty value', ->
csv()
.fromPath "#{__dirname}/quotes/empty_value.in",
quote: '"'
escape: '"'
.toPath("#{__dirname}/quotes/empty_value.tmp")
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/quotes/empty_value.out").toString(),
- fs.readFileSync("#{__dirname}/quotes/empty_value.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/quotes/empty_value.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/empty_value.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/quotes/empty_value.tmp"
- 'Test quoted quote': ->
+ it 'Test quoted quote', ->
csv()
.fromPath "#{__dirname}/quotes/quoted.in",
quote: '"',
escape: '"',
.toPath("#{__dirname}/quotes/quoted.tmp")
.on 'data', (data,index) ->
- assert.strictEqual(5,data.length)
+ data.length.should.eql 5
if index is 0
- assert.strictEqual('"',data[1])
- assert.strictEqual('"ok"',data[4])
+ data[1].should.eql '"'
+ data[4].should.eql '"ok"'
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/quotes/quoted.out").toString(),
- fs.readFileSync("#{__dirname}/quotes/quoted.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/quotes/quoted.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/quoted.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/quotes/quoted.tmp"
- 'Test quoted linebreak': ->
+ it 'Test quoted linebreak', ->
csv()
.fromPath "#{__dirname}/quotes/linebreak.in",
quote: '"',
escape: '"'
.toPath("#{__dirname}/quotes/linebreak.tmp")
.on 'data', (data,index) ->
- assert.strictEqual(5,data.length)
+ data.length.should.eql 5
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/quotes/linebreak.out").toString(),
- fs.readFileSync("#{__dirname}/quotes/linebreak.tmp").toString()
- )
+ expect = fs.readFileSync("#{__dirname}/quotes/linebreak.out").toString()
+ result = fs.readFileSync("#{__dirname}/quotes/linebreak.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/quotes/linebreak.tmp"
- 'Test unclosed quote': (next) ->
- n = 0
+ it 'Test unclosed quote', (next) ->
csv()
.fromPath "#{__dirname}/quotes/unclosed.in",
quote: '"'
escape: '"'
.toPath( "#{__dirname}/quotes/unclosed.tmp" )
.on 'end', ->
- assert.ok false, 'end was raised'
- .on 'error', ->
- ++n
- next -> # finish
- assert.equal 1, n, 'error was not raised'
+ false.should.be.ok
+ .on 'error', (e) ->
+ e.message.should.eql 'Quoted field not terminated'
fs.unlink "#{__dirname}/quotes/unclosed.tmp"
- 'Test invalid quotes': (next) ->
- n = 0
+ next()
+ it 'Test invalid quotes', (next) ->
csv()
- .on 'error', (e) ->
- assert.equal e.message.split(';')[0], 'Invalid closing quote'
- ++n
.fromPath "#{__dirname}/quotes/invalid.in",
quote: '"',
escape: '"'
.toPath( "#{__dirname}/quotes/invalid.tmp" )
.on 'end', ->
- assert.ok false, 'end was raised'
- next -> # finish
- assert.equal 1, n, 'error was not raised'
+ false.should.be.ok
+ .on 'error', (e) ->
+ e.message.should.match /Invalid closing quote/
fs.unlink "#{__dirname}/quotes/invalid.tmp"
- 'Test invalid quotes from string': (next) ->
- n = 0
+ next()
+ it 'Test invalid quotes from string', (next) ->
csv()
- .on 'error', (e) ->
- assert.equal e.message.split(';')[0], 'Invalid closing quote'
- ++n
.from '"",1974,8.8392926E7,""t,""',
quote: '"'
escape: '"'
.toPath( "#{__dirname}/quotes/invalidstring.tmp" )
.on 'end', ->
- assert.ok false, 'end was raised'
- next -> # finish
- assert.equal 1, n
+ false.should.be.ok
+ .on 'error', (e) ->
+ e.message.should.match /Invalid closing quote/
fs.unlink "#{__dirname}/quotes/invalidstring.tmp"
+ next()
View
113 test/transform.coffee
@@ -2,106 +2,97 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test reorder fields': ->
+describe 'transform', ->
+ it 'Test reorder fields', ->
count = 0
csv()
.fromPath("#{__dirname}/transform/reorder.in")
.toPath("#{__dirname}/transform/reorder.tmp")
- .transform( (data, index) ->
- assert.strictEqual count, index
+ .transform (data, index) ->
+ count.should.eql index
count++
data.unshift data.pop()
return data
- )
.on 'end', ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync("#{__dirname}/transform/reorder.out").toString(),
- fs.readFileSync("#{__dirname}/transform/reorder.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/transform/reorder.out").toString()
+ result = fs.readFileSync("#{__dirname}/transform/reorder.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/transform/reorder.tmp"
- 'Test return undefined - skip all lines': ->
+ it 'Test return undefined - skip all lines', ->
count = 0
csv()
- .fromPath(__dirname+'/transform/undefined.in')
+ .fromPath("#{__dirname}/transform/undefined.in")
.toPath("#{__dirname}/transform/undefined.tmp")
- .transform( (data, index) ->
- assert.strictEqual count, index
+ .transform (data, index) ->
+ count.should.eql index
count++
return
- )
.on 'end', ->
- assert.strictEqual 2, count
- assert.equal(
- fs.readFileSync("#{__dirname}/transform/undefined.out").toString(),
- fs.readFileSync("#{__dirname}/transform/undefined.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/transform/undefined.out").toString()
+ result = fs.readFileSync("#{__dirname}/transform/undefined.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/transform/undefined.tmp"
- 'Test return null - skip one of two lines': ->
+ it 'Test return null - skip one of two lines', ->
count = 0
csv()
- .fromPath(__dirname+'/transform/null.in')
+ .fromPath("#{__dirname}/transform/null.in")
.toPath("#{__dirname}/transform/null.tmp")
- .transform( (data, index) ->
- assert.strictEqual(count,index)
+ .transform (data, index) ->
+ count.should.eql index
count++
- return if index % 2 then data else null
- )
+ if index % 2 then data else null
.on 'end', ->
- assert.strictEqual(6,count)
- assert.equal(
- fs.readFileSync("#{__dirname}/transform/null.out").toString(),
- fs.readFileSync("#{__dirname}/transform/null.tmp").toString()
- )
+ count.should.eql 6
+ expect = fs.readFileSync("#{__dirname}/transform/null.out").toString()
+ result = fs.readFileSync("#{__dirname}/transform/null.tmp").toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/transform/null.tmp"
- 'Test return object': ->
+ it 'Test return object', ->
# we don't define columns
# recieve and array and return an object
# also see the columns test
csv()
- .fromPath(__dirname+'/transform/object.in')
+ .fromPath("#{__dirname}/transform/object.in")
.toPath("#{__dirname}/transform/object.tmp")
- .transform( (data, index) ->
- return { field_1: data[4], field_2: data[3] }
- )
+ .transform (data, index) ->
+ { field_1: data[4], field_2: data[3] }
.on 'end', (count) ->
- assert.strictEqual(2,count)
- assert.equal(
- fs.readFileSync("#{__dirname}/transform/object.out").toString(),
- fs.readFileSync("#{__dirname}/transform/object.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/transform/object.out").toString()
+ result = fs.readFileSync("#{__dirname}/transform/object.tmp").toString()
+ result.should.eql expect
fs.unlink("#{__dirname}/transform/object.tmp")
- 'Test return string': ->
+ it 'Test return string', ->
csv()
- .fromPath(__dirname+'/transform/string.in')
+ .fromPath("#{__dirname}/transform/string.in")
.toPath("#{__dirname}/transform/string.tmp")
- .transform( (data, index) ->
- return ( if index > 0 then ',' else '') + data[4] + ":" + data[3]
- )
+ .transform (data, index) ->
+ ( if index > 0 then ',' else '') + data[4] + ":" + data[3]
.on 'end', (count) ->
- assert.strictEqual(2,count)
- assert.equal(
- fs.readFileSync("#{__dirname}/transform/string.out").toString(),
- fs.readFileSync("#{__dirname}/transform/string.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/transform/string.out").toString()
+ result = fs.readFileSync("#{__dirname}/transform/string.tmp").toString()
+ result.should.eql expect
fs.unlink("#{__dirname}/transform/string.tmp")
- 'Test types': ->
+ it 'Test types', ->
# Test date, int and float
csv()
.fromPath(__dirname+'/transform/types.in')
.toPath("#{__dirname}/transform/types.tmp")
- .transform( (data, index) ->
+ .transform (data, index) ->
data[3] = data[3].split('-')
- return [parseInt(data[0]), parseFloat(data[1]), parseFloat(data[2]) ,Date.UTC(data[3][0], data[3][1], data[3][2]), !!data[4], !!data[5]]
- )
+ [parseInt(data[0]), parseFloat(data[1]), parseFloat(data[2]) ,Date.UTC(data[3][0], data[3][1], data[3][2]), !!data[4], !!data[5]]
.on 'end', (count) ->
- assert.strictEqual(2,count)
- assert.equal(
- fs.readFileSync("#{__dirname}/transform/types.out").toString(),
- fs.readFileSync("#{__dirname}/transform/types.tmp").toString()
- )
+ count.should.eql 2
+ expect = fs.readFileSync("#{__dirname}/transform/types.out").toString()
+ result = fs.readFileSync("#{__dirname}/transform/types.tmp").toString()
+ result.should.eql expect
fs.unlink("#{__dirname}/transform/types.tmp")
+
+
+
View
53 test/trim.coffee
@@ -1,57 +1,56 @@
-# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
+###
+Node CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
+Testing the read options `trim`, `ltrim` and `rtrim`.
+###
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test ignoring whitespace immediately following the delimiter': ->
+describe 'trim', ->
+ it 'should ignore the whitespaces immediately following the delimiter', ->
csv()
.fromPath( "#{__dirname}/trim/ltrim.in", ltrim: true )
.toPath( "#{__dirname}/trim/ltrim.tmp" )
.transform( (data, index) -> data )
.on 'end', (count) ->
- assert.strictEqual(3,count);
- assert.equal(
- fs.readFileSync( "#{__dirname}/trim/ltrim.out" ).toString(),
- fs.readFileSync( "#{__dirname}/trim/ltrim.tmp" ).toString()
- );
+ count.should.eql 3
+ expect = fs.readFileSync( "#{__dirname}/trim/ltrim.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/trim/ltrim.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/trim/ltrim.tmp"
- 'Test rtrim - ignoring whitespace immediately preceding the delimiter': ->
+ it 'should ignore the whitespaces immediately preceding the delimiter', ->
csv()
.fromPath( "#{__dirname}/trim/rtrim.in", rtrim: true )
.toPath( "#{__dirname}/trim/rtrim.tmp" )
.transform( (data, index) -> data )
.on 'end', (count) ->
- assert.strictEqual(3,count);
- assert.equal(
- fs.readFileSync( "#{__dirname}/trim/rtrim.out" ).toString(),
- fs.readFileSync( "#{__dirname}/trim/rtrim.tmp" ).toString()
- );
+ count.should.eql 3
+ expect = fs.readFileSync( "#{__dirname}/trim/rtrim.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/trim/rtrim.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/trim/rtrim.tmp"
- 'Test trim - ignoring whitespace both immediately preceding and following the delimiter': ->
+ it 'should ignore the whitespaces immediately preceding and following the delimiter', ->
csv()
.fromPath( "#{__dirname}/trim/trim.in", trim: true )
.toPath( "#{__dirname}/trim/trim.tmp" )
.transform( (data, index) -> data )
.on 'end', (count) ->
- assert.strictEqual 3, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/trim/trim.out" ).toString(),
- fs.readFileSync( "#{__dirname}/trim/trim.tmp" ).toString()
- )
+ count.should.eql 3
+ expect = fs.readFileSync( "#{__dirname}/trim/trim.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/trim/trim.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/trim/trim.tmp"
- 'Test no trim': ->
+ it 'should preserve surrounding whitespaces', ->
csv()
.fromPath( "#{__dirname}/trim/notrim.in" )
.toPath( "#{__dirname}/trim/notrim.tmp" )
.transform( (data, index) -> data )
.on 'end', (count) ->
- assert.strictEqual 3, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/trim/notrim.out" ).toString(),
- fs.readFileSync( "#{__dirname}/trim/notrim.tmp" ).toString()
- )
+ count.should.eql 3
+ expect = fs.readFileSync( "#{__dirname}/trim/notrim.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/trim/notrim.tmp" ).toString()
+ result.should.eql expect
fs.unlink "#{__dirname}/trim/notrim.tmp"
View
73 test/write.coffee
@@ -2,63 +2,58 @@
# Test CSV - Copyright David Worms <open@adaltas.com> (BSD Licensed)
fs = require 'fs'
-assert = require 'assert'
+should = require 'should'
csv = require '..'
-module.exports =
- 'Test write array': ->
+describe 'write', ->
+ it 'Test write array', ->
count = 0;
test = csv()
.toPath( "#{__dirname}/write/write_array.tmp" )
.on 'data', (data, index) ->
- assert.ok Array.isArray data
- assert.eql count, index
+ data.should.be.an.instanceof Array
+ count.should.eql index
count++
.on 'end', ->
- assert.eql(1000,count);
- assert.equal(
- fs.readFileSync( "#{__dirname}/write/write.out" ).toString(),
- fs.readFileSync( "#{__dirname}/write/write_array.tmp" ).toString()
- )
+ count.should.eql 1000
+ expect = fs.readFileSync( "#{__dirname}/write/write.out" ).toString()
+ result = fs.readFileSync( "#{__dirname}/write/write_array.tmp" ).toString()
+ result.should.eql expect
fs.unlinkSync "#{__dirname}/write/write_array.tmp"
for i in [0...1000]
test.write ["Test #{i}", i, '"']
test.end()
- 'Test write object with column options': ->
+ it 'Test write object with column options', ->
count = 0
test = csv()
- .toPath( "#{__dirname}/write/write_object.tmp",
- columns: ['name','value','escape']
- )
+ .toPath( "#{__dirname}/write/write_object.tmp", columns: ['name','value','escape'] )
.on 'data', (data, index) ->
- assert.ok typeof data is 'object'
- assert.ok not Array.isArray data
- assert.eql count, index
+ data.should.be.a 'object'
+ data.should.not.be.an.instanceof Array
+ count.should.eql index
count++
.on 'end', ->
- assert.eql 1000, count
- assert.equal(
- fs.readFileSync( "#{__dirname}/write/write.out").toString(),
- fs.readFileSync( "#{__dirname}/write/write_object.tmp").toString()
- )
+ count.should.eql 1000
+ expect = fs.readFileSync( "#{__dirname}/write/write.out").toString()
+ result = fs.readFileSync( "#{__dirname}/write/write_object.tmp").toString()
+ result.should.eql expect
fs.unlinkSync "#{__dirname}/write/write_object.tmp"
for i in [0...1000]
- test.write {name: "Test #{i}", value:i, escape: '"', ovni: 'ET '+i}
+ test.write {name: "Test #{i}", value:i, escape: '"', ovni: "ET #{i}"}
test.end()
- 'Test write string': ->
+ it 'Test write string', ->
count = 0
test = csv()
.toPath( "#{__dirname}/write/write_string.tmp" )
.on 'data', (data, index) ->
- assert.ok Array.isArray data
- assert.eql count, index
+ data.should.be.an.instanceof Array
+ count.should.eql index
count++
.on 'end', ->
- assert.eql 1000, count
- assert.equal(
- fs.readFileSync("#{__dirname}/write/write.out").toString(),
- fs.readFileSync("#{__dirname}/write/write_string.tmp").toString()
- );
+ count.should.eql 1000
+ expect = fs.readFileSync("#{__dirname}/write/write.out").toString()
+ result = fs.readFileSync("#{__dirname}/write/write_string.tmp").toString()
+ result.should.eql expect
fs.unlinkSync "#{__dirname}/write/write_string.tmp"
buffer = ''
for i in [0...1000]
@@ -68,25 +63,23 @@ module.exports =
buffer = buffer.substr 250
test.write buffer
test.end()
- 'Test write string with preserve': ->
+ it 'Test write string with preserve', ->
count = 0
test = csv()
.toPath( "#{__dirname}/write/string_preserve.tmp" )
- .transform( (data, index) ->
+ .transform (data, index) ->
if index is 0
test.write '--------------------\n', true
test.write data
test.write '\n--------------------', true
- assert.ok Array.isArray data
- assert.eql count, index
+ data.should.be.an.instanceof Array
+ count.should.eql index
count++
null
- )
.on 'end', ->
- assert.equal(
- fs.readFileSync("#{__dirname}/write/string_preserve.out").toString(),
- fs.readFileSync("#{__dirname}/write/string_preserve.tmp").toString()
- );
+ expect = fs.readFileSync("#{__dirname}/write/string_preserve.out").toString()
+ result = fs.readFileSync("#{__dirname}/write/string_preserve.tmp").toString()
+ result.should.eql expect
fs.unlinkSync "#{__dirname}/write/string_preserve.tmp"
test.write '# This line should not be parsed', true
test.write '\n', true
Please sign in to comment.
Something went wrong with that request. Please try again.