Skip to content

Commit

Permalink
added other test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmin2 committed Dec 27, 2023
1 parent 92c01f9 commit b2db1f7
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/generators/php/constrainer/ConstantConstrainer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defaultConstantConstraints } from '../../../../src/generators/php/constrainer/ConstantConstrainer';

const mockConstantContext = {
constrainedMetaModel: {} as any
};

describe('defaultConstantConstraints', () => {
it('should return a function that returns undefined', () => {
const constantConstraintsFunction = defaultConstantConstraints();
const result = constantConstraintsFunction(mockConstantContext);
expect(result).toBeUndefined();
});

it('should return a ConstantConstraint type', () => {
const constantConstraintsFunction = defaultConstantConstraints();
const result = constantConstraintsFunction;
expect(typeof result).toBe('function');
});
});
168 changes: 168 additions & 0 deletions test/generators/php/constrainer/EnumConstrainer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { PhpDefaultConstraints } from '../../../../src/generators/php/PhpConstrainer';
import { EnumModel } from '../../../../src/models/MetaModel';
import {
ConstrainedEnumModel,
ConstrainedEnumValueModel
} from '../../../../src';
import {
defaultEnumKeyConstraints,
ModelEnumKeyConstraints,
DefaultEnumKeyConstraints
} from '../../../../src/generators/php/constrainer/EnumConstrainer';

describe('Php EnumConstrainer', () => {
const enumModel = new EnumModel('test', undefined, {}, []);
const constrainedEnumModel = new ConstrainedEnumModel(
'test',
undefined,
{},
'',
[]
);

describe('enum keys', () => {
test('should never render special chars', () => {
const constrainedKey = PhpDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: '%'
});
expect(constrainedKey).toEqual('PERCENT');
});

test('should not render number as start char', () => {
const constrainedKey = PhpDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: '1'
});
expect(constrainedKey).toEqual('NUMBER_1');
});

test('should not contain duplicate keys', () => {
const existingConstrainedEnumValueModel = new ConstrainedEnumValueModel(
'EMPTY',
'return',
'return'
);
const constrainedEnumModel = new ConstrainedEnumModel(
'test',
undefined,
{},
'',
[existingConstrainedEnumValueModel]
);
const constrainedKey = PhpDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: ''
});
expect(constrainedKey).toEqual('RESERVED_EMPTY');
});

test('should never contain empty keys', () => {
const constrainedKey = PhpDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: ''
});
expect(constrainedKey).toEqual('RESERVED_EMPTY');
});

test('should use constant naming format', () => {
const constrainedKey = PhpDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: 'some weird_value!"#2'
});
expect(constrainedKey).toEqual(
'SOME_WEIRD_VALUE_EXCLAMATION_QUOTATION_HASH_2'
);
});

test('should never render reserved keywords', () => {
const constrainedKey = PhpDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: 'return'
});
expect(constrainedKey).toEqual('RESERVED_RETURN');
});
});

describe('enum values', () => {
test('should render string values', () => {
const constrainedValue = PhpDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: 'string value'
});
expect(constrainedValue).toEqual('"string value"');
});
test('should render boolean values', () => {
const constrainedBooleanValue = PhpDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: true
});
expect(constrainedBooleanValue).toEqual('"true"');
});

test('should render numbers', () => {
const constrainedNumberValue = PhpDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: 123
});
expect(constrainedNumberValue).toEqual(123);
});

test('should render object values', () => {
const constrainedObjectValue = PhpDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: { test: 'test' }
});
expect(constrainedObjectValue).toEqual('"{\\"test\\":\\"test\\"}"');
});

test('should render unknown value', () => {
const constrainedUnknownValue = PhpDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: undefined
});
expect(constrainedUnknownValue).toEqual(`"undefined"`);
});
});

