Search in sources :

Example 16 with MagikType

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");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) CombinedType(nl.ramsolutions.sw.magik.analysis.typing.types.CombinedType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) AstNode(com.sonar.sslr.api.AstNode) Test(org.junit.jupiter.api.Test)

Example 17 with MagikType

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");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 18 with MagikType

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");
}
Also used : ExpressionResult(nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult) MagikTypedFile(nl.ramsolutions.sw.magik.MagikTypedFile) AstNode(com.sonar.sslr.api.AstNode) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 19 with MagikType

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();
}
Also used : IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Example 20 with MagikType

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();
}
Also used : IntrinsicType(nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType) AbstractType(nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType) MagikType(nl.ramsolutions.sw.magik.analysis.typing.types.MagikType) Test(org.junit.jupiter.api.Test)

Aggregations

MagikType (nl.ramsolutions.sw.magik.analysis.typing.types.MagikType)64 Test (org.junit.jupiter.api.Test)47 AstNode (com.sonar.sslr.api.AstNode)33 ExpressionResult (nl.ramsolutions.sw.magik.analysis.typing.types.ExpressionResult)32 MagikTypedFile (nl.ramsolutions.sw.magik.MagikTypedFile)28 ITypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper)22 AbstractType (nl.ramsolutions.sw.magik.analysis.typing.types.AbstractType)20 TypeKeeper (nl.ramsolutions.sw.magik.analysis.typing.TypeKeeper)18 SlottedType (nl.ramsolutions.sw.magik.analysis.typing.types.SlottedType)17 GlobalReference (nl.ramsolutions.sw.magik.analysis.typing.types.GlobalReference)16 Method (nl.ramsolutions.sw.magik.analysis.typing.types.Method)14 Location (nl.ramsolutions.sw.magik.analysis.Location)10 IntrinsicType (nl.ramsolutions.sw.magik.analysis.typing.types.IntrinsicType)10 Parameter (nl.ramsolutions.sw.magik.analysis.typing.types.Parameter)9 Position (org.eclipse.lsp4j.Position)9 IndexedType (nl.ramsolutions.sw.magik.analysis.typing.types.IndexedType)8 Path (java.nio.file.Path)7 URI (java.net.URI)6 Collections (java.util.Collections)5 EnumSet (java.util.EnumSet)5