{"size": 2106, "ext": "js", "max_stars_repo_path": "lib/mocha-fonteva-data-generator/lib/classy/inject.js", "max_stars_repo_name": "yury-sannikov/funcon", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/mocha-fonteva-data-generator/lib/classy/inject.js", "max_issues_repo_name": "yury-sannikov/funcon", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/mocha-fonteva-data-generator/lib/classy/inject.js", "max_forks_repo_name": "yury-sannikov/funcon", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.4, "max_line_length": 101, "alphanum_fraction": 0.6571699905}
import { validateCheckIsAsync } from '../generate'
import { annotes } from './decorators'

export const EMPTY_INJECTABLE_DESCRIPTOR = {
    repeatCount: 1,
    overrideIndices: null
}
export const inject = (...injectable) => {
    let context = Object.assign({}, EMPTY_INJECTABLE_DESCRIPTOR);

    const injectDecorator = injectDecoratorFactory(context, injectable);

    // Set up how many times new set of data should be generated
    injectDecorator.times = (repeatCount) => {
        context.repeatCount = repeatCount;
        return injectDecorator;
    }

    // Partial override previously injected values at specified indices. Arity should match
    injectDecorator.byOverrideAt = (...indices) => {
        context.overrideIndices = indices;
        return injectDecorator;
    }

    return injectDecorator;
}

function checkAsync(injectable, descriptor) {
    const isAsync = validateCheckIsAsync(injectable, descriptor.value);

    if (descriptor.value[annotes.asyncTest] === undefined) {
        descriptor.value[annotes.asyncTest] = isAsync
    } else {
        if (descriptor.value[annotes.asyncTest] !== isAsync) {
            throw new Error('Inconsistent async test function state')
        }
    }
}

function injectDecoratorFactory(ctx, injectable) {
    return function injectDecorator(target, name, descriptor) {

        if (!ctx.overrideIndices) {
            checkAsync(injectable, descriptor);
        } else {
            if (ctx.overrideIndices.length !== injectable.length) {
                throw new Error('Override indices arity should match with amount of injecting items')
            }
        }

        let injectableDescriptor = []
        if (descriptor.value[annotes.inject]) {
            injectableDescriptor = descriptor.value[annotes.inject];
        }
        injectableDescriptor.push({
            injectable,
            repeatCount: ctx.repeatCount,
            overrideIndices: ctx.overrideIndices,
            title: descriptor.value[annotes.it]
        })
        descriptor.value[annotes.inject] = injectableDescriptor;
        return descriptor
    }
}
