aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/sources
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-06 18:33:29 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-06 18:33:29 +0100
commitd6e286f8b595d07fd3d2656a235fe49e3843faf9 (patch)
tree7dee50e20f373f1627fe602a85c045e525ad6e82 /src/test/java/org/shedlang/compiler/sources
parentd579ddb4c0da50059a263e80ba7e8af375c83f87 (diff)
Add tokeniser
Diffstat (limited to 'src/test/java/org/shedlang/compiler/sources')
-rw-r--r--src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java110
-rw-r--r--src/test/java/org/shedlang/compiler/sources/SourceMatchers.java15
2 files changed, 125 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java b/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java
new file mode 100644
index 00000000..534f10c5
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java
@@ -0,0 +1,110 @@
+package org.shedlang.compiler.sources;
+
+import org.junit.jupiter.api.DynamicTest;
+import org.junit.jupiter.api.TestFactory;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class FileFragmentSourceTests {
+ private record TestCase(
+ String name,
+ String contents,
+ int characterIndexStart,
+ String expectedContext
+ ) {
+ public int characterIndexEnd() {
+ return characterIndexStart;
+ }
+ }
+
+ @TestFactory
+ public List<DynamicTest> sourceContextIsSourceLineWithPointer() {
+ return Stream.of(
+ new TestCase(
+ "one line, first character",
+ "abcd",
+ 0,
+ """
+ <filename>:1:1
+ abcd
+ ^"""
+ ),
+ new TestCase(
+ "one line, last character",
+ "abcd",
+ 3,
+ """
+ <filename>:1:4
+ abcd
+ ^"""
+ ),
+ new TestCase(
+ "one line, end",
+ "abcd",
+ 4,
+ """
+ <filename>:1:5
+ abcd
+ ^"""
+ ),
+ new TestCase(
+ "many lines, end",
+ """
+ abc
+ def""",
+ 7,
+ """
+ <filename>:2:4
+ def
+ ^"""
+ ),
+ new TestCase(
+ "first line, first character",
+ """
+ abc
+ def""",
+ 0,
+ """
+ <filename>:1:1
+ abc
+ ^"""
+ ),
+ new TestCase(
+ "first line, last character",
+ """
+ abc
+ def""",
+ 2,
+ """
+ <filename>:1:3
+ abc
+ ^"""
+ ),
+ new TestCase(
+ "last line, first character",
+ """
+ abc
+ def""",
+ 4,
+ """
+ <filename>:2:1
+ def
+ ^"""
+ )
+ ).map(testCase -> DynamicTest.dynamicTest(testCase.name, () -> {
+ var source = new FileFragmentSource(
+ "<filename>",
+ testCase.contents,
+ testCase.characterIndexStart,
+ testCase.characterIndexEnd()
+ );
+
+ assertThat(source.describe(), equalTo(testCase.expectedContext));
+ })).collect(Collectors.toList());
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/sources/SourceMatchers.java b/src/test/java/org/shedlang/compiler/sources/SourceMatchers.java
new file mode 100644
index 00000000..64e70d18
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/sources/SourceMatchers.java
@@ -0,0 +1,15 @@
+package org.shedlang.compiler.sources;
+
+import org.zwobble.precisely.Matcher;
+
+import static org.zwobble.precisely.Matchers.instanceOf;
+
+public class SourceMatchers {
+ private SourceMatchers() {
+
+ }
+
+ public static Matcher<Source> isFileFragmentSource(Matcher<FileFragmentSource> matcher) {
+ return instanceOf(FileFragmentSource.class, matcher);
+ }
+}