All files / simple-long numbers-long-test.ts

100% Statements 43/43
100% Branches 0/0
100% Functions 9/9
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 1x   1x 1x                           1x 1x   1x 1x                           1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x   1x 1x 1x   1x 1x 1x                                                                                      
import { Reader } from 'protobufjs';
import * as Long from 'long';
import { Numbers } from './simple';
import { simple as pbjs } from './pbjs';
import INumbers = pbjs.INumbers;
import PbNumbers = pbjs.Numbers;
 
describe('number', () => {
  it('generates types correctly', () => {
    const simple: Numbers = {
      double: 0,
      float: 1,
      int32: 2,
      int64: Long.fromNumber(3),
      uint32: 4,
      uint64: Long.fromNumber(5, true),
      sint32: 6,
      sint64: Long.fromNumber(7),
      fixed32: 8,
      fixed64: Long.fromNumber(9, true),
      sfixed32: 10,
      sfixed64: Long.fromNumber(11)
    };
    expect(simple.int64).toEqual(Long.fromNumber(3));
    expect(simple.uint64).toEqual(Long.fromNumber(5, true));
  });
 
  it('can decode', () => {
    const s1: INumbers = {
      double: 0,
      float: 1,
      int32: 2,
      int64: Long.fromNumber(3),
      uint32: 4,
      uint64: Long.fromNumber(5, true),
      sint32: 6,
      sint64: Long.fromNumber(7),
      fixed32: 8,
      fixed64: Long.fromNumber(9, true),
      sfixed32: 10,
      sfixed64: Long.fromNumber(11)
    };
    const s2 = Numbers.decode(Reader.create(PbNumbers.encode(PbNumbers.fromObject(s1)).finish()));
    expect(s2).toEqual(s1);
  });
 
  it('can encode', () => {
    const s1: Numbers = {
      double: 0,
      float: 1,
      int32: 2,
      int64: Long.fromNumber(3),
      uint32: 4,
      uint64: Long.fromNumber(5, true),
      sint32: 6,
      sint64: Long.fromNumber(7),
      fixed32: 8,
      fixed64: Long.fromNumber(9, true),
      sfixed32: 10,
      sfixed64: Long.fromNumber(11)
    };
    const s2 = PbNumbers.toObject(PbNumbers.decode(Numbers.encode(s1).finish()));
    expect(s2).toEqual({
      ...s1
    });
  });
 
  it('can decode and fallback to default values', () => {
    const s1: INumbers = {};
    const s2 = Numbers.decode(Reader.create(PbNumbers.encode(PbNumbers.fromObject(s1)).finish()));
    expect(s2.double).toEqual(0);
    expect(s2.float).toEqual(0);
    expect(s2.int32).toEqual(0);
    expect(s2.int64).toEqual(Long.ZERO);
    expect(s2.uint32).toEqual(0);
    expect(s2.uint64).toEqual(Long.UZERO);
    expect(s2.sint32).toEqual(0);
    expect(s2.sint64).toEqual(Long.ZERO);
    expect(s2.fixed32).toEqual(0);
    expect(s2.fixed64).toEqual(Long.UZERO);
    expect(s2.sfixed32).toEqual(0);
  });
 
  it('observes how pbjs handles null', () => {
    // the types are in theory only useful for construction
    const s1 = PbNumbers.fromObject({ double: null, float: 1 });
    // as after construction, they return the empty string
    expect(s1.double).toEqual(0);
    const s2 = PbNumbers.decode(PbNumbers.encode(s1).finish());
    expect(s2.double).toEqual(0);
  });
 
  it('has fromPartial', () => {
    const s1 = Numbers.fromPartial({});
    expect(s1).toMatchInlineSnapshot(`
  Object {
    "double": 0,
    "fixed32": 0,
    "fixed64": Long {
      "high": 0,
      "low": 0,
      "unsigned": true,
    },
    "float": 0,
    "int32": 0,
    "int64": Long {
      "high": 0,
      "low": 0,
      "unsigned": false,
    },
    "sfixed32": 0,
    "sfixed64": Long {
      "high": 0,
      "low": 0,
      "unsigned": false,
    },
    "sint32": 0,
    "sint64": Long {
      "high": 0,
      "low": 0,
      "unsigned": false,
    },
    "uint32": 0,
    "uint64": Long {
      "high": 0,
      "low": 0,
      "unsigned": true,
    },
  }
  `);
  });
});