IO Operators
This commit is contained in:
parent
b9f419a59d
commit
82a8fccf87
22 changed files with 450 additions and 199 deletions
|
|
@ -12,8 +12,8 @@ class SourceFileReaderTests {
|
|||
|
||||
@Test
|
||||
fun a() {
|
||||
val inputFile = "tests/better_parser/resources/a.pl"
|
||||
val reader = SourceFileReader()
|
||||
val inputFile = "tests/parser/resources/a.pl"
|
||||
val reader = FileLoader()
|
||||
|
||||
reader.readFile(inputFile)
|
||||
|
||||
|
|
@ -22,8 +22,8 @@ class SourceFileReaderTests {
|
|||
|
||||
@Test
|
||||
fun foo() {
|
||||
val inputFile = "tests/better_parser/resources/foo.pl"
|
||||
val reader = SourceFileReader()
|
||||
val inputFile = "tests/parser/resources/foo.pl"
|
||||
val reader = FileLoader()
|
||||
|
||||
reader.readFile(inputFile)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
package parser
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertInstanceOf
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import prolog.ast.logic.Fact
|
||||
import prolog.ast.terms.Atom
|
||||
import prolog.logic.equivalent
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ScriptParserTests {
|
||||
private lateinit var parser: ScriptParser
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
parser = ScriptParser()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parse single atom`() {
|
||||
val input = """
|
||||
a.
|
||||
""".trimIndent()
|
||||
|
||||
val result = parser.parse(input)
|
||||
val expected = Fact(Atom("a"))
|
||||
|
||||
assertEquals(1, result.size, "Should return one result")
|
||||
assertInstanceOf(Fact::class.java, result[0], "Result should be a fact")
|
||||
assertTrue(
|
||||
equivalent(expected.head, result[0].head, emptyMap()),
|
||||
"Expected fact 'a'"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -4,9 +4,4 @@ female(mary).
|
|||
parent(john, jimmy).
|
||||
parent(mary, jimmy).
|
||||
father(X, Y) :- parent(X, Y), male(X).
|
||||
mother(X, Y) :- parent(X, Y), female(X).
|
||||
|
||||
:- write(hello),
|
||||
nl.
|
||||
|
||||
:- write(hello2).
|
||||
mother(X, Y) :- parent(X, Y), female(X).
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
package prolog.builtins
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertInstanceOf
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Disabled
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.ValueSource
|
||||
|
|
@ -13,6 +15,7 @@ import java.io.ByteArrayOutputStream
|
|||
import java.io.PrintStream
|
||||
import prolog.ast.arithmetic.Integer
|
||||
import prolog.ast.terms.Variable
|
||||
import java.io.ByteArrayInputStream
|
||||
|
||||
class IoOperatorsTests {
|
||||
private var outStream = ByteArrayOutputStream()
|
||||
|
|
@ -44,7 +47,7 @@ class IoOperatorsTests {
|
|||
|
||||
assertEquals(1, result.size, "Should return one result")
|
||||
assertTrue(result[0].isSuccess, "Result should be successful")
|
||||
assertEquals(name, outStream.toString().trim(), "Output should match the atom")
|
||||
assertEquals(name, outStream.toString(), "Output should match the atom")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -79,7 +82,86 @@ class IoOperatorsTests {
|
|||
assertEquals(1, result.size, "Should return one result")
|
||||
assertTrue(result[0].isSuccess, "Result should be successful")
|
||||
|
||||
val output = outStream.toString().trim()
|
||||
val output = outStream.toString()
|
||||
assertTrue(output == expected1 || output == expected2, "Output should match the arithmetic expression")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `write nl`() {
|
||||
val nl = Nl
|
||||
|
||||
val result = nl.satisfy(emptyMap()).toList()
|
||||
|
||||
assertEquals(1, result.size, "Should return one result")
|
||||
assertTrue(result[0].isSuccess, "Result should be successful")
|
||||
assertTrue(outStream.toString().contains("\n"), "Output should contain a newline")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `read term`() {
|
||||
val inputStream = ByteArrayInputStream("hello.".toByteArray())
|
||||
System.setIn(inputStream)
|
||||
|
||||
val read = Read(Variable("X"))
|
||||
|
||||
val result = read.satisfy(emptyMap()).toList()
|
||||
|
||||
assertEquals(1, result.size, "Should return one result")
|
||||
assertTrue(result[0].isSuccess, "Result should be successful")
|
||||
val answer = result[0].getOrNull()!!
|
||||
assertTrue(answer.containsKey(Variable("X")), "Result should be successful")
|
||||
assertInstanceOf(Atom::class.java, answer[Variable("X")], "Output should be an atom")
|
||||
assertEquals(Atom("hello"), answer[Variable("X")], "Output should match the read term")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `read between(1, 2, 3)`() {
|
||||
val inputStream = ByteArrayInputStream("between(1, 2, 3).".toByteArray())
|
||||
System.setIn(inputStream)
|
||||
|
||||
val read = Read(Variable("X"))
|
||||
|
||||
val result = read.satisfy(emptyMap()).toList()
|
||||
|
||||
assertEquals(1, result.size, "Should return one result")
|
||||
assertTrue(result[0].isSuccess, "Result should be successful")
|
||||
val answer = result[0].getOrNull()!!
|
||||
assertTrue(answer.containsKey(Variable("X")), "Result should be successful")
|
||||
assertInstanceOf(CompoundTerm::class.java, answer[Variable("X")], "Output should be a compound term")
|
||||
assertEquals(
|
||||
CompoundTerm(Atom("between"), listOf(Integer(1), Integer(2), Integer(3))),
|
||||
answer[Variable("X")],
|
||||
"Output should match the read term"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `read foo(a, X, b, Y, c, Z)`() {
|
||||
val inputStream = ByteArrayInputStream("foo(A, x, B, y, C, z).".toByteArray())
|
||||
System.setIn(inputStream)
|
||||
|
||||
val read = Read(CompoundTerm(Atom("foo"), listOf(
|
||||
Atom("a"),
|
||||
Variable("X"),
|
||||
Atom("b"),
|
||||
Variable("Y"),
|
||||
Atom("c"),
|
||||
Variable("Z")
|
||||
)))
|
||||
|
||||
val result = read.satisfy(emptyMap()).toList()
|
||||
|
||||
assertEquals(1, result.size, "Should return one result")
|
||||
assertTrue(result[0].isSuccess, "Result should be successful")
|
||||
|
||||
val answer = result[0].getOrNull()!!
|
||||
|
||||
assertTrue(answer.containsKey(Variable("X")), "Result should be successful")
|
||||
assertTrue(answer.containsKey(Variable("Y")), "Result should be successful")
|
||||
assertTrue(answer.containsKey(Variable("Z")), "Result should be successful")
|
||||
|
||||
assertEquals(Atom("x"), answer[Variable("X")], "Output should match the read term")
|
||||
assertEquals(Atom("y"), answer[Variable("Y")], "Output should match the read term")
|
||||
assertEquals(Atom("z"), answer[Variable("Z")], "Output should match the read term")
|
||||
}
|
||||
}
|
||||
Reference in a new issue