11import * as fs from 'fs'
22import { sha256 } from 'js-sha256'
33import TextWrapper from '../src/TextWrap'
4- //*********************************************************/
4+ // import TextWrapper3 from '../dist/bundle.esm.js'
5+ //const TextWrapper2 = require('../dist/main.umd.js').default
56
6- describe ( 'Test `getVisualLength()`:' , ( ) => {
7- it ( 'Check `getVisualLength()`' , ( ) => {
8- const obj = new TextWrapper ( )
9-
10- const a = 'Hello'
11- const b = 'world!'
12-
13- // @ts -ignore
14- expect ( obj . getVisualLength ( `\t${ a } ${ b } ` ) ) . toBe ( 4 + a . length + 1 + b . length )
15- // @ts -ignore
16- expect ( obj . getVisualLength ( `${ a } \t${ b } ` , 4 ) ) . toBe ( a . length + 3 + b . length )
17- // @ts -ignore
18- expect ( obj . getVisualLength ( `${ a } \t${ b } ` , 9 ) ) . toBe ( a . length + 1 + 1 + b . length )
19- // @ts -ignore
20- expect ( obj . getVisualLength ( `\t${ a } \t${ b } ` , 8 ) ) . toBe ( 4 + a . length + 3 + b . length )
21- // @ts -ignore
22- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` ) ) . toBe ( 4 + a . length + 3 + b . length + 2 )
23- // @ts -ignore
24- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 1 ) ) . toBe ( 3 + a . length + 3 + b . length + 2 )
25- // @ts -ignore
26- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 2 ) ) . toBe ( 2 + a . length + 3 + b . length + 2 )
27- // @ts -ignore
28- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 3 ) ) . toBe ( 1 + a . length + 3 + b . length + 2 )
29- // @ts -ignore
30- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 4 ) ) . toBe ( 4 + a . length + 3 + b . length + 2 )
31- // @ts -ignore
32- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 11 ) ) . toBe ( 1 + a . length + 3 + b . length + 2 )
33- // @ts -ignore
34- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 12 ) ) . toBe ( 4 + a . length + 3 + b . length + 2 )
35- // @ts -ignore
36- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 13 ) ) . toBe ( 3 + a . length + 3 + b . length + 2 )
37- // @ts -ignore
38- expect ( obj . getVisualLength ( `\t${ a } \t${ b } \t` , 14 ) ) . toBe ( 2 + a . length + 3 + b . length + 2 )
39- } )
40- } )
41- //*********************************************************/
7+ /**
8+ * @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
9+ * Created on 1398/2/13 (2019/5/3).
10+ */
11+
12+ const inputExpectedHash = '117677f3e12ded864c527d4f03583d4dd0be3cc0542c3cbbdbb01574dcf280c8'
13+ const outputExpectedHash = '2e1bd0f9ae5b0ee9406908f58bd5b4030bbcdf464e5462e0fd1b142d49dbac2d'
4214
43- const input =
44- fs . readFileSync ( './test/input.txt' , 'utf8' ) . replace ( / (?: \r \n | \r ) / g, '\n' )
15+ const input = fs . readFileSync ( './test/input.txt' , 'utf8' ) . replace ( / \r \n | \r / g, '\n' )
4516
4617const indents = ''
47- const maxLineLength = 120
18+ const maxLineLength = 50
4819const textWrapper = new TextWrapper ( { wrapOn : maxLineLength } )
4920const wrapResult = textWrapper . wrap ( input , indents )
5021const output = wrapResult . wrappedText
51- //*********************************************************/
5222
5323const joinedInput = input . replace ( / \n / g, '' )
5424const joinedOutput = output . replace ( / \n / g, '' )
55- //*********************************************************/
5625
5726describe ( 'General tests:' , ( ) => {
5827 it ( 'Check output itself' , ( ) => expect ( joinedOutput ) . toBe ( joinedInput ) )
@@ -63,14 +32,12 @@ describe('General tests:', () => {
6332 // Two markers which [the distance between the first marker and the first breakable character after the second
6433 // marker] is less than or equal with [maxLineLength]
6534 ( ) => {
66- // noinspection JSUnusedAssignment
6735 let a = 0
6836 for ( let b of wrapResult . markers ) {
6937 const regexp = / [ ^ \S \xA0 ] / g
7038 regexp . lastIndex = b
7139
7240 const upBound = regexp . test ( input ) ? regexp . lastIndex - 1 : input . length
73- // @ts -ignore
7441 const distance = textWrapper . getVisualLength ( input . slice ( a , upBound ) , 0 )
7542
7643 expect ( distance ) . toBeGreaterThan ( maxLineLength )
@@ -101,14 +68,13 @@ describe('General tests:', () => {
10168
10269 expect ( anotherOutput ) . toBe ( output )
10370 } )
104- //*********************************************************/
10571} )
10672
10773describe ( 'Case-specific tests:' , ( ) => {
10874 it ( "Check input's hash" , ( ) =>
109- expect ( sha256 ( input ) ) . toBe ( '117677f3e12ded864c527d4f03583d4dd0be3cc0542c3cbbdbb01574dcf280c8' )
75+ expect ( sha256 ( input ) ) . toBe ( inputExpectedHash )
11076 )
11177 it ( "Check output's hash" , ( ) =>
112- expect ( sha256 ( output ) ) . toBe ( '96673830a9ed2cf2de55e9442dc17540b1ae0cf497d62c54d8268ec54352f23a' )
78+ expect ( sha256 ( output ) ) . toBe ( outputExpectedHash )
11379 )
11480} )
0 commit comments