Skip to content

Commit

Permalink
fix(formula): fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Oct 11, 2024
1 parent 686d922 commit 6402b26
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import { describe, expect, it } from 'vitest';

import { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Regexextract } from '../index';
import { BooleanValueObject, NullValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Regexextract } from '../index';

describe('Test regexextract function', () => {
const testFunction = new Regexextract(FUNCTION_NAMES_TEXT.REGEXEXTRACT);
Expand All @@ -31,45 +32,45 @@ describe('Test regexextract function', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = StringValueObject.create('c.*f');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual('cdef');
expect(getObjectValue(result)).toStrictEqual('cdef');
});

it('RegularExpression is maybe backtrace', () => {
const text = StringValueObject.create('https://www.example.com');
const regularExpression = StringValueObject.create('^(https?://)?([a-z0-9.-]+).*');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.REF);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
});

it('Value is boolean', () => {
const text = BooleanValueObject.create(true);
const regularExpression = StringValueObject.create('c.*f');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.NA);
expect(getObjectValue(result)).toStrictEqual(ErrorType.NA);

const text2 = StringValueObject.create('abcdefg');
const regularExpression2 = BooleanValueObject.create(true);
const result2 = testFunction.calculate(text2, regularExpression2);
expect(result2.getValue()).toStrictEqual(ErrorType.NA);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);
});

it('Value is blank cell', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = NullValueObject.create();
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual('');
expect(getObjectValue(result)).toStrictEqual('');

const text2 = NullValueObject.create();
const regularExpression2 = StringValueObject.create('c.*f');
const result2 = testFunction.calculate(text2, regularExpression2);
expect(result2.getValue()).toStrictEqual(ErrorType.NA);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.NA);
});

it('Value is error', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = ErrorValueObject.create(ErrorType.NAME);
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);
});

it('Value is array', () => {
Expand All @@ -86,49 +87,56 @@ describe('Test regexextract function', () => {
});
const regularExpression = StringValueObject.create('c.*f');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
});

it('Result length > 1', () => {
let text = StringValueObject.create('我的生日20220104');
let regularExpression = StringValueObject.create('生日(2022\\d+)');
let text = StringValueObject.create('111aaa111');
let regularExpression = StringValueObject.create('(\\d+)([a-z]+)(\\d+)');
let result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual('20220104');

regularExpression = StringValueObject.create('生日(?:2022)(\\d+)');
result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual('0104');

text = StringValueObject.create('111aaa111');
regularExpression = StringValueObject.create('(\\d+)([a-z]+)(\\d+)');
result = testFunction.calculate(text, regularExpression);
expect((result as ArrayValueObject).toValue()).toStrictEqual([
expect(getObjectValue(result)).toStrictEqual([
['111', 'aaa', '111'],
]);

text = StringValueObject.create('abcd');
regularExpression = StringValueObject.create('((((a)b)c)d)');
result = testFunction.calculate(text, regularExpression);
expect((result as ArrayValueObject).toValue()).toStrictEqual([
expect(getObjectValue(result)).toStrictEqual([
['abcd', 'abc', 'ab', 'a'],
]);

text = StringValueObject.create('Visit our website at https://www.example.com');
regularExpression = StringValueObject.create('https?://[^\s]+');
result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual('https://www.example.com');
});

it('Is not valid or safe RegExp', () => {
let text = StringValueObject.create('');
let regularExpression = StringValueObject.create('[a-Z]+');
let result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.REF);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);

text = StringValueObject.create('abcd');
regularExpression = StringValueObject.create('((((a');
result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.REF);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
});

it('More test', () => {
let text = StringValueObject.create('我的生日20220104');
let regularExpression = StringValueObject.create('生日(2022\\d+)');
let result = testFunction.calculate(text, regularExpression);
expect(getObjectValue(result)).toStrictEqual('20220104');

regularExpression = StringValueObject.create('生日(?:2022)(\\d+)');
result = testFunction.calculate(text, regularExpression);
expect(getObjectValue(result)).toStrictEqual('0104');

text = StringValueObject.create('The total is 123.45 or € 987.65');
regularExpression = StringValueObject.create('[€]\d{1,3}(,\d{3})*(.\d{2})?');
result = testFunction.calculate(text, regularExpression);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);

text = StringValueObject.create('Visit our website at https://www.example.com');
regularExpression = StringValueObject.create('https?://[^\s]+');
result = testFunction.calculate(text, regularExpression);
expect(getObjectValue(result)).toStrictEqual('https://www.example.com');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import { describe, expect, it } from 'vitest';

import { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Regexmatch } from '../index';
import { BooleanValueObject, NullValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Regexmatch } from '../index';

describe('Test regexmatch function', () => {
const testFunction = new Regexmatch(FUNCTION_NAMES_TEXT.REGEXMATCH);
Expand All @@ -31,45 +32,45 @@ describe('Test regexmatch function', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = StringValueObject.create('c.*f');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(true);
expect(getObjectValue(result)).toStrictEqual(true);
});

it('RegularExpression is maybe backtrace', () => {
const text = StringValueObject.create('https://www.example.com');
const regularExpression = StringValueObject.create('^(https?://)?([a-z0-9.-]+).*');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.REF);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
});

it('Value is boolean', () => {
const text = BooleanValueObject.create(true);
const regularExpression = StringValueObject.create('c.*f');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(false);
expect(getObjectValue(result)).toStrictEqual(false);

const text2 = StringValueObject.create('abcdefg');
const regularExpression2 = BooleanValueObject.create(true);
const result2 = testFunction.calculate(text2, regularExpression2);
expect(result2.getValue()).toStrictEqual(false);
expect(getObjectValue(result2)).toStrictEqual(false);
});

it('Value is blank cell', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = NullValueObject.create();
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(true);
expect(getObjectValue(result)).toStrictEqual(true);

const text2 = NullValueObject.create();
const regularExpression2 = StringValueObject.create('c.*f');
const result2 = testFunction.calculate(text2, regularExpression2);
expect(result2.getValue()).toStrictEqual(false);
expect(getObjectValue(result2)).toStrictEqual(false);
});

it('Value is error', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = ErrorValueObject.create(ErrorType.NAME);
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);
});

