-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAlgorithmTest.kt
More file actions
123 lines (106 loc) · 4.07 KB
/
Copy pathAlgorithmTest.kt
File metadata and controls
123 lines (106 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import io.github.zerumi.csa3.isa.readCode
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.io.File
import java.util.Properties
/**
* Class with golden tests definition
*
* <b>Important notice!</b>
* You shouldn't run these tests simultaneously!
*
* JUnit 5 doesn't have support for golden tests
* (see https://github.com/junit-team/junit5/discussions/2697)
*
* I've fixed this problem as much as it possible...
*
* Example of correct test usage:
* ./gradlew :comp:integrationTest --tests "AlgorithmTest.helloUserNameTest"
*/
class AlgorithmTest {
@Test
fun catTest() {
abstractAlgorithmTest("cat_golden")
}
@Test
fun helloTest() {
abstractAlgorithmTest("hello_golden")
}
@Test
fun helloUserNameTest() {
abstractAlgorithmTest("hello_username_golden")
}
@Test
fun prob2Test() {
abstractAlgorithmTest("prob2_golden")
}
@Test
fun facTest() {
abstractAlgorithmTest("fac_golden")
}
private fun abstractAlgorithmTest(directoryName: String) {
// load resources (assembly code)
val classLoader = javaClass.classLoader
val propFs = classLoader.getResourceAsStream("$directoryName/comp.properties")!!.bufferedReader()
val props = Properties()
props.load(propFs)
val asmFile = File(classLoader.getResource("$directoryName/${props.getProperty("assembly")}")!!.file)
val translatorOutput = File.createTempFile(props.getProperty("name"), ".json")
translatorOutput.deleteOnExit()
val expectedAssemblyCode = File(
classLoader.getResource("$directoryName/${props.getProperty("expected_assembly")}")!!.file
)
// run translator
io.github.zerumi.csa3.asm.main(
arrayOf(
asmFile.absolutePath,
translatorOutput.absolutePath
)
)
// prepare comp arguments
val stdin = File(classLoader.getResource("$directoryName/${props.getProperty("stdin")}")!!.file)
val stdout = File.createTempFile("${props.getProperty("name")}_stdout", ".txt")
val logFile = File.createTempFile("${props.getProperty("name")}_log", ".txt")
stdout.deleteOnExit()
logFile.deleteOnExit()
// load expected results
val stdoutExpected =
File(
classLoader.getResource("$directoryName/${props.getProperty("expected_out")}")!!.file
)
val logFileExpected =
File(
classLoader.getResource("$directoryName/${props.getProperty("expected_log")}")!!.file
)
// run comp
io.github.zerumi.csa3.comp.main(
arrayOf(
"--program-file",
translatorOutput.absolutePath,
"--input-file",
stdin.absolutePath,
"--output-file",
stdout.absolutePath,
"--log-file",
logFile.absolutePath
)
)
if (System.getProperty("updateGolden") == "true") {
val assemblyExpectedResourceFile = File(props.getProperty("expected_assembly_resource"))
translatorOutput.copyTo(assemblyExpectedResourceFile, true)
val outExpectedResourceFile = File(props.getProperty("expected_out_resource"))
stdout.copyTo(outExpectedResourceFile, true)
val logExpectedResourceFile = File(props.getProperty("expected_log_resource"))
logFile.copyTo(logExpectedResourceFile, true)
} else {
// assert results
assertEquals(readCode(expectedAssemblyCode.toPath()), readCode(translatorOutput.toPath()))
val stdoutText = stdout.readText(Charsets.UTF_8)
val stdoutExpectedText = stdoutExpected.readText(Charsets.UTF_8)
assertEquals(stdoutText, stdoutExpectedText)
val logText = logFile.readText(Charsets.UTF_8)
val logExpectedText = logFileExpected.readText(Charsets.UTF_8)
assertEquals(logText, logExpectedText)
}
}
}