mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Use stages in programs test
This commit is contained in:
parent
42e55bd4cc
commit
f0317c44c7
@ -4,5 +4,10 @@ public interface StagesData {
|
||||
|
||||
<T> T get(String tag);
|
||||
|
||||
default <T> T getOrDefault(String tag, T other) {
|
||||
T value = get(tag);
|
||||
return value != null ? value : other;
|
||||
}
|
||||
|
||||
void put(String tag, Object input);
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
package com.annimon.ownlang.stages;
|
||||
package com.annimon.ownlang.parser.error;
|
||||
|
||||
import com.annimon.ownlang.Console;
|
||||
import com.annimon.ownlang.parser.Pos;
|
||||
import com.annimon.ownlang.parser.Range;
|
||||
import com.annimon.ownlang.parser.error.ParseError;
|
||||
import com.annimon.ownlang.parser.error.ParseErrors;
|
||||
import com.annimon.ownlang.stages.SourceLoaderStage;
|
||||
import com.annimon.ownlang.stages.Stage;
|
||||
import com.annimon.ownlang.stages.StagesData;
|
||||
|
||||
public class ParseErrorsFormatterStage implements Stage<ParseErrors, String> {
|
||||
|
@ -10,7 +10,7 @@ public record OptimizationStage(int level)
|
||||
|
||||
@Override
|
||||
public Statement perform(StagesData stagesData, Statement input) {
|
||||
boolean showSummary = stagesData.get(TAG_OPTIMIZATION_SUMMARY);
|
||||
boolean showSummary = stagesData.getOrDefault(TAG_OPTIMIZATION_SUMMARY, false);
|
||||
return Optimizer.optimize(input, level, showSummary);
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,43 @@
|
||||
package com.annimon.ownlang.parser;
|
||||
|
||||
import com.annimon.ownlang.Console;
|
||||
import com.annimon.ownlang.lib.*;
|
||||
import com.annimon.ownlang.outputsettings.OutputSettings;
|
||||
import com.annimon.ownlang.outputsettings.StringOutputSettings;
|
||||
import com.annimon.ownlang.lib.FunctionValue;
|
||||
import com.annimon.ownlang.lib.NumberValue;
|
||||
import com.annimon.ownlang.lib.ScopeHandler;
|
||||
import com.annimon.ownlang.parser.ast.FunctionDefineStatement;
|
||||
import com.annimon.ownlang.parser.ast.Statement;
|
||||
import com.annimon.ownlang.parser.ast.Visitor;
|
||||
import com.annimon.ownlang.parser.visitors.AbstractVisitor;
|
||||
import com.annimon.ownlang.stages.*;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.annimon.ownlang.parser.TestDataUtil.scanDirectory;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class ProgramsTest {
|
||||
private static final String RES_DIR = "src/test/resources";
|
||||
private static Stage<String, Statement> testPipeline;
|
||||
|
||||
public static Stream<String> data() {
|
||||
return scanDirectory(RES_DIR)
|
||||
.map(File::getPath);
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public static void createStage() {
|
||||
testPipeline = new SourceLoaderStage()
|
||||
.then(new LexerStage())
|
||||
.then(new ParserStage())
|
||||
.then(new ExecutionStage())
|
||||
.then((stagesData, input) -> {
|
||||
input.accept(testFunctionsExecutor);
|
||||
return input;
|
||||
});
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void initialize() {
|
||||
ScopeHandler.resetScope();
|
||||
@ -69,30 +79,11 @@ public class ProgramsTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("data")
|
||||
public void testProgram(String programPath) throws IOException {
|
||||
final String source = SourceLoader.readSource(programPath);
|
||||
final Statement s = Parser.parse(Lexer.tokenize(source));
|
||||
public void testProgram(String programPath) {
|
||||
try {
|
||||
s.execute();
|
||||
s.accept(testFunctionsExecutor);
|
||||
testPipeline.perform(new StagesDataMap(), programPath);
|
||||
} catch (Exception oae) {
|
||||
fail(oae.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOutput() {
|
||||
OutputSettings oldSettings = Console.getSettings();
|
||||
Console.useSettings(new StringOutputSettings());
|
||||
String source = "for i = 0, i <= 5, i++\n print i";
|
||||
final Statement s = Parser.parse(Lexer.tokenize(source));
|
||||
try {
|
||||
s.execute();
|
||||
assertEquals("012345", Console.text());
|
||||
} catch (Exception oae) {
|
||||
fail(oae.toString());
|
||||
} finally {
|
||||
Console.useSettings(oldSettings);
|
||||
fail(oae);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user