unittests/tests/utils.js
author hamidouk
Tue, 27 Dec 2011 10:06:05 +0100
branchpopcorn-port
changeset 530 3cc4a789dae7
parent 374 138e76fe73a6
permissions -rw-r--r--
added unit tests for sparklineWidget
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
308
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     1
function test_utils() {
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     2
  module("Utility function tests");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     3
  
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     4
  test("test a function to preserve the scope of a method in a callback", function() {
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     5
    var obj = { a : 2};
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     6
    obj.b = function(e, f) { 
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     7
      equal(this.a, 2, "the scope is preserved");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     8
      equal(e, 1, "arg 1 passed correctly");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
     9
      equal(f, 2, "arg 2 passed correctly");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    10
    };
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    11
    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    12
    (IriSP.wrap(obj, obj.b))(1, 2);
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    13
  
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    14
  });
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    15
  
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    16
  test("test function to convert a ratio to a percentage", function() {
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    17
    var time = 2;
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    18
    var total = 3;
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    19
    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    20
    equal(IriSP.timeToPourcent(2, 3), 66, "the function returns the correct result");    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    21
    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    22
    var total = -total;    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    23
    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    24
    equal(IriSP.timeToPourcent(2, 3), 66, "the function is immune to negative numbers");            
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    25
  });
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    26
  
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    27
  test("test padding function", function() {
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    28
    equal(IriSP.padWithZeros(3), "03", "function works correctly");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    29
  });
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    30
  
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    31
  test("test function to convert from seconds to a time", function() {
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    32
    var h = 13, m = 7, s = 41;
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    33
    var t = 13 * 3600 + 7* 60 + 41;
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    34
    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    35
    var r = IriSP.secondsToTime(t);
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    36
    ok(r.hours === h && r.minutes === m && r.seconds === s, "the converted time is correct");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    37
    
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    38
    t = -t;
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    39
    var r = IriSP.secondsToTime(t);
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    40
    ok(r.hours === h && r.minutes === m && r.seconds === s, "the function is immune to negative numbers.");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    41
    equal(IriSP.secondsToTime(t), "13:07:41");
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    42
  });
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    43
  
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    44
  test("test function to format a tweet", function() {
374
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    45
    var inputs = ["@handle", "@bundle", "#hashtag", "http://t.co/11111", "++", "--"];
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    46
    var outputs = ["<a href='http://twitter.com/handle'>@handle</a>", 
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    47
                   "<a href='http://twitter.com/bundle'>@bundle</a>",
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    48
                   "<a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a>",
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    49
                   "<a href='http://t.co/11111'>http://t.co/11111</a>",
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    50
                   "<span class='Ldt-PolemicPlusPlus'>++</span>",
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    51
                   "<span class='Ldt-PolemicMinusMinus'>--</span>"];
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    52
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    53
    var i = 0;
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    54
    for(i = 0; i < inputs.length; i++) {
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    55
      equal(IriSP.formatTweet(inputs[i]), outputs[i], "the correct output is given");
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    56
    }
308
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
    57
  });
361
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    58
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    59
  test("test function to convert decimal color to hexadecimal", function() {
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    60
    equal(IriSP.DEC_HEXA_COLOR(125), "7D", "first test passes");
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    61
    equal(IriSP.DEC_HEXA_COLOR(24345), "5F19", "second test passes");
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    62
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    63
  });
362
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 361
diff changeset
    64
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 361
diff changeset
    65
  test("test template function", function() {
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 361
diff changeset
    66
    IriSP.default_templates_vars["test_fixture"] = "FIXTURE";
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 361
diff changeset
    67
    equal(IriSP.templToHTML("{{test_fixture}} {{foo}}", {foo: 2}), "FIXTURE 2", "correct template returned"); 
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 361
diff changeset
    68
  });
369
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    69
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    70
  test("test url encoding function", function() {
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    71
    equal(IriSP.encodeURI("!'()*"), "%21%27%28%29%2A", "the returned string is correct");
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    72
  });
361
15074091bdba added tests for DEC_TO_HEXA.
hamidouk
parents: 308
diff changeset
    73
}