summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/scriptaculous/test/unit/bdd_test.html
blob: 02cb10bff703b0be58cd8bb5192bbd059b4379e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>script.aculo.us Unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../../lib/prototype.js" type="text/javascript"></script>
  <script src="../../src/scriptaculous.js" type="text/javascript"></script>
  <script src="../../src/unittest.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>
<h1>script.aculo.us Unit test file</h1>

<!-- Log output -->
<div id="testlog"> </div>

<div id="d">initial</div>

<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
var moo = 0;

var assertMethods = [];
for(method in Test.Unit.Assertions.prototype) {
  if(/^assert/.test(method)) assertMethods.push(method);
}

var testObj = {
  isNice: function(){
    return true;
  },
  isBroken: function(){
    return false;
  }
}

Test.context("BDD-style testing",{
  
  setup: function() {
    $('d').update('setup!');
    moo++;
  },
  
  teardown: function() {
    moo--;
  },
  
  'should run setup before each specification': function(){
    assert($('d').innerHTML == 'setup!');
    assert(moo == 1);
  },
  
  'should run teardown after each specification': function(){
    assert(moo == 1);
  },
  
  'should provide extensions to tie in isSomething and respondsTo object methods': function(){
    Object.extend(testObj, Test.BDDMethods);
    
    testObj.shouldBe('nice');
    testObj.shouldNotBe('broken');
    
    testObj.shouldRespondTo('isNice');
    testObj.shouldRespondTo('isBroken');
  },
  
  'should automatically add extensions to strings': function(){
    'a'.shouldEqual('a');
    'a'.shouldNotEqual('b');
    'a'.shouldNotBeNull();
    'a'.shouldBeA(String);
    
    var aString = 'boo!';
    aString.shouldEqual('boo!');
    aString.shouldBeA(String);
    aString.shouldNotBeA(Number);
  },
  
  'should automatically add extensions to numbers': function(){
    var n = 123;    
    n.shouldEqual(123);
    n.shouldNotEqual(4);
    
    n.shouldBeA(Number);
    n.shouldNotBeA(Test);
  },
  
  'should automatically add extensions to arrays': function(){
    ['a'].shouldNotBeA(String);
    [1,2,3].shouldBeAn(Array);
    [1,2,3].shouldEqualEnum([1,2,3]);
  },
  
  'should support the eval() method': function(){
    eval('2*2').shouldEqual(4);
  },
  
  'should support equality assertion': function(){
    assertEqual(1, 1);
    assertEqual('a', 'a');
    assertEqual(1, '1');
    
    var x = 1;
    var y = 1;
    assertEqual(1, x)
    assertEqual(x, y);
  },
  
  'should provide all assertions': function(){
    assertMethods.each(function(m){
      assert(typeof this[m] == 'function');
    }.bind(this)); 
  },
  
  'should support deferred execution': function(){
    wait(10,function(){
      'a'.shouldEqual('a');
    });
    
    wait(10,function(){
      'a'.shouldEqual('a');
      wait(10,function(){
        'a'.shouldEqual('a');
        wait(10,function(){
          'a'.shouldEqual('a');
        });
      });
    });
  }
  
});

// ]]>
</script>
</body>
</html>