test/base-skip.test.js
60 |
const { expect } = require('chai');
61 | const Base = require('../src/base');
62 |
63 | /** @test {CorrodeBase#skip} */
64 | describe('CorrodeBase#skip', () => {
65 | beforeEach(function(){
66 | this.base = new Base();
67 | this.eqArray = require('./helpers/asserts').eqArray.bind(this);
68 | this.eqMultiArray = require('./helpers/asserts').eqMultiArray.bind(this);
69 | });
70 |
71 | it('allows us to skip content', function(done){
72 | this.base
73 | .uint8('var_1')
74 | .skip(2)
75 | .uint8('var_2')
76 | .skip(4)
77 | .uint8('var_3');
78 |
79 | this.eqArray([1, 0, 0, 2, 0, 0, 0, 0, 3], done, {
80 | var_1: 1,
81 | var_2: 2,
82 | var_3: 3
83 | });
84 | });
85 |
86 | /** @test {CorrodeBase#isSeeking} */
87 | it('prevents us from unskipping content with isSeeking = false', function(done){
88 | this.base
89 | .uint8('var_1')
90 | .skip(2)
91 | .uint8('var_2')
92 | .skip(-3)
93 | .uint8('var_3');
94 |
95 | expect(this.eqMultiArray.bind(this, [[1], [3], [0], [2]], done, {})).to.throw(RangeError);
96 |
97 | done();
98 | });
99 |
100 | /** @test {CorrodeBase#isSeeking} */
101 | it('allows us to unskip content with isSeeking = true', function(done){
102 | this.base.isSeeking = true;
103 |
104 | this.base
105 | .uint8('var_1')
106 | .skip(2)
107 | .uint8('var_2')
108 | .skip(-3)
109 | .uint8('var_3');
110 |
111 | this.eqArray([1, 3, 0, 2], done, {
112 | var_1: 1,
113 | var_2: 2,
114 | var_3: 3
115 | });
116 | });
117 |
118 | it('prevents us from unskipping too far', function(){
119 | this.base
120 | .uint8('var_1')
121 | .skip(-10);
122 |
123 | expect(this.eqArray.bind(this, [1, 2], () => {}, {})).to.throw(RangeError);
124 | });
125 | });
126 |
127 |
128 |
129 |
130 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/docs/manual/CHANGELOG.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | test/corrode-pointer.test.js
60 |
const { expect } = require('chai');
61 | const Corrode = require('../src');
62 |
63 | /** @test {Corrode#pointer} */
64 | describe('Corrode#pointer', () => {
65 | beforeEach(function(){
66 | this.base = new Corrode();
67 | this.eqArray = require('./helpers/asserts').eqArray.bind(this);
68 | });
69 |
70 | it('retrieves from array', function(done){
71 | this.base
72 | .loop('array', function(end, discard, i){
73 | if(i >= 3){
74 | return end();
75 | }
76 | this
77 | .uint8('values')
78 | .map.push();
79 | })
80 | .pointer('alphabet', ['a', 'b', 'c'], 'uint8')
81 | .pointer('numeric', 'array', 'uint8');
82 |
83 | this.eqArray([3, 5, 7, 1, 2], done, {
84 | array: [3, 5, 7],
85 | alphabet: 'b',
86 | numeric: 7
87 | });
88 | });
89 |
90 | it('retrieves from object', function(done){
91 | this.base
92 | .tap('obj', function(){
93 | this.loop(function(end, discard, i){
94 | if(i >= 3){
95 | return end();
96 | }
97 | this.uint8(i);
98 | });
99 | })
100 | .pointer('alphabet', { 0: 'a', 1: 'b', 2: 'c' }, 'uint8')
101 | .pointer('numeric', 'obj', 'uint8');
102 |
103 | this.eqArray([3, 5, 7, 1, 2], done, {
104 | obj: { 0: 3, 1: 5, 2: 7},
105 | alphabet: 'b',
106 | numeric: 7
107 | });
108 | });
109 |
110 | // why not?
111 | it('retrieves from string', function(done){
112 | this.base
113 | .terminatedString('string')
114 | .pointer('numeric', 'string', 'uint8');
115 |
116 | this.eqArray([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0, 4], done, {
117 | string: 'hello',
118 | numeric: 'o'
119 | });
120 | });
121 | });
122 |
123 |
124 |
125 |
126 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
--------------------------------------------------------------------------------