<commit_before>import { pumlhorse } from '../../PumlhorseGlobal';
import * as _ from 'underscore';
import enforce from '../../util/enforce';

export class AssertModule {
    public static isTrue(result): void {
        if (result === true || result === 'true') return;
        throw AssertModule.getExpectedError('isTrue', true, result);
    }
    
    public static isFalse(result): void {
        if (result === false || result === 'false') return;
        throw AssertModule.getExpectedError('isFalse', false, result);
    }
    
    public static isNull(result): void {
        if (result === null || result === undefined) return;
        throw AssertModule.getMessageError('isNull', 'Value is not null');
    }
    
    public static isNotNull(result): void {
        if (result !== null && result !== undefined) return;
        throw AssertModule.getMessageError('isNotNull', 'Value is null');
    }
    
    public static areEqual(expected: any, actual: any, partial: boolean): void {
        if (AssertModule.areEqualInternal(expected, actual, partial)) return;        

        throw AssertModule.getExpectedError('areEqual', expected, actual);
    }
    
    public static areNotEqual(expected: any, actual: any): void {
        if (!AssertModule.areEqualInternal(expected, actual, false)) return;        

        throw AssertModule.getExpectedError('areNotEqual', expected, actual);
    }
    
    public static isEmpty(actual: any[]): void {
        enforce(actual)
            .isNotNull()
            .isArray();    

        if (actual.length != 0) throw AssertModule.getExpectedError('isEmpty', 0, actual.length);
    }
    
    public static isNotEmpty(actual: any): void {
        enforce(actual)
            .isNotNull()
            .isArray();    

        if (actual.length == 0) throw AssertModule.getMessageError('isNotEmpty', 'Array is empty');
    }
    
    public static contains(array: any[], expectedValue: any, partial: boolean): void {
        enforce(array, 'array')
            .isNotNull()
            .isArray();    

        if (array.length == 0) throw AssertModule.getMessageError('contains', 'Array is empty');

        let isFound = false;
        array.forEach(val => {
            if (AssertModule.areEqualInternal(expectedValue, val, partial)) {
                isFound = true;
                return;
            }
        });

        var val = AssertModule.serializeParameter(expectedValue);
        if (!isFound) throw AssertModule.getMessageError('contains', `Array did not contain ${val}`)
    }

    private static getMessageError(command: string, message: string) {
        return new Error(`Assertion '${command}' failed. ${message}`);
    }

    private static getExpectedError(command: string, expected: any, actual: any) {
        const exp = AssertModule.serializeParameter(expected);
        const act = this.serializeParameter(actual);
        return new Error(`Assertion '${command}' failed. Expected '${exp}', actual '${act}'`)
    }

    private static serializeParameter(val: any): string {
        if (_.isObject(val) ||
            _.isArray(val))
        {
            return JSON.stringify(val);
        }

        return val;
    }

    private static areEqualInternal(expected: any, actual: any, partial: boolean) {
        if (partial) {
            if (_.isMatch(actual, expected)) return true;
        }
        else {
            if (_.isEqual(actual, expected)) return true;
        }

        return false;
    }

}

pumlhorse.module('assert')
    .function('isTrue', ['$all', AssertModule.isTrue])
    .function('isFalse', ['$all', AssertModule.isFalse])
    .function('isNull', ['$all', AssertModule.isNull])
    .function('isNotNull', ['$all', AssertModule.isNotNull])
    .function('areEqual', AssertModule.areEqual)
    .function('areNotEqual', AssertModule.areNotEqual)
    .function('isEmpty', ['$all', AssertModule.isEmpty])
    .function('isNotEmpty', ['$all', AssertModule.isNotEmpty])
    .function('contains', ['array', 'value', 'partial', AssertModule.contains]);<commit_msg>Added fail function<commit_after>import { pumlhorse } from '../../PumlhorseGlobal';
import * as _ from 'underscore';
import enforce from '../../util/enforce';

