# all files / files/utils/ FeatureLexerFormatter.js

98.91% Statements 91/92
100% Branches 30/30
95% Functions 19/20
96.88% Lines 31/32
22 statements, 1 function, 17 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81

    1
    1
    1

    1
    17
    17

    17

    16

    11

    1
    1
    1

    1
    1
    1

    1
    1

    1
    1
    1

    1
    1
    1

    3
    1

    2

    1
    1
    1

    5

     'use strict';

    // Constants:
    import constants from '../../constants';
    const IN_ORDER_TO = /^In order to /;
    const AS_A = /^As a /;
    const I_WANT = /^I want /;

    // Utilities:
    import _ from 'lodash';

    export default class FeatureLexerFormatter {
        constructor () {
            this.features = [];
            this.tags = [];

            /* eslint-disable camelcase */
            this.comment = this.doc_string = this.examples = this.eof = _.noop;
            /* eslint-enable camelcase */
        }

        get lastFeature () {
            return _.last(this.features);
        }
        get lastElement () {
            return _.last(this.lastFeature.elements);
        }

        tag (value) {
            this.tags.push(value);
        }

        feature (type, name, description) {
            let [inOrderTo, asA, iWant] = description.split(constants.FEATURE_NEWLINE);
            inOrderTo = inOrderTo.replace(IN_ORDER_TO, '').replace(/\r/g, '');
            asA = asA.replace(AS_A, '').replace(/\r/g, '');
            iWant = iWant.replace(I_WANT, '');

            let feature = { type, name, inOrderTo, asA, iWant, elements: [], tag: this.tags };
            this.features.push(feature);
            this.tags = []
        }

        background (type, name, description) {
            let background = { type, name, description, examples: [], stepDeclarations: [] };
            this.lastFeature.elements.push(background);
        }

        scenario (type, name, description) {
            let scenario = { type, name, description, examples: [], stepDeclarations: [], tag: this.tags };
            this.lastFeature.elements.push(scenario);
            this.tags = [];
        }

        /* eslint-disable camelcase */
        scenario_outline (type, name, description) {
            let scenario_outline = { type, name, description, examples: [], stepDeclarations: [], tag: this.tags };
            this.lastFeature.elements.push(scenario_outline);
            this.tags = [];
        }
        /* eslint-enable camelcase */

        row (row) {
            if (this.lastElement.variables) {
                this.lastElement.examples.push(row);
            } else {
                this.lastElement.variables = row;
            }
        }

        step (type, step) {
            type = type.replace(/ $/, '');
            let stepDeclaration = { type, step };
            this.lastElement.stepDeclarations.push(stepDeclaration);
        }

        done () {
            return this.features;
        }
    }