Call & Ignore

This commit is contained in:
Tibo De Peuter 2025-05-09 08:36:11 +02:00
parent 9b454a9669
commit 5bfeb96176
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
7 changed files with 118 additions and 28 deletions

View file

@ -0,0 +1,34 @@
package prolog.builtins
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import prolog.ast.arithmetic.Integer
import prolog.ast.lists.List.Cons
import prolog.ast.lists.List.Empty
import prolog.ast.terms.Variable
class MetaOperatorsTests {
@Test
fun `ignore of failing goal succeeds`() {
val goal = Member(Integer(4), Cons(Integer(1), Cons(Integer(2), Cons(Integer(3), Empty))))
val result = Ignore(goal).satisfy(emptyMap()).toList()
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
assertTrue(result[0].getOrNull()!!.isEmpty(), "Expected empty substitutions")
}
@Test
fun `ignore of succeeding goal returns first solution`() {
val goal = Member(Variable("X"), Cons(Integer(1), Cons(Integer(2), Cons(Integer(3), Empty))))
val result = Ignore(goal).satisfy(emptyMap()).toList()
assertEquals(1, result.size, "Should return one result")
assertTrue(result[0].isSuccess, "Result should be successful")
val subs = result[0].getOrNull()!!
assertEquals(Integer(1), subs[Variable("X")], "Expected first solution to be 1")
}
}