-{assert} = require('chai')
-{exec} = require('child_process')
+{assert} = require 'chai'
+{exec} = require 'child_process'
 express = require 'express'
 clone = require 'clone'
 

      page: 1
      items: [incorrectUserPayload]

    before((done) ->
      app = express()

      # Attaching endpoint for each testing scenario
      app.get('/bricks/XYZ42', (req, res) ->
        res.setHeader('Content-Type', 'application/json')
        res.status(200).send(incorrectBrickTypePayload)
      )
      app.post('/bricks', (req, res) ->
        res.setHeader('Content-Type', 'application/json')
        res.status(200).send(incorrectBrickTypePayload)
      )
      app.get('/customers', (req, res) ->
        res.setHeader('Content-Type', 'application/json')
        res.status(200).send(incorrectUserArrayPayload)
      )
      app.post('/customers', (req, res) ->
        res.setHeader('Content-Type', 'application/json')
        res.status(200).send(incorrectUserPayload)
      )

      # Spinning up the Express server, running Dredd, and saving results
      server = app.listen(PORT, ->
        runDredd('./test/fixtures/regression-319.apib', (err, result) ->
          results = parseOutput(result.stdout)
          server.close(done)
        )
      )
    )

    it('outputs failures', ->
      assert.ok(results.failures.length)
    )
    it('results in exactly four tests', ->
      assert.include(results.summary, '4 total')
    )
    it('results in four failing tests', ->
      assert.include(results.summary, '4 failing')
    )

    describe('Attributes defined in resource are referenced from payload [GET /bricks/XYZ42]', ->
      it('fails on missing required property and invalid type', ->
        assert.include(results.failures[0], 'GET /bricks/XYZ42')
        assert.include(results.failures[1], 'Missing required property: name')
        assert.include(results.failures[1], 'Invalid type: number')
      )
      it('has no request body', ->
        assert.isUndefined(results.bodies[0])
      )
      it('has correct ‘expected’ response body', ->
        assert.deepEqual(results.bodies[1], brickTypePayload)
      )
      it('has incorrect ‘actual’ response body', ->
        assert.deepEqual(results.bodies[2], incorrectBrickTypePayload)
      )
      it('has correct schema', ->
        assert.deepEqual(results.schemas[0], brickTypeSchema)
      )
    )
    describe('Attributes defined in resource are referenced from action [POST /bricks]', ->
      it('fails on missing required property and invalid type', ->
        assert.include(results.failures[2], 'POST /bricks')
        assert.include(results.failures[3], 'Missing required property: name')
        assert.include(results.failures[3], 'Invalid type: number')
      )
      it('has correct request body', ->
        assert.deepEqual(results.bodies[3], brickTypePayload)
      )
      it('has correct ‘expected’ response body', ->
        assert.deepEqual(results.bodies[4], brickTypePayload)
      )
      it('has incorrect ‘actual’ response body', ->
        assert.deepEqual(results.bodies[5], incorrectBrickTypePayload)
      )
      it('has correct schema', ->
        assert.deepEqual(results.schemas[1], brickTypeSchema)
      )
    )
    describe('Attributes defined as data structure are referenced from payload [GET /customers]', ->
      it('fails on invalid type', ->
        assert.include(results.failures[4], 'GET /customers')
        assert.include(results.failures[5], 'Invalid type: object')
      )
      it('has no request body', ->
        assert.isUndefined(results.bodies[6])
      )
      it('has correct ‘expected’ response body', ->
        assert.deepEqual(results.bodies[7], userArrayPayload)
      )
      it('has incorrect ‘actual’ response body', ->
        assert.deepEqual(results.bodies[8], incorrectUserArrayPayload)
      )
      it('has correct schema', ->
        assert.deepEqual(results.schemas[2], userArraySchema)
      )
    )
    describe('Attributes defined as data structure are referenced from action [POST /customers]', ->
      it('fails on invalid types', ->
        assert.include(results.failures[6], 'POST /customers')
        assert.include(results.failures[7], 'Invalid type: null')
        assert.include(results.failures[7], 'Invalid type: string')
      )
      it('has correct request body', ->
        assert.deepEqual(results.bodies[9], userPayload)
      )
      it('has correct ‘expected’ response body', ->
        assert.deepEqual(results.bodies[10], userPayload)
      )
      it('has incorrect ‘actual’ response body', ->
        assert.deepEqual(results.bodies[11], incorrectUserPayload)
      )
      it('has correct schema', ->
        assert.deepEqual(results.schemas[3], userSchema)
      )
    )
  )
)

  # Parse individual entries (deals also with multi-line entries)
  entries = []
  entry = undefined
  for line in output.split /\r?\n/
    match = line.match /^(\w+): (.+)?$/
    if match
      if entry
        entry.body = entry.body.trim()
        entries.push entry
      entry = {label: match[1], body: match[2] or ''}
    else
      entry.body += "\n#{line.trim()}"