describe('custom constraints', () => {
test('should be able to overwrite all hooks for enum key', () => {
const mockedConstraintCallbacks: Partial<ModelEnumKeyConstraints> = {
NAMING_FORMATTER: jest.fn().mockReturnValue(''),
NO_SPECIAL_CHAR: jest.fn().mockReturnValue(''),
NO_NUMBER_START_CHAR: jest.fn().mockReturnValue(''),
NO_EMPTY_VALUE: jest.fn().mockReturnValue(''),
NO_RESERVED_KEYWORDS: jest.fn().mockReturnValue('')
};
const constrainFunction = defaultEnumKeyConstraints(
mockedConstraintCallbacks
);
constrainFunction({ enumModel, constrainedEnumModel, enumKey: '' });
for (const jestMockCallback of Object.values(mockedConstraintCallbacks)) {
expect(jestMockCallback).toHaveBeenCalled();
}
});
test('should be able to overwrite specific hooks for enum key', () => {
const mockedConstraintCallbacks: Partial<ModelEnumKeyConstraints> = {
NAMING_FORMATTER: jest.fn().mockReturnValue('')
};
const constrainFunction = defaultEnumKeyConstraints(
mockedConstraintCallbacks
);
constrainFunction({ enumModel, constrainedEnumModel, enumKey: '' });

expect(mockedConstraintCallbacks.NAMING_FORMATTER).toHaveBeenCalled();
});
});
});
75 changes: 75 additions & 0 deletions test/generators/php/constrainer/ModelNameConstrainer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
DefaultModelNameConstraints,
defaultModelNameConstraints,
ModelNameConstraints
} from '../../../../src/generators/php/constrainer/ModelNameConstrainer';

describe('PHP ModelNameConstrainer', () => {
const PhpDefaultConstraints: ModelNameConstraints =
DefaultModelNameConstraints;

test('should replace special characters with underscores', () => {
const constrainedName = PhpDefaultConstraints.NO_SPECIAL_CHAR('name%$test');
expect(constrainedName).toEqual('name_percent_dollar_test');
});

test('should handle number start characters', () => {
const constrainedName = PhpDefaultConstraints.NO_NUMBER_START_CHAR('1Test');
expect(constrainedName).toEqual('number_1Test');
});

test('should handle empty value by default', () => {
const constrainedName = PhpDefaultConstraints.NO_EMPTY_VALUE('');
expect(constrainedName).toEqual('empty');
});

test('should convert to PascalCase by default', () => {
const constrainedName = PhpDefaultConstraints.NAMING_FORMATTER('test_name');
expect(constrainedName).toEqual('TestName');
});

test('should handle reserved PHP keywords', () => {
const constrainedName =
PhpDefaultConstraints.NO_RESERVED_KEYWORDS('return');
expect(constrainedName).toEqual('reserved_return');
});

describe('Custom PHP constraints', () => {
test('should be able to overwrite all hooks for PHP', () => {
const mockedConstraintCallbacks: Partial<ModelNameConstraints> = {
NAMING_FORMATTER: jest.fn().mockReturnValue(''),
NO_SPECIAL_CHAR: jest.fn().mockReturnValue(''),
NO_NUMBER_START_CHAR: jest.fn().mockReturnValue(''),
NO_EMPTY_VALUE: jest.fn().mockReturnValue(''),
NO_RESERVED_KEYWORDS: jest.fn().mockReturnValue('')
};
const constrainFunction = defaultModelNameConstraints(
mockedConstraintCallbacks
);
constrainFunction({ modelName: '' });
// Expect all callbacks to be called
for (const jestMockCallback of Object.values(mockedConstraintCallbacks)) {
expect(jestMockCallback).toHaveBeenCalled();
}
});

test('should be able to overwrite one hook for PHP', () => {
// All but NAMING_FORMATTER, as we customize that
const spies = [
jest.spyOn(DefaultModelNameConstraints, 'NO_SPECIAL_CHAR'),
jest.spyOn(DefaultModelNameConstraints, 'NO_NUMBER_START_CHAR'),
jest.spyOn(DefaultModelNameConstraints, 'NO_EMPTY_VALUE'),
jest.spyOn(DefaultModelNameConstraints, 'NO_RESERVED_KEYWORDS')
];
const jestCallback = jest.fn().mockReturnValue('');
const constrainFunction = defaultModelNameConstraints({
NAMING_FORMATTER: jestCallback
});
const constrainedValue = constrainFunction({ modelName: '' });
expect(constrainedValue).toEqual('');
for (const jestMockCallback of spies) {
expect(jestMockCallback).toHaveBeenCalled();
}
});
});
});
Loading

0 comments on commit b2db1f7

Please sign in to comment.