export class AssertModule {
    public static isTrue(result): void {
        if (result === true || result === 'true') return;
        throw AssertModule.getExpectedError('isTrue', true, result);
    }
    
    public static isFalse(result): void {
        if (result === false || result === 'false') return;
        throw AssertModule.getExpectedError('isFalse', false, result);
    }
    
    public static isNull(result): void {
        if (result === null || result === undefined) return;
        throw AssertModule.getMessageError('isNull', 'Value is not null');
    }
    
    public static isNotNull(result): void {
        if (result !== null && result !== undefined) return;
        throw AssertModule.getMessageError('isNotNull', 'Value is null');
    }
    
    public static areEqual(expected: any, actual: any, partial: boolean): void {
        if (AssertModule.areEqualInternal(expected, actual, partial)) return;        

        throw AssertModule.getExpectedError('areEqual', expected, actual);
    }
    
    public static areNotEqual(expected: any, actual: any): void {
        if (!AssertModule.areEqualInternal(expected, actual, false)) return;        

        throw AssertModule.getExpectedError('areNotEqual', expected, actual);
    }
    
    public static isEmpty(actual: any[]): void {
        enforce(actual)
            .isNotNull()
            .isArray();    

        if (actual.length != 0) throw AssertModule.getExpectedError('isEmpty', 0, actual.length);
    }
    
    public static isNotEmpty(actual: any): void {
        enforce(actual)
            .isNotNull()
            .isArray();    

        if (actual.length == 0) throw AssertModule.getMessageError('isNotEmpty', 'Array is empty');
    }
    
    public static contains(array: any[], expectedValue: any, partial: boolean): void {
        enforce(array, 'array')
            .isNotNull()
            .isArray();    

        if (array.length == 0) throw AssertModule.getMessageError('contains', 'Array is empty');

        let isFound = false;
        array.forEach(val => {
            if (AssertModule.areEqualInternal(expectedValue, val, partial)) {
                isFound = true;
                return;
            }
        });

        var val = AssertModule.serializeParameter(expectedValue);
        if (!isFound) throw AssertModule.getMessageError('contains', `Array did not contain ${val}`)
    }

    public static fail(message) {
        throw new Error(message == null ? "Fail called" : message);
    }

    private static getMessageError(command: string, message: string) {
        return new Error(`Assertion '${command}' failed. ${message}`);
    }

    private static getExpectedError(command: string, expected: any, actual: any) {
        const exp = AssertModule.serializeParameter(expected);
        const act = this.serializeParameter(actual);
        return new Error(`Assertion '${command}' failed. Expected '${exp}', actual '${act}'`)
    }

    private static serializeParameter(val: any): string {
        if (_.isObject(val) ||
            _.isArray(val))
        {
            return JSON.stringify(val);
        }

        return val;
    }

    private static areEqualInternal(expected: any, actual: any, partial: boolean) {
        if (partial) {
            if (_.isMatch(actual, expected)) return true;
        }
        else {
            if (_.isEqual(actual, expected)) return true;
        }

        return false;
    }

}

pumlhorse.module('assert')
    .function('isTrue', ['$all', AssertModule.isTrue])
    .function('isFalse', ['$all', AssertModule.isFalse])
    .function('isNull', ['$all', AssertModule.isNull])
    .function('isNotNull', ['$all', AssertModule.isNotNull])
    .function('areEqual', AssertModule.areEqual)
    .function('areNotEqual', AssertModule.areNotEqual)
    .function('isEmpty', ['$all', AssertModule.isEmpty])
    .function('isNotEmpty', ['$all', AssertModule.isNotEmpty])
    .function('contains', ['array', 'value', 'partial', AssertModule.contains])
    .function('fail', AssertModule.fail);
{'fraction_non_alphanumeric': 0.1357160106254528, 'fraction_numerical': 0.0009659502535619416, 'mean_word_length': 2.699419383653417, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1527383', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2215, 'n_words': 575}