|
| 1 | +'use strict' |
| 2 | + |
| 3 | +let Interchange = require('../interchange.js'); |
| 4 | + |
| 5 | +describe('Interchange', function () { |
| 6 | + let interchange; |
| 7 | + beforeEach(function () { |
| 8 | + interchange = new Interchange(); |
| 9 | + interchange.tracker = { accept: function () {} }; |
| 10 | + spyOn(interchange, 'setup'); |
| 11 | + spyOn(interchange.tracker, 'accept'); |
| 12 | + }); |
| 13 | + it('should accept a message without an envelope', function () { |
| 14 | + expect(function () { interchange.accept({ name: 'UNH' }); }).not.toThrow(); |
| 15 | + expect(interchange.depth.maximum).toEqual(0); |
| 16 | + expect(interchange.depth.current).toEqual(1); |
| 17 | + expect(function () { interchange.next(); }).not.toThrow(); |
| 18 | + expect(interchange.depth.current).toEqual(0); |
| 19 | + }); |
| 20 | + it('cannot nest interchanges', function () { |
| 21 | + expect(function () { interchange.accept({ name: 'UNB' }); }).not.toThrow(); |
| 22 | + expect(interchange.depth.minimum).toEqual(1); |
| 23 | + expect(interchange.depth.current).toEqual(1); |
| 24 | + expect(function () { interchange.accept({ name: 'UNB' }); }).toThrow(); |
| 25 | + }); |
| 26 | + it('cannot open an interchange after a single message', function () { |
| 27 | + expect(function () { interchange.accept({ name: 'UNH' }); }).not.toThrow(); |
| 28 | + expect(interchange.depth.maximum).toEqual(0); |
| 29 | + expect(interchange.depth.current).toEqual(1); |
| 30 | + expect(function () { interchange.next(); }).not.toThrow(); |
| 31 | + expect(interchange.depth.current).toEqual(0); |
| 32 | + expect(function () { interchange.accept({ name: 'UNB' }); }).toThrow(); |
| 33 | + }); |
| 34 | + it('cannot open a group without an interchange', function () { |
| 35 | + expect(function () { interchange.accept({ name: 'UNG' }); }).toThrow(); |
| 36 | + }); |
| 37 | + for (var name of ['UNB', 'UNZ', 'UNG', 'UNE']) { |
| 38 | + it('should not validate a ' + name + ' segment while tracking a message', function () { |
| 39 | + // While no valid message contains an enveloping segment in it's segment |
| 40 | + // table, this test is the equivalence of allowing such a messsage |
| 41 | + // definition. Prohibiting enveloping segments in messages should be |
| 42 | + // done by providing a correct segment table, not by algorithm design. |
| 43 | + expect(function () { |
| 44 | + interchange.accept({ name: 'UNH' }); |
| 45 | + }).not.toThrow(); |
| 46 | + expect(interchange.depth.maximum).toEqual(0); |
| 47 | + expect(interchange.depth.current).toEqual(1); |
| 48 | + expect(function () { interchange.accept({ name: name }); }).not.toThrow(); |
| 49 | + expect(interchange.tracker.accept).toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + } |
| 52 | + it('should accept a message in an interchange', function () { |
| 53 | + expect(function () { interchange.accept({ name: 'UNB' }); }).not.toThrow(); |
| 54 | + expect(interchange.depth.minimum).toEqual(1); |
| 55 | + expect(interchange.depth.current).toEqual(1); |
| 56 | + expect(function () { interchange.accept({ name: 'UNH' }); }).not.toThrow(); |
| 57 | + expect(interchange.depth.maximum).toEqual(1); |
| 58 | + expect(interchange.depth.current).toEqual(2); |
| 59 | + expect(function () { interchange.next(); }).not.toThrow(); |
| 60 | + expect(interchange.depth.current).toEqual(1); |
| 61 | + expect(function () { interchange.accept({ name: 'UNZ' }); }).not.toThrow(); |
| 62 | + expect(interchange.depth.current).toEqual(0); |
| 63 | + }); |
| 64 | + it('should not accept a group after a message', function () { |
| 65 | + expect(function () { interchange.accept({ name: 'UNB' }); }).not.toThrow(); |
| 66 | + expect(interchange.depth.minimum).toEqual(1); |
| 67 | + expect(interchange.depth.current).toEqual(1); |
| 68 | + expect(function () { interchange.accept({ name: 'UNH' }); }).not.toThrow(); |
| 69 | + expect(interchange.depth.maximum).toEqual(1); |
| 70 | + expect(interchange.depth.current).toEqual(2); |
| 71 | + expect(function () { interchange.next(); }).not.toThrow(); |
| 72 | + expect(interchange.depth.current).toEqual(1); |
| 73 | + expect(function () { interchange.accept({ name: 'UNG' }); }).toThrow(); |
| 74 | + }); |
| 75 | + it('should not accept a group without an interchange', function () { |
| 76 | + expect(function () { interchange.accept({ name: 'UNG' }); }).toThrow(); |
| 77 | + }); |
| 78 | + it('should not accept a message after a group', function () { |
| 79 | + expect(function () { interchange.accept({ name: 'UNB' }); }).not.toThrow(); |
| 80 | + expect(interchange.depth.minimum).toEqual(1); |
| 81 | + expect(interchange.depth.current).toEqual(1); |
| 82 | + expect(function () { interchange.accept({ name: 'UNG' }); }).not.toThrow(); |
| 83 | + expect(interchange.depth.minimum).toEqual(2); |
| 84 | + expect(interchange.depth.current).toEqual(2); |
| 85 | + expect(function () { interchange.accept({ name: 'UNE' }); }).not.toThrow(); |
| 86 | + expect(interchange.depth.current).toEqual(1); |
| 87 | + expect(function () { interchange.accept({ name: 'UNH' }); }).toThrow(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments