Permalink
Browse files

tests ported to expresso

  • Loading branch information...
1 parent 14d4f53 commit 9721352ae372f59ffd633c01f1331d88990e07b4 @substack committed Aug 9, 2010
View
@@ -0,0 +1,23 @@
+// Test advancing the buffer
+var Buffer = require('buffer').Buffer;
+var BufferList = require('bufferlist');
+
+exports.advance = function (assert) {
+ var b = new BufferList;
+ var buf1 = new Buffer(5); buf1.write('abcde');
+ var buf2 = new Buffer(3); buf2.write('xyz');
+ var buf3 = new Buffer(5); buf3.write('11358');
+
+ b.push(buf1);
+ assert.equal(b.take(b.length), 'abcde', 'pushed correctly');
+ b.advance(3);
+ assert.equal(b.take(b.length), 'de', 'advanced with one buffer');
+ b.advance(3);
+ assert.equal(b.take(b.length), '', 'advanced one buffer past the end');
+ b.push(buf2);
+ assert.equal(b.take(b.length), 'yz', 'offset preserved past the end');
+ b.push(buf3);
+ assert.equal(b.take(b.length), 'yz11358', 'second push after advance');
+ b.advance(4);
+ assert.equal(b.take(b.length), '358', 'advance after two pushes');
+};
View
@@ -0,0 +1,89 @@
+// Test the binary interface to bufferlists
+var Buffer = require('buffer').Buffer;
+var BufferList = require('bufferlist').BufferList; // old style
+var Binary = require('bufferlist/binary').Binary; // old style
+var sys = require('sys');
+
+exports.binary = function (assert) {
+ function runTest(bufs, check) {
+ var bList = new BufferList;
+
+ var binary = Binary(bList)
+ .getWord16be('xLen')
+ .when('xLen', 0, function (vars) {
+ assert.equal(vars.xLen, 0, 'when check for 0 failed');
+ this
+ .getWord32le('msgLen')
+ .getBuffer('msg', function (vars) {
+ return vars.msgLen
+ })
+ .tap(function (vars) {
+ vars.moo = 42;
+ })
+ .exit()
+ ;
+ })
+ .getBuffer('xs', 'xLen')
+ .tap(function (vars) {
+ vars.moo = 100;
+ })
+ .end()
+ ;
+
+ var iv = setInterval(function () {
+ var buf = bufs.shift();
+ if (!buf) {
+ clearInterval(iv);
+ check(binary.vars);
+ }
+ else {
+ bList.push(buf);
+ }
+ }, 50);
+ }
+
+ runTest(
+ ['\x00','\x04m','eow'].map(function (s) {
+ var b = new Buffer(Buffer.byteLength(s,'binary'));
+ b.write(s,'binary');
+ return b;
+ }),
+ function (vars) {
+ assert.equal(
+ vars.xLen,
+ 4,
+ 'xLen == 4 failed (xLen == ' + sys.inspect(vars.xLen) + ')'
+ );
+
+ var xs = vars.xs.toString();
+ assert.equal(
+ xs, 'meow', 'xs != "meow", xs = ' + sys.inspect(xs)
+ );
+ assert.equal(
+ vars.moo, 100, 'moo != 100, moo == ' + sys.inspect(vars.moo)
+ );
+ }
+ );
+
+ runTest(
+ ['\x00\x00','\x12\x00\x00\x00hap','py pur','ring c','ats']
+ .map(function (s) {
+ var b = new Buffer(Buffer.byteLength(s,'binary'));
+ b.write(s,'binary');
+ return b;
+ }),
+ function (vars) {
+ assert.equal(vars.xLen, 0, 'xLen == 0 in "\\x00\\x12happy purring cats"');
+ assert.equal(
+ vars.msgLen, 18,
+ 'msgLen != 18, msgLen = ' + sys.inspect(vars.msgLen)
+ );
+ assert.equal(
+ vars.msg, 'happy purring cats',
+ 'msg != "happy purring cats", msg == ' + sys.inspect(vars.msg)
+ );
+ assert.equal(vars.moo, 42, 'moo != 42');
+ }
+ );
+};
+
View
@@ -0,0 +1,72 @@
+// Test .into and hierarchical addressing assignment
+var sys = require('sys');
+var Buffer = require('buffer').Buffer;
+var BufferList = require('bufferlist');
+var Binary = require('bufferlist/binary');
+
+exports['binary assign'] = function (assert) {
+ var tapped = 0;
+ var bList = new BufferList;
+
+ Binary(bList)
+ .getWord16be('foo','bar','baz')
+ .tap(function (vars) {
+ assert.equal(
+ vars.foo.bar.baz, 24930,
+ 'vars.foo.bar.baz != 24930, '
+ + 'vars.foo.bar.baz == ' + sys.inspect(vars.foo.bar.baz)
+ );
+ tapped ++;
+ })
+ .getWord32le(['one','two','three'])
+ .tap(function (vars) {
+ assert.equal(
+ vars.one.two.three, 1717920867,
+ 'vars.one.two.three != 1717920867, '
+ + 'vars.one.two.three == ' + sys.inspect(vars.one.two.three)
+ );
+ tapped ++;
+ })
+ .into(['what.the','fuck'],function () {
+ this
+ .getWord8('w')
+ .getWord8('t')
+ .getWord8('f')
+ .getWord32le('?!')
+ .tap(function (vars) {
+ vars.meow = 9000;
+ })
+ ;
+ })
+ .tap(function (vars) {
+ assert.equal(
+ vars.what.the.fuck.w, 119,
+ '.w != 119, .w == ' + vars.what.the.fuck.w
+ );
+ assert.equal(
+ vars.what.the.fuck.t, 116,
+ '.t != 119, .t == ' + vars.what.the.fuck.t
+ );
+ assert.equal(
+ vars.what.the.fuck.f, 102,
+ '.f != 119, .f == ' + vars.what.the.fuck.f
+ );
+ assert.equal(
+ vars.what.the.fuck['?!'], 825303359,
+ '.?! != 825303359, .?! == ' + vars.what.the.fuck['?!']
+ );
+ assert.equal(
+ vars.what.the.fuck['meow'], 9000,
+ '.meow != 9000, .meow == ' + vars.what.the.fuck.meow
+ );
+ tapped ++;
+ })
+ .end()
+ ;
+
+ var buf = new Buffer(13);
+ buf.write('abcdefwtf?!11');
+ bList.push(buf);
+ assert.equal(tapped, 3, 'tapped != 3, tapped == ' + tapped);
+};
+
Oops, something went wrong.

0 comments on commit 9721352

Please sign in to comment.