coerce.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var assert = require('assert')
  2. var ref = require('../')
  3. describe('coerce', function () {
  4. it('should return `ref.types.void` for "void"', function () {
  5. var type = ref.coerceType('void')
  6. assert.strictEqual(ref.types.void, type)
  7. })
  8. it('should return a ref type when a "*" is present', function () {
  9. var type = ref.coerceType('void *')
  10. assert(type !== ref.types.void)
  11. assert.equal(type.indirection, ref.types.void.indirection + 1)
  12. })
  13. it('should coerce the "type" property of a Buffer', function () {
  14. var buf = new Buffer(ref.sizeof.int)
  15. buf.type = 'int'
  16. var type = ref.getType(buf)
  17. assert.strictEqual(ref.types.int, type)
  18. assert.strictEqual('int', buf.type)
  19. })
  20. it('should coerce "Object" to `ref.types.Object`', function () {
  21. assert.strictEqual(ref.types.Object, ref.coerceType('Object'))
  22. })
  23. it('should coerce the optional type in `ref.get()`', function () {
  24. var b = new Buffer(ref.sizeof.int8)
  25. b[0] = 5
  26. assert.strictEqual(5, ref.get(b, 0, 'int8'))
  27. })
  28. it('should coerce the optional type in `ref.set()`', function () {
  29. var b = new Buffer(ref.sizeof.int8)
  30. ref.set(b, 0, 5, 'int8')
  31. assert.strictEqual(5, b[0])
  32. })
  33. it('should throw a TypeError if a "type" can not be inferred', function () {
  34. assert.throws(function () {
  35. ref.coerceType({ })
  36. }, /could not determine a proper \"type\"/)
  37. })
  38. })