# HG changeset patch # User hamidouk # Date 1322047427 -3600 # Node ID 495ef0f3e48317c7f63ed395d0b26f365c413f2d # Parent db5ecd5bbcf29a422ea5be630f7381426f7ea29f added a toString method to the object returned by IriSP.secondsToTime. diff -r db5ecd5bbcf2 -r 495ef0f3e483 src/js/utils.js --- a/src/js/utils.js Wed Nov 23 11:49:02 2011 +0100 +++ b/src/js/utils.js Wed Nov 23 12:23:47 2011 +0100 @@ -46,9 +46,19 @@ var minutes = Math.abs(parseInt( secs / 60 ) % 60); var seconds = parseFloat(Math.abs(secs % 60).toFixed(0)); - return {"hours" : hours, "minutes" : minutes, "seconds" : seconds}; + var toString_fn = function() { + var ret = ""; + if (hours > 0) + ret = IriSP.padWithZeros(this.hours) + ":"; + ret += IriSP.padWithZeros(this.minutes) + ":" + IriSP.padWithZeros(this.seconds); + + return ret; + } + return {"hours" : hours, "minutes" : minutes, "seconds" : seconds, toString: toString_fn}; }; +IriSP.secondsToString + /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ IriSP.formatTweet = function(tweet) { var rNickname = /@(\w+)/gi; // matches a @handle @@ -69,4 +79,4 @@ if ("set" in desc) obj.__defineSetter__(prop,desc.set); } } -*/ \ No newline at end of file +*/ diff -r db5ecd5bbcf2 -r 495ef0f3e483 unittests/tests/utils.js --- a/unittests/tests/utils.js Wed Nov 23 11:49:02 2011 +0100 +++ b/unittests/tests/utils.js Wed Nov 23 12:23:47 2011 +0100 @@ -1,46 +1,49 @@ -function test_utils() { - module("Utility function tests"); - - test("test a function to preserve the scope of a method in a callback", function() { - var obj = { a : 2}; - obj.b = function(e, f) { - equal(this.a, 2, "the scope is preserved"); - equal(e, 1, "arg 1 passed correctly"); - equal(f, 2, "arg 2 passed correctly"); - }; - - (IriSP.wrap(obj, obj.b))(1, 2); - - }); - - test("test function to convert a ratio to a percentage", function() { - var time = 2; - var total = 3; - - equal(IriSP.timeToPourcent(2, 3), 66, "the function returns the correct result"); - - var total = -total; - - equal(IriSP.timeToPourcent(2, 3), 66, "the function is immune to negative numbers"); - }); - - test("test padding function", function() { - equal(IriSP.padWithZeros(3), "03", "function works correctly"); - }); - - test("test function to convert from seconds to a time", function() { - var h = 13, m = 7, s = 41; - var t = 13 * 3600 + 7* 60 + 41; - deepEqual(IriSP.secondsToTime(t), {"hours" : h, "minutes" : m, "seconds" : s}, - "the converted time is correct"); - - t = -t; - deepEqual(IriSP.secondsToTime(t), {"hours" : h, "minutes" : m, "seconds" : s}, "the function is immune to negative numbers."); - }); - - test("test function to format a tweet", function() { - var input = "@handle @bundle #hashtag http://t.co/11111"; - var output = "@handle @bundle #hashtag http://t.co/11111"; - equal(IriSP.formatTweet(input), output, "the correct output is given"); - }); +function test_utils() { + module("Utility function tests"); + + test("test a function to preserve the scope of a method in a callback", function() { + var obj = { a : 2}; + obj.b = function(e, f) { + equal(this.a, 2, "the scope is preserved"); + equal(e, 1, "arg 1 passed correctly"); + equal(f, 2, "arg 2 passed correctly"); + }; + + (IriSP.wrap(obj, obj.b))(1, 2); + + }); + + test("test function to convert a ratio to a percentage", function() { + var time = 2; + var total = 3; + + equal(IriSP.timeToPourcent(2, 3), 66, "the function returns the correct result"); + + var total = -total; + + equal(IriSP.timeToPourcent(2, 3), 66, "the function is immune to negative numbers"); + }); + + test("test padding function", function() { + equal(IriSP.padWithZeros(3), "03", "function works correctly"); + }); + + test("test function to convert from seconds to a time", function() { + var h = 13, m = 7, s = 41; + var t = 13 * 3600 + 7* 60 + 41; + + var r = IriSP.secondsToTime(t); + ok(r.hours === h && r.minutes === m && r.seconds === s, "the converted time is correct"); + + t = -t; + var r = IriSP.secondsToTime(t); + ok(r.hours === h && r.minutes === m && r.seconds === s, "the function is immune to negative numbers."); + equal(IriSP.secondsToTime(t), "13:07:41"); + }); + + test("test function to format a tweet", function() { + var input = "@handle @bundle #hashtag http://t.co/11111"; + var output = "@handle @bundle #hashtag http://t.co/11111"; + equal(IriSP.formatTweet(input), output, "the correct output is given"); + }); } \ No newline at end of file