-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
499 additions
and
4 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
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,41 @@ | ||
# C++ Constraints | ||
|
||
These are the C++ specific constraints applied to the MetaModel before reaching the presets. [Read here to get a more general idea on the overall process](../input-processing.md) of converting a MetaModel to a ConstrainedMetaModel. | ||
|
||
## Model Naming | ||
These are the constraints that is applied to model naming. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). | ||
|
||
|Rule key|Rule|Resolution| | ||
|---|---|---| | ||
|NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For C++ `_` are an exception to this rule. | | ||
|NO_NUMBER_START_CHAR|No numbers as starting characters|Default behavior is pre pending `number_` in front of the first character| | ||
|NO_EMPTY_VALUE|No empty values|Default behavior is to use `empty` as name. | | ||
|NO_RESERVED_KEYWORDS|No reserved keywords|C++ has a list of reserved keywords ([see the full list here](../../src/generators/cplusplus/Constants.ts))| | ||
|NAMING_FORMATTER|Must be formatted equally|Model name is formatted using snake case| | ||
|
||
## Property naming | ||
These are the constraints that is applied to object properties and the naming of them. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). | ||
|Rule key|Rule|Resolution| | ||
|---|---|---| | ||
|NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For C++ `_` are an exception to this rule. | | ||
|NO_NUMBER_START_CHAR|No numbers as starting characters|Default behavior is pre pending `number_` in front of the first character| | ||
|NO_EMPTY_VALUE|No empty values|Default behavior is to use `empty` as name. | | ||
|NO_RESERVED_KEYWORDS|No reserved keywords|C++ has a list of reserved keywords ([see the full list here](../../src/generators/cplusplus/Constants.ts))| | ||
|NAMING_FORMATTER|Must be formatted equally|Property naming is formatted using snake case| | ||
|NO_DUPLICATE_PROPERTIES|No duplicate properties|If any of the above constraints changes the property name, we must make sure that no duplicates exist within the same object. If any is encountered `reserved_` is pre-pended. This is done recursively until no duplicates are found.| | ||
|
||
|
||
## Enum key constraints | ||
These are the constraints that is applied to enum keys. The `Rule key` is what you can use in the options to overwrite the default behavior. See [constraint customization](./README.md#Customization). | ||
|
||
|Rule key|Rule|Resolution| | ||
|---|---|---| | ||
|NO_SPECIAL_CHAR|No special characters| Special characters are replaced by their name, for example `!` is replaced with `exclamation`. For C++ `_` are an exception to this rule. | | ||
|NO_NUMBER_START_CHAR|No numbers as starting characters|Default behavior is pre pending `number_` in front of the first character| | ||
|NO_EMPTY_VALUE|No empty values|Default behavior is to use `empty` as name. | | ||
|NO_RESERVED_KEYWORDS|No reserved keywords|C++ has a list of reserved keywords ([see the full list here](../../src/generators/cplusplus/Constants.ts))| | ||
|NAMING_FORMATTER|Must be formatted equally|Enum key is formatted using snake case| | ||
|NO_DUPLICATE_KEYS|No duplicate enum keys|If any of the above constraints changes the enum key, we must make sure that no duplicates exist within the same enum. If any is encountered `reserved_` is pre-pended. This is done recursively until no duplicates are found.| | ||
|
||
## Constant | ||
These are the constraints that are applied to constants. Currently, there are no hooks one can overwrite inside it. |
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/php/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/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'); | ||
}); | ||
}); |
167 changes: 167 additions & 0 deletions
167
test/generators/php/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,167 @@ | ||
import { PhpDefaultConstraints } from '../../../../src/generators/php/PhpConstrainer'; | ||
import { EnumModel } from '../../../../src/models/MetaModel'; | ||
import { | ||
ConstrainedEnumModel, | ||
ConstrainedEnumValueModel | ||
} from '../../../../src'; | ||
import { | ||
defaultEnumKeyConstraints, | ||
ModelEnumKeyConstraints | ||
} 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
75
test/generators/php/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,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(); | ||
} | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.