precis-js
Version:
A JavaScript implementation of RFC 7564 (The PRECIS Framework).
101 lines (63 loc) • 3.52 kB
text/coffeescript
{ucs2} = require 'punycode'
Normalizer = require '../../../src/unicode/Normalizer'
precis = require '../../../src/constants'
describe 'Normalizer', ->
beforeEach ->
= String.prototype.normalize
= sinon.spyObject 'unorm', ['nfc', 'nfd', 'nfkc', 'nfkd']
= [97, 98]
afterEach ->
String.prototype.normalize = if ?
it 'throws an error if no normalizer is available', ->
delete String.prototype.normalize
= new Normalizer()
assert.throws (=> .normalize precis.NORMALIZATION.C, .slice()),
'No Unicode normalizer available.'
describe 'with ES6', ->
beforeEach ->
String.prototype.normalize = (form) -> "#{@ + form}"
= new Normalizer
describe 'normalize()', ->
it 'supports NONE normalization', ->
actual = .normalize precis.NORMALIZATION.NONE, .slice()
assert.deepEqual actual,
it 'supports C normalization', ->
actual = .normalize precis.NORMALIZATION.C, .slice()
assert.deepEqual actual, ucs2.decode 'abNFC'
it 'supports D normalization', ->
actual = .normalize precis.NORMALIZATION.D, .slice()
assert.deepEqual actual, ucs2.decode 'abNFD'
it 'supports KC normalization', ->
actual = .normalize precis.NORMALIZATION.KC, .slice()
assert.deepEqual actual, ucs2.decode 'abNFKC'
it 'supports KD normalization', ->
actual = .normalize precis.NORMALIZATION.KD, .slice()
assert.deepEqual actual, ucs2.decode 'abNFKD'
it 'rejects unsupported normalization forms', ->
assert.throws (=> .normalize 111, []), 'Normalization form not implemented.'
describe 'without ES6', ->
beforeEach ->
delete String.prototype.normalize
= new Normalizer
describe 'normalize()', ->
it 'supports NONE normalization', ->
actual = .normalize precis.NORMALIZATION.NONE, .slice()
assert.deepEqual actual,
it 'supports C normalization', ->
.nfc.returns 'cd'
actual = .normalize precis.NORMALIZATION.C, .slice()
assert.deepEqual actual, [99, 100]
it 'supports D normalization', ->
.nfd.returns 'cd'
actual = .normalize precis.NORMALIZATION.D, .slice()
assert.deepEqual actual, [99, 100]
it 'supports KC normalization', ->
.nfkc.returns 'cd'
actual = .normalize precis.NORMALIZATION.KC, .slice()
assert.deepEqual actual, [99, 100]
it 'supports KD normalization', ->
.nfkd.returns 'cd'
actual = .normalize precis.NORMALIZATION.KD, .slice()
assert.deepEqual actual, [99, 100]
it 'rejects unsupported normalization forms', ->
assert.throws (=> .normalize 111, []), 'Normalization form not implemented.'