Back to TTCN-3 Refactoring Catalog
Inline Template
A template that is used only once is inlined for improved readability.
Motivation
In some cases, a template declaration is referenced only once throughout the whole TTCN-3 code. Using the value list notation instead of a reference for this template may improve readability as the test engineer does not have to search through the code to find the corresponding declaration and it may shorten the code length. Candidates for this refactoring are only simple templates as inlined templates are typically written in a single line.
Mechanics
- Identify the source template declaration. It should be referenced only once throughout the TTCN-3 code and it should be simple. Otherwise, the use of this refactoring may not be appropriate. If the only reference is due to a modifies, this refactoring is not applicable. (Instead you should consider to apply Collapse Templates? to these two templates.
- Find the code location where the source template declaration is referenced. This is the source reference.
- Replace the source reference with the value list notation of the source template.
- When a normal template is inlined, its notation has the standard notation of an inlined template.
- When a modified template is inlined, its notation must be adjusted to reflect the notation of modified inlined templates.
- When a parameterized template is inlined, the actual parameter values of the reference must be inlined into this value list notation.
- Compile and validate.
- Remove the source template declaration. If the source template declaration is located in a different module than the source reference, the corresponding import statement in the module of the source reference should be adjusted to exclude the source template declaration.
- Compile and validate.
Example
The next listing shows the unrefactored version. The module contains the record declaration MyMessageType? and the source template declaration a_myMessageTemplate. In the test case a message is sent by using a reference to the source template declaration a_myMessageTemplate. As this is the only reference to the template, it is the source reference.
// ...
type record MyMessageType {
integer field1 optional,
charstring field2,
boolean field3
}
template MyMessageType a_myMessageTemplate := {
field1 := omit,
field2 := "My string",
field3 := true
}
// ...
testcase tc_exampleTestCase() runs on ExampleComponent {
// ...
pt.send( a_myMessageTemplate );
// ...
}
The following listing demonstrates the result of applying Inline Template. The source template declaration is inlined into the send statement and replaces the source reference. No imports must be adjusted as the the source template declaration and the source reference are both in the same module.
// ...
type record MyMessageType {
integer field1 optional,
charstring field2,
boolean field3
}
// ...
testcase tc_exampleTestCase() runs on ExampleComponent {
// ...
pt.send( MyMessageType:{omit, "My string", true} );
// ...
}
