-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3. Check the actual byte length of strings.
UTF-8 means that a string's 'length' property might not equal the byte length. This fix uses Buffer.byteLength to check for the true number of bytes that keys (or data) will take up on disk. Additionally this adds a test suite to ensure that UTF-8 is supported.
- Loading branch information
1 parent
57ea854
commit 449f412
Showing
3 changed files
with
122 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict'; | ||
|
||
var vows = require('vows'), | ||
assert = require('assert'), | ||
fs = require('fs'), | ||
writable = require('../src/writable-cdb'), | ||
readable = require('../src/readable-cdb'), | ||
tempFile = 'test/tmp'; | ||
|
||
try { | ||
fs.unlinkSync(tempFile); | ||
} catch (err) {} | ||
|
||
vows.describe('cdb-utf8-test').addBatch({ | ||
'A writable cdb': { | ||
topic: function() { | ||
return new writable(tempFile); | ||
}, | ||
|
||
'when opened': { | ||
topic: function(cdb) { | ||
cdb.open(this.callback); | ||
}, | ||
|
||
'should write UTF8 characters': { | ||
topic: function(cdb) { | ||
cdb.put('é', 'unicode test'); | ||
cdb.put('€', 'unicode test'); | ||
cdb.put('key', 'ᚠᛇᚻ'); | ||
cdb.put('대한민국', '안성기'); | ||
|
||
cdb.close(this.callback); | ||
}, | ||
|
||
'and close successfully': function(err) { | ||
assert.equal(err, null); | ||
}, | ||
} | ||
} | ||
} | ||
}).addBatch({ | ||
'A readable cdb should find that': { | ||
topic: function() { | ||
(new readable(tempFile)).open(this.callback); | ||
}, | ||
|
||
'é': { | ||
topic: function(cdb) { | ||
cdb.get('é', this.callback); | ||
}, | ||
|
||
'exists': function(err, data) { | ||
assert.isNull(err); | ||
assert.isNotNull(data); | ||
}, | ||
|
||
'has the right value': function(err, data) { | ||
assert.equal(data, 'unicode test'); | ||
} | ||
}, | ||
|
||
'€': { | ||
topic: function(cdb) { | ||
cdb.get('€', this.callback); | ||
}, | ||
|
||
'exists': function(err, data) { | ||
assert.isNull(err); | ||
assert.isNotNull(data); | ||
}, | ||
|
||
'has the right value': function(err, data) { | ||
assert.equal(data, 'unicode test'); | ||
} | ||
}, | ||
|
||
'key': { | ||
topic: function(cdb) { | ||
cdb.get('key', this.callback); | ||
}, | ||
|
||
'exists': function(err, data) { | ||
assert.isNull(err); | ||
assert.isNotNull(data); | ||
}, | ||
|
||
'has the right value': function(err, data) { | ||
assert.equal(data, 'ᚠᛇᚻ'); | ||
} | ||
}, | ||
|
||
'대한민국': { | ||
topic: function(cdb) { | ||
cdb.get('대한민국', this.callback); | ||
}, | ||
|
||
'exists': function(err, data) { | ||
assert.isNull(err); | ||
assert.isNotNull(data); | ||
}, | ||
|
||
'has the right value': function(err, data) { | ||
assert.equal(data, '안성기'); | ||
} | ||
}, | ||
|
||
teardown: function() { | ||
fs.unlinkSync(tempFile); | ||
} | ||
} | ||
|
||
}).export(module); |