package scotch.compiler.analyzer; import static java.util.Arrays.asList; import static scotch.compiler.syntax.value.Values.apply; import static scotch.compiler.syntax.type.Types.t; import static scotch.util.TestUtil.arg; import static scotch.util.TestUtil.capture; import static scotch.util.TestUtil.id; import static scotch.util.TestUtil.literal; import static scotch.util.TestUtil.matcher; import static scotch.util.TestUtil.pattern; import java.util.Optional; import java.util.function.Function; import org.junit.Test; import scotch.compiler.ClassLoaderResolver; import scotch.compiler.Compiler; import scotch.compiler.CompilerTest; import scotch.compiler.syntax.definition.DefinitionGraph; public class NameQualifierTest extends CompilerTest<ClassLoaderResolver> { @Test public void shouldQualifyNames() { compile( "module scotch.test", "fn1 a b = fn2 a b", "fn2 a b = a b" ); shouldNotHaveErrors(); shouldHaveValue("scotch.test.fn1", matcher("scotch.test.(fn1#0)", t(13), asList(arg("#0", t(11)), arg("#1", t(12))), pattern( "scotch.test.(fn1#0#0)", asList(capture(arg("#0", t(14)), "a", t(1)), capture(arg("#1", t(15)), "b", t(2))), apply( apply(id("scotch.test.fn2", t(3)), id("a", t(4)), t(16)), id("b", t(5)), t(17) ) )) ); } @Test public void shouldQualifyBindNames() { compile( "module scotch.test", "import scotch.control.monad", "import scotch.data.either", "run = Right \"Yes\" >>= \\which -> Left \"No\"" ); shouldHaveValue("scotch.test.run", apply( apply( id("scotch.control.monad.(>>=)", t(1)), apply(id("scotch.data.either.Right", t(0)), literal("Yes"), t(6)), t(7) ), matcher("scotch.test.(run#0)", t(2), arg("#0", t(4)), pattern( "scotch.test.(run#0#1)", asList(capture(arg("#0", t(9)), "which", t(3))), apply(id("scotch.data.either.Left", t(5)), literal("No"), t(10)) )), t(8) )); } @Test public void shouldQualifySymbolsDefinedInJava() { compile( "module scotch.test", "head (x:xs) = x", "head _ = raise \"Can't get head of empty list!\"", "run = expect (head []) toRaise \"Can't get head of empty list!\"" ); shouldHaveValue("scotch.test.run", apply( apply( apply( id("scotch.test.expect", t(9)), apply( id("scotch.test.head", t(10)), id("scotch.data.list.[]", t(11)), t(13) ), t(14) ), id("scotch.test.toRaise", t(12)), t(15) ), literal("Can't get head of empty list!"), t(16) )); } @Override protected Function<Compiler, DefinitionGraph> compile() { return Compiler::qualifyNames; } @Override protected ClassLoaderResolver initResolver() { return new ClassLoaderResolver(Optional.empty(), getClass().getClassLoader()); } }