Browse Source

Update tests for condition Operators

pull/6419/head
mohamedsalem401 2 years ago
parent
commit
8a225b2ffa
  1. 39
      packages/core/test/specs/data_sources/model/conditional_variables/operators/BooleanOperator.ts
  2. 45
      packages/core/test/specs/data_sources/model/conditional_variables/operators/NumberOperator.ts
  3. 55
      packages/core/test/specs/data_sources/model/conditional_variables/operators/StringOperator.ts

39
packages/core/test/specs/data_sources/model/conditional_variables/operators/LogicalOperator.ts → packages/core/test/specs/data_sources/model/conditional_variables/operators/BooleanOperator.ts

@ -1,59 +1,66 @@
import {
LogicalOperator,
LogicalOperation,
} from '../../../../../../src/data_sources/model/conditional_variables/operators/LogicalOperator';
BooleanOperator,
BooleanOperation,
} from '../../../../../../src/data_sources/model/conditional_variables/operators/BooleanOperator';
import Editor from '../../../../../../src/editor/model/Editor';
import EditorModel from '../../../../../../src/editor/model/Editor';
describe('LogicalOperator', () => {
let em: EditorModel;
beforeEach(() => {
em = new Editor();
});
afterEach(() => {
em.destroy();
});
describe('Operator: and', () => {
test('should return true when all statements are true', () => {
const operator = new LogicalOperator(LogicalOperation.and);
const operator = new BooleanOperator(BooleanOperation.and, { em });
expect(operator.evaluate([true, true, true])).toBe(true);
});
test('should return false when at least one statement is false', () => {
const operator = new LogicalOperator(LogicalOperation.and);
const operator = new BooleanOperator(BooleanOperation.and, { em });
expect(operator.evaluate([true, false, true])).toBe(false);
});
});
describe('Operator: or', () => {
test('should return true when at least one statement is true', () => {
const operator = new LogicalOperator(LogicalOperation.or);
const operator = new BooleanOperator(BooleanOperation.or, { em });
expect(operator.evaluate([false, true, false])).toBe(true);
});
test('should return false when all statements are false', () => {
const operator = new LogicalOperator(LogicalOperation.or);
const operator = new BooleanOperator(BooleanOperation.or, { em });
expect(operator.evaluate([false, false, false])).toBe(false);
});
});
describe('Operator: xor', () => {
test('should return true when exactly one statement is true', () => {
const operator = new LogicalOperator(LogicalOperation.xor);
const operator = new BooleanOperator(BooleanOperation.xor, { em });
expect(operator.evaluate([true, false, false])).toBe(true);
});
test('should return false when more than one statement is true', () => {
const operator = new LogicalOperator(LogicalOperation.xor);
const operator = new BooleanOperator(BooleanOperation.xor, { em });
expect(operator.evaluate([true, true, false])).toBe(false);
});
test('should return false when no statement is true', () => {
const operator = new LogicalOperator(LogicalOperation.xor);
const operator = new BooleanOperator(BooleanOperation.xor, { em });
expect(operator.evaluate([false, false, false])).toBe(false);
});
});
describe('Edge Case Tests', () => {
test('should return false for xor with all false inputs', () => {
const operator = new LogicalOperator(LogicalOperation.xor);
const operator = new BooleanOperator(BooleanOperation.xor, { em });
expect(operator.evaluate([false, false])).toBe(false);
});
test('should throw error for unsupported operator', () => {
const operator = new LogicalOperator('unsupported' as LogicalOperation);
expect(() => operator.evaluate([true, false])).toThrow('Unsupported logical operator: unsupported');
});
});
});

45
packages/core/test/specs/data_sources/model/conditional_variables/operators/NumberOperator.ts

