Completed
Push — dev ( d30456...4fd03f )
by Fike
33s
created

test/support/fixture.js   A

Complexity

Total Complexity 14
Complexity/F 1.56

Size

Lines of Code 61
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 61
rs 10
wmc 14
mnd 3
bc 13
fnc 9
bpm 1.4443
cpm 1.5555
noi 1
1
/* global allure */
2
/* eslint-env mocha */
3
4
var Path = require('path')
5
var FileSystem = require('fs')
6
var Yaml = require('js-yaml')
7
var Chai = require('chai')
8
var expect = Chai.expect
9
10
var root = Path.join(__dirname, '..')
11
var sources = Path.join(root, 'fixture')
12
13
function Fixture (method, definition) {
14
  this.apply = function (factory) {
15
    for (var i = 0; i < definition.length; i++) {
16
      var suite = definition[i]
17
      for (var j = 0; j < suite.tests.length; j++) {
18
        (function (suite, j) {
19
          var replacements = {index: j}
20
          var statement = suite.name || 'complies to assertion {index}'
21
          Object.keys(replacements).forEach(function (key) {
22
            statement = statement.replace('{' + key + '}', replacements[key])
23
          })
24
          var test = suite.tests[j]
25
          it(statement, function () {
26
            var input = test.input
27
            var output = test.output
28
            for (var k = 0; k < input.length; k++) {
29
              allure.addArgument(k.toString(), JSON.stringify(input[k]))
30
            }
31
            var expectation = Yaml.safeDump(output || null)
32
            var name = 'expectation.yml'
33
            allure.createAttachment(name, expectation, 'application/x-yaml')
34
            var object = factory.call ? factory.call() : factory
35
            expect(object[method].apply(factory, input)).to.deep.eq(output)
36
          })
37
        })(suite, j)
38
      }
39
    }
40
  }
41
}
42
43
function Fixtures (source, feature) {
44
  var location = Path.join(sources, source, feature + '.yml')
45
  var content = FileSystem.readFileSync(location)
46
  var definitions = Yaml.safeLoad(content)
47
  var self = this
48
49
  Object.keys(definitions).forEach(function (method) {
50
    self[method] = new Fixture(method, definitions[method])
51
  })
52
53
  this.apply = function (object) {
54
    Object.keys(definitions).forEach(function (method) {
55
      describe('.' + method, function () {
56
        self[method].apply(object)
57
      })
58
    })
59
  }
60
}
61
62
module.exports = {
63
  Fixtures: Fixtures
64
}
65