aboutsummaryrefslogtreecommitdiffstats
path: root/lib/jasmine-jstd-adapter/JasmineAdapter.js
blob: ba54251a3d3250d8e535f6d5fd79a7f0771276c9 (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
/**
 * @fileoverview Jasmine JsTestDriver Adapter.
 * @author ibolmo@gmail.com (Olmo Maldonado)
 * @author misko@hevery.com (Misko Hevery)
 */

(function() {

  function bind(_this, _function){
    return function(){
      return _function.call(_this);
    }
  }

  var currentFrame = frame(null, null);

  function frame(parent, name){
    var caseName = (parent && parent.caseName ? parent.caseName + " " : '') + (name ? name : '');
    var frame = {
      name: name,
      caseName: caseName,
      parent: parent,
      testCase: TestCase(caseName),
      before: [],
      after: [],
      runBefore: function(){
        if (parent) parent.runBefore.apply(this);
        for ( var i = 0; i < frame.before.length; i++) {
          frame.before[i].apply(this);
        }
      },
      runAfter: function(){
        for ( var i = 0; i < frame.after.length; i++) {
          frame.after[i].apply(this);
        }
        if (parent) parent.runAfter.apply(this);
      }
    };
    return frame;
  };

  jasmine.Env.prototype.describe = (function(describe){
    return function(description){
      currentFrame = frame(currentFrame, description);
      var val = describe.apply(this, arguments);
      currentFrame = currentFrame.parent;
      return val;
    };

  })(jasmine.Env.prototype.describe);


  jasmine.Env.prototype.it = (function(it){
    return function(desc, itFn){
      var self = this;
      var spec = it.apply(this, arguments);
      var currentSpec = this.currentSpec;
      var frame = this.jstdFrame = currentFrame;
      this.jstdFrame.testCase.prototype['test that it ' + desc] = function(){
        frame.runBefore.apply(currentSpec);
        try {
          itFn.apply(currentSpec);
        } finally {
          frame.runAfter.apply(currentSpec);
        }
      };
      return spec;
    };

  })(jasmine.Env.prototype.it);


  jasmine.Env.prototype.beforeEach = (function(beforeEach){
    return function(beforeEachFunction) {
      beforeEach.apply(this, arguments);
      currentFrame.before.push(beforeEachFunction);
    };

  })(jasmine.Env.prototype.beforeEach);


  jasmine.Env.prototype.afterEach = (function(afterEach){
    return function(afterEachFunction) {
      afterEach.apply(this, arguments);
      currentFrame.after.push(afterEachFunction);
    };

  })(jasmine.Env.prototype.afterEach);


  jasmine.NestedResults.prototype.addResult = (function(addResult){
    return function(result) {
      addResult.call(this, result);
      if (result.type != 'MessageResult' && !result.passed()) fail(result.message);
    };

  })(jasmine.NestedResults.prototype.addResult);

  // Reset environment with overriden methods.
  jasmine.currentEnv_ = null;
  jasmine.getEnv();

})();