# all files / files/ FeatureFile.js

100% Statements 96/96
100% Branches 36/36
100% Functions 17/17
100% Lines 18/18
17 statements, 16 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

    3

    1
    1

    4
    1
    1

    4

    3
    3

    1
    1

    1
    5

    5

    5
    5
    5

     'use strict';

    // Constants:
    import constants from '../constants';

    // Utilities:
    import os from 'os';

    // Dependencies:
    import FeatureLexerFormatter from './utils/FeatureLexerFormatter';
    import File from './File';
    import gherkin from 'gherkin';
    import StepDefinitionGenerator from './utils/StepDefinitionGenerator';

    // Errors:
    import TractorError from '../errors/TractorError';

    export default class FeatureFile extends File {
        read () {
            return super.read()
            .then(() => generateTokens.call(this))
            .catch((error) => {
                console.error(error);
                throw new TractorError(`Lexing "${this.path}" failed.`, constants.REQUEST_ERROR);
            });
        }

        save (data) {
            if (data) {
                data = data.replace(constants.FEATURE_NEWLINE, os.EOL);
                this.content = data;
            }
            return super.save()
            // this is workaround for filestructure to hold tokens for new steps in a feature.
            .then(() => generateTokens.call(this))
            .then(() => {
                let generator = new StepDefinitionGenerator(this);
                return generator.generate();
            })
            .catch((error) => {
                console.error(error);
                throw new TractorError(`Generating step definitions from "${this.path}" failed.`, constants.REQUEST_ERROR);
            });
        }
    }

    function generateTokens () {
        let formatter = new FeatureLexerFormatter();
        /* eslint-disable new-cap */
        let EnLexer = gherkin.Lexer('en');
        /* eslint-enable new-cap */
        let enLexer = new EnLexer(formatter);
        enLexer.scan(this.content);
        this.tokens = formatter.done();
    }