guard-jasmine 0.9.14 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ (function() {
2
+ var ConsoleReporter;
3
+
4
+ ConsoleReporter = (function() {
5
+
6
+ function ConsoleReporter() {}
7
+
8
+ ConsoleReporter.prototype.runnerResult = {
9
+ passed: false,
10
+ stats: {
11
+ specs: 0,
12
+ failures: 0,
13
+ time: 0.0
14
+ },
15
+ suites: []
16
+ };
17
+
18
+ ConsoleReporter.prototype.specCount = 0;
19
+
20
+ ConsoleReporter.prototype.currentSpecs = [];
21
+
22
+ ConsoleReporter.prototype.nestedSuiteResults = {};
23
+
24
+ ConsoleReporter.prototype.reportSpecStarting = function(spec) {
25
+ return console.log("SPEC_START: " + spec.id);
26
+ };
27
+
28
+ ConsoleReporter.prototype.reportSpecResults = function(spec) {
29
+ var messages, result, specResult, _i, _len, _ref;
30
+ if (!spec.results().skipped) {
31
+ specResult = {
32
+ id: spec.id,
33
+ description: spec.description,
34
+ passed: spec.results().failedCount === 0
35
+ };
36
+ if (spec.results().failedCount !== 0) {
37
+ messages = [];
38
+ _ref = spec.results().getItems();
39
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
40
+ result = _ref[_i];
41
+ messages.push(result.message);
42
+ }
43
+ if (messages.length !== 0) specResult['messages'] = messages;
44
+ }
45
+ this.specCount += 1;
46
+ return this.currentSpecs.push(specResult);
47
+ }
48
+ };
49
+
50
+ ConsoleReporter.prototype.reportSuiteResults = function(suite) {
51
+ var parent, suiteResult, _ref;
52
+ if (!suite.results().skipped) {
53
+ suiteResult = {
54
+ id: suite.id,
55
+ parent: (_ref = suite.parentSuite) != null ? _ref.id : void 0,
56
+ description: suite.description,
57
+ passed: suite.results().failedCount === 0,
58
+ specs: this.currentSpecs,
59
+ suites: []
60
+ };
61
+ if (suite.parentSuite != null) {
62
+ parent = suite.parentSuite.id;
63
+ if (!this.nestedSuiteResults[parent]) {
64
+ this.nestedSuiteResults[parent] = [];
65
+ }
66
+ this.nestedSuiteResults[parent].push(suiteResult);
67
+ } else {
68
+ this.addNestedSuites(suiteResult);
69
+ this.removeEmptySuites(suiteResult);
70
+ if (suiteResult.specs.length !== 0 || suiteResult.suites.length !== 0) {
71
+ this.runnerResult.suites.push(suiteResult);
72
+ }
73
+ }
74
+ }
75
+ return this.currentSpecs = [];
76
+ };
77
+
78
+ ConsoleReporter.prototype.reportRunnerResults = function(runner) {
79
+ var runtime;
80
+ runtime = (new Date().getTime() - this.startTime) / 1000;
81
+ this.runnerResult['passed'] = runner.results().failedCount === 0;
82
+ this.runnerResult['stats'] = {
83
+ specs: this.specCount,
84
+ failures: runner.results().failedCount,
85
+ time: runtime
86
+ };
87
+ return console.log("RUNNER_RESULT: " + (JSON.stringify(this.runnerResult)));
88
+ };
89
+
90
+ ConsoleReporter.prototype.reportRunnerStarting = function(runner) {
91
+ return this.startTime = new Date().getTime();
92
+ };
93
+
94
+ ConsoleReporter.prototype.addNestedSuites = function(suiteResult) {
95
+ var suite, _i, _len, _ref, _results;
96
+ if (this.nestedSuiteResults[suiteResult.id]) {
97
+ _ref = this.nestedSuiteResults[suiteResult.id];
98
+ _results = [];
99
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
100
+ suite = _ref[_i];
101
+ this.addNestedSuites(suite);
102
+ _results.push(suiteResult.suites.push(suite));
103
+ }
104
+ return _results;
105
+ }
106
+ };
107
+
108
+ ConsoleReporter.prototype.removeEmptySuites = function(suiteResult) {
109
+ var suite, suites, _i, _len, _ref;
110
+ suites = [];
111
+ _ref = suiteResult.suites;
112
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
113
+ suite = _ref[_i];
114
+ this.removeEmptySuites(suite);
115
+ if (suite.suites.length !== 0 || suite.specs.length !== 0) {
116
+ suites.push(suite);
117
+ }
118
+ }
119
+ return suiteResult.suites = suites;
120
+ };
121
+
122
+ ConsoleReporter.prototype.log = function(message) {};
123
+
124
+ return ConsoleReporter;
125
+
126
+ })();
127
+
128
+ if (typeof module !== 'undefined' && module.exports) {
129
+ module.exports = ConsoleReporter;
130
+ } else {
131
+ window.ConsoleReporter = ConsoleReporter;
132
+ }
133
+
134
+ }).call(this);
@@ -0,0 +1,149 @@
1
+ # Simplified console logger rreplacement.
2
+ #
3
+ class Console
4
+
5
+ # Construct the console wrapper and attach
6
+ # the log methods.
7
+ #
8
+ constructor: (console) ->
9
+ log = console.log
10
+
11
+ console.log = (args...) ->
12
+ log.call console, Console.format(args...)
13
+
14
+ console.info = (args...) ->
15
+ log.call console, "INFO: #{ Console.format(args...) }"
16
+
17
+ console.warn = (args...) ->
18
+ log.call console, "WARN: #{ Console.format(args...) }"
19
+
20
+ console.error = (args...) ->
21
+ log.call console, "ERROR: #{ Console.format(args...) }"
22
+
23
+ console.debug = (args...) ->
24
+ log.call console, "DEBUG: #{ Console.format(args...) }"
25
+
26
+ @MAX_OBJECT_DEPTH: 2
27
+
28
+ # Format the console arguments. This parses the known
29
+ # % placeholder with the object value and/or concatenates
30
+ # the arguments.
31
+ #
32
+ # @param [Array] args the log arguments
33
+ #
34
+ @format: (args...) ->
35
+ result = []
36
+
37
+ if typeof args[0] is 'string' and /%[sdifo]/gi.test args[0]
38
+ arg = args.shift()
39
+ result.push arg.replace /%[sdifo]/gi, (str) => Console.inspect args.shift(), str
40
+
41
+ result.push Console.inspect arg for arg in args
42
+ result.join ' '
43
+
44
+ # Inspect a log object and return a string representation.
45
+ #
46
+ # @param [Object] object the object to inspect
47
+ # @param [String] type the format type
48
+ # @return [String] a string representation
49
+ #
50
+ @inspect: (object, type) ->
51
+ switch type
52
+ when '%s'
53
+ result = String(object)
54
+ result = match[1] if match = /'(.*)'/.exec result
55
+
56
+ when '%d', '%i'
57
+ result = parseInt object
58
+
59
+ when '%f'
60
+ result = parseFloat object
61
+
62
+ else
63
+ type = Object::toString.call(object).slice 8, -1
64
+
65
+ if type is 'Object' and object.toJSON
66
+ result = Console.pp object.toJSON()
67
+
68
+ else if type is 'Object' and object.toString and object.toString() isnt '[object Object]'
69
+ result = Console.pp object.toString()
70
+ result = match[1] if match = /'(.*)'/.exec result
71
+
72
+ else if type is 'String'
73
+ result = String(object)
74
+ result = match[1] if match = /'(.*)'/.exec result
75
+
76
+ else
77
+ result = Console.pp object
78
+
79
+ result
80
+
81
+ # Pretty print an object
82
+ #
83
+ # @param [Object] object the object to inspect
84
+ # @param [Number] depth the object depth
85
+ # @return [String] a string representation
86
+ #
87
+ @pp: (object, depth = 0) ->
88
+ type = Object::toString.call(object).slice 8, -1
89
+ result = ''
90
+
91
+ switch type
92
+ when 'Undefined', 'Null'
93
+ result += type.toLowerCase()
94
+
95
+ when 'Boolean', 'Number', 'Date'
96
+ result += object.toString()
97
+
98
+ when 'String'
99
+ result += "'#{ object.toString() }'"
100
+
101
+ when 'Array'
102
+ if object.length > 0
103
+ result += '['
104
+
105
+ for value in object
106
+ if depth < Console.MAX_OBJECT_DEPTH or Object::toString.call(value).slice(8, -1) isnt 'Object'
107
+ result += "#{ Console.pp value, depth + 1 }, "
108
+ else
109
+ result += "[Object], "
110
+
111
+ result = result.slice(0, -2)
112
+ result += ']'
113
+ else
114
+ result += '[]'
115
+
116
+ when 'Object'
117
+ if object.jquery
118
+ if object.length > 0
119
+ result += '['
120
+
121
+ object.each -> result += jQuery(@).html()
122
+
123
+ result += ']'
124
+ else
125
+ result += '[]'
126
+
127
+ else if Object.keys(object).length > 0
128
+ result += '{ '
129
+
130
+ for key, value of object
131
+ if depth < Console.MAX_OBJECT_DEPTH or Object::toString.call(value).slice(8, -1) isnt 'Object'
132
+ result += "#{ key }: #{ Console.pp value, depth + 1 }, " if object.hasOwnProperty key
133
+ else
134
+ result += "#{ key }: [Object], "
135
+
136
+ result = result.slice(0, -2)
137
+ result += ' }'
138
+ else
139
+ result += '{}'
140
+
141
+ when 'Function'
142
+ result += '[Function]'
143
+
144
+ result
145
+
146
+ if typeof module isnt 'undefined' and module.exports
147
+ module.exports = Console if module
148
+ else
149
+ new Console(window.console) if window
@@ -0,0 +1,134 @@
1
+ # Jasmine Reporter that logs reporter steps
2
+ # and results to the console.
3
+ #
4
+ class ConsoleReporter
5
+
6
+ runnerResult: {
7
+ passed: false
8
+ stats: {
9
+ specs: 0
10
+ failures: 0
11
+ time: 0.0
12
+ }
13
+ suites: []
14
+ }
15
+
16
+ specCount: 0
17
+ currentSpecs: []
18
+ nestedSuiteResults: {}
19
+
20
+ # Report the start of a spec.
21
+ #
22
+ # @param spec [jasmine.Spec] the spec
23
+ #
24
+ reportSpecStarting: (spec) ->
25
+ console.log "SPEC_START: #{ spec.id }"
26
+
27
+ # Report results from a spec.
28
+ #
29
+ # @param spec [jasmine.Spec] the spec
30
+ #
31
+ reportSpecResults: (spec) ->
32
+ unless spec.results().skipped
33
+ specResult = {
34
+ id: spec.id
35
+ description: spec.description
36
+ passed: spec.results().failedCount is 0
37
+ }
38
+
39
+ if spec.results().failedCount isnt 0
40
+ messages = []
41
+ messages.push result.message for result in spec.results().getItems()
42
+ specResult['messages'] = messages if messages.length isnt 0
43
+
44
+ @specCount += 1
45
+ @currentSpecs.push specResult
46
+
47
+ # Report results from a suite.
48
+ #
49
+ # @param suite [jasmine.Suite] the suite
50
+ #
51
+ reportSuiteResults: (suite) ->
52
+ unless suite.results().skipped
53
+ suiteResult = {
54
+ id: suite.id
55
+ parent: suite.parentSuite?.id
56
+ description: suite.description
57
+ passed: suite.results().failedCount is 0
58
+ specs: @currentSpecs
59
+ suites: []
60
+ }
61
+
62
+ if suite.parentSuite?
63
+ parent = suite.parentSuite.id
64
+ @nestedSuiteResults[parent] = [] unless @nestedSuiteResults[parent]
65
+ @nestedSuiteResults[parent].push suiteResult
66
+ else
67
+ @addNestedSuites suiteResult
68
+ @removeEmptySuites suiteResult
69
+
70
+ if suiteResult.specs.length isnt 0 || suiteResult.suites.length isnt 0
71
+ @runnerResult.suites.push suiteResult
72
+
73
+ @currentSpecs = []
74
+
75
+ # Report results from the runner.
76
+ #
77
+ # @param runner [jasmine.Runner] the runner
78
+ #
79
+ reportRunnerResults: (runner) ->
80
+ runtime = (new Date().getTime() - @startTime) / 1000
81
+
82
+ @runnerResult['passed'] = runner.results().failedCount is 0
83
+
84
+ @runnerResult['stats'] = {
85
+ specs: @specCount
86
+ failures: runner.results().failedCount
87
+ time: runtime
88
+ }
89
+
90
+ console.log "RUNNER_RESULT: #{ JSON.stringify(@runnerResult) }"
91
+
92
+ # Report the start of the runner
93
+ #
94
+ # @param runner [jasmine.Runner] the runner
95
+ #
96
+ reportRunnerStarting: (runner) ->
97
+ @startTime = new Date().getTime()
98
+
99
+ # Add all nested suites that have previously
100
+ # been processed.
101
+ #
102
+ # @param suiteResult [Object] the suite result
103
+ #
104
+ addNestedSuites: (suiteResult) ->
105
+ if @nestedSuiteResults[suiteResult.id]
106
+ for suite in @nestedSuiteResults[suiteResult.id]
107
+ @addNestedSuites suite
108
+ suiteResult.suites.push suite
109
+
110
+ # Removes suites without child suites or specs.
111
+ #
112
+ # @param suiteResult [Object] the suite result
113
+ #
114
+ removeEmptySuites: (suiteResult) ->
115
+ suites = []
116
+
117
+ for suite in suiteResult.suites
118
+ @removeEmptySuites suite
119
+
120
+ suites.push suite if suite.suites.length isnt 0 || suite.specs.length isnt 0
121
+
122
+ suiteResult.suites = suites
123
+
124
+ # Log a message
125
+ #
126
+ # @param message [String] the log message
127
+ #
128
+ log: (message) ->
129
+
130
+
131
+ if typeof module isnt 'undefined' and module.exports
132
+ module.exports = ConsoleReporter
133
+ else
134
+ window.ConsoleReporter = ConsoleReporter
@@ -0,0 +1,125 @@
1
+ sinon = require 'sinon'
2
+ {expect} = require 'chai'
3
+
4
+ Console = require '../src/console'
5
+
6
+ describe 'console', ->
7
+ beforeEach ->
8
+ @log = sinon.stub()
9
+ @console = { log: @log }
10
+
11
+ new Console(@console)
12
+
13
+ describe '#log', ->
14
+ describe 'with strings', ->
15
+ it 'logs a single string', ->
16
+ @console.log 'Hello logger'
17
+ expect(@log.args[0][0]).to.equal 'Hello logger'
18
+
19
+ it 'concatenates multipe strings', ->
20
+ @console.log 'Hello logger', 'We welcome you', 'Here on Earth'
21
+ expect(@log.args[0][0]).to.equal 'Hello logger We welcome you Here on Earth'
22
+
23
+ it 'replaces a single %s', ->
24
+ @console.log 'Hello %s!', 'logger'
25
+ expect(@log.args[0][0]).to.equal 'Hello logger!'
26
+
27
+ it 'replaces multiple %s', ->
28
+ @console.log 'Hello %s, we welcome you to %s!', 'logger', 'Switzerland'
29
+ expect(@log.args[0][0]).to.equal 'Hello logger, we welcome you to Switzerland!'
30
+
31
+ it 'attaches %s surplus strings', ->
32
+ @console.log 'Hello %s, we welcome you to Switzerland!', 'logger', 'Yay!'
33
+ expect(@log.args[0][0]).to.equal 'Hello logger, we welcome you to Switzerland! Yay!'
34
+
35
+ describe 'with numbers', ->
36
+ it 'logs a single number', ->
37
+ @console.log 1
38
+ expect(@log.args[0][0]).to.equal '1'
39
+
40
+ it 'concatenates multipe numbers', ->
41
+ @console.log 1, 2, 3, 4
42
+ expect(@log.args[0][0]).to.equal '1 2 3 4'
43
+
44
+ it 'replaces a single %d', ->
45
+ @console.log 'Hello %d!', 1
46
+ expect(@log.args[0][0]).to.equal 'Hello 1!'
47
+
48
+ it 'replaces a single %i', ->
49
+ @console.log 'Hello %i!', 3
50
+ expect(@log.args[0][0]).to.equal 'Hello 3!'
51
+
52
+ it 'replaces multiple %d', ->
53
+ @console.log 'I can count %d, %d and %d!', 1, 2, 3
54
+ expect(@log.args[0][0]).to.equal 'I can count 1, 2 and 3!'
55
+
56
+ it 'replaces multiple %i', ->
57
+ @console.log 'I can count reverse %i, %i and %i!', 3, 2, 1
58
+ expect(@log.args[0][0]).to.equal 'I can count reverse 3, 2 and 1!'
59
+
60
+ it 'attaches %d surplus numbers', ->
61
+ @console.log 'Hello %d!', 1, 2, 3
62
+ expect(@log.args[0][0]).to.equal 'Hello 1! 2 3'
63
+
64
+ it 'attaches %i surplus numbers', ->
65
+ @console.log 'Hello %i!', 1, 2, 3
66
+ expect(@log.args[0][0]).to.equal 'Hello 1! 2 3'
67
+
68
+ describe 'with objects', ->
69
+ it 'logs a boolean', ->
70
+ @console.log true, false
71
+ expect(@log.args[0][0]).to.equal 'true false'
72
+
73
+ it 'logs a date', ->
74
+ @console.log new Date('Thu Mar 08 2012 20:28:56 GMT+0100 (CET)')
75
+ expect(@log.args[0][0]).to.equal 'Thu Mar 08 2012 20:28:56 GMT+0100 (CET)'
76
+
77
+ it 'logs an array', ->
78
+ @console.log [1, 2, 3, 4]
79
+ expect(@log.args[0][0]).to.equal '[1, 2, 3, 4]'
80
+
81
+ it 'logs an object', ->
82
+ @console.log { a: 1 }
83
+ expect(@log.args[0][0]).to.equal '{ a: 1 }'
84
+
85
+ it 'logs a nested object', ->
86
+ @console.log "Hello object %o. Nice to meet you", { a: 1, b: { x: 1 } }
87
+ expect(@log.args[0][0]).to.equal 'Hello object { a: 1, b: { x: 1 } }. Nice to meet you'
88
+
89
+ it 'logs a nested object until depth 2', ->
90
+ @console.log "Hello object %o. Nice to meet you", { a: 1, b: { x: { a: 1, b: { x: 1 } } } }
91
+ expect(@log.args[0][0]).to.equal 'Hello object { a: 1, b: { x: { a: 1, b: [Object] } } }. Nice to meet you'
92
+
93
+ describe 'with an Object that implements toString()', ->
94
+ it '%s logs the custom string representation', ->
95
+ @console.log 'I have a toString(): %s!', { toString: -> '[Yepa]' }
96
+ expect(@log.args[0][0]).to.equal 'I have a toString(): [Yepa]!'
97
+
98
+ it '%o logs the custom string representation', ->
99
+ @console.log 'I have a toString(): %o!', { toString: -> '[Yepa]' }
100
+ expect(@log.args[0][0]).to.equal 'I have a toString(): [Yepa]!'
101
+
102
+ describe 'with an Object that implements toJSON()', ->
103
+ it '%o logs the custom JSON representation', ->
104
+ @console.log 'I have a toJSON(): %o!', { toJSON: -> { a: 1 } }
105
+ expect(@log.args[0][0]).to.equal 'I have a toJSON(): { a: 1 }!'
106
+
107
+ describe '#info', ->
108
+ it 'prefixes a string with INFO', ->
109
+ @console.info true, { a: 1 }, 'Hello logger'
110
+ expect(@log.args[0][0]).to.equal 'INFO: true { a: 1 } Hello logger'
111
+
112
+ describe '#warn', ->
113
+ it 'prefixes a string with WARN', ->
114
+ @console.warn true, { a: 1 }, 'Hello logger'
115
+ expect(@log.args[0][0]).to.equal 'WARN: true { a: 1 } Hello logger'
116
+
117
+ describe '#error', ->
118
+ it 'prefixes a string with ERROR', ->
119
+ @console.error true, { a: 1 }, 'Hello logger'
120
+ expect(@log.args[0][0]).to.equal 'ERROR: true { a: 1 } Hello logger'
121
+
122
+ describe '#debug', ->
123
+ it 'prefixes a string with DEBUG', ->
124
+ @console.debug true, { a: 1 }, 'Hello logger'
125
+ expect(@log.args[0][0]).to.equal 'DEBUG: true { a: 1 } Hello logger'