@ -2,94 +2,101 @@ import {
NumberOperator,
NumberOperation,
} from '../../../../../../src/data_sources/model/conditional_variables/operators/NumberOperator';
import Editor from '../../../../../../src/editor/model/Editor';
import EditorModel from '../../../../../../src/editor/model/Editor';
describe('NumberOperator', () => {
let em: EditorModel;
beforeEach(() => {
em = new Editor();
});
afterEach(() => {
em.destroy();
});
describe('Operator: greaterThan', () => {
test('should return true when left is greater than right', () => {
const operator = new NumberOperator(NumberOperation.greaterThan);
const operator = new NumberOperator(NumberOperation.greaterThan, { em });
expect(operator.evaluate(5, 3)).toBe(true);
});
test('should return false when left is not greater than right', () => {
const operator = new NumberOperator(NumberOperation.greaterThan);
const operator = new NumberOperator(NumberOperation.greaterThan, { em });
expect(operator.evaluate(2, 3)).toBe(false);
});
});
describe('Operator: lessThan', () => {
test('should return true when left is less than right', () => {
const operator = new NumberOperator(NumberOperation.lessThan);
const operator = new NumberOperator(NumberOperation.lessThan, { em });
expect(operator.evaluate(2, 3)).toBe(true);
});
test('should return false when left is not less than right', () => {
const operator = new NumberOperator(NumberOperation.lessThan);
const operator = new NumberOperator(NumberOperation.lessThan, { em });
expect(operator.evaluate(5, 3)).toBe(false);
});
});
describe('Operator: greaterThanOrEqual', () => {
test('should return true when left is greater than or equal to right', () => {
const operator = new NumberOperator(NumberOperation.greaterThanOrEqual);
const operator = new NumberOperator(NumberOperation.greaterThanOrEqual, { em });
expect(operator.evaluate(3, 3)).toBe(true);
});
test('should return false when left is not greater than or equal to right', () => {
const operator = new NumberOperator(NumberOperation.greaterThanOrEqual);
const operator = new NumberOperator(NumberOperation.greaterThanOrEqual, { em });
expect(operator.evaluate(2, 3)).toBe(false);
});
});
describe('Operator: lessThanOrEqual', () => {
test('should return true when left is less than or equal to right', () => {
const operator = new NumberOperator(NumberOperation.lessThanOrEqual);
const operator = new NumberOperator(NumberOperation.lessThanOrEqual, { em });
expect(operator.evaluate(3, 3)).toBe(true);
});
test('should return false when left is not less than or equal to right', () => {
const operator = new NumberOperator(NumberOperation.lessThanOrEqual);
const operator = new NumberOperator(NumberOperation.lessThanOrEqual, { em });
expect(operator.evaluate(5, 3)).toBe(false);
});
});
describe('Operator: equals', () => {
test('should return true when numbers are equal', () => {
const operator = new NumberOperator(NumberOperation.equals);
const operator = new NumberOperator(NumberOperation.equals, { em });
expect(operator.evaluate(4, 4)).toBe(true);
});
test('should return false when numbers are not equal', () => {
const operator = new NumberOperator(NumberOperation.equals);
const operator = new NumberOperator(NumberOperation.equals, { em });
expect(operator.evaluate(4, 5)).toBe(false);
});
});
describe('Operator: notEquals', () => {
test('should return true when numbers are not equal', () => {
const operator = new NumberOperator(NumberOperation.notEquals);
const operator = new NumberOperator(NumberOperation.notEquals, { em });
expect(operator.evaluate(4, 5)).toBe(true);
});
test('should return false when numbers are equal', () => {
const operator = new NumberOperator(NumberOperation.notEquals);
const operator = new NumberOperator(NumberOperation.notEquals, { em });
expect(operator.evaluate(4, 4)).toBe(false);
});
});
describe('Edge Case Tests', () => {
test('should handle boundary values correctly', () => {
const operator = new NumberOperator(NumberOperation.lessThan);
const operator = new NumberOperator(NumberOperation.lessThan, { em });
expect(operator.evaluate(Number.MIN_VALUE, 1)).toBe(true);
});
test('should return false for NaN comparisons', () => {
const operator = new NumberOperator(NumberOperation.equals);
const operator = new NumberOperator(NumberOperation.equals, { em });
expect(operator.evaluate(NaN, NaN)).toBe(false);
});
test('should throw error for unsupported operator', () => {
const operator = new NumberOperator('unsupported' as NumberOperation);
expect(() => operator.evaluate(1, 2)).toThrow('Unsupported number operator: unsupported');
});
});
});

55
packages/core/test/specs/data_sources/model/conditional_variables/operators/StringOperator.ts

