precis-js
Version:
A JavaScript implementation of RFC 7564 (The PRECIS Framework).
77 lines (57 loc) • 3.62 kB
text/coffeescript
fs = require 'fs'
UnicodeTrie = require 'unicode-trie'
CodepointPropertyReader = require '../../src/unicode/CodepointPropertyReader'
InvalidCodepointError = require '../../src/error/InvalidCodepointError'
precis = require '../../src/constants'
PrecisPreparer = require '../../src/PrecisPreparer'
WidthMapper = require '../../src/unicode/WidthMapper'
describe 'PrecisPreparer', ->
before ->
trieData = fs.readFileSync __dirname + '/../../data/properties.trie'
widthMappingData = JSON.parse fs.readFileSync __dirname + '/../../data/width-mapping.json'
= new UnicodeTrie trieData
= new CodepointPropertyReader
= new WidthMapper widthMappingData
beforeEach ->
= new PrecisPreparer ,
describe 'prepare()', ->
it 'supports custom prepare logic', ->
= prepare: sinon.spy()
.prepare , 'ab'
sinon.assert.calledWith .prepare, 'ab',
it 'supports custom pre-prepare mapping logic', ->
passedCodepoints = [-1, -1]
=
stringClass: precis.STRING_CLASS.FREEFORM
prePrepareMap: (codepoints) ->
passedCodepoints[0] = codepoints[0]
passedCodepoints[1] = codepoints[1]
codepoints[0] = 111
codepoints[1] = 222
assert.deepEqual .prepare(, 'ab'), [111, 222]
assert.deepEqual passedCodepoints, [97, 98]
it 'throws an error if the string class is not implemented', ->
assert.throws (=> .prepare stringClass: 111, ''), 'PRECIS string class not implemented.'
describe 'for FreeformClass string class profiles', ->
beforeEach ->
= stringClass: precis.STRING_CLASS.FREEFORM
it 'allows characters in the FreeformClass string class', ->
assert.deepEqual .prepare(, ' !'), [0x0020, 0x0021]
it 'rejects characters outside the FreeformClass string class', ->
assert.throws (=> .prepare , "\u0000"), InvalidCodepointError
assert.throws (=> .prepare , "\u007F"), InvalidCodepointError
assert.throws (=> .prepare , "\u00B7"), InvalidCodepointError
assert.throws (=> .prepare , "\u0378"), InvalidCodepointError
assert.throws (=> .prepare , "\u200C"), InvalidCodepointError
describe 'for IdentifierClass string class profiles', ->
beforeEach ->
= stringClass: precis.STRING_CLASS.IDENTIFIER
it 'allows characters in the IdentifierClass string class', ->
assert.deepEqual .prepare(, '!'), [0x0021]
it 'rejects characters outside the IdentifierClass string class', ->
assert.throws (=> .prepare , "\u0000"), InvalidCodepointError
assert.throws (=> .prepare , "\u0020"), InvalidCodepointError
assert.throws (=> .prepare , "\u007F"), InvalidCodepointError
assert.throws (=> .prepare , "\u00B7"), InvalidCodepointError
assert.throws (=> .prepare , "\u0378"), InvalidCodepointError
assert.throws (=> .prepare , "\u200C"), InvalidCodepointError