aboutsummaryrefslogtreecommitdiffstats
path: root/src/scenario/Runner.js
blob: 9e20d3945c83ff9ed88d690857b47bc4c4a84add (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
angular['scenario']  = (angular['scenario'] = {});

angular.scenario.Runner = function(scope, jQuery){
  var self = scope.$scenario = this;
  this.scope = scope;
  this.jQuery = jQuery;

  var specs = this.specs = {};
  var path = [];
  this.scope.describe = function describe(name, body){
    path.push(name);
    body();
    path.pop();
  };
  this.scope.it = function it(name, body) {
    var specName = path.join(' ') + ': it ' + name;
    self.currentSpec = specs[specName] = {
        name: specName,
        steps:[]
     };
    body();
    self.currentSpec = null;
  };
  this.logger = function returnNoop(){
    return angular.extend(returnNoop, {close:angular.noop, fail:angular.noop});;
  };
};

angular.scenario.Runner.prototype = {
  run: function(body){
    var jQuery = this.jQuery;
    body.append(
      '<div id="runner">' +
        '<div class="console"></div>' +
      '</div>' +
      '<div id="testView">' +
        '<iframe></iframe>' +
      '</div>');
    var console = body.find('#runner .console');
    console.find('li').live('click', function(){
      jQuery(this).toggleClass('collapsed');
    });
    this.testFrame = body.find('#testView iframe');
    this.testWindow = this.testFrame[0].contentWindow;
    function logger(parent) {
      var container;
      return function(type, text) {
        if (!container) {
          container = jQuery('<ul></ul>');
          parent.append(container);
        }
        var element = jQuery('<li class="collapsed running '+type+'"><span></span></li>');
        element.find('span').text(text);
        container.append(element);
        return angular.extend(logger(element), {
          close: function(){
            element.removeClass('running');
          },
          fail: function(){
            element.removeClass('running');
            var current = element;
            while (current[0] != console[0]) {
              if (current.is('li'))
                current.addClass('fail');
              current.removeClass('collapsed');
              current = current.parent();
            }
          }
        });;
      };
    }
    this.logger = logger(console);
    var specNames = [];
    angular.foreach(this.specs, function(spec, name){
      specNames.push(name);
    }, this);
    specNames.sort();
    var self = this;
    function callback(){
      var next = specNames.shift();
      if(next) {
        self.execute(next, callback);
      }
    };
    callback();
  },

  addStep: function(name, step) {
    this.currentSpec.steps.push({name:name, fn:step});
  },

  execute: function(name, callback) {
   var spec = this.specs[name],
       result = {
           passed: false,
           failed: false,
           finished: false,
           fail: function(error) {
             result.passed = false;
             result.failed = true;
             result.error = error;
             result.log('fail', angular.isString(error) ? error : angular.toJson(error)).fail();
           }
         };
       specThis = {
         result: result,
         testWindow: this.testWindow,
         testFrame: this.testFrame
       };
   var stepLogger = this.logger('spec', name);
   spec.nextStepIndex = 0;
   function done() {
     result.finished = true;
     stepLogger.close();
     (callback||angular.noop).call(specThis);
   }
   function next(){
     var step = spec.steps[spec.nextStepIndex];
     (result.log || {close:angular.noop}).close();
     result.log = null;
     if (step) {
       spec.nextStepIndex ++;
       result.log = stepLogger('step', step.name);
       try {
         step.fn.call(specThis, next);
       } catch (e) {
         result.fail(e);
         done();
       }
     } else {
       result.passed = !result.failed;
       done();
     }
   };
   next();
   return specThis;
  }
};