added test for sprintf function, removing dead code and optimizing test cases

pull/208/head
El RIDO 7 years ago
parent b00bcd1352
commit b9c05b06d0
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92

@ -179,6 +179,9 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
/**
* minimal sprintf emulation for %s and %d formats
*
* Note that this function needs the parameters in the same order as the
* format strings appear in the string, contrary to the original.
*
* @see {@link https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format#4795914}
* @name helper.sprintf
* @function
@ -195,27 +198,22 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
}
var format = args[0],
i = 1;
return format.replace(/%((%)|s|d)/g, function (m) {
return format.replace(/%(s|d)/g, function (m) {
// m is the matched format, e.g. %s, %d
var val;
if (m[2]) {
val = m[2];
} else {
val = args[i];
// A switch statement so that the formatter can be extended.
switch (m)
{
case '%d':
val = parseFloat(val);
if (isNaN(val)) {
val = 0;
}
break;
default:
// Default is %s
}
++i;
var val = args[i];
// A switch statement so that the formatter can be extended.
switch (m)
{
case '%d':
val = parseFloat(val);
if (isNaN(val)) {
val = 0;
}
break;
default:
// Default is %s
}
++i;
return val;
});
},

@ -93,6 +93,10 @@ describe('helper', function () {
});
describe('setElementText', function () {
after(function () {
cleanup();
});
jsc.property(
'replaces the content of an element',
jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
@ -104,20 +108,23 @@ describe('helper', function () {
ids.forEach(function(item, i) {
html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
});
var clean = jsdom(html);
var elements = $('<body />').html(html);
ids.forEach(function(item, i) {
var id = item.join(''),
element = $(document.getElementById(id));
element = elements.find('#' + id).first();
$.PrivateBin.helper.setElementText(element, replacingContent);
result *= replacingContent === $(document.getElementById(id)).text();
result *= replacingContent === element.text();
});
clean();
return Boolean(result);
}
);
});
describe('setMessage', function () {
after(function () {
cleanup();
});
jsc.property(
'replaces the content of an empty element, analog to setElementText',
jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
@ -129,14 +136,13 @@ describe('helper', function () {
ids.forEach(function(item, i) {
html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
});
var clean = jsdom(html);
var elements = $('<body />').html(html);
ids.forEach(function(item, i) {
var id = item.join(''),
element = $(document.getElementById(id));
element = elements.find('#' + id).first();
$.PrivateBin.helper.setMessage(element, replacingContent);
result *= replacingContent === $(document.getElementById(id)).text();
result *= replacingContent === element.text();
});
clean();
return Boolean(result);
}
);
@ -152,20 +158,23 @@ describe('helper', function () {
ids.forEach(function(item, i) {
html += '<div id="' + item.join('') + '"><span class="' + (classes[i] || classes[0]) + '"></span> ' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
});
var clean = jsdom(html);
var elements = $('<body />').html(html);
ids.forEach(function(item, i) {
var id = item.join(''),
element = $(document.getElementById(id));
element = elements.find('#' + id).first();
$.PrivateBin.helper.setMessage(element, replacingContent);
result *= ' ' + replacingContent === $(document.getElementById(id)).contents()[1].nodeValue;
result *= ' ' + replacingContent === element.contents()[1].nodeValue;
});
clean();
return Boolean(result);
}
);
});
describe('urls2links', function () {
after(function () {
cleanup();
});
jsc.property(
'ignores non-URL content',
'string',
@ -223,6 +232,77 @@ describe('helper', function () {
);
});
describe('sprintf', function () {
after(function () {
cleanup();
});
jsc.property(
'replaces %s in strings with first given parameter',
'string',
'(small nearray) string',
'string',
function (prefix, params, postfix) {
params.unshift(prefix + '%s' + postfix);
var result = prefix + params[1] + postfix;
return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
result === $.PrivateBin.helper.sprintf(params);
}
);
jsc.property(
'replaces %d in strings with first given parameter',
'string',
'(small nearray) nat',
'string',
function (prefix, params, postfix) {
params.unshift(prefix + '%d' + postfix);
var result = prefix + params[1] + postfix;
return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
result === $.PrivateBin.helper.sprintf(params);
}
);
jsc.property(
'replaces %d in strings with 0 if first parameter is not a number',
'string',
'(small nearray) falsy',
'string',
function (prefix, params, postfix) {
params.unshift(prefix + '%d' + postfix);
var result = prefix + '0' + postfix;
return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
result === $.PrivateBin.helper.sprintf(params);
}
);
jsc.property(
'replaces %d and %s in strings in order',
'string',
'nat',
'string',
'string',
'string',
function (prefix, uint, middle, string, postfix) {
var params = [prefix + '%d' + middle + '%s' + postfix, uint, string],
result = prefix + uint + middle + string + postfix;
return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
result === $.PrivateBin.helper.sprintf(params);
}
);
jsc.property(
'replaces %d and %s in strings in reverse order',
'string',
'nat',
'string',
'string',
'string',
function (prefix, uint, middle, string, postfix) {
var params = [prefix + '%s' + middle + '%d' + postfix, string, uint],
result = prefix + string + middle + uint + postfix;
return result === $.PrivateBin.helper.sprintf.apply(this, params) &&
result === $.PrivateBin.helper.sprintf(params);
}
);
});
describe('scriptLocation', function () {
jsc.property(
'returns the URL without query & fragment',

@ -69,7 +69,7 @@ if ($MARKDOWN):
<?php
endif;
?>
<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-2xPbJySqwLEOH/wTiKWSsMT1qZnHkELGrRwKNGeNxfFnssFH7u3GtNKeccu5SB4+2Lctpd1Bt6vphTRWGi5+cw==" crossorigin="anonymous"></script>
<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-bNqQCYlXhhJaoCspMQq5mmRFDsV5bXN1b+jr0N7zcG7Yd4hKXDYrSyxHrp1bavnpk1iG62o9rY4yTXHqarEZjw==" crossorigin="anonymous"></script>
<!--[if lt IE 10]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
<![endif]-->

@ -47,7 +47,7 @@ if ($MARKDOWN):
<?php
endif;
?>
<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-2xPbJySqwLEOH/wTiKWSsMT1qZnHkELGrRwKNGeNxfFnssFH7u3GtNKeccu5SB4+2Lctpd1Bt6vphTRWGi5+cw==" crossorigin="anonymous"></script>
<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-bNqQCYlXhhJaoCspMQq5mmRFDsV5bXN1b+jr0N7zcG7Yd4hKXDYrSyxHrp1bavnpk1iG62o9rY4yTXHqarEZjw==" crossorigin="anonymous"></script>
<!--[if lt IE 10]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
<![endif]-->

Loading…
Cancel
Save