[Pkg-javascript-commits] [node-hash-base] 02/03: Add type checking in .update
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 4 10:21:25 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to annotated tag v2.0.2
in repository node-hash-base.
commit 96acf4a5650fceb9dbaaba9873805a5c9d71eb88
Author: Kirill Fomichev <fanatid at ya.ru>
Date: Sun Apr 17 13:32:13 2016 +0300
Add type checking in .update
---
index.js | 1 +
test/index.js | 144 +++++++++++++++++++++++++++++-----------------------------
2 files changed, 73 insertions(+), 72 deletions(-)
diff --git a/index.js b/index.js
index 6703851..9cd6a70 100644
--- a/index.js
+++ b/index.js
@@ -39,6 +39,7 @@ HashBase.prototype._flush = function (callback) {
}
HashBase.prototype.update = function (data, encoding) {
+ if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
if (this._finalized) throw new Error('Digest already called')
if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding || 'binary')
diff --git a/test/index.js b/test/index.js
index 3b9a616..5cd9c93 100644
--- a/test/index.js
+++ b/test/index.js
@@ -12,6 +12,64 @@ function beforeEach (t) {
}
}
+test('_transform', function (t) {
+ beforeEach(t)
+
+ t.test('should use update', function (t) {
+ t.plan(2)
+ var buffer = new Buffer(42)
+ t.base.update = function (data) { t.true(data === buffer) }
+ t.base._transform(buffer, 'buffer', function (err) {
+ t.same(err, null)
+ })
+ t.end()
+ })
+
+ t.test('should decode string with custom encoding', function (t) {
+ t.plan(2)
+ t.base.update = function (data) { t.same(data, new Buffer('УТФ-8 text', 'utf8')) }
+ t.base._transform('УТФ-8 text', 'utf8', function (err) {
+ t.same(err, null)
+ })
+ t.end()
+ })
+
+ t.test('should handle error in update', function (t) {
+ t.plan(1)
+ var err = new Error('hey')
+ t.base.update = function () { throw err }
+ t.base._transform(new Buffer(42), 'buffer', function (_err) {
+ t.true(_err === err)
+ })
+ t.end()
+ })
+
+ t.end()
+})
+
+test('_flush', function (t) {
+ beforeEach(t)
+
+ t.test('should use _digest', function (t) {
+ t.plan(2)
+ var buffer = new Buffer(42)
+ t.base._digest = function () { return buffer }
+ t.base.push = function (data) { t.true(data === buffer) }
+ t.base._flush(function (err) { t.same(err, null) })
+ t.end()
+ })
+
+ t.test('should handle errors in _digest', function (t) {
+ t.plan(1)
+ var err = new Error('hey')
+ t.base._digest = function () { throw err }
+ t.base._flush(function (_err) { t.true(_err === err) })
+ t.end()
+ })
+
+ t.end()
+})
+
test('update', function (t) {
beforeEach(t)
@@ -53,7 +111,7 @@ test('update', function (t) {
t.base._digest = function () {}
t.base.digest()
t.throws(function () {
- t.base.update()
+ t.base.update(new Buffer(0))
}, /^Error: Digest already called$/)
t.end()
})
@@ -61,6 +119,19 @@ test('update', function (t) {
t.end()
})
+test('_update', function (t) {
+ beforeEach(t)
+
+ t.test('should throw error by default', function (t) {
+ t.throws(function () {
+ t.base._update()
+ }, /^Error: _update is not implemented$/)
+ t.end()
+ })
+
+ t.end()
+})
+
test('digest', function (t) {
beforeEach(t)
@@ -99,19 +170,6 @@ test('digest', function (t) {
t.end()
})
-test('_update', function (t) {
- beforeEach(t)
-
- t.test('should throw error by default', function (t) {
- t.throws(function () {
- t.base._update()
- }, /^Error: _update is not implemented$/)
- t.end()
- })
-
- t.end()
-})
-
test('_digest', function (t) {
beforeEach(t)
@@ -124,61 +182,3 @@ test('_digest', function (t) {
t.end()
})
-
-test('_transform', function (t) {
- beforeEach(t)
-
- t.test('should use update', function (t) {
- t.plan(2)
- var buffer = new Buffer(42)
- t.base.update = function (data) { t.true(data === buffer) }
- t.base._transform(buffer, 'buffer', function (err) {
- t.same(err, null)
- })
- t.end()
- })
-
- t.test('should decode string with custom encoding', function (t) {
- t.plan(2)
- t.base.update = function (data) { t.same(data, new Buffer('УТФ-8 text', 'utf8')) }
- t.base._transform('УТФ-8 text', 'utf8', function (err) {
- t.same(err, null)
- })
- t.end()
- })
-
- t.test('should handle error in update', function (t) {
- t.plan(1)
- var err = new Error('hey')
- t.base.update = function () { throw err }
- t.base._transform(new Buffer(42), 'buffer', function (_err) {
- t.true(_err === err)
- })
- t.end()
- })
-
- t.end()
-})
-
-test('_flush', function (t) {
- beforeEach(t)
-
- t.test('should use _digest', function (t) {
- t.plan(2)
- var buffer = new Buffer(42)
- t.base._digest = function () { return buffer }
- t.base.push = function (data) { t.true(data === buffer) }
- t.base._flush(function (err) { t.same(err, null) })
- t.end()
- })
-
- t.test('should handle errors in _digest', function (t) {
- t.plan(1)
- var err = new Error('hey')
- t.base._digest = function () { throw err }
- t.base._flush(function (_err) { t.true(_err === err) })
- t.end()
- })
-
- t.end()
-})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hash-base.git
More information about the Pkg-javascript-commits
mailing list