From d6e286f8b595d07fd3d2656a235fe49e3843faf9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 6 Jun 2023 18:33:29 +0100 Subject: Add tokeniser --- .../compiler/sources/FileFragmentSourceTests.java | 110 +++++++++++++++++++++ .../shedlang/compiler/sources/SourceMatchers.java | 15 +++ 2 files changed, 125 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java create mode 100644 src/test/java/org/shedlang/compiler/sources/SourceMatchers.java (limited to 'src/test/java/org/shedlang/compiler/sources') 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 sourceContextIsSourceLineWithPointer() { + return Stream.of( + new TestCase( + "one line, first character", + "abcd", + 0, + """ + :1:1 + abcd + ^""" + ), + new TestCase( + "one line, last character", + "abcd", + 3, + """ + :1:4 + abcd + ^""" + ), + new TestCase( + "one line, end", + "abcd", + 4, + """ + :1:5 + abcd + ^""" + ), + new TestCase( + "many lines, end", + """ + abc + def""", + 7, + """ + :2:4 + def + ^""" + ), + new TestCase( + "first line, first character", + """ + abc + def""", + 0, + """ + :1:1 + abc + ^""" + ), + new TestCase( + "first line, last character", + """ + abc + def""", + 2, + """ + :1:3 + abc + ^""" + ), + new TestCase( + "last line, first character", + """ + abc + def""", + 4, + """ + :2:1 + def + ^""" + ) + ).map(testCase -> DynamicTest.dynamicTest(testCase.name, () -> { + var source = new FileFragmentSource( + "", + 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 isFileFragmentSource(Matcher matcher) { + return instanceOf(FileFragmentSource.class, matcher); + } +} -- cgit v1.2.3