unittests/tests/utils.js
author hamidouk
Mon, 21 Nov 2011 16:20:16 +0100
branchpopcorn-port
changeset 297 ed4a459c9290
parent 285 b7aa28af2c10
child 308 495ef0f3e483
permissions -rw-r--r--
fixed the formatTweet to create links to urls.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
252
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
     1
function test_utils() {
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
     2
  module("Utility function tests");
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
     3
  
253
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
     4
  test("test a function to preserve the scope of a method in a callback", function() {
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
     5
    var obj = { a : 2};
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
     6
    obj.b = function(e, f) { 
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
     7
      equal(this.a, 2, "the scope is preserved");
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
     8
      equal(e, 1, "arg 1 passed correctly");
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
     9
      equal(f, 2, "arg 2 passed correctly");
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    10
    };
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    11
    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    12
    (IriSP.wrap(obj, obj.b))(1, 2);
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    13
  
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    14
  });
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    15
  
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    16
  test("test function to convert a ratio to a percentage", function() {
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    17
    var time = 2;
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    18
    var total = 3;
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    19
    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    20
    equal(IriSP.timeToPourcent(2, 3), 66, "the function returns the correct result");    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    21
    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    22
    var total = -total;    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    23
    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    24
    equal(IriSP.timeToPourcent(2, 3), 66, "the function is immune to negative numbers");            
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    25
  });
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    26
  
256
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
    27
  test("test padding function", function() {
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
    28
    equal(IriSP.padWithZeros(3), "03", "function works correctly");
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
    29
  });
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
    30
  
252
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    31
  test("test function to convert from seconds to a time", function() {
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    32
    var h = 13, m = 7, s = 41;
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    33
    var t = 13 * 3600 + 7* 60 + 41;
254
cafaa694b709 fixed the conversion function to accomodate mustache.
hamidouk
parents: 253
diff changeset
    34
    deepEqual(IriSP.secondsToTime(t), {"hours" : h, "minutes" : m, "seconds" : s}, 
cafaa694b709 fixed the conversion function to accomodate mustache.
hamidouk
parents: 253
diff changeset
    35
              "the converted time is correct");
252
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    36
    
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    37
    t = -t;
254
cafaa694b709 fixed the conversion function to accomodate mustache.
hamidouk
parents: 253
diff changeset
    38
    deepEqual(IriSP.secondsToTime(t),  {"hours" : h, "minutes" : m, "seconds" : s}, "the function is immune to negative numbers.");
252
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    39
  });
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    40
  
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    41
  test("test function to format a tweet", function() {
297
ed4a459c9290 fixed the formatTweet to create links to urls.
hamidouk
parents: 285
diff changeset
    42
    var input = "@handle @bundle #hashtag http://t.co/11111";
ed4a459c9290 fixed the formatTweet to create links to urls.
hamidouk
parents: 285
diff changeset
    43
    var output = "<a href='http://twitter.com/handle'>@handle</a> <a href='http://twitter.com/bundle'>@bundle</a> <a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a> <a href='http://t.co/11111'>http://t.co/11111</a>";
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    44
    equal(IriSP.formatTweet(input), output, "the correct output is given");
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    45
  });
252
fd84e0fb26d8 added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
diff changeset
    46
}