From dc34f357a8de5bcd783941eca441f2759a28545a Mon Sep 17 00:00:00 2001 From: Uladzislau Rastargueu Date: Thu, 2 Feb 2023 23:16:41 +0100 Subject: [PATCH 1/2] fix: tests code improvements --- CONTRIBUTORS.md | 1 + test/test.js | 176 ++++++++++++++++++++-------------------- test/test_validation.js | 38 ++++----- 3 files changed, 107 insertions(+), 108 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a16608e..70c7dba 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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) diff --git a/test/test.js b/test/test.js index 9588da6..42ad9a6 100644 --- a/test/test.js +++ b/test/test.js @@ -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 + 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)) + 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', function() { // 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', function() { + assert.strictEqual('foobar', sprintf('%s', function() { return 'foobar' })) }) }) diff --git a/test/test_validation.js b/test/test_validation.js index 93b97cf..ffe1d64 100644 --- a/test/test_validation.js +++ b/test/test_validation.js @@ -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') @@ -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) @@ -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]]) @@ -58,12 +56,12 @@ 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) { @@ -71,13 +69,13 @@ describe('sprintfjs', function() { } }) - 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) }) From c8592ca8e75e50e38fcb68edc495586bb9d66702 Mon Sep 17 00:00:00 2001 From: Uladzislau Rastargueu Date: Thu, 2 Feb 2023 23:23:19 +0100 Subject: [PATCH 2/2] fix: replace functions with arrow functions --- test/test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test.js b/test/test.js index 42ad9a6..c64c2b3 100644 --- a/test/test.js +++ b/test/test.js @@ -6,10 +6,10 @@ const assert = require('assert'), sprintfjs = require('../src/sprintf.js'), sprintf = sprintfjs.sprintf -describe('sprintfjs', function() { +describe('sprintfjs', () => { const pi = Math.PI - it('should return formated strings for simple placeholders', function() { + it('should return formated strings for simple placeholders', () => { assert.strictEqual('%', sprintf('%%')) assert.strictEqual('10', sprintf('%b', 2)) assert.strictEqual('A', sprintf('%c', 65)) @@ -60,7 +60,7 @@ describe('sprintfjs', function() { assert.strictEqual('/<("[^"]*"|\'[^\']*\'|[^\'">])*>/', sprintf('%v', /<("[^"]*"|'[^']*'|[^'">])*>/)) }) - it('should return formatted strings for complex placeholders', function() { + it('should return formatted strings for complex placeholders', () => { // sign assert.strictEqual('2', sprintf('%d', 2)) assert.strictEqual('-2', sprintf('%d', -2)) @@ -106,7 +106,7 @@ describe('sprintfjs', function() { }) - it('should return formatted strings for callbacks', function() { - assert.strictEqual('foobar', sprintf('%s', function() { return 'foobar' })) + it('should return formatted strings for callbacks', () => { + assert.strictEqual('foobar', sprintf('%s', () => 'foobar')) }) })