use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testLoopResultOptional.
@Test
void testLoopResultOptional() {
final String code = "" + "_method object.test\n" + " _return _for i _over 1.upto(10)\n" + " _loop\n" + " _if a\n" + " _then\n" + " _leave _with i\n" + " _endif\n" + " _endloop\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType integerType = (MagikType) typeKeeper.getType(GlobalReference.of("sw:integer"));
integerType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "upto()", Collections.emptyList(), null, new ExpressionResult(), new ExpressionResult(integerType));
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final CombinedType resultType = (CombinedType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:integer|sw:unset");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testUnaryOperator.
@Test
void testUnaryOperator() {
final String code = "" + "_method object.test\n" + " _return _not _true\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final AbstractType falseType = typeKeeper.getType(GlobalReference.of("sw:false"));
final UnaryOperator unaryOp = new UnaryOperator(UnaryOperator.Operator.valueFor("_not"), falseType, falseType);
typeKeeper.addUnaryOperator(unaryOp);
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:false");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testIfElseStatement.
@Test
void testIfElseStatement() {
final String code = "" + "_method object.test\n" + " a << _true\n" + " _return _if a\n" + " _then\n" + " >> 1\n" + " _else\n" + " >> 2\n" + " _endif\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
// Do analysis.
final MagikTypedFile magikFile = this.createMagikFile(code, typeKeeper);
final LocalTypeReasoner reasoner = magikFile.getTypeReasoner();
// Assert user:object.test type determined.
final AstNode topNode = magikFile.getTopNode();
final AstNode methodNode = topNode.getFirstChild(MagikGrammar.METHOD_DEFINITION);
final ExpressionResult result = reasoner.getNodeType(methodNode);
assertThat(result.size()).isEqualTo(1);
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:integer");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class TypeMatcherTest method testIsKindOfDirectParent.
@Test
void testIsKindOfDirectParent() {
final AbstractType baseType = new IntrinsicType(GlobalReference.of("sw:base"));
final MagikType childType = new IntrinsicType(GlobalReference.of("sw:child"));
childType.addParent(baseType);
final AbstractType criterium = new IntrinsicType(GlobalReference.of("sw:base"));
final boolean isKindOf = TypeMatcher.isKindOf(childType, criterium);
assertThat(isKindOf).isTrue();
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class TypeMatcherTest method testTypeIsKindOf.
@Test
void testTypeIsKindOf() {
final AbstractType baseType = new IntrinsicType(GlobalReference.of("sw:base"));
final MagikType childType = new IntrinsicType(GlobalReference.of("sw:child"));
childType.addParent(baseType);
final AbstractType criterium = new IntrinsicType(GlobalReference.of("sw:base"));
final boolean matches = TypeMatcher.typeMatches(childType, criterium);
assertThat(matches).isTrue();
}
Aggregations