Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma-jasmine / lib / adapter.js
1 (function(window) {
2
3 var formatFailedStep = function(step) {
4   var stack   = step.stack;
5   var message = step.message;
6
7   if (stack) {
8     // remove the trailing dot
9     var firstLine = stack.substring(0, stack.indexOf('\n') - 1);
10
11     if (message && message.indexOf(firstLine) === -1) {
12       stack = message +'\n'+ stack;
13     }
14
15     // remove jasmine stack entries
16     return stack.replace(/\n.+jasmine\.js\?\w*\:.+(?=(\n|$))/g, '');
17   }
18
19   return message;
20 };
21
22
23 var indexOf = function(collection, item) {
24   if (collection.indexOf) {
25     return collection.indexOf(item);
26   }
27
28   for (var i = 0, l = collection.length; i < l; i++) {
29     if (collection[i] === item) {
30       return i;
31     }
32   }
33
34   return -1;
35 };
36
37
38 var SuiteNode = function(name, parent) {
39   this.name = name;
40   this.parent = parent;
41   this.children = [];
42
43   this.addChild = function(name) {
44     var suite = new SuiteNode(name, this);
45     this.children.push(suite);
46     return suite;
47   };
48 };
49
50
51 var getAllSpecNames = function(topSuite) {
52   var specNames = {};
53
54   function processSuite(suite, pointer) {
55     var child;
56     var childPointer;
57
58     for (var i = 0; i < suite.children.length; i++) {
59       child = suite.children[i];
60
61       if (child.children) {
62         childPointer = pointer[child.description] = {_: []};
63         processSuite(child, childPointer);
64       } else {
65         pointer._.push(child.description);
66       }
67     }
68   }
69
70   processSuite(topSuite, specNames);
71
72   return specNames;
73 };
74
75
76 /**
77  * Very simple reporter for Jasmine.
78  */
79 var KarmaReporter = function(tc, jasmineEnv) {
80
81   var currentSuite = new SuiteNode();
82
83   /**
84    * Jasmine 2.0 dispatches the following events:
85    *
86    *  - jasmineStarted
87    *  - jasmineDone
88    *  - suiteStarted
89    *  - suiteDone
90    *  - specStarted
91    *  - specDone
92    */
93
94   this.jasmineStarted = function(data) {
95     // TODO(vojta): Do not send spec names when polling.
96     tc.info({
97       total: data.totalSpecsDefined,
98       specs: getAllSpecNames(jasmineEnv.topSuite())
99     });
100   };
101
102
103   this.jasmineDone = function() {
104     tc.complete({
105       coverage: window.__coverage__
106     });
107   };
108
109
110   this.suiteStarted = function(result) {
111     currentSuite = currentSuite.addChild(result.description);
112   };
113
114
115   this.suiteDone = function(result) {
116     // In the case of xdescribe, only "suiteDone" is fired.
117     // We need to skip that.
118     if (result.description !== currentSuite.name) {
119       return;
120     }
121
122     currentSuite = currentSuite.parent;
123   };
124
125
126   this.specStarted = function(specResult) {
127     specResult.startTime = new Date().getTime();
128   };
129
130
131   this.specDone = function(specResult) {
132     var skipped = specResult.status === 'disabled' || specResult.status === 'pending';
133
134     var result = {
135       description : specResult.description,
136       id          : specResult.id,
137       log         : [],
138       skipped     : skipped,
139       success     : specResult.failedExpectations.length === 0,
140       suite       : [],
141       time        : skipped ? 0 : new Date().getTime() - specResult.startTime
142     };
143
144     // generate ordered list of (nested) suite names
145     var suitePointer = currentSuite;
146     while (suitePointer.parent) {
147       result.suite.unshift(suitePointer.name);
148       suitePointer = suitePointer.parent;
149     }
150
151     if (!result.success) {
152       var steps = specResult.failedExpectations;
153       for (var i = 0, l = steps.length; i < l; i++) {
154         result.log.push(formatFailedStep(steps[i]));
155       }
156     }
157
158     tc.result(result);
159     delete specResult.startTime;
160   };
161 };
162
163
164 var createStartFn = function(tc, jasmineEnvPassedIn) {
165   return function(config) {
166     // we pass jasmineEnv during testing
167     // in production we ask for it lazily, so that adapter can be loaded even before jasmine
168     var jasmineEnv = jasmineEnvPassedIn || window.jasmine.getEnv();
169
170     jasmineEnv.addReporter(new KarmaReporter(tc, jasmineEnv));
171     jasmineEnv.executeFiltered();
172   };
173 };
174
175
176 window.__karma__.start = createStartFn(window.__karma__);
177
178 })(window);