Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests code improvements #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Raphael Pigulla [@pigulla](https://github.com/pigulla)
rebeccapeltz [@rebeccapeltz](https://github.com/rebeccapeltz)
Stefan Tingström [@stingstrom](https://github.com/stingstrom)
Tim Gates [@timgates42](https://github.com/timgates42)
Uladzislau Rastarhuyeu [@rastorc3v](https://github.com/rastorc3v)
180 changes: 90 additions & 90 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,111 @@

'use strict'

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

describe('sprintfjs', function() {
var pi = 3.141592653589793
describe('sprintfjs', () => {
const pi = Math.PI

it('should return formated strings for simple placeholders', function() {
assert.equal('%', sprintf('%%'))
assert.equal('10', sprintf('%b', 2))
assert.equal('A', sprintf('%c', 65))
assert.equal('2', sprintf('%d', 2))
assert.equal('2', sprintf('%i', 2))
assert.equal('2', sprintf('%d', '2'))
assert.equal('2', sprintf('%i', '2'))
assert.equal('{"foo":"bar"}', sprintf('%j', {foo: 'bar'}))
assert.equal('["foo","bar"]', sprintf('%j', ['foo', 'bar']))
assert.equal('2e+0', sprintf('%e', 2))
assert.equal('2', sprintf('%u', 2))
assert.equal('4294967294', sprintf('%u', -2))
assert.equal('2.2', sprintf('%f', 2.2))
assert.equal('3.141592653589793', sprintf('%g', pi))
assert.equal('10', sprintf('%o', 8))
assert.equal('37777777770', sprintf('%o', -8))
assert.equal('%s', sprintf('%s', '%s'))
assert.equal('ff', sprintf('%x', 255))
assert.equal('ffffff01', sprintf('%x', -255))
assert.equal('FF', sprintf('%X', 255))
assert.equal('FFFFFF01', sprintf('%X', -255))
assert.equal('Polly wants a cracker', sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants'))
assert.equal('Hello world!', sprintf('Hello %(who)s!', {who: 'world'}))
assert.equal('true', sprintf('%t', true))
assert.equal('t', sprintf('%.1t', true))
assert.equal('true', sprintf('%t', 'true'))
assert.equal('true', sprintf('%t', 1))
assert.equal('false', sprintf('%t', false))
assert.equal('f', sprintf('%.1t', false))
assert.equal('false', sprintf('%t', ''))
assert.equal('false', sprintf('%t', 0))
it('should return formated strings for simple placeholders', () => {
assert.strictEqual('%', sprintf('%%'))
assert.strictEqual('10', sprintf('%b', 2))
assert.strictEqual('A', sprintf('%c', 65))
assert.strictEqual('2', sprintf('%d', 2))
assert.strictEqual('2', sprintf('%i', 2))
assert.strictEqual('2', sprintf('%d', '2'))
assert.strictEqual('2', sprintf('%i', '2'))
assert.strictEqual('{"foo":"bar"}', sprintf('%j', {foo: 'bar'}))
assert.strictEqual('["foo","bar"]', sprintf('%j', ['foo', 'bar']))
assert.strictEqual('2e+0', sprintf('%e', 2))
assert.strictEqual('2', sprintf('%u', 2))
assert.strictEqual('4294967294', sprintf('%u', -2))
assert.strictEqual('2.2', sprintf('%f', 2.2))
assert.strictEqual('3.141592653589793', sprintf('%g', pi))
assert.strictEqual('10', sprintf('%o', 8))
assert.strictEqual('37777777770', sprintf('%o', -8))
assert.strictEqual('%s', sprintf('%s', '%s'))
assert.strictEqual('ff', sprintf('%x', 255))
assert.strictEqual('ffffff01', sprintf('%x', -255))
assert.strictEqual('FF', sprintf('%X', 255))
assert.strictEqual('FFFFFF01', sprintf('%X', -255))
assert.strictEqual('Polly wants a cracker', sprintf('%2$s %3$s a %1$s', 'cracker', 'Polly', 'wants'))
assert.strictEqual('Hello world!', sprintf('Hello %(who)s!', {who: 'world'}))
assert.strictEqual('true', sprintf('%t', true))
assert.strictEqual('t', sprintf('%.1t', true))
assert.strictEqual('true', sprintf('%t', 'true'))
assert.strictEqual('true', sprintf('%t', 1))
assert.strictEqual('false', sprintf('%t', false))
assert.strictEqual('f', sprintf('%.1t', false))
assert.strictEqual('false', sprintf('%t', ''))
assert.strictEqual('false', sprintf('%t', 0))

assert.equal('undefined', sprintf('%T', undefined))
assert.equal('null', sprintf('%T', null))
assert.equal('boolean', sprintf('%T', true))
assert.equal('number', sprintf('%T', 42))
assert.equal('string', sprintf('%T', 'This is a string'))
assert.equal('function', sprintf('%T', Math.log))
assert.equal('array', sprintf('%T', [1, 2, 3]))
assert.equal('object', sprintf('%T', {foo: 'bar'}))
assert.equal('regexp', sprintf('%T', /<('[^']*'|'[^']*'|[^''>])*>/))
assert.strictEqual('undefined', sprintf('%T', undefined))
assert.strictEqual('null', sprintf('%T', null))
assert.strictEqual('boolean', sprintf('%T', true))
assert.strictEqual('number', sprintf('%T', 42))
assert.strictEqual('string', sprintf('%T', 'This is a string'))
assert.strictEqual('function', sprintf('%T', Math.log))
assert.strictEqual('array', sprintf('%T', [1, 2, 3]))
assert.strictEqual('object', sprintf('%T', {foo: 'bar'}))
assert.strictEqual('regexp', sprintf('%T', /<('[^']*'|'[^']*'|[^''>])*>/))

assert.equal('true', sprintf('%v', true))
assert.equal('42', sprintf('%v', 42))
assert.equal('This is a string', sprintf('%v', 'This is a string'))
assert.equal('1,2,3', sprintf('%v', [1, 2, 3]))
assert.equal('[object Object]', sprintf('%v', {foo: 'bar'}))
assert.equal('/<("[^"]*"|\'[^\']*\'|[^\'">])*>/', sprintf('%v', /<("[^"]*"|'[^']*'|[^'">])*>/))
assert.strictEqual('true', sprintf('%v', true))
assert.strictEqual('42', sprintf('%v', 42))
assert.strictEqual('This is a string', sprintf('%v', 'This is a string'))
assert.strictEqual('1,2,3', sprintf('%v', [1, 2, 3]))
assert.strictEqual('[object Object]', sprintf('%v', {foo: 'bar'}))
assert.strictEqual('/<("[^"]*"|\'[^\']*\'|[^\'">])*>/', sprintf('%v', /<("[^"]*"|'[^']*'|[^'">])*>/))
})

it('should return formated strings for complex placeholders', function() {
it('should return formatted strings for complex placeholders', () => {
// sign
assert.equal('2', sprintf('%d', 2))
assert.equal('-2', sprintf('%d', -2))
assert.equal('+2', sprintf('%+d', 2))
assert.equal('-2', sprintf('%+d', -2))
assert.equal('2', sprintf('%i', 2))
assert.equal('-2', sprintf('%i', -2))
assert.equal('+2', sprintf('%+i', 2))
assert.equal('-2', sprintf('%+i', -2))
assert.equal('2.2', sprintf('%f', 2.2))
assert.equal('-2.2', sprintf('%f', -2.2))
assert.equal('+2.2', sprintf('%+f', 2.2))
assert.equal('-2.2', sprintf('%+f', -2.2))
assert.equal('-2.3', sprintf('%+.1f', -2.34))
assert.equal('-0.0', sprintf('%+.1f', -0.01))
assert.equal('3.14159', sprintf('%.6g', pi))
assert.equal('3.14', sprintf('%.3g', pi))
assert.equal('3', sprintf('%.1g', pi))
assert.equal('-000000123', sprintf('%+010d', -123))
assert.equal('______-123', sprintf("%+'_10d", -123))
assert.equal('-234.34 123.2', sprintf('%f %f', -234.34, 123.2))
assert.strictEqual('2', sprintf('%d', 2))
assert.strictEqual('-2', sprintf('%d', -2))
assert.strictEqual('+2', sprintf('%+d', 2))
assert.strictEqual('-2', sprintf('%+d', -2))
assert.strictEqual('2', sprintf('%i', 2))
assert.strictEqual('-2', sprintf('%i', -2))
assert.strictEqual('+2', sprintf('%+i', 2))
assert.strictEqual('-2', sprintf('%+i', -2))
assert.strictEqual('2.2', sprintf('%f', 2.2))
assert.strictEqual('-2.2', sprintf('%f', -2.2))
assert.strictEqual('+2.2', sprintf('%+f', 2.2))
assert.strictEqual('-2.2', sprintf('%+f', -2.2))
assert.strictEqual('-2.3', sprintf('%+.1f', -2.34))
assert.strictEqual('-0.0', sprintf('%+.1f', -0.01))
assert.strictEqual('3.14159', sprintf('%.6g', pi))
assert.strictEqual('3.14', sprintf('%.3g', pi))
assert.strictEqual('3', sprintf('%.1g', pi))
assert.strictEqual('-000000123', sprintf('%+010d', -123))
assert.strictEqual('______-123', sprintf("%+'_10d", -123))
assert.strictEqual('-234.34 123.2', sprintf('%f %f', -234.34, 123.2))

// padding
assert.equal('-0002', sprintf('%05d', -2))
assert.equal('-0002', sprintf('%05i', -2))
assert.equal(' <', sprintf('%5s', '<'))
assert.equal('0000<', sprintf('%05s', '<'))
assert.equal('____<', sprintf("%'_5s", '<'))
assert.equal('> ', sprintf('%-5s', '>'))
assert.equal('>0000', sprintf('%0-5s', '>'))
assert.equal('>____', sprintf("%'_-5s", '>'))
assert.equal('xxxxxx', sprintf('%5s', 'xxxxxx'))
assert.equal('1234', sprintf('%02u', 1234))
assert.equal(' -10.235', sprintf('%8.3f', -10.23456))
assert.equal('-12.34 xxx', sprintf('%f %s', -12.34, 'xxx'))
assert.equal('{\n "foo": "bar"\n}', sprintf('%2j', {foo: 'bar'}))
assert.equal('[\n "foo",\n "bar"\n]', sprintf('%2j', ['foo', 'bar']))
assert.strictEqual('-0002', sprintf('%05d', -2))
assert.strictEqual('-0002', sprintf('%05i', -2))
assert.strictEqual(' <', sprintf('%5s', '<'))
assert.strictEqual('0000<', sprintf('%05s', '<'))
assert.strictEqual('____<', sprintf("%'_5s", '<'))
assert.strictEqual('> ', sprintf('%-5s', '>'))
assert.strictEqual('>0000', sprintf('%0-5s', '>'))
assert.strictEqual('>____', sprintf("%'_-5s", '>'))
assert.strictEqual('xxxxxx', sprintf('%5s', 'xxxxxx'))
assert.strictEqual('1234', sprintf('%02u', 1234))
assert.strictEqual(' -10.235', sprintf('%8.3f', -10.23456))
assert.strictEqual('-12.34 xxx', sprintf('%f %s', -12.34, 'xxx'))
assert.strictEqual('{\n "foo": "bar"\n}', sprintf('%2j', {foo: 'bar'}))
assert.strictEqual('[\n "foo",\n "bar"\n]', sprintf('%2j', ['foo', 'bar']))

// precision
assert.equal('2.3', sprintf('%.1f', 2.345))
assert.equal('xxxxx', sprintf('%5.5s', 'xxxxxx'))
assert.equal(' x', sprintf('%5.1s', 'xxxxxx'))
assert.strictEqual('2.3', sprintf('%.1f', 2.345))
assert.strictEqual('xxxxx', sprintf('%5.5s', 'xxxxxx'))
assert.strictEqual(' x', sprintf('%5.1s', 'xxxxxx'))

})

it('should return formated strings for callbacks', function() {
assert.equal('foobar', sprintf('%s', function() { return 'foobar' }))
it('should return formatted strings for callbacks', () => {
assert.strictEqual('foobar', sprintf('%s', () => 'foobar'))
})
})
38 changes: 18 additions & 20 deletions test/test_validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

'use strict'

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

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

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

describe('sprintfjs cache', function() {
describe('sprintfjs cache', () => {

it('should not throw Error (cache consistency)', function() {
it('should not throw Error (cache consistency)', () => {
// redefine object properties to ensure that is not affect to the cache
sprintf('hasOwnProperty')
sprintf('constructor')
Expand All @@ -26,9 +24,9 @@ describe('sprintfjs cache', function() {
})
})

describe('sprintfjs', function() {
describe('sprintfjs', () => {

it('should throw SyntaxError for placeholders', function() {
it('should throw SyntaxError for placeholders', () => {
should_throw('%', [], SyntaxError)
should_throw('%A', [], SyntaxError)
should_throw('%s%', [], SyntaxError)
Expand All @@ -39,17 +37,17 @@ describe('sprintfjs', function() {
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() {
const numeric = 'bcdiefguxX'.split('')
numeric.forEach((specifier) => {
const fmt = sprintf('%%%s',specifier)
it(fmt + ' should throw TypeError for invalid numbers', () => {
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() {
it(fmt + ' should not throw TypeError for something implicitly castable to number', () => {
should_not_throw(fmt, [1/0])
should_not_throw(fmt, [true])
should_not_throw(fmt, [[1]])
Expand All @@ -58,26 +56,26 @@ describe('sprintfjs', function() {
})
})

it('should not throw Error for expression which evaluates to undefined', function() {
it('should not throw Error for expression which evaluates to undefined', () => {
should_not_throw("%(x.y)s", {x : {}})
})

it('should throw own Error when expression evaluation would raise TypeError', function() {
var fmt = "%(x.y)s"
it('should throw own Error when expression evaluation would raise TypeError', () => {
const fmt = "%(x.y)s"
try {
sprintf(fmt, {})
} catch (e) {
assert(e.message.indexOf('[sprintf]') !== -1)
}
})

it('should not throw when accessing properties on the prototype', function() {
it('should not throw when accessing properties on the prototype', () => {
function C() { }
C.prototype = {
get x() { return 2 },
set y(v) { /*Noop */}
}
var c = new C()
const c = new C()
should_not_throw("%(x)s", c)
should_not_throw("%(y)s", c)
})
Expand Down