@ -1,70 +1,82 @@
import {
StringOperator,
StringOperation,
} from '../../../../../../src/data_sources/model/conditional_variables/operators/StringOperations';
} from '../../../../../../src/data_sources/model/conditional_variables/operators/StringOperator';
import Editor from '../../../../../../src/editor/model/Editor';
import EditorModel from '../../../../../../src/editor/model/Editor';
describe('StringOperator', () => {
let em: EditorModel;
beforeEach(() => {
em = new Editor();
});
afterEach(() => {
em.destroy();
});
describe('Operator: contains', () => {
test('should return true when left contains right', () => {
const operator = new StringOperator(StringOperation.contains);
const operator = new StringOperator(StringOperation.contains, { em });
expect(operator.evaluate('hello world', 'world')).toBe(true);
});
test('should return false when left does not contain right', () => {
const operator = new StringOperator(StringOperation.contains);
const operator = new StringOperator(StringOperation.contains, { em });
expect(operator.evaluate('hello world', 'moon')).toBe(false);
});
});
describe('Operator: startsWith', () => {
test('should return true when left starts with right', () => {
const operator = new StringOperator(StringOperation.startsWith);
const operator = new StringOperator(StringOperation.startsWith, { em });
expect(operator.evaluate('hello world', 'hello')).toBe(true);
});
test('should return false when left does not start with right', () => {
const operator = new StringOperator(StringOperation.startsWith);
const operator = new StringOperator(StringOperation.startsWith, { em });
expect(operator.evaluate('hello world', 'world')).toBe(false);
});
});
describe('Operator: endsWith', () => {
test('should return true when left ends with right', () => {
const operator = new StringOperator(StringOperation.endsWith);
const operator = new StringOperator(StringOperation.endsWith, { em });
expect(operator.evaluate('hello world', 'world')).toBe(true);
});
test('should return false when left does not end with right', () => {
const operator = new StringOperator(StringOperation.endsWith);
const operator = new StringOperator(StringOperation.endsWith, { em });
expect(operator.evaluate('hello world', 'hello')).toBe(false);
});
});
describe('Operator: matchesRegex', () => {
test('should return true when left matches the regex right', () => {
const operator = new StringOperator(StringOperation.matchesRegex);
const operator = new StringOperator(StringOperation.matchesRegex, { em });
expect(operator.evaluate('hello world', '^hello')).toBe(true);
});
test('should return false when left does not match the regex right', () => {
const operator = new StringOperator(StringOperation.matchesRegex);
const operator = new StringOperator(StringOperation.matchesRegex, { em });
expect(operator.evaluate('hello world', '^world')).toBe(false);
});
});
describe('Operator: equalsIgnoreCase', () => {
test('should return true when left equals right ignoring case', () => {
const operator = new StringOperator(StringOperation.equalsIgnoreCase);
const operator = new StringOperator(StringOperation.equalsIgnoreCase, { em });
expect(operator.evaluate('Hello World', 'hello world')).toBe(true);
});
test('should return false when left does not equal right ignoring case', () => {
const operator = new StringOperator(StringOperation.equalsIgnoreCase);
const operator = new StringOperator(StringOperation.equalsIgnoreCase, { em });
expect(operator.evaluate('Hello World', 'hello there')).toBe(false);
});
test('should handle empty strings correctly', () => {
const operator = new StringOperator(StringOperation.equalsIgnoreCase);
const operator = new StringOperator(StringOperation.equalsIgnoreCase, { em });
expect(operator.evaluate('', '')).toBe(true);
expect(operator.evaluate('Hello', '')).toBe(false);
expect(operator.evaluate('', 'Hello')).toBe(false);
@ -73,17 +85,17 @@ describe('StringOperator', () => {
describe('Operator: trimEquals', () => {
test('should return true when left equals right after trimming', () => {
const operator = new StringOperator(StringOperation.trimEquals);
const operator = new StringOperator(StringOperation.trimEquals, { em });
expect(operator.evaluate(' Hello World ', 'Hello World')).toBe(true);
});
test('should return false when left does not equal right after trimming', () => {
const operator = new StringOperator(StringOperation.trimEquals);
const operator = new StringOperator(StringOperation.trimEquals, { em });
expect(operator.evaluate(' Hello World ', 'Hello there')).toBe(false);
});
test('should handle cases with only whitespace', () => {
const operator = new StringOperator(StringOperation.trimEquals);
const operator = new StringOperator(StringOperation.trimEquals, { em });
expect(operator.evaluate(' ', '')).toBe(true); // Both should trim to empty
expect(operator.evaluate(' ', 'non-empty')).toBe(false);
});
@ -91,28 +103,23 @@ describe('StringOperator', () => {
describe('Edge Case Tests', () => {
test('should return false for contains with empty right string', () => {
const operator = new StringOperator(StringOperation.contains);
const operator = new StringOperator(StringOperation.contains, { em });
expect(operator.evaluate('hello world', '')).toBe(true); // Empty string is included in any string
});
test('should return true for startsWith with empty right string', () => {
const operator = new StringOperator(StringOperation.startsWith);
const operator = new StringOperator(StringOperation.startsWith, { em });
expect(operator.evaluate('hello world', '')).toBe(true); // Any string starts with an empty string
});
test('should return true for endsWith with empty right string', () => {
const operator = new StringOperator(StringOperation.endsWith);
const operator = new StringOperator(StringOperation.endsWith, { em });
expect(operator.evaluate('hello world', '')).toBe(true); // Any string ends with an empty string
});
test('should throw error for invalid regex', () => {
const operator = new StringOperator(StringOperation.matchesRegex);
const operator = new StringOperator(StringOperation.matchesRegex, { em });
expect(() => operator.evaluate('hello world', '[')).toThrow();
});
test('should throw error for unsupported operator', () => {
const operator = new StringOperator('unsupported' as StringOperation);
expect(() => operator.evaluate('test', 'test')).toThrow('Unsupported string operator: unsupported');
});
});
});

Loading…
Cancel
Save