1- #!/usr/bin/env node
1+ "use strict" ;
2+ var bops = require ( 'bops' ) ;
3+ var test = require ( 'tape' ) ;
24var msgpack = require ( './msgpack' ) ;
35var util = require ( 'util' ) ;
4- var assert = require ( 'assert' ) ;
56
67var tests = [
78 true , false , null , undefined ,
@@ -12,61 +13,29 @@ var tests = [
1213 0x20000 , - 0x20000 , 0x40000 , - 0x40000 ,
1314 10 , 100 , 1000 , 10000 , 100000 , 1000000 ,
1415 - 10 , - 100 , - 1000 , - 10000 , - 100000 , - 1000000 ,
15- 'hello' , 'world' , Buffer ( "Hello" ) , Buffer ( "World" ) ,
16+ 'hello' , 'world' , bops . from ( "Hello" ) , bops . from ( "World" ) ,
1617 [ 1 , 2 , 3 ] , [ ] , { name : "Tim" , age : 29 } , { } ,
1718 { a : 1 , b : 2 , c : [ 1 , 2 , 3 ] } ,
1819] ;
19- for ( var i = 0 , l = tests . length ; i < l ; i ++ ) {
20- var test = tests [ i ] ;
21- if ( typeof test === 'number' ) {
22- tests . push ( test + 1 ) ;
23- tests . push ( test - 1 ) ;
24- tests . push ( test + 0.5 ) ;
25- }
26- }
27- [ 0x100 , 0x1000 , 0x10000 , 0x100000 ] . forEach ( function ( length ) {
28- var list = new Array ( length ) , obj = { } ;
29- for ( var i = 0 ; i < length ; i ++ ) {
30- list [ i ] = i ;
31- obj [ i ] = i ;
32- }
33- tests . push ( list ) ;
34- tests . push ( obj ) ;
35- } ) ;
3620
37- var width = 80 ;
38- if ( process . stdout . isTTY ) {
39- width = process . stdout . getWindowSize ( ) [ 0 ] ;
40- }
41- var mistakes = 0 ;
42- function dump ( value ) {
43- if ( typeof value === 'undefined' || Buffer . isBuffer ( value ) ) {
44- return util . inspect ( value ) . replace ( / \n * / g, '' ) ;
45- }
46- return JSON . stringify ( value ) ;
47- }
48- tests . forEach ( function ( test ) {
49- console . log ( dump ( test ) . substr ( 0 , width ) ) ;
50- var encoded = msgpack . encode ( test ) ;
51- console . log ( encoded . inspect ( ) . substr ( 0 , width ) ) ;
52- var decoded = msgpack . decode ( encoded ) ;
53- try {
54- assert . deepEqual ( test , decoded ) ;
55- if ( typeof test === "object" && test !== null ) {
56- assert . equal ( test . constructor , decoded . constructor ) ;
21+ test ( 'codec works as expected' , function ( assert ) {
22+
23+ tests . forEach ( function ( input ) {
24+ var packed = msgpack . encode ( input ) ;
25+ console . log ( packed ) ;
26+ var output = msgpack . decode ( packed ) ;
27+ if ( bops . is ( input ) ) {
28+ assert . true ( bops . is ( output ) ) ;
29+ for ( var i = 0 , l = input . length ; i < l ; i ++ ) {
30+ assert . equal ( input [ i ] , output [ i ] ) ;
31+ }
32+ assert . equal ( input . length , output . length ) ;
33+ }
34+ else {
35+ assert . deepEqual ( input , output ) ;
5736 }
58- } catch ( err ) {
59- console . error ( ) ;
60- console . error ( dump ( test ) . substr ( 0 , width ) ) ;
61- console . error ( encoded . inspect ( ) . substr ( 0 , width ) ) ;
62- console . error ( dump ( decoded ) . substr ( 0 , width ) ) ;
63- console . error ( err . stack ) ;
64- mistakes ++ ;
65- }
37+ } ) ;
38+
39+ assert . end ( ) ;
40+
6641} ) ;
67- if ( mistakes ) {
68- console . error ( mistakes + " tests failed!" ) ;
69- } else {
70- console . log ( "\nAll tests passed successfully!" ) ;
71- }
72- process . exit ( mistakes . length ) ;
0 commit comments