use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testBinaryOperatorChained.
@Test
void testBinaryOperatorChained() {
final String code = "" + "_method object.test\n" + " _return 1 + :a + 'a'\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final AbstractType char16VectorType = typeKeeper.getType(GlobalReference.of("sw:char16_vector"));
final AbstractType symbolType = typeKeeper.getType(GlobalReference.of("sw:symbol"));
final AbstractType integerType = typeKeeper.getType(GlobalReference.of("sw:integer"));
final BinaryOperator binOp1 = new BinaryOperator(BinaryOperator.Operator.valueFor("+"), integerType, symbolType, symbolType);
typeKeeper.addBinaryOperator(binOp1);
final BinaryOperator binOp2 = new BinaryOperator(BinaryOperator.Operator.valueFor("+"), symbolType, char16VectorType, char16VectorType);
typeKeeper.addBinaryOperator(binOp2);
// 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:char16_vector");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testTryWith.
@Test
void testTryWith() {
final String code = "" + "_method object.test\n" + " _try _with cond\n" + " _when warning\n" + " _when error\n" + " _endtry\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();
final AstNode topNode = magikFile.getTopNode();
final AstNode tryVarNode = topNode.getFirstDescendant(MagikGrammar.TRY_VARIABLE);
final ExpressionResult result = reasoner.getNodeType(tryVarNode);
assertThat(result).isNotNull();
final MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:condition");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testReasonMethodReturn.
@Test
void testReasonMethodReturn() {
final String code = "" + "_package sw\n" + "_method object.test\n" + " _return rope.new()\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final MagikType ropeType = new SlottedType(GlobalReference.of("sw:rope"));
ropeType.addMethod(EnumSet.noneOf(Method.Modifier.class), null, "new()", Collections.emptyList(), null, new ExpressionResult(ropeType));
typeKeeper.addType(ropeType);
// 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:rope");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testBinaryOperator.
@Test
void testBinaryOperator() {
final String code = "" + "_method object.test\n" + " _return 'a' + 'b'\n" + "_endmethod\n";
// Set up TypeKeeper/TypeReasoner.
final TypeKeeper typeKeeper = new TypeKeeper();
final AbstractType char16VectorType = typeKeeper.getType(GlobalReference.of("sw:char16_vector"));
final BinaryOperator binOp = new BinaryOperator(BinaryOperator.Operator.valueFor("+"), char16VectorType, char16VectorType, char16VectorType);
typeKeeper.addBinaryOperator(binOp);
// 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:char16_vector");
}
use of nl.ramsolutions.sw.magik.analysis.typing.types.MagikType in project magik-tools by StevenLooman.
the class LocalTypeReasonerTest method testLoopStatementUseIterator.
@Test
void testLoopStatementUseIterator() {
final String code = "" + "_method object.test\n" + " _return _for i _over 1.upto(10)\n" + " _loop\n" + " _leave _with i\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 MagikType resultType = (MagikType) result.get(0, null);
assertThat(resultType).isNotNull();
assertThat(resultType.getFullName()).isEqualTo("sw:integer");
}
Aggregations