From 203d467784f4a8b572bd569cc8ba5661d1f43f88 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Tue, 28 Nov 2023 14:23:37 +0100 Subject: [PATCH] cleanup in lambda tests including removal of deprecated JUnit exception rule --- .../META-INF/MANIFEST.MF | 2 +- ...pse.xsemantics.example.lambda.tests.launch | 31 ++- .../lambda/tests/LambdaAbstractBaseTest.java | 234 ++++++++++++++++++ .../lambda/tests/LambdaAbstractBaseTest.xtend | 205 --------------- .../lambda/tests/LambdaNotOccurTest.java | 66 +++++ .../lambda/tests/LambdaNotOccurTest.xtend | 92 ------- .../example/lambda/tests/LambdaUnifyTest.java | 136 ++++++++++ .../lambda/tests/LambdaUnifyTest.xtend | 185 -------------- 8 files changed, 455 insertions(+), 496 deletions(-) create mode 100644 examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.java delete mode 100644 examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.xtend create mode 100644 examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.java delete mode 100644 examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.xtend create mode 100644 examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.java delete mode 100644 examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.xtend diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/META-INF/MANIFEST.MF b/examples/org.eclipse.xsemantics.example.lambda.tests/META-INF/MANIFEST.MF index 71a7222ab..f610915d0 100644 --- a/examples/org.eclipse.xsemantics.example.lambda.tests/META-INF/MANIFEST.MF +++ b/examples/org.eclipse.xsemantics.example.lambda.tests/META-INF/MANIFEST.MF @@ -7,7 +7,7 @@ Bundle-Version: 1.23.0.qualifier Bundle-SymbolicName: org.eclipse.xsemantics.example.lambda.tests; singleton:=true Bundle-ActivationPolicy: lazy Require-Bundle: org.eclipse.xsemantics.example.lambda, - org.junit;bundle-version="4.12.0", + org.junit;bundle-version="4.13.0", org.eclipse.xtext.testing, org.eclipse.xtext.xbase.testing, org.eclipse.xtext.xbase.lib;bundle-version="2.14.0" diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/org.eclipse.xsemantics.example.lambda.tests.launch b/examples/org.eclipse.xsemantics.example.lambda.tests/org.eclipse.xsemantics.example.lambda.tests.launch index e93a60f46..ee450809c 100644 --- a/examples/org.eclipse.xsemantics.example.lambda.tests/org.eclipse.xsemantics.example.lambda.tests.launch +++ b/examples/org.eclipse.xsemantics.example.lambda.tests/org.eclipse.xsemantics.example.lambda.tests.launch @@ -1,16 +1,21 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.java b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.java new file mode 100644 index 000000000..bf7086cf4 --- /dev/null +++ b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.java @@ -0,0 +1,234 @@ +/** + * Copyright (c) 2013-2017 Lorenzo Bettini. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lorenzo Bettini - Initial contribution and API + */ +package org.eclipse.xsemantics.example.lambda.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.eclipse.xsemantics.example.lambda.lambda.Abstraction; +import org.eclipse.xsemantics.example.lambda.lambda.ArrowType; +import org.eclipse.xsemantics.example.lambda.lambda.Constant; +import org.eclipse.xsemantics.example.lambda.lambda.Program; +import org.eclipse.xsemantics.example.lambda.lambda.Term; +import org.eclipse.xsemantics.example.lambda.lambda.Type; +import org.eclipse.xsemantics.example.lambda.lambda.TypeVariable; +import org.eclipse.xsemantics.example.lambda.lambda.Variable; +import org.eclipse.xsemantics.example.lambda.tests.util.LambdaTestsUtil; +import org.eclipse.xsemantics.example.lambda.validation.LambdaValidator; +import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaStringRepresentation; +import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaStringRepresentationWithTypeBeautifier; +import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaUtils; +import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaXsemanticsSystem; +import org.eclipse.xsemantics.example.lambda.xsemantics.TypeSubstitutions; +import org.eclipse.xsemantics.runtime.Result; +import org.eclipse.xsemantics.runtime.RuleApplicationTrace; +import org.eclipse.xsemantics.runtime.StringRepresentation; +import org.eclipse.xsemantics.runtime.TraceUtils; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.XtextRunner; +import org.eclipse.xtext.testing.util.ParseHelper; +import org.eclipse.xtext.testing.validation.ValidationTestHelper; +import org.eclipse.xtext.xbase.lib.Exceptions; +import org.eclipse.xtext.xbase.lib.Extension; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; + +import com.google.inject.Inject; +import com.google.inject.Injector; + +@RunWith(XtextRunner.class) +@InjectWith(LambdaInjectorProvider.class) +public abstract class LambdaAbstractBaseTest { + @Inject + protected LambdaValidator validator; + + @Inject + protected Injector injector; + + @Inject + @Extension + protected ParseHelper parseHelper; + + @Inject + @Extension + protected ValidationTestHelper validationTestHelper; + + @Inject + protected LambdaXsemanticsSystem system; + + @Inject + @Extension + private StringRepresentation stringRepresentation; + + @Inject + protected LambdaExpectedTraces traces; + + @Inject + @Extension + private TraceUtils traceUtils; + + protected TypeSubstitutions substitutions; + + @Inject + protected LambdaStringRepresentation reprForSubstitutions; + + @Inject + protected LambdaStringRepresentationWithTypeBeautifier reprBeautifier; + + @Inject + protected LambdaUtils lambdaUtils; + + protected RuleApplicationTrace trace; + + @BeforeClass + public static void setNewLine() { + System.setProperty("line.separator", "\n"); + } + + @Before + public void setUp() { + lambdaUtils.resetCounter(); + substitutions = new TypeSubstitutions(); + trace = new RuleApplicationTrace(); + } + + public Program parseAndGetProgram(final CharSequence prog) { + try { + return parseHelper.parse(prog); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + public Program parseAndAssertNoError(final CharSequence prog) { + try { + final Program model = parseHelper.parse(prog); + validationTestHelper.assertNoErrors(model); + return model; + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + protected Program getLambdaProgram(final String program) { + try { + return parseHelper.parse(program); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + public void assertFailure(final Result result) { + if (!result.failed()) { + assertTrue( + "should have failed, instead got: " + stringRepresentation.string(result.getValue()) + + "\n" + traceUtils.traceAsString(trace), + result.failed()); + } + } + + public void assertFailure(final Boolean result) { + if (result.booleanValue()) { + assertTrue("should have failed", result.booleanValue()); + } + } + + public void assertFailureTrace(final Result result, final CharSequence expectedTrace) { + assertFailure(result); + assertEqualsStrings(expectedTrace, traceUtils.failureTraceAsString(result.getRuleFailedException())); + } + + public void assertEqualsStrings(final Object expected, final Object actual) { + Assert.assertEquals(("" + expected).replace("\r", ""), ("" + actual).replace("\r", "")); + } + + public void assertResultAsString(final Result result, final String expected) { + if (result.failed()) { + assertFalse("should not have failed: " + + traceUtils.failureTraceAsString(result.getRuleFailedException()), + result.failed()); + } + assertEqualsStrings(expected, + stringRepresentation.string(result.getValue())); + } + + public void assertResultAsString(final T result, final String expected) { + assertNotNull(result); + assertEqualsStrings(expected, stringRepresentation.string(result)); + } + + public void assertResultAsStringBeautifier(final Result result, final String expected) { + if (result.failed()) { + Assert.assertFalse("should not have failed: " + + traceUtils.failureTraceAsString(result.getRuleFailedException()), + result.failed()); + } + assertEqualsStrings(expected, reprBeautifier.string(result.getValue())); + } + + public void assertResult2AsString(final Type result, final String expected) { + assertNotNull(result); + assertNotNull(result); + assertEqualsStrings(expected, stringRepresentation.string(result)); + } + + public void assertResultTrue(final Boolean result) { + assertTrue((result).booleanValue()); + } + + public TypeVariable getTypeVariable(final Type type) { + return (TypeVariable) type; + } + + public ArrowType getArrowType(final Type type) { + return (ArrowType) type; + } + + public Term getTerm(final CharSequence prog) { + return parseAndGetProgram(prog).getTerm(); + } + + public Abstraction getAbstraction(final CharSequence prog) { + return getAbstraction(getTerm(prog)); + } + + public Abstraction getAbstraction(final Term term) { + return (Abstraction) term; + } + + public Variable getVariable(final Term term) { + return (Variable) term; + } + + public Constant getConstant(final Term term) { + return (Constant) term; + } + + public void assertTypeVariable(final Type type, final String expectedName) { + assertEquals(expectedName, getTypeVariable(type).getTypevarName()); + } + + public void assertArrowType(final Type type, final String expectedName) { + assertEquals(expectedName, stringRepresentation.string(getArrowType(type))); + } + + public void assertTypeSubstitutions(final TypeSubstitutions substitutions, final String expected) { + assertEqualsStrings("subst{" + expected + "}", reprForSubstitutions.string(substitutions)); + } + + public String rep(final Type type) { + return LambdaTestsUtil.rep(type); + } +} diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.xtend b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.xtend deleted file mode 100644 index b6a4e8bb0..000000000 --- a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaAbstractBaseTest.xtend +++ /dev/null @@ -1,205 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013-2017 Lorenzo Bettini. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Lorenzo Bettini - Initial contribution and API - *******************************************************************************/ - -package org.eclipse.xsemantics.example.lambda.tests - -import com.google.inject.Inject -import com.google.inject.Injector -import org.eclipse.xsemantics.example.lambda.lambda.Abstraction -import org.eclipse.xsemantics.example.lambda.lambda.ArrowType -import org.eclipse.xsemantics.example.lambda.lambda.Constant -import org.eclipse.xsemantics.example.lambda.lambda.Program -import org.eclipse.xsemantics.example.lambda.lambda.Term -import org.eclipse.xsemantics.example.lambda.lambda.Type -import org.eclipse.xsemantics.example.lambda.lambda.TypeVariable -import org.eclipse.xsemantics.example.lambda.lambda.Variable -import org.eclipse.xsemantics.example.lambda.validation.LambdaValidator -import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaStringRepresentation -import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaStringRepresentationWithTypeBeautifier -import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaUtils -import org.eclipse.xsemantics.example.lambda.xsemantics.LambdaXsemanticsSystem -import org.eclipse.xsemantics.example.lambda.xsemantics.TypeSubstitutions -import org.eclipse.xsemantics.runtime.Result -import org.eclipse.xsemantics.runtime.RuleApplicationTrace -import org.eclipse.xsemantics.runtime.StringRepresentation -import org.eclipse.xsemantics.runtime.TraceUtils -import org.eclipse.xtext.testing.InjectWith -import org.eclipse.xtext.testing.XtextRunner -import org.eclipse.xtext.testing.util.ParseHelper -import org.eclipse.xtext.testing.validation.ValidationTestHelper -import org.junit.Assert -import org.junit.Before -import org.junit.BeforeClass -import org.junit.runner.RunWith -import org.eclipse.xsemantics.example.lambda.tests.util.LambdaTestsUtil - -@RunWith(typeof(XtextRunner)) -@InjectWith(typeof(LambdaInjectorProvider)) -abstract class LambdaAbstractBaseTest { - @Inject protected LambdaValidator validator - - @Inject protected Injector injector - - @Inject protected extension ParseHelper - - @Inject protected extension ValidationTestHelper - - @Inject protected LambdaXsemanticsSystem system - - @Inject extension StringRepresentation - - @Inject protected LambdaExpectedTraces traces - - @Inject extension TraceUtils - - protected TypeSubstitutions substitutions - - @Inject protected LambdaStringRepresentation reprForSubstitutions - - @Inject protected LambdaStringRepresentationWithTypeBeautifier reprBeautifier - - @Inject protected LambdaUtils lambdaUtils - - protected RuleApplicationTrace trace - - @BeforeClass - def static void setNewLine() { - System::setProperty("line.separator", "\n") - } - - @Before - def void setUp() { - lambdaUtils.resetCounter - substitutions = new TypeSubstitutions() - trace = new RuleApplicationTrace() - } - - def parseAndGetProgram(CharSequence prog) { - prog.parse - } - - def parseAndAssertNoError(CharSequence prog) { - val model = prog.parse - model.assertNoErrors - model - } - - def protected Program getLambdaProgram(String program) { - program.parse - } - - def void assertFailure(Result result) { - if (!result.failed) - Assert::assertTrue( - "should have failed, instead got: " + result.value.string + "\n" + trace.traceAsString, - result.failed - ); - } - - def void assertFailure(Boolean result) { - if (result) - Assert::assertTrue("should have failed", result); - } - - def void assertFailureTrace(Result result, CharSequence expectedTrace) { - assertFailure(result) - assertEqualsStrings(expectedTrace, result.ruleFailedException.failureTraceAsString) - } - - def assertEqualsStrings(Object expected, Object actual) { - Assert.assertEquals( - ("" + expected).replaceAll("\r", ""), - ("" + actual).replaceAll("\r", "") - ) - } - - def void assertResultAsString(Result result, String expected) { - if (result.failed) { - Assert::assertFalse( - "should not have failed: " + result.ruleFailedException.failureTraceAsString, - result.failed - ) - } - assertEqualsStrings(expected, result.value.string) - } - - def void assertResultAsString(T result, String expected) { - Assert::assertNotNull(result) - assertEqualsStrings(expected, result.string) - } - - def void assertResultAsStringBeautifier(Result result, String expected) { - if (result.failed) { - Assert::assertFalse( - "should not have failed: " + result.ruleFailedException.failureTraceAsString, - result.failed - ) - } - assertEqualsStrings(expected, reprBeautifier.string(result.value)) - } - - def void assertResult2AsString(Type result, String expected) { - Assert::assertNotNull(result); - Assert::assertNotNull(result); - assertEqualsStrings(expected, result.string) - } - - def void assertResultTrue(Boolean result) { - Assert.assertTrue(result) - } - - def getTypeVariable(Type type) { - type as TypeVariable - } - - def getArrowType(Type type) { - type as ArrowType - } - - def getTerm(CharSequence prog) { - prog.parseAndGetProgram.term - } - - def getAbstraction(CharSequence prog) { - prog.term.abstraction - } - - def getAbstraction(Term term) { - term as Abstraction - } - - def getVariable(Term term) { - term as Variable - } - - def getConstant(Term term) { - term as Constant - } - - def assertTypeVariable(Type type, String expectedName) { - Assert::assertEquals(expectedName, getTypeVariable(type).typevarName) - } - - def assertArrowType(Type type, String expectedName) { - Assert::assertEquals(expectedName, getArrowType(type).string) - } - - def assertTypeSubstitutions(TypeSubstitutions substitutions, String expected) { - assertEqualsStrings( - "subst{" + expected + "}", - reprForSubstitutions.string(substitutions) - ) - } - - def String rep(Type type) { - return LambdaTestsUtil.rep(type); - } -} diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.java b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.java new file mode 100644 index 000000000..92cff7835 --- /dev/null +++ b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2013-2017 Lorenzo Bettini. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lorenzo Bettini - Initial contribution and API + */ +package org.eclipse.xsemantics.example.lambda.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import org.eclipse.xsemantics.example.lambda.lambda.ArrowType; +import org.eclipse.xsemantics.example.lambda.lambda.TypeVariable; +import org.eclipse.xsemantics.runtime.RuleFailedException; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.XtextRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(XtextRunner.class) +@InjectWith(LambdaInjectorWithNonBeautifiedTypesProvider.class) +public class LambdaNotOccurTest extends LambdaAbstractBaseTest { + @Test + public void NotOccurConstantTypes() { + assertResultTrue( + system.notoccur(lambdaUtils.createStringType(), lambdaUtils.createIntType())); + } + + @Test + public void NotOccurDifferentTypeVariables() { + assertResultTrue(system.notoccur(lambdaUtils.createFreshTypeVariable(), + lambdaUtils.createFreshTypeVariable())); + } + + @Test + public void NotOccurTypeVariablesFails() { + final TypeVariable variable = lambdaUtils.createFreshTypeVariable(); + TypeVariable typeVariable = lambdaUtils.createTypeVariable("X1"); + RuleFailedException thrown = assertThrows(RuleFailedException.class, () -> { + system.notoccur(variable, typeVariable); + }); + assertEquals("failed: X1 occurs in X1", thrown.getMessage()); + } + + @Test + public void NotOccurTypeVariablesInArrowType() { + assertResultTrue(system.notoccur(lambdaUtils.createFreshTypeVariable(), + lambdaUtils.createFreshArrowType())); + } + + @Test + public void notOccurTypeVariablesInArrowTypeFails() { + final TypeVariable variable = lambdaUtils.createTypeVariable("a"); + ArrowType arrowType = lambdaUtils.createArrowType(lambdaUtils.createFreshArrowType(), + lambdaUtils.createArrowType(lambdaUtils.createFreshTypeVariable(), + lambdaUtils.createTypeVariable("a"))); + RuleFailedException thrown = assertThrows(RuleFailedException.class, () -> { + system.notoccur(trace, variable, arrowType); + }); + assertEquals("failed: a occurs in ((X1 -> X2) -> (X3 -> a))", thrown.getMessage()); + } +} diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.xtend b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.xtend deleted file mode 100644 index ff19892a8..000000000 --- a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaNotOccurTest.xtend +++ /dev/null @@ -1,92 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013-2017 Lorenzo Bettini. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Lorenzo Bettini - Initial contribution and API - *******************************************************************************/ - -package org.eclipse.xsemantics.example.lambda.tests - -import org.eclipse.xsemantics.runtime.RuleFailedException -import org.eclipse.xtext.testing.InjectWith -import org.eclipse.xtext.testing.XtextRunner -import org.junit.Rule -import org.junit.Test -import org.junit.rules.ExpectedException -import org.junit.runner.RunWith - -@RunWith(typeof(XtextRunner)) -@InjectWith(typeof(LambdaInjectorWithNonBeautifiedTypesProvider)) -class LambdaNotOccurTest extends LambdaAbstractBaseTest { - - @Rule public ExpectedException thrown = ExpectedException.none(); - - @Test - def void NotOccurConstantTypes() { - assertResultTrue( - system.notoccur(lambdaUtils.createStringType, lambdaUtils.createIntType) - ) - } - - @Test - def void NotOccurDifferentTypeVariables() { - assertResultTrue( - system.notoccur(lambdaUtils.createFreshTypeVariable, lambdaUtils.createFreshTypeVariable) - ) - } - - @Test - def void NotOccurTypeVariablesFails() { - thrown.expect(RuleFailedException); - thrown.expectMessage("failed: X1 occurs in X1"); - - val variable = lambdaUtils.createFreshTypeVariable - system.notoccur(variable, lambdaUtils.createTypeVariable("X1")) - } - - @Test - def void NotOccurTypeVariablesInArrowType() { - assertResultTrue( - system.notoccur( - lambdaUtils.createFreshTypeVariable, - lambdaUtils.createFreshArrowType - ) - ) - } - - @Test - def void notOccurTypeVariablesInArrowTypeFails() { - thrown.expect(RuleFailedException); - thrown.expectMessage("failed: a occurs in ((X1 -> X2) -> (X3 -> a))"); - - val variable = lambdaUtils.createTypeVariable("a") - system.notoccur( - trace, - variable, - lambdaUtils.createArrowType( - lambdaUtils.createFreshArrowType, - lambdaUtils.createArrowType(lambdaUtils.createFreshTypeVariable, lambdaUtils.createTypeVariable("a")) - ) - ) -// val trace = new RuleApplicationTrace -// var notoccur = false -// try { -// notoccur = system.notoccur(trace, variable, -// lambdaUtils.createArrowType(lambdaUtils.createFreshArrowType, -// lambdaUtils.createArrowType -// (lambdaUtils.createFreshTypeVariable, lambdaUtils.createTypeVariable("a")) -// ) -// ) -// } finally { -// assertFailureTrace( -// notoccur, -// trace, -// traces.notOccurTypeVariablesInArrowTypeFails() -// ) -// } - } -} diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.java b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.java new file mode 100644 index 000000000..727375b8c --- /dev/null +++ b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.java @@ -0,0 +1,136 @@ +/** + * Copyright (c) 2013-2017 Lorenzo Bettini. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lorenzo Bettini - Initial contribution and API + */ +package org.eclipse.xsemantics.example.lambda.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertThrows; + +import org.eclipse.xsemantics.example.lambda.lambda.ArrowType; +import org.eclipse.xsemantics.example.lambda.lambda.IntType; +import org.eclipse.xsemantics.example.lambda.lambda.StringType; +import org.eclipse.xsemantics.example.lambda.lambda.Type; +import org.eclipse.xsemantics.example.lambda.lambda.TypeVariable; +import org.eclipse.xsemantics.runtime.RuleFailedException; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.XtextRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(XtextRunner.class) +@InjectWith(LambdaInjectorWithNonBeautifiedTypesProvider.class) +public class LambdaUnifyTest extends LambdaAbstractBaseTest { + @Test + public void testUnifyTypeVar() { + assertUnify(lambdaUtils.createFreshTypeVariable(), lambdaUtils.createFreshTypeVariable(), "X3", + "X1=X3, X2=X3"); + } + + @Test + public void testUnifyStringType() { + assertUnify(lambdaUtils.createStringType(), lambdaUtils.createStringType(), "String", ""); + } + + @Test + public void testUnifyIntType() { + assertUnify(lambdaUtils.createIntType(), lambdaUtils.createIntType(), "int", ""); + } + + @Test + public void testBasicTypeFails() { + StringType stringType = lambdaUtils.createStringType(); + IntType intType = lambdaUtils.createIntType(); + RuleFailedException thrown = assertThrows(RuleFailedException.class, () -> { + system.unify(substitutions, stringType, intType); + }); + assertEquals("failed: cannot unify String with int", thrown.getMessage()); + } + + @Test + public void testUnifyTypeVarBasicType() { + assertUnify(lambdaUtils.createFreshTypeVariable(), lambdaUtils.createStringType(), "String", + "X1=String"); + } + + @Test + public void testUnifyBasicTypeTypeVar() { + assertUnify(lambdaUtils.createIntType(), lambdaUtils.createFreshTypeVariable(), "int", "X1=int"); + } + + @Test + public void testUnifyTypeVarArrowType() { + assertUnify(lambdaUtils.createFreshTypeVariable(), lambdaUtils.createFreshArrowType(), + "(X2 -> X3)", "X1=(X2 -> X3)"); + } + + @Test + public void testUnifyArrowTypeTypeVar() { + assertUnify(lambdaUtils.createFreshArrowType(), lambdaUtils.createFreshTypeVariable(), + "(X1 -> X2)", "X3=(X1 -> X2)"); + } + + @Test + public void testUnifyArrowTypes() { + assertUnify(lambdaUtils.createFreshArrowType(), lambdaUtils.createFreshArrowType(), "(X5 -> X6)", + "X1=X5, X2=X6, X3=X5, X4=X6"); + } + + @Test + public void testUnifyArrowTypes2() { + assertUnify(lambdaUtils.createFreshArrowType(), + lambdaUtils.createArrowType(lambdaUtils.createFreshArrowType(), + lambdaUtils.createFreshArrowType()), + "((X3 -> X4) -> (X5 -> X6))", "X1=(X3 -> X4), X2=(X5 -> X6)"); + } + + @Test + public void testUnifyArrowTypesWithBasicTypes() { + assertUnify(lambdaUtils.createFreshArrowType(), + lambdaUtils.createArrowType(lambdaUtils.createArrowType(lambdaUtils.createIntType(), + lambdaUtils.createStringType()), lambdaUtils.createFreshArrowType()), + "((int -> String) -> (X3 -> X4))", "X1=(int -> String), X2=(X3 -> X4)"); + } + + @Test + public void testUnifyArrowTypesWithBasicTypes2() { + assertUnify( + lambdaUtils.createArrowType(lambdaUtils.createFreshArrowType(), + lambdaUtils.createArrowType(lambdaUtils.createStringType(), + lambdaUtils.createIntType())), + lambdaUtils.createArrowType(lambdaUtils.createArrowType(lambdaUtils.createIntType(), + lambdaUtils.createStringType()), lambdaUtils.createFreshArrowType()), + "((int -> String) -> (String -> int))", "X1=int, X2=String, X3=String, X4=int"); + } + + @Test + public void unifyTypeVariableOccursInArrowTypeFails() { + final TypeVariable variable = lambdaUtils.createTypeVariable("a"); + ArrowType arrowType = lambdaUtils.createArrowType(lambdaUtils.createFreshArrowType(), + lambdaUtils.createArrowType(lambdaUtils.createFreshTypeVariable(), + lambdaUtils.createTypeVariable("a"))); + RuleFailedException thrown = assertThrows(RuleFailedException.class, () -> { + system.unify(substitutions, variable, + arrowType); + }); + assertEquals("failed: cannot unify a with ((X1 -> X2) -> (X3 -> a))", thrown.getMessage()); + } + + public void assertUnify(final Type left, final Type right, final String expectedResult, + final String expectedSubsts) { + // make sure we use the same instance of LambdaUtils + system.setLambdaUtils(lambdaUtils); + final Type result = system.unify(substitutions, left, right); + assertResult2AsString(result, expectedResult); + assertTypeSubstitutions(substitutions, expectedSubsts); + assertNotSame(left, result); + assertNotSame(right, result); + } +} diff --git a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.xtend b/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.xtend deleted file mode 100644 index 59d943e52..000000000 --- a/examples/org.eclipse.xsemantics.example.lambda.tests/src/org/eclipse/xsemantics/example/lambda/tests/LambdaUnifyTest.xtend +++ /dev/null @@ -1,185 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013-2017 Lorenzo Bettini. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Lorenzo Bettini - Initial contribution and API - *******************************************************************************/ - -package org.eclipse.xsemantics.example.lambda.tests - -import org.eclipse.xsemantics.example.lambda.lambda.Type -import org.eclipse.xsemantics.runtime.RuleFailedException -import org.eclipse.xtext.testing.InjectWith -import org.eclipse.xtext.testing.XtextRunner -import org.junit.Assert -import org.junit.Rule -import org.junit.Test -import org.junit.rules.ExpectedException -import org.junit.runner.RunWith - -@RunWith(typeof(XtextRunner)) -@InjectWith(typeof(LambdaInjectorWithNonBeautifiedTypesProvider)) -class LambdaUnifyTest extends LambdaAbstractBaseTest { - - @Rule public ExpectedException thrown = ExpectedException.none(); - - @Test - def void testUnifyTypeVar() { - assertUnify( - lambdaUtils.createFreshTypeVariable, - lambdaUtils.createFreshTypeVariable, - "X3", - "X1=X3, X2=X3" - ) - } - - @Test - def void testUnifyStringType() { - assertUnify( - lambdaUtils.createStringType, - lambdaUtils.createStringType, - "String", - "" - ) - } - - @Test - def void testUnifyIntType() { - assertUnify( - lambdaUtils.createIntType, - lambdaUtils.createIntType, - "int", - "" - ) - } - - @Test - def void testBasicTypeFails() { - thrown.expect(RuleFailedException); - thrown.expectMessage("failed: cannot unify String with int"); - - system.unify(substitutions, lambdaUtils.createStringType, lambdaUtils.createIntType) - } - - @Test - def void testUnifyTypeVarBasicType() { - assertUnify( - lambdaUtils.createFreshTypeVariable, - lambdaUtils.createStringType, - "String", - "X1=String" - ) - } - - @Test - def void testUnifyBasicTypeTypeVar() { - assertUnify( - lambdaUtils.createIntType, - lambdaUtils.createFreshTypeVariable, - "int", - "X1=int" - ) - } - - @Test - def void testUnifyTypeVarArrowType() { - assertUnify( - lambdaUtils.createFreshTypeVariable, - lambdaUtils.createFreshArrowType, - "(X2 -> X3)", - "X1=(X2 -> X3)" - ) - } - - @Test - def void testUnifyArrowTypeTypeVar() { - assertUnify( - lambdaUtils.createFreshArrowType, - lambdaUtils.createFreshTypeVariable, - "(X1 -> X2)", - "X3=(X1 -> X2)" - ) - } - - @Test - def void testUnifyArrowTypes() { - assertUnify( - lambdaUtils.createFreshArrowType, - lambdaUtils.createFreshArrowType, - "(X5 -> X6)", - "X1=X5, X2=X6, X3=X5, X4=X6" - ) - } - - @Test - def void testUnifyArrowTypes2() { - assertUnify( - lambdaUtils.createFreshArrowType, - lambdaUtils.createArrowType(lambdaUtils.createFreshArrowType, lambdaUtils.createFreshArrowType), - "((X3 -> X4) -> (X5 -> X6))", - "X1=(X3 -> X4), X2=(X5 -> X6)" - ) - } - - @Test - def void testUnifyArrowTypesWithBasicTypes() { - assertUnify( - lambdaUtils.createFreshArrowType, - lambdaUtils.createArrowType( - lambdaUtils.createArrowType(lambdaUtils.createIntType, lambdaUtils.createStringType), - lambdaUtils.createFreshArrowType - ), - "((int -> String) -> (X3 -> X4))", - "X1=(int -> String), X2=(X3 -> X4)" - ) - } - - @Test - def void testUnifyArrowTypesWithBasicTypes2() { - assertUnify( - lambdaUtils.createArrowType( - lambdaUtils.createFreshArrowType, - lambdaUtils.createArrowType(lambdaUtils.createStringType, lambdaUtils.createIntType) - ), - lambdaUtils.createArrowType( - lambdaUtils.createArrowType(lambdaUtils.createIntType, lambdaUtils.createStringType), - lambdaUtils.createFreshArrowType - ), - "((int -> String) -> (String -> int))", - "X1=int, X2=String, X3=String, X4=int" - ) - } - - @Test - def void unifyTypeVariableOccursInArrowTypeFails() { - thrown.expect(RuleFailedException); - thrown.expectMessage("failed: cannot unify a with ((X1 -> X2) -> (X3 -> a))"); - - val variable = lambdaUtils.createTypeVariable("a") - system.unify( - substitutions, - variable, - lambdaUtils.createArrowType( - lambdaUtils.createFreshArrowType, - lambdaUtils.createArrowType(lambdaUtils.createFreshTypeVariable, lambdaUtils.createTypeVariable("a")) - ) - ) - } - - def assertUnify(Type left, Type right, String expectedResult, String expectedSubsts) { - // make sure we use the same instance of LambdaUtils - system.lambdaUtils = lambdaUtils - val result = system.unify(substitutions, left, right) - assertResult2AsString( - result, - expectedResult - ) - assertTypeSubstitutions(substitutions, expectedSubsts) - Assert::assertNotSame(left, result) - Assert::assertNotSame(right, result) - } -}