it('Value is array', () => {
Expand All @@ -86,7 +87,14 @@ describe('Test regexmatch function', () => {
});
const regularExpression = StringValueObject.create('c.*f');
const result = testFunction.calculate(text, regularExpression);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
});

it('More test', () => {
const text = StringValueObject.create('13912345678');
const regularExpression = StringValueObject.create('^1[3456789]\\d{9}$');
const result = testFunction.calculate(text, regularExpression);
expect(getObjectValue(result)).toStrictEqual(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

import { describe, expect, it } from 'vitest';

import { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Regexreplace } from '../index';
import { BooleanValueObject, NullValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_TEXT } from '../../function-names';
import { Regexreplace } from '../index';

describe('Test regexreplace function', () => {
const testFunction = new Regexreplace(FUNCTION_NAMES_TEXT.REGEXREPLACE);
Expand All @@ -32,57 +33,57 @@ describe('Test regexreplace function', () => {
const regularExpression = StringValueObject.create('c.*f');
const replacement = StringValueObject.create('xyz');
const result = testFunction.calculate(text, regularExpression, replacement);
expect(result.getValue()).toStrictEqual('abxyzg');
expect(getObjectValue(result)).toStrictEqual('abxyzg');
});

it('RegularExpression is maybe backtrace', () => {
const text = StringValueObject.create('https://www.example.com');
const regularExpression = StringValueObject.create('^(https?://)?([a-z0-9.-]+).*');
const replacement = StringValueObject.create('xyz');
const result = testFunction.calculate(text, regularExpression, replacement);
expect(result.getValue()).toStrictEqual(ErrorType.REF);
expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
});

it('Value is boolean', () => {
const text = BooleanValueObject.create(true);
const regularExpression = StringValueObject.create('c.*f');
const replacement = StringValueObject.create('xyz');
const result = testFunction.calculate(text, regularExpression, replacement);
expect(result.getValue()).toStrictEqual('TRUE');
expect(getObjectValue(result)).toStrictEqual('TRUE');

const text2 = StringValueObject.create('abcdefg');
const regularExpression2 = BooleanValueObject.create(true);
const result2 = testFunction.calculate(text2, regularExpression2, replacement);
expect(result2.getValue()).toStrictEqual('abcdefg');
expect(getObjectValue(result2)).toStrictEqual('abcdefg');

const replacement2 = BooleanValueObject.create(true);
const result3 = testFunction.calculate(text2, regularExpression, replacement2);
expect(result3.getValue()).toStrictEqual('abTRUEg');
expect(getObjectValue(result3)).toStrictEqual('abTRUEg');
});

it('Value is blank cell', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = NullValueObject.create();
const replacement = StringValueObject.create('xyz');
const result = testFunction.calculate(text, regularExpression, replacement);
expect(result.getValue()).toStrictEqual('xyzaxyzbxyzcxyzdxyzexyzfxyzgxyz');
expect(getObjectValue(result)).toStrictEqual('xyzaxyzbxyzcxyzdxyzexyzfxyzgxyz');

const text2 = NullValueObject.create();
const regularExpression2 = StringValueObject.create('c.*f');
const result2 = testFunction.calculate(text2, regularExpression2, replacement);
expect(result2.getValue()).toStrictEqual('');
expect(getObjectValue(result2)).toStrictEqual('');

const replacement2 = NullValueObject.create();
const result3 = testFunction.calculate(text, regularExpression2, replacement2);
expect(result3.getValue()).toStrictEqual('abg');
expect(getObjectValue(result3)).toStrictEqual('abg');
});

it('Value is error', () => {
const text = StringValueObject.create('abcdefg');
const regularExpression = ErrorValueObject.create(ErrorType.NAME);
const replacement = StringValueObject.create('xyz');
const result = testFunction.calculate(text, regularExpression, replacement);
expect(result.getValue()).toStrictEqual(ErrorType.NAME);
expect(getObjectValue(result)).toStrictEqual(ErrorType.NAME);
});

it('Value is array', () => {
Expand All @@ -100,7 +101,21 @@ describe('Test regexreplace function', () => {
const regularExpression = StringValueObject.create('c.*f');
const replacement = StringValueObject.create('xyz');
const result = testFunction.calculate(text, regularExpression, replacement);
expect(result.getValue()).toStrictEqual(ErrorType.VALUE);
expect(getObjectValue(result)).toStrictEqual(ErrorType.VALUE);
});

it('More test', () => {
let text = StringValueObject.create('Sheets');
let regularExpression = StringValueObject.create('e{2}');
let replacement = StringValueObject.create('X');
let result = testFunction.calculate(text, regularExpression, replacement);
expect(getObjectValue(result)).toStrictEqual('ShXts');

text = StringValueObject.create('The event will be held on June 15, 2024.');
regularExpression = StringValueObject.create('\\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\\s+\\d{1,2},\\s+\\d{4}\\b');
replacement = StringValueObject.create('[REDACTED]');
result = testFunction.calculate(text, regularExpression, replacement);
expect(getObjectValue(result)).toStrictEqual('The event will be held on [REDACTED].');
});
});
});

0 comments on commit 6402b26

Please sign in to comment.