package interpreter import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import prolog.ast.terms.Atom import prolog.ast.terms.CompoundTerm import prolog.ast.terms.Term import prolog.builtins.Conjunction import prolog.builtins.Disjunction import prolog.builtins.Cut import prolog.builtins.Fail import prolog.builtins.True import prolog.builtins.Not class PreprocessorTests { class OpenPreprocessor : Preprocessor() { public override fun preprocess(input: Term): Term { return super.preprocess(input) } } @Nested class `Arithmetic operators` { @Test fun `evaluates to different`() { assertEquals(1, 2) } } @Nested class `Control operators` { private var preprocessor = OpenPreprocessor() @Test fun fail() { val tests = mapOf( Atom("fail") to Fail, CompoundTerm(Atom("fail"), emptyList()) to Fail, Atom("Fail") to Atom("Fail"), CompoundTerm(Atom("Fail"), emptyList()) to CompoundTerm(Atom("Fail"), emptyList()), CompoundTerm(Atom("fail"), listOf(Atom("a"))) to CompoundTerm(Atom("fail"), listOf(Atom("a"))), CompoundTerm(Atom("fail"), listOf(Atom("fail"))) to CompoundTerm(Atom("fail"), listOf(Fail)) ) for ((input, expected) in tests) { val result = preprocessor.preprocess(input) assertEquals(expected, result, "Expected preprocessed") assertEquals(expected::class, result::class, "Expected same class") } } @Test fun `true`() { val tests = mapOf( Atom("true") to True, CompoundTerm(Atom("true"), emptyList()) to True, Atom("True") to Atom("True"), CompoundTerm(Atom("True"), emptyList()) to CompoundTerm(Atom("True"), emptyList()), CompoundTerm(Atom("true"), listOf(Atom("a"))) to CompoundTerm(Atom("true"), listOf(Atom("a"))), CompoundTerm(Atom("true"), listOf(Atom("true"))) to CompoundTerm(Atom("true"), listOf(True)) ) for ((input, expected) in tests) { val result = preprocessor.preprocess(input) assertEquals(expected, result, "Expected preprocessed") assertEquals(expected::class, result::class, "Expected same class") } } @Test fun cut() { val tests = mapOf( Atom("!") to Cut(), CompoundTerm(Atom("!"), emptyList()) to Cut(), CompoundTerm(Atom("!"), listOf(Atom("a"))) to CompoundTerm(Atom("!"), listOf(Atom("a"))), CompoundTerm(Atom("!"), listOf(Atom("!"))) to CompoundTerm(Atom("!"), listOf(Cut())) ) for ((input, expected) in tests) { val result = preprocessor.preprocess(input) assertEquals(expected, result, "Expected preprocessed") assertEquals(expected::class, result::class, "Expected same class") } } @Test fun conjunction() { val tests = mapOf( CompoundTerm(Atom(","), listOf(Atom("a"), Atom("b"))) to Conjunction(Atom("a"), Atom("b")), CompoundTerm(Atom(","), listOf(Atom("a"), Atom("b"), Atom("c"))) to CompoundTerm( Atom(","), listOf(Atom("a"), Atom("b"), Atom("c")) ), // Nested conjunctions CompoundTerm( Atom(","), listOf(Atom("a"), CompoundTerm(Atom(","), listOf(Atom("b"), Atom("c")))) ) to Conjunction(Atom("a"), Conjunction(Atom("b"), Atom("c"))), ) for ((input, expected) in tests) { val result = preprocessor.preprocess(input) assertEquals(expected, result, "Expected preprocessed") assertEquals(expected::class, result::class, "Expected same class") } } @Test fun disjunction() { val tests = mapOf( CompoundTerm(Atom(";"), listOf(Atom("a"), Atom("b"))) to Disjunction(Atom("a"), Atom("b")), CompoundTerm(Atom(";"), listOf(Atom("a"), Atom("b"), Atom("c"))) to CompoundTerm( Atom(";"), listOf(Atom("a"), Atom("b"), Atom("c")) ), // Nested disjunctions CompoundTerm( Atom(";"), listOf(Atom("a"), CompoundTerm(Atom(";"), listOf(Atom("b"), Atom("c")))) ) to Disjunction(Atom("a"), Disjunction(Atom("b"), Atom("c"))), ) for ((input, expected) in tests) { val result = preprocessor.preprocess(input) assertEquals(expected, result, "Expected preprocessed") assertEquals(expected::class, result::class, "Expected same class") } } @Test fun not() { val tests = mapOf( CompoundTerm(Atom("\\+"), listOf(Atom("a"))) to Not(Atom("a")), CompoundTerm(Atom("\\+"), listOf(Atom("a"), Atom("b"))) to CompoundTerm( Atom("\\+"), listOf(Atom("a"), Atom("b")) ), // Nested not CompoundTerm( Atom("foo"), listOf( Atom("bar"), CompoundTerm(Atom("\\+"), listOf(CompoundTerm(Atom("\\+"), listOf(Atom("baz"))))) ) ) to CompoundTerm(Atom("foo"), listOf(Atom("bar"), Not(Not(Atom("baz"))))), ) for ((input, expected) in tests) { val result = preprocessor.preprocess(input) assertEquals(expected, result, "Expected preprocessed") assertEquals(expected::class, result::class, "Expected same class") } } } }