Skip to content

Commit

Permalink
feat(formatters): add console duration formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Oct 12, 2013
1 parent 1843172 commit 429146b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions specs/support/fixtures/formatters/duration.txt
@@ -0,0 +1,3 @@
Specs loaded in 0.002s
Finished in 0.004s

25 changes: 25 additions & 0 deletions specs/units/formatters/console/duration_formatter.spec.coffee
@@ -0,0 +1,25 @@
DurationFormatter = spectacular.formatters.console.DurationFormatter

describe DurationFormatter, ->
fixture 'formatters/duration.txt', as: 'expected'

given 'options', ->
{
colors: false
}

given 'runner', ->
{
options: @options
loadStartedAt: { getTime: -> 0 }
loadEndedAt: { getTime: -> 2 }
specsStartedAt: { getTime: -> 0 }
specsEndedAt: { getTime: -> 4 }
}

given 'formatter', -> new DurationFormatter @runner

subject -> @formatter.format()

itBehavesLike 'a formatter'

27 changes: 27 additions & 0 deletions src/formatters/console.coffee
Expand Up @@ -11,6 +11,33 @@ class spectacular.formatters.console.SeedFormatter
promise.resolve " Seed #{spectacular.utils.colorize @seed.toString(), 'cyan', @options.colors}\n\n"

promise

class spectacular.formatters.console.DurationFormatter
constructor: (@runner) ->
{@options} = @runner

format: ->
promise = new spectacular.Promise

{loadStartedAt, loadEndedAt, specsStartedAt, specsEndedAt} = @runner

if loadStartedAt? and loadEndedAt?
loadDuration = @formatDuration loadStartedAt, loadEndedAt

specsDuration = @formatDuration specsStartedAt, specsEndedAt

res = ''
res += " Specs loaded in #{loadDuration}\n" if loadDuration?
res += " Finished in #{specsDuration}\n\n"

promise.resolve res

promise

formatDuration: (start, end) ->
duration = (end.getTime() - start.getTime()) / 1000
spectacular.utils.colorize "#{Math.max 0, duration}s", 'yellow', @options.colors

class spectacular.formatters.console.ErrorStackFormatter
constructor: (@stack, @options) ->
@parser = new spectacular.errors.ErrorParser @stack
Expand Down

0 comments on commit 429146b

Please sign in to comment.