-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add python constraint tests (#1703)
- Loading branch information
1 parent
014fc78
commit bffee57
Showing
7 changed files
with
415 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
test/generators/python/constrainer/ConstantConstrainer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
134
test/generators/python/constrainer/EnumConstrainer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
test/generators/python/constrainer/ModelNameConstrainer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.