UNPKG

@knod/prose-stepper

Version:

Navigate through the words and sentences of prose text, stepping backward and forward sequentially

1,158 lines (926 loc) 86.1 kB
var ProseStepper = require('../dist/prose-stepper.js'); /* Notes: * - To fully test retention of position once a position has been set, * the 'current position' test needs to be run after every test. There * are a few tests explicitly for retention, but otherwise, they are * at the end of every other relevant test. * - Any problems with `splitterOptions`, the third argument, will be * handled by the splitter (in this case, hyphenaxe). */ describe("When a ProseStepper instance", function () { var sentences = [ ['Victorious,', 'you', 'brave', 'flag.'], ['Delirious,', 'I', 'come', 'back.'], ['\n'], ['Why,', 'oh', 'wattlebird?'] ]; // "watt-" "elbi-" "rd?" var ps, state; beforeEach( function () { state = { maxNumCharacters: 5 }; ps = new ProseStepper( state ); }) // ==== EXPECTED INPUT ==== describe("is given an array of arrays with an empty string ([['']])", function () { it("it should give an empty string on `.getFragment()`.", function () { ps.process( [['']] ); expect( ps.getFragment( [0, 0, 0] )).toEqual( '' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( '' ); expect( ps.getFragment( [0, 1, 0] )).toEqual( '' ); expect( ps.getFragment( [0, -1, 0] )).toEqual( '' ); }); }); describe("is given an array of arrays of strings", function () { beforeEach( function () { ps.process( sentences ); }); // --- Defaults --- describe("and formed with no state object values", function () { beforeEach( function () { ps = new ProseStepper(); ps.process( sentences ); }); it("should use its defaults", function () { expect( ps.getFragment( [0, 0, 0] )).toEqual( 'Victorious,'); }); describe("and then `.setState()` is used to reference an object", function () { it("should save and use the new state object.", function () { var newState = { maxNumCharacters: 5 }; ps.setState( newState ); expect( ps.getFragment( [0, 0, 0] )).toEqual( 'Vict-'); expect( ps._state ).toBe( newState ); }); }); }); // --- Current --- describe("and asked for the current fragment without any stepping", function () { it("should give the 1st fragment", function () { expect( ps.getFragment( [0, 0, 0] )).toEqual( 'Vict-') } ); }); // --- Reset --- describe("and is manipulated in any way,", function () { it("`.restart()` should start from the beginning again.", function () { ps.getFragment( [0, 1, 0] ); ps.getFragment( [0, 1, 0] ); ps.getFragment( [0, 1, 0] ); ps.restart(); expect( ps.getFragment( [0, 0, 0] )).toEqual( 'Vict-' ); }); }); // --- Forward --- describe("and stepped forward (1st non-zero integer found is the one that counts)", function () { // --- Sentences --- describe("1 sentence", function () { it("at the very last sentence, it should return the last word in the last sentence.", function () { var result = 'watt-'; ps.getFragment( [3, 0, 0] ); expect( ps.getFragment( [1, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); // sentence boundry it("should give the 1st fragment of the next sentence.", function () { var result = 'Del-'; expect( ps.getFragment( [1, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment of the next sentence, no matter what later integers are provided.", function () { var result = 'Del-'; expect( ps.getFragment( [1, 1, 0] )).toEqual( result ); // same as [1, 0, 0] ps.restart(); expect( ps.getFragment( [1, 0, 1] )).toEqual( result ); // same as [1, 0, 0] ps.restart(); expect( ps.getFragment( [1, 2, 0] )).toEqual( result ); // same as [1, 0, 0] ps.restart(); expect( ps.getFragment( [1, 0, 2] )).toEqual( result ); // same as [1, 0, 0] ps.restart(); expect( ps.getFragment( [1, -1, 0] )).toEqual( result ); // same as [1, 0, 0] ps.restart(); expect( ps.getFragment( [1, 0, -1] )).toEqual( result ); // same as [1, 0, 0] }); describe("and then asked for the current fragment", function () { // Explicit retention tests it("should retain the mid-text sentence position.", function () { var frag1 = ps.getFragment( [1, 0, 0] ) expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); }); }); // End 1 sentence describe("3 sentences", function () { it("at the very last sentence, it should return the last word in the last sentence.", function () { var result = 'watt-'; ps.getFragment( [3, 0, 0] ); expect( ps.getFragment( [3, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); // sentence boundry it("should give the 1st fragment 3 sentence forward.", function () { var result = 'Why,'; expect( ps.getFragment( [3, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 3 sentence forward, no matter what later integers are provided.", function () { var result = 'Why,'; expect( ps.getFragment( [3, 1, 0] )).toEqual( result ); // same as [3, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 1] )).toEqual( result ); // same as [3, 0, 0] ps.restart(); expect( ps.getFragment( [3, 2, 0] )).toEqual( result ); // same as [3, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 2] )).toEqual( result ); // same as [3, 0, 0] ps.restart(); expect( ps.getFragment( [3, -1, 0] )).toEqual( result ); // same as [3, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, -1] )).toEqual( result ); // same as [3, 0, 0] }); }); // End 3 sentences describe("more sentences than exist", function () { // TODO: Discuss behavior - should return the last fragment in the last word? it("it should return the 1st fragment in the last word in the last sentence.", function () { var result = 'watt-'; expect( ps.getFragment( [5, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); // --- Words --- describe("1 word", function () { it("at the very last word, it should return the last word in the last sentence.", function () { var result = 'watt-'; expect( ps.getFragment( [4, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 1, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); // word boundry it("should give the 1st fragment from 1 word forward.", function () { var result = 'you'; expect( ps.getFragment( [0, 1, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment from 1 word forward, no matter what later integers are provided.", function () { var result = 'you'; expect( ps.getFragment( [0, 1, 1] )).toEqual( result ); // same as [0, 1, 0] ps.restart(); expect( ps.getFragment( [0, 1, 2] )).toEqual( result ); // same as [0, 1, 0] ps.restart(); expect( ps.getFragment( [0, 1, -1] )).toEqual( result ); // same as [0, 1, 0] }); // sentence boundry describe("past the last word in the current sentence,", function () { it("it should give the 1st fragment 1 sentence forward.", function () { var result = 'Del-'; ps.getFragment( [0, 3, 0] ) expect( ps.getFragment( [0, 1, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("it should give the 1st fragment 1 sentence forward, no matter what later integers are provided.", function () { ps.getFragment( [0, 3, 0] ) expect( ps.getFragment( [0, 1, -1] )).toEqual( 'Del-' ); }); }); describe("and then asked for the current fragment", function () { it("should retain the mid-sentence position.", function () { var frag1 = ps.getFragment( [0, 1, 0] ) expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); }); }); // End 1 word describe("2 words", function () { it("at the very last word, it should return the last word in the last sentence.", function () { var result = 'watt-'; expect( ps.getFragment( [4, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 2, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); // word boundry it("should give the 1st fragment from 2 words forward.", function () { var result = 'brave'; expect( ps.getFragment( [0, 2, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment from 2 words forward, no matter what later integers are provided.", function () { var result = 'brave'; expect( ps.getFragment( [0, 2, 1] )).toEqual( result ); // same as [0, 2, 0] ps.restart(); expect( ps.getFragment( [0, 2, 2] )).toEqual( result ); // same as [0, 2, 0] ps.restart(); expect( ps.getFragment( [0, 2, -1] )).toEqual( result ); // same as [0, 2, 0] }); // sentence boundry it("past the last word in the current sentence, it should give the 1st fragment from the 2nd word, 1 sentence forward.", function () { var result = 'I'; ps.getFragment( [0, 3, 0] ); expect( ps.getFragment( [0, 2, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("past the last word in the current sentence, it should give the 1st fragment from the 2nd word, 1 sentence forward, no matter what later integers are provided.", function () { var result = 'I'; ps.getFragment( [0, 3, 0] ); expect( ps.getFragment( [0, 2, -1] )).toEqual( result ); // same as [0, 2, 0] }); }); // End 2 words describe("more words than exist", function () { // TODO: Discuss behavior - should return the last fragment in the last word? it("should return the 1st fragment in the last word in the last sentence.", function () { var result = 'watt-'; expect( ps.getFragment( [0, 100, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); // --- Fragments --- describe("1 fragment", function () { describe("at the 1st fragment of the last word,", function () { describe("of a 3 fragment word,", function () { it("it should return the 2nd fragment in the last word in the last sentence.", function () { var result = 'lebi-'; expect( ps.getFragment( [4, 0, 0] )).toEqual( 'watt-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); describe("and then asked for the current fragment", function () { it("should retain a the position at the last fragment.", function () { ps.getFragment( [4, 0, 0] ) var frag1 = ps.getFragment( [0, 0, 1] ) expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); }); }); }); // end 1st frag of last word describe("at the 2nd fragment of the last word,", function () { describe("of a 3 fragment word,", function () { it("it should return the last fragment in the last word in the last sentence.", function () { var result = 'rd?'; expect( ps.getFragment( [4, 0, 0] )).toEqual( 'watt-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( 'lebi-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); describe("and then asked for the current fragment", function () { it("should retain a the position at the last fragment.", function () { ps.getFragment( [4, 0, 0] ) ps.getFragment( [0, 0, 1] ) var frag1 = ps.getFragment( [0, 0, 1] ) expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); }); }); }); // end 2d frag of last word describe("at the very last fragment,", function () { describe("of a 3 fragment word,", function () { it("it should return the last fragment in the last word in the last sentence.", function () { var result = 'rd?'; expect( ps.getFragment( [4, 0, 0] )).toEqual( 'watt-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( 'lebi-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); describe("and then asked for the current fragment", function () { it("should retain a the position at the last fragment.", function () { ps.getFragment( [4, 0, 0] ); ps.getFragment( [0, 0, 1] ); ps.getFragment( [0, 0, 1] ); var frag1 = ps.getFragment( [0, 0, 1] ); expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); }); }); }); // end 3rd/last frag of last word describe("should give the next fragment", function () { it("- the 2nd fragment when at the start of a 3 fragment word.", function () { var result = 'orio-'; expect( ps.getFragment( [0, 0, 0] )).toEqual( 'Vict-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("- the 3rd fragment when in the 2nd fragment of a 3 fragment word.", function () { var result = 'us,'; expect( ps.getFragment( [0, 0, 1] )).toEqual( 'orio-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); describe("and then, asked for the current fragment,", function () { it("should retain the mid-word fragment position (the 2nd fragment when at the start of a 3 fragment word).", function () { var frag1 = ps.getFragment( [0, 0, 1] ) expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); it("should retain the mid-word fragment position (the 3rd fragment when in the 2nd fragment of a 3 fragment word).", function () { ps.getFragment( [0, 0, 1] ) var frag1 = ps.getFragment( [0, 0, 1] ) expect( ps.getFragment( [0, 0, 0] )).toEqual( frag1 ) }); }); }); // End multi-fragment stepping describe("past the last fragment in the current word,", function () { it("it should give the 1st fragment 1 word forward.", function () { var result = 'you'; ps.getFragment( [0, 0, 2] ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("past the last fragment in the current sentence,.", function () { it("it should give the 1st fragment 1 sentence forward.", function () { var result = 'Del-'; expect( ps.getFragment( [0, 3, 0] )).toEqual( 'flag.' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("into the last word,", function () { it("it should give the 1st fragment last word.", function () { var result = 'watt-'; expect( ps.getFragment( [4, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, -1] )).toEqual( 'oh' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("past the last word just as `.maxNumCharacters` changes,", function () { it("it should give the last fragment last word.", function () { var result = 'wattlebird?'; expect( ps.getFragment( [4, 0, 0] )).toEqual( 'watt-' ); expect( ps.getFragment( [0, 0, -1] )).toEqual( 'oh' ); state.maxNumCharacters = 20; expect( ps.getFragment( [0, 0, 1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); }); // End 1 fragment describe("2 fragments", function () { describe("at the very last fragment,", function () { // TODO: Desired behavior? Elsewhere, we get the first fragment in the // next word. On the last word, which fragment do we want? it("it should return the last fragment in the last word (3 fragments) in the last sentence.", function () { var result = 'rd?'; expect( ps.getFragment( [4, 0, 0] )).toEqual( 'watt-' ); expect( ps.getFragment( [0, 0, 2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); it("should give the fragment 2 fragments forward from this one.", function () { var result = 'us,'; expect( ps.getFragment( [0, 0, 2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("or more past the last fragment in the current word, it should give the 1st fragment 1 word forward.", function () { var result = 'you'; expect( ps.getFragment( [0, 0, 6] )).toEqual( result ); // ??: Should this be the behavior? What should it be? expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("past the last fragment in the current sentence, it should give the 1st fragment 1 sentence forward.", function () { var result = 'Del-'; ps.getFragment( [0, 2, 0] ); expect( ps.getFragment( [0, 0, 1] )).toEqual( 'flag.' ); expect( ps.getFragment( [0, 0, 6] )).toEqual( result ); // ??: Should this be the behavior? What should it b e? expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); // End 2 fragments }); // End forward // --- Backward --- describe("and stepped backward (1st non-zero integer found is the one that counts)", function () { // --- Sentences --- describe("1 sentence", function () { it("at the very 1st sentence, it should return the 1st word in the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( [-1, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment of the previous sentence.", function () { var result = '\n'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-1, 0, 0] )).toEqual( result ); // same as [-1, 0, 0] expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment of the previous sentence, no matter what later integers are provided.", function () { var result = '\n'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-1, 2, 0] )).toEqual( result ); // same as [-1, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-1, 0, 2] )).toEqual( result ); // same as [-1, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-1, -1, 0] )).toEqual( result ); // same as [-1, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-1, 0, -1] )).toEqual( result ); // same as [-1, 0, 0] }); }); describe("2 sentences", function () { it("at the very 1st sentence, it should return the 1st word in the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( [-2, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 2 sentence backward.", function () { var result = 'Del-'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-2, 0, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 2 sentence backward, no matter what later integers are provided.", function () { var result = 'Del-'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-2, 2, 0] )).toEqual( result ); // same as [-2, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-2, 0, 2] )).toEqual( result ); // same as [-2, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-2, -1, 0] )).toEqual( result ); // same as [-2, 0, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [-2, 0, -1] )).toEqual( result ); // same as [-2, 0, 0] }); }); // --- Words --- describe("1 word", function () { it("at the very 1st word, it should return the 1st word in the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, -1, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 1 word backward.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -1, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 1 word backward, no matter what later integers are provided.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -1, 1] )).toEqual( result ); // same as [0, -1, 0] ps.restart(); expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -1, 2] )).toEqual( result ); // same as [0, -1, 0] ps.restart(); expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -1, -1] )).toEqual( result ); // same as [0, -1, 0] }); it("before the 1st word in the current sentence, it should give the 1st fragment 1 word backward, crossing sentence boundries.", function () { var result = '\n'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -1, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st word in the current sentence, it should give the 1st fragment 1 word backward, crossing sentence boundries, no matter what later integers are provided.", function () { var result = '\n'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -1, 1] )).toEqual( result ); // same as [0, -1, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -1, 2] )).toEqual( result ); // same as [0, -1, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -1, -1] )).toEqual( result ); // same as [0, -1, 0] }); }); describe("2 words", function () { it("at the very 1st word, it should return the 1st word in the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, -2, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 2 words backward.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 2, 0] )).toEqual( 'brave' ); expect( ps.getFragment( [0, -2, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("should give the 1st fragment 2 words backward, no matter what later integers are provided.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -2, 1] )).toEqual( result ); // same as [0, -2, 0] ps.restart(); expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -2, 2] )).toEqual( result ); // same as [0, -2, 0] ps.restart(); expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, -2, -1] )).toEqual( result ); // same as [0, -2, 0] }); it("before the 1st word in the current sentence, it should give the 1st fragment 2 words backward, crossing sentence boundries.", function () { var result = 'back.'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -2, 0] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st word in the current sentence, it should give the 1st fragment 2 words backward, crossing sentence boundries, no matter what later integers are provided.", function () { var result = 'back.'; expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -2, 1] )).toEqual( result ); // same as [0, -2, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -2, 2] )).toEqual( result ); // same as [0, -2, 0] ps.restart(); expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Why,' ); expect( ps.getFragment( [0, -2, -1] )).toEqual( result ); // same as [0, -2, 0] }); }); // --- Fragments --- describe("1 fragment", function () { it("at the very 1st fragment of the 1st word, it should return the 1st word in the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 0, -1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); describe("should give the previous fragment", function () { it("- the 1st fragment when in the 2nd fragment of a three fragment word.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 0, 1] )).toEqual( 'orio-' ); expect( ps.getFragment( [0, 0, -1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("- the 2nd fragment when in the 3rd fragment of a three fragment word.", function () { var result = 'orio-'; expect( ps.getFragment( [0, 0, 2] )).toEqual( 'us,' ); expect( ps.getFragment( [0, 0, -1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); it("before the 1st fragment in the current word, it should give the 1st fragment 1 word backward.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, 0, -1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st fragment in the current word, it should give the 1st fragment 1 word backward, not just always go to the start of the sentence.", function () { var result = 'you'; expect( ps.getFragment( [0, 2, 0] )).toEqual( 'brave' ); expect( ps.getFragment( [0, 0, -1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st fragment in the current sentence, it should give the 1st fragment of the last word 1 sentence backward.", function () { var result = 'flag.'; expect( ps.getFragment( [1, 0, 0] )).toEqual( 'Del-' ); expect( ps.getFragment( [0, 0, -1] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); // Going back multiple fragments is only different within one word with multiple fragments describe("2 fragments", function () { it("at the very 1st fragment of the 1st word, it should return the 1st word in the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("while at the 3rd fragment, should give the 1st fragment.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 0, 2] )).toEqual( 'us,' ); expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); // TODO: Change this to something not crossing sentence boundries? it("while at the 2nd fragment, should give the 1st fragment 1 word backward.", function () { var result = 'flag.'; expect( ps.getFragment( [1, 0, 0] )).toEqual( 'Del-' ); expect( ps.getFragment( [0, 0, 1] )).toEqual( 'irio-' ); expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); // TODO: Change this to something not crossing sentence boundries? it("while at the 1nd fragment in the array as a whole, should give the 1st fragment, not attempting to travel out of the array.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st fragment in the current word, it should give the 1st fragment 1 word backward.", function () { var result = 'Vict-'; expect( ps.getFragment( [0, 1, 0] )).toEqual( 'you' ); expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st fragment in the current word, it should give the 1st fragment 1 word backward, not just always go to the start of the sentence.", function () { var result = 'you'; expect( ps.getFragment( [0, 2, 0] )).toEqual( 'brave' ); expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("before the 1st fragment in the current sentence, it should give the 1st fragment 1 sentence backward.", function () { var result = 'flag.'; expect( ps.getFragment( [1, 0, 0] )).toEqual( 'Del-' ); expect( ps.getFragment( [0, 0, -2] )).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); }); // End backward // --- Index --- describe("and given an index position", function () { describe("of -1 should get the last word", function () { it("when starting at the start of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 0 ) ).toEqual( 'Vict-' ); expect( ps.getFragment( -1 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the middle of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 5 ) ).toEqual( 'I' ); expect( ps.getFragment( -1 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the end of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 11 ) ).toEqual( result ); expect( ps.getFragment( -1 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("of -2 should get the second to last word", function () { it("when starting at the start of the collection.", function () { var result = 'oh'; expect( ps.getFragment( 0 ) ).toEqual( 'Vict-' ); expect( ps.getFragment( -2 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the middle of the collection.", function () { var result = 'oh'; expect( ps.getFragment( 5 ) ).toEqual( 'I' ); expect( ps.getFragment( -2 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the end of the collection.", function () { var result = 'oh'; expect( ps.getFragment( 11 ) ).toEqual( 'watt-' ); expect( ps.getFragment( -2 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("of 0 should get the 1st word", function () { it("even when starting at the start of the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( 0 ) ).toEqual( 'Vict-' ); expect( ps.getFragment( 0 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the middle of the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( 5 ) ).toEqual( 'I' ); expect( ps.getFragment( 0 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the end of the collection.", function () { var result = 'Vict-'; expect( ps.getFragment( 11 ) ).toEqual( 'watt-' ); expect( ps.getFragment( 0 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("of 5 should get the 6th word", function () { it("when starting at the start of the collection.", function () { var result = 'I'; expect( ps.getFragment( 0 ) ).toEqual( 'Vict-' ); expect( ps.getFragment( 5 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the middle of the collection.", function () { var result = 'I'; expect( ps.getFragment( 5 ) ).toEqual( 'I' ); expect( ps.getFragment( 5 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the end of the collection.", function () { var result = 'I'; expect( ps.getFragment( 11 ) ).toEqual( 'watt-' ); expect( ps.getFragment( 5 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); describe("of 11, in the 12 word collection, should get the 12th word", function () { it("when starting at the start of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 0 ) ).toEqual( 'Vict-' ); expect( ps.getFragment( 11 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("when starting at the middle of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 5 ) ).toEqual( 'I' ); expect( ps.getFragment( 11 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); it("even when starting at the end of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 11 ) ).toEqual( 'watt-' ); expect( ps.getFragment( 11 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); // TODO: Discuss behavior - Jump past end, back at start? Error? Undefined...? last word? last fragment? describe("of 100, in the 12 word collection, should get the 12th word", function () { it("when starting at the start of the collection.", function () { var result = 'watt-'; expect( ps.getFragment( 0 ) ).toEqual( 'Vict-' ); expect( ps.getFragment( 100 ) ).toEqual( result ); expect( ps.getFragment( [0, 0, 0] )).toEqual( result ); // retain position }); }); }); // End call `.getFragment()` using an index number // ---- One Fragment ---- // TODO: Reorganize tests to be more like this set describe("that is a one-fragment collection", function () { beforeEach( function () { ps.process( [['Zap']] ); }); it("its `.getIndex()` should return 0.", function () { expect( ps.getIndex() ).toEqual(0) }); it("its `.getLength()` should return 1.", function () { expect( ps.getLength() ).toEqual(1) }); describe("and moved 1 sentence forward", function () { it("should return that word.", function () { expect( ps.getFragment( [1, 0, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([1, 0, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 1 sentence back", function () { it("should return that word.", function () { expect( ps.getFragment( [-1, 0, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([-1, 0, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 3 sentence forward", function () { it("should return that word.", function () { expect( ps.getFragment( [3, 0, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([3, 0, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 3 sentence back", function () { it("should return that word.", function () { expect( ps.getFragment( [-3, 0, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([-3, 0, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 1 word forward", function () { it("should return that word.", function () { expect( ps.getFragment( [0, 1, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 1, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 1 word back", function () { it("should return that word.", function () { expect( ps.getFragment( [0, -1, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, -1, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 3 word forward", function () { it("should return that word.", function () { expect( ps.getFragment( [0, 3, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 3, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 3 word back", function () { it("should return that word.", function () { expect( ps.getFragment( [0, -3, 0] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, -3, 0]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 1 fragment forward", function () { it("should return that word.", function () { expect( ps.getFragment( [0, 0, 1] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 0, 1]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 1 fragment back", function () { it("should return that word.", function () { expect( ps.getFragment( [0, 0, -1] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 0, -1]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 3 fragment forward", function () { it("should return that word.", function () { expect( ps.getFragment( [0, 0, 3] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 0, 3]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and moved 3 fragment back", function () { it("should return that word.", function () { expect( ps.getFragment( [0, 0, -3] )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 0, -3]) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (0) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( 0 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(0) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (1) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( 1 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(1) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (-1) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( -1 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(-1) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (2) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( 2 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(2) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (-2) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( -2 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(-2) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (5) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( 5 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(5) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); describe("and an index (-5) number is used", function () { it("should return that word.", function () { expect( ps.getFragment( -5 )).toEqual( 'Zap' ); }); describe("its", function () { beforeEach(function () { ps.getFragment(-5) }); it("`.getProgress()` should return 1.", function () {expect( ps.getProgress() ).toEqual(1) }); it("`.getRelativeProgress()` should return [1, 1, 1].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1] ) }); }); }); }); // End one-word collection // ---- One Word (multi-fragment) ---- // TODO: Reorganize tests to be more like this set describe("that is a 1 word, 3 fragment collection", function () { beforeEach( function () { ps.setState( {maxNumCharacters: 5} ) ps.process( [['wattlebird?']] ); }); it("its `.getIndex()` should return 0.", function () { expect( ps.getIndex() ).toEqual(0) }); it("its `.getLength()` should return 1.", function () { expect( ps.getLength() ).toEqual(1) }); describe("and moved 1 sentence forward", function () { it("should return the 1st fragment.", function () { expect( ps.getFragment( [1, 0, 0] )).toEqual( 'watt-' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([1, 0, 0]) }); it("`.getProgress()` should return 1/3.", function () {expect( ps.getProgress() ).toEqual(1/3) }); it("`.getRelativeProgress()` should return [1, 1, 1/3].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1/3] ) }); }); }); describe("and moved 1 sentence back", function () { it("should return the 1st fragment.", function () { expect( ps.getFragment( [-1, 0, 0] )).toEqual( 'watt-' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([-1, 0, 0]) }); it("`.getProgress()` should return 1/3.", function () {expect( ps.getProgress() ).toEqual(1/3) }); it("`.getRelativeProgress()` should return [1, 1, 1/3].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1/3] ) }); }); }); describe("and moved 3 sentence forward", function () { it("should return the 1st fragment.", function () { expect( ps.getFragment( [3, 0, 0] )).toEqual( 'watt-' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([3, 0, 0]) }); it("`.getProgress()` should return 1/3.", function () {expect( ps.getProgress() ).toEqual(1/3) }); it("`.getRelativeProgress()` should return [1, 1, 1/3].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1/3] ) }); }); }); describe("and moved 3 sentence back", function () { it("should return the 1st fragment.", function () { expect( ps.getFragment( [-3, 0, 0] )).toEqual( 'watt-' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([-3, 0, 0]) }); it("`.getProgress()` should return 1/3.", function () {expect( ps.getProgress() ).toEqual(1/3) }); it("`.getRelativeProgress()` should return [1, 1, 1/3].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1/3] ) }); }); }); describe("and moved 1 word forward", function () { it("should return the 1st fragment.", function () { expect( ps.getFragment( [0, 1, 0] )).toEqual( 'watt-' ); }); describe("its", function () { beforeEach(function () { ps.getFragment([0, 1, 0]) }); it("`.getProgress()` should return 1/3.", function () {expect( ps.getProgress() ).toEqual(1/3) }); it("`.getRelativeProgress()` should return [1, 1, 1/3].", function () { expect( ps.getRelativeProgress() ).toEqual( [1, 1, 1/3] ) }); }); }); describe("and moved 1 word back", function