2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-10-04 21:30:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test cases for the Format module
|
|
|
|
*/
|
|
|
|
|
|
|
|
const JsUnit = imports.jsUnit;
|
|
|
|
const assertEquals = JsUnit.assertEquals;
|
|
|
|
const assertRaises = JsUnit.assertRaises;
|
|
|
|
|
|
|
|
// We can't depend on environment.js to set up the String.prototype.format,
|
|
|
|
// because the tests run in one JS context, and the imports run in the GJS
|
|
|
|
// "load context" which has its own copy of the String class
|
|
|
|
const Format = imports.misc.format;
|
|
|
|
String.prototype.format = Format.format;
|
|
|
|
|
|
|
|
// Test common usage and %% handling
|
|
|
|
assertEquals("foo", "%s".format('foo'));
|
|
|
|
assertEquals("%s", "%%s".format('foo'));
|
|
|
|
assertEquals("%%s", "%%%%s".format('foo'));
|
|
|
|
assertEquals("foo 5", "%s %d".format('foo', 5));
|
|
|
|
assertEquals("8", "%d".format(8));
|
2010-06-23 21:46:59 +00:00
|
|
|
assertEquals("f", "%x".format(15));
|
2009-10-04 21:30:18 +00:00
|
|
|
assertEquals("2.58 6.96", "%f %.2f".format(2.58, 6.958));
|
|
|
|
|
2010-06-23 21:46:59 +00:00
|
|
|
// Test field width
|
|
|
|
assertEquals("007 foo", "%03d %4s".format(7, 'foo'));
|
|
|
|
assertEquals(" 2.58 06.96", "%5f %05.2f".format(2.58, 6.958));
|
|
|
|
assertEquals("cafe", "%2x".format(0xcafe));
|
|
|
|
assertEquals("foo", "%0s".format('foo'));
|
|
|
|
|
2009-10-04 21:30:18 +00:00
|
|
|
// Precision is only allowed for %f
|
|
|
|
assertRaises(function() { "%.2d".format(5.21) });
|
|
|
|
|
|
|
|
// Wrong conversion character ' '
|
|
|
|
assertRaises( function() { "%s is 50% done".format('foo') });
|