This repository has been archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
60 lines (51 loc) · 1.7 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict'
const path = require('path')
const fs = require('fs')
const { toBeWithin } = require('@coderbyheart/jest-expect-tobewithin')
const { PDFKitTextMeasurer } = require('gh-badges/lib/text-measurer')
const { createConsumer } = require('.')
const { verdanaWidths } = require('./test-fixtures')
expect.extend({ toBeWithin })
const consumer = createConsumer(verdanaWidths)
it('Basic test', () => {
expect(consumer.widthOf('m')).toBe(106.99)
})
const fontPath = path.join(__dirname, 'Verdana.ttf')
const haveFont = fs.existsSync(fontPath)
const maybeDescribe = haveFont ? describe : describe.skip
maybeDescribe('Parity with PDFKitTextMeasurer', () => {
let measurer
beforeAll(() => {
if (haveFont) {
measurer = new PDFKitTextMeasurer(fontPath)
}
})
describe('ASCII strings, with a fairly small tolerance', () => {
const EPSILON_PIXELS = 0.75
test.each([
'This is the dawning of the Age of Aquariums',
'v1.2.511',
'5 passed, 2 failed, 1 skipped',
'[prismic "1.1"]',
])('%s', str => {
// PDFKitTextMeasurer uses 12 pt Verdana; the test data is for 120 pt.
const expected = 10.0 * measurer.widthOf(str)
const actual = consumer.widthOf(str)
expect(actual).toBeWithin(
expected - EPSILON_PIXELS,
expected + EPSILON_PIXELS
)
})
})
describe('Non-ASCII strings, with a very large tolerance', () => {
const EPSILON_PIXELS = 50.0
test.each(['爱', '★★★½☆', '\u2026'])('%s', str => {
const expected = 10.0 * measurer.widthOf(str)
const actual = consumer.widthOf(str)
expect(actual).toBeWithin(
expected - EPSILON_PIXELS,
expected + EPSILON_PIXELS
)
})
})
})