Skip to content

Commit

Permalink
test: add python constraint tests (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
devilkiller-ag authored Jan 1, 2024
1 parent 014fc78 commit bffee57
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('EnumConstrainer', () => {
});
expect(constrainedKey).toEqual('');
});
test('should use constant naming format', () => {
test('should use pascal naming format', () => {
const constrainedKey = JavaScriptDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('ModelNameConstrainer', () => {
});
expect(constrainedKey).toEqual('Empty');
});
test('should use constant naming format', () => {
test('should use pascal naming format', () => {
const constrainedKey = JavaScriptDefaultConstraints.modelName({
modelName: 'some weird_value!"#2'
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('PropertyKeyConstrainer', () => {
const constrainedKey = constrainPropertyName('');
expect(constrainedKey).toEqual('empty');
});
test('should use constant naming format', () => {
test('should use pascal naming format', () => {
const constrainedKey = constrainPropertyName('some weird_value!"#2');
expect(constrainedKey).toEqual('someWeirdValueExclamationQuotationHash_2');
});
Expand Down
19 changes: 19 additions & 0 deletions test/generators/python/constrainer/ConstantConstrainer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defaultConstantConstraints } from '../../../../src/generators/python/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');
});
});
134 changes: 134 additions & 0 deletions test/generators/python/constrainer/EnumConstrainer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { PythonDefaultConstraints } from '../../../../src/generators/python/PythonConstrainer';
import { EnumModel } from '../../../../src/models/MetaModel';
import {
ConstrainedEnumModel,
ConstrainedEnumValueModel
} from '../../../../src';

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

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

test('should not render number as start char', () => {
const constrainedKey = PythonDefaultConstraints.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 = PythonDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: ''
});
expect(constrainedKey).toEqual('RESERVED_EMPTY');
});

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

test('should use constant naming format', () => {
const constrainedKey = PythonDefaultConstraints.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 = PythonDefaultConstraints.enumKey({
enumModel,
constrainedEnumModel,
enumKey: 'return'
});
expect(constrainedKey).toEqual('RESERVED_RETURN');
});
});

describe('enum values', () => {
test('should render value as is', () => {
const constrainedValue = PythonDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: 'string value'
});
expect(constrainedValue).toEqual('"string value"');
});

test('should render boolean values', () => {
const constrainedValue = PythonDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: true
});
expect(constrainedValue).toEqual('"true"');
});

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

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

test('should render unknown value', () => {
const constrainedValue = PythonDefaultConstraints.enumValue({
enumModel,
constrainedEnumModel,
enumValue: undefined
});
expect(constrainedValue).toEqual(`"undefined"`);
});
});
});
76 changes: 76 additions & 0 deletions test/generators/python/constrainer/ModelNameConstrainer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { PythonDefaultConstraints } from '../../../../src/generators/python/PythonConstrainer';
import {
DefaultModelNameConstraints,
defaultModelNameConstraints,
ModelNameConstraints
} from '../../../../src/generators/python/constrainer/ModelNameConstrainer';

describe('ModelNameConstrainer', () => {
test('should never render special chars', () => {
const constrainedKey = PythonDefaultConstraints.modelName({
modelName: '%'
});
expect(constrainedKey).toEqual('Percent');
});
test('should never render number as start char', () => {
const constrainedKey = PythonDefaultConstraints.modelName({
modelName: '1'
});
expect(constrainedKey).toEqual('Number_1');
});
test('should never contain empty name', () => {
const constrainedKey = PythonDefaultConstraints.modelName({
modelName: ''
});
expect(constrainedKey).toEqual('Empty');
});
test('should use pascal naming format', () => {
const constrainedKey = PythonDefaultConstraints.modelName({
modelName: 'some weird_value!"#2'
});
expect(constrainedKey).toEqual('SomeWeirdValueExclamationQuotationHash_2');
});
test('should never render reserved keywords', () => {
const constrainedKey = PythonDefaultConstraints.modelName({
modelName: 'return'
});
expect(constrainedKey).toEqual('ReservedReturn');
});
describe('custom constraints', () => {
test('should be able to overwrite all hooks', () => {
const mockedConstraintCallbacks: 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 hooks', () => {
//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 bffee57

Please sign in to comment.