# HG changeset patch # User hamidouk # Date 1321440057 -3600 # Node ID 95c6b77f005e826f595a2655f6ccfc17908e35f6 # Parent fd84e0fb26d89ec54c9855028b368052c4383274 added more test for utility functions. diff -r fd84e0fb26d8 -r 95c6b77f005e src/js/utils.js --- a/src/js/utils.js Wed Nov 16 11:20:52 2011 +0100 +++ b/src/js/utils.js Wed Nov 16 11:40:57 2011 +0100 @@ -17,15 +17,17 @@ a closure. This way, the */ IriSP.wrap = function (obj, fn) { - return function() { - var args = [].slice.call(this, arguments); - return fn.call(obj, args); + return function() { + var args = Array.prototype.slice.call(arguments, 0); + return fn.apply(obj, args); } } /* convert a time to a percentage in the media */ IriSP.timeToPourcent = function(time, timetotal){ - // return (parseInt(Math.round(time/timetotal*100))); + var time = Math.abs(time); + var timetotal = Math.abs(timetotal); + return Math.floor((time/timetotal) * 100); }; diff -r fd84e0fb26d8 -r 95c6b77f005e unittests/tests/utils.js --- a/unittests/tests/utils.js Wed Nov 16 11:20:52 2011 +0100 +++ b/unittests/tests/utils.js Wed Nov 16 11:40:57 2011 +0100 @@ -1,6 +1,29 @@ 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 function to convert from seconds to a time", function() { var h = 13, m = 7, s = 41; var t = 13 * 3600 + 7* 60 + 41;