test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var protoify = require("./index.js"),
  2. ByteBuffer = require("protobufjs").ByteBuffer,
  3. assert = require("assert");
  4. // Array of samples to test
  5. var samples = [
  6. 1, -1, 0x80000000|0, 0x7fffffff|0, // Integers
  7. 0.1, 0.2, 1.234, // Doubles
  8. "John", // String
  9. true, false, // Booleans
  10. null, // null
  11. [], // Array
  12. {}, // Object
  13. undefined, // undefined
  14. [ // Array holding each data type
  15. 1,
  16. 0.1,
  17. "John",
  18. true,
  19. false,
  20. null,
  21. [],
  22. {},
  23. undefined
  24. ],
  25. { // Object holding each data type
  26. 1: 1,
  27. 0.1: 0.1,
  28. "John": "John",
  29. true: true,
  30. false: false,
  31. null: null,
  32. array: [],
  33. object: {},
  34. undefined: undefined
  35. }
  36. ];
  37. samples.forEach(function(sample) {
  38. // Encode each sample to a Buffer
  39. var buf = protoify(sample);
  40. // Print some nice debugging information
  41. console.log(JSON.stringify(sample));
  42. console.log("-------------------------------------------------------------------");
  43. console.log(ByteBuffer.wrap(buf).toDebug(true));
  44. // Decode the Buffer back to JSON
  45. var decodedSample = protoify.parse(buf);
  46. // And assert that it's actually equal
  47. assert.deepEqual(decodedSample, sample);
  48. });
  49. // If no assertion errors are thrown, print
  50. console.log("OK");