precis-js
Version:
A JavaScript implementation of RFC 7564 (The PRECIS Framework).
73 lines (50 loc) • 2.58 kB
text/coffeescript
fs = require 'fs'
UnicodeTrie = require 'unicode-trie'
{ucs2} = require 'punycode'
CodepointPropertyReader = require '../../../src/unicode/CodepointPropertyReader'
EmptyStringError = require '../../../src/error/EmptyStringError'
NicknameProfile = require '../../../src/profile/NicknameProfile'
precis = require '../../../src/constants'
describe 'NicknameProfile', ->
before ->
data = fs.readFileSync __dirname + '/../../../data/properties.trie'
= new UnicodeTrie data
beforeEach ->
= new NicknameProfile()
= propertyReader: new CodepointPropertyReader
it 'has the correct properties', ->
assert.strictEqual .stringClass, precis.STRING_CLASS.FREEFORM
assert.strictEqual .widthMapping, precis.WIDTH_MAPPING.NONE
assert.strictEqual .caseMapping, precis.CASE_MAPPING.LOWERCASE
assert.strictEqual .normalization, precis.NORMALIZATION.KC
assert.strictEqual .directionality, precis.DIRECTIONALITY.NONE
describe 'map()', ->
it 'right-trims strings', ->
codepoints = ucs2.decode 'ab '
.map codepoints,
assert.strictEqual ucs2.encode(codepoints), 'ab'
it 'left-trims strings', ->
codepoints = ucs2.decode ' ab'
.map codepoints,
assert.strictEqual ucs2.encode(codepoints), 'ab'
it 'trims strings', ->
codepoints = ucs2.decode ' ab '
.map codepoints,
assert.strictEqual ucs2.encode(codepoints), 'ab'
it 'collapses inner whitespace', ->
codepoints = ucs2.decode 'ab cd ef'
.map codepoints,
assert.strictEqual ucs2.encode(codepoints), 'ab cd ef'
it 'collapses inner whitespace and trims at the same time', ->
codepoints = ucs2.decode ' ab cd ef '
.map codepoints,
assert.strictEqual ucs2.encode(codepoints), 'ab cd ef'
it 'maps non-ASCII spaces to ASCII spaces', ->
codepoints = ucs2.decode '\u3000ab\u3000cd\u3000ef\u3000'
.map codepoints,
assert.strictEqual ucs2.encode(codepoints), 'ab cd ef'
describe 'validate()', ->
it 'allows non-empty strings', ->
assert.doesNotThrow => .validate [0]
it 'rejects empty strings', ->
assert.throws (=> .validate []), EmptyStringError