Sha256: b454464c1f6665436109ea3bf4b4d90d2aff7b5a941e75fc9c7b8b60b3b8c671

Contents?: true

Size: 1.92 KB

Versions: 19

Compression:

Stored size: 1.92 KB

Contents

/* global describe, it */

'use strict'

var assert = require('assert'),
    sprintfjs = require('../src/sprintf.js'),
    sprintf = sprintfjs.sprintf,
    vsprintf = sprintfjs.vsprintf

function should_throw(format,args,err) {
    assert.throws(function() { vsprintf(format,args) }, err)
}

function should_not_throw(format,args) {
    assert.doesNotThrow(function() { vsprintf(format,args) })
}

describe('sprintfjs cache', function() {

    it('should not throw Error (cache consistency)', function() {
        // redefine object properties to ensure that is not affect to the cache
        sprintf('hasOwnProperty')
        sprintf('constructor')
        should_not_throw('%s', ['caching...'])
        should_not_throw('%s', ['crash?'])
    })
})

describe('sprintfjs', function() {

    it('should throw SyntaxError for placeholders', function() {
        should_throw('%', [], SyntaxError)
        should_throw('%A', [], SyntaxError)
        should_throw('%s%', [], SyntaxError)
        should_throw('%(s', [], SyntaxError)
        should_throw('%)s', [], SyntaxError)
        should_throw('%$s', [], SyntaxError)
        should_throw('%()s', [], SyntaxError)
        should_throw('%(12)s', [], SyntaxError)
    })

    var numeric = 'bcdiefguxX'.split('')
    numeric.forEach(function(specifier) {
        var fmt = sprintf('%%%s',specifier)
        it(fmt + ' should throw TypeError for invalid numbers', function() {
            should_throw(fmt, [], TypeError)
            should_throw(fmt, ['str'], TypeError)
            should_throw(fmt, [{}], TypeError)
            should_throw(fmt, ['s'], TypeError)
        })

        it(fmt + ' should not throw TypeError for something implicitly castable to number', function() {
            should_not_throw(fmt, [1/0])
            should_not_throw(fmt, [true])
            should_not_throw(fmt, [[1]])
            should_not_throw(fmt, ['200'])
            should_not_throw(fmt, [null])
        })
    })
})

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ela-4.1.6 node_modules/sprintf-js/test/test_validation.js
ela-4.1.5 node_modules/sprintf-js/test/test_validation.js
ela-4.1.4 node_modules/sprintf-js/test/test_validation.js
ela-4.1.3 node_modules/sprintf-js/test/test_validation.js
ela-4.1.2 node_modules/sprintf-js/test/test_validation.js
ela-4.1.1 node_modules/sprintf-js/test/test_validation.js
ela-4.1.0 node_modules/sprintf-js/test/test_validation.js
ela-4.0.0 node_modules/sprintf-js/test/test_validation.js
ela-3.4.3 node_modules/sprintf-js/test/test_validation.js
ela-3.4.2 node_modules/sprintf-js/test/test_validation.js
ela-3.4.0 node_modules/sprintf-js/test/test_validation.js
ela-3.3.1 node_modules/sprintf-js/test/test_validation.js
ela-3.3.0 node_modules/sprintf-js/test/test_validation.js
ela-3.2.0 node_modules/sprintf-js/test/test_validation.js
ela-3.1.1 node_modules/sprintf-js/test/test_validation.js
ela-3.1.0 node_modules/sprintf-js/test/test_validation.js
ela-3.0.0 node_modules/sprintf-js/test/test_validation.js
ela-2.0.0 node_modules/sprintf-js/test/test_validation.js
ela-1.1.0 node_modules/sprintf-js/test/test_validation.js