Skip to content
This repository was archived by the owner on Jun 15, 2019. It is now read-only.

Commit 57ebdd4

Browse files
committed
test(text-wrap): Got more complicated
The test of `getVisualLength()` got more complicated and was moved to its own file. Also, the value of `maxLineLength` (for subsequent tests) was reduced (120 => 50) to be more difficult.
1 parent 6df4530 commit 57ebdd4

File tree

5 files changed

+68
-57
lines changed

5 files changed

+68
-57
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ jobs:
1111
if: type == push && tag !~ ^v\d+\.\d+\.\d+ && commit_message =~ /^(test|feat|fix|refactor|pref|build|revert)\(/
1212
node_js: "11"
1313
before_script: echo -e "BUILD_DIR = $TRAVIS_BUILD_DIR\nBUILD_ID = $TRAVIS_BUILD_ID\nBUILD_NUMBER = $TRAVIS_BUILD_NUMBER\nBUILD_WEB_URL = $TRAVIS_BUILD_WEB_URL\nCOMMIT = $TRAVIS_COMMIT\nCOMMIT_MESSAGE = $TRAVIS_COMMIT_MESSAGE\nCOMMIT_RANGE = $TRAVIS_COMMIT_RANGE\nDEBUG_MODE = $TRAVIS_DEBUG_MODE\nEVENT_TYPE = $TRAVIS_EVENT_TYPE\nJOB_ID = $TRAVIS_JOB_ID\nJOB_NAME = $TRAVIS_JOB_NAME\nJOB_NUMBER = $TRAVIS_JOB_NUMBER\nJOB_WEB_URL = $TRAVIS_JOB_WEB_URL\nOS_NAME = $TRAVIS_OS_NAME\nOSX_IMAGE = $TRAVIS_OSX_IMAGE\nPULL_REQUEST = $TRAVIS_PULL_REQUEST\nPULL_REQUEST_BRANCH= $TRAVIS_PULL_REQUEST_BRANCH\nPULL_REQUEST_SHA = $TRAVIS_PULL_REQUEST_SHA\nPULL_REQUEST_SLUG = $TRAVIS_PULL_REQUEST_SLUG\nSECURE_ENV_VARS = $TRAVIS_SECURE_ENV_VARS\nSUDO = $TRAVIS_SUDO\nTEST_RESULT = $TRAVIS_TEST_RESULT\nTAG = $TRAVIS_TAG\nBUILD_STAGE_NAME = $TRAVIS_BUILD_STAGE_NAME"
14-
script: npm run test
14+
script: npm run test:prod
1515
- name: "Unit Test (10) + Report Coverage"
1616
if: type == push && tag !~ ^v\d+\.\d+\.\d+ && commit_message =~ /^(test|feat|fix|refactor|pref|build|revert)\(/
1717
node_js: "10"
18-
script: npm run test
18+
script: npm run test:prod
1919
after_success: npx codecov
2020

2121
- stage: "Release"
2222
name: "Build and Release"
2323
if: type == push && tag !~ ^v\d+\.\d+\.\d+ && branch == master && commit_message =~ /^(feat|fix|refactor|pref|build|revert)\(/
24-
script: npm run build
24+
script: npm run build:prod
2525
node_js: lts/*
2626
deploy:
2727
provider: script

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"types": "dist/types/TextWrap.d.ts",
88
"scripts": {
99
"lint": "tslint --project tsconfig.json -t codeFrame 'src/**/*.ts' 'test/**/*.ts'",
10-
"test:dev": "jest",
11-
"test:watch": "jest --watch",
12-
"test": "jest --coverage --no-cache",
10+
"test": "jest --verbose --colors",
11+
"test:watch": "npm run test -- --watch",
12+
"test:prod": "npm run test -- --coverage --no-cache",
1313
"prebuild": "rimraf dist",
14-
"start": "rollup --config --watch",
15-
"build:dev": "rollup --config --environment BUILD:development",
16-
"build": "rollup --config --environment BUILD:production",
14+
"build": "rollup --config",
15+
"build:watch": "npm run build -- --watch",
16+
"build:prod": "npm run build -- --environment BUILD:production",
1717
"tsc": "tsc --module commonjs",
1818
"tsc:watch": "tsc --module commonjs --watch",
1919
"commit": "git-cz",

src/TextWrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class TextWrap implements WrapStyle {
9999
}
100100
}
101101

102-
private getVisualLength(s: string, visualOffset: number = 0): number {
102+
getVisualLength(s: string, visualOffset: number = 0): number {
103103
let a = 0
104104
let b: number
105105
let vLen = 0

test/getVisualLength.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import TextWrapper from '../src/TextWrap'
2+
3+
/**
4+
* @author [S. Mahdi Mir-Ismaili](https://mirismaili.github.io)
5+
* Created on 1398/2/30 (2019/5/20).
6+
*/
7+
8+
const obj = new TextWrapper()
9+
10+
describe('Test `getVisualLength()`:', () => {
11+
it('Check `getVisualLength()`', () => {
12+
const a = 'Hello'
13+
const b = 'world!'
14+
const m = a.length
15+
const n = b.length
16+
17+
expect(obj.getVisualLength(`${a} ${b}`, 15)).toBe(m + 1 + n)
18+
19+
expect(obj.getVisualLength(`\t${a} ${b}`)).toBe(4 + m + 1 + n)
20+
21+
expect(obj.getVisualLength(`${a}\t${b}`, 4)).toBe(m + 3 + n)
22+
23+
expect(obj.getVisualLength(`${a} \t${b}`, 9)).toBe(m + 1 + 1 + n)
24+
25+
expect(obj.getVisualLength(`\t${a}\t\t${b}`, 8)).toBe(4 + m + 3 + 4 + n)
26+
27+
expect(obj.getVisualLength(`\t\t${a}\t${b}\t`)).toBe(4 + 4 + m + 3 + n + 2)
28+
29+
expect(obj.getVisualLength(`\t${a}\t${b}\t\t`, 1)).toBe(3 + m + 3 + n + 2 + 4)
30+
31+
expect(obj.getVisualLength(`\t${a}\t${b}\t\t\t`, 2)).toBe(2 + m + 3 + n + 2 + 4 + 4)
32+
33+
expect(obj.getVisualLength(`\t\t${a}\t\t${b}\t`, 3)).toBe(1 + 4 + m + 3 + 4 + n + 2)
34+
35+
expect(obj.getVisualLength(`\t\t${a}\t${b}\t\t`, 4)).toBe(4 + 4 + m + 3 + n + 2 + 4)
36+
37+
expect(obj.getVisualLength(`\t\t\t${a}\t${b}\t\t`, 11)).toBe(1 + 4 + 4 + m + 3 + n + 2 + 4)
38+
39+
expect(obj.getVisualLength(`\t\t\t${a}\t\t${b}\t\t`, 12)).toBe(4 + 4 + 4 + m + 3 + 4 + n + 2 + 4)
40+
41+
expect(obj.getVisualLength(`\t${a}\t\t\t\t${b}\t`, 13)).toBe(3 + m + 3 + 4 + 4 + 4 + n + 2)
42+
43+
expect(obj.getVisualLength(`\t\t${a}\t\t${b}\t\t`, 14)).toBe(2 + 4 + m + 3 + 4 + n + 2 + 4)
44+
})
45+
})

test/text-wrap.test.ts

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,27 @@
11
import * as fs from 'fs'
22
import {sha256} from 'js-sha256'
33
import 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

4617
const indents = ''
47-
const maxLineLength = 120
18+
const maxLineLength = 50
4819
const textWrapper = new TextWrapper({wrapOn: maxLineLength})
4920
const wrapResult = textWrapper.wrap(input, indents)
5021
const output = wrapResult.wrappedText
51-
//*********************************************************/
5222

5323
const joinedInput = input.replace(/\n/g, '')
5424
const joinedOutput = output.replace(/\n/g, '')
55-
//*********************************************************/
5625

5726
describe('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

10773
describe('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

Comments
 (0)