Compare commits
3 Commits
c8a89849ee
...
7a13b032f8
Author | SHA1 | Date | |
---|---|---|---|
7a13b032f8 | |||
5a154c8a69 | |||
ae77e3b55e |
@ -63,9 +63,12 @@ public class Environment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> T init(Key<T> key, T val) {
|
public <T> T init(Key<T> key, T val) {
|
||||||
if (!has(key)) this.add(key, val);
|
if (!has(key)) {
|
||||||
|
this.add(key, val);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
else return get(key);
|
||||||
|
}
|
||||||
public <T> T initFrom(Key<T> key, Supplier<T> val) {
|
public <T> T initFrom(Key<T> key, Supplier<T> val) {
|
||||||
if (!has(key)) {
|
if (!has(key)) {
|
||||||
var res = val.get();
|
var res = val.get();
|
||||||
|
@ -32,9 +32,9 @@ public abstract class Location implements Comparable<Location> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
public final Location nextLine() {
|
public final Location nextLine() {
|
||||||
return changeLine(1);
|
return nextLine(1);
|
||||||
}
|
}
|
||||||
public final Location changeLine(int offset) {
|
public final Location nextLine(int offset) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
return new Location() {
|
return new Location() {
|
||||||
@ -60,7 +60,14 @@ public abstract class Location implements Comparable<Location> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public int compareTo(Location other) {
|
@Override public int compareTo(Location other) {
|
||||||
int a = filename().toString().compareTo(other.filename().toString());
|
int a;
|
||||||
|
var filenameA = filename();
|
||||||
|
var filenameB = other.filename();
|
||||||
|
if (filenameB == null && filenameA == null) a = 0;
|
||||||
|
else if (filenameA == null) a = -1;
|
||||||
|
else if (filenameB == null) a = 1;
|
||||||
|
else a = filenameA.toString().compareTo(filenameB.toString());
|
||||||
|
|
||||||
int b = Integer.compare(line(), other.line());
|
int b = Integer.compare(line(), other.line());
|
||||||
int c = Integer.compare(start(), other.start());
|
int c = Integer.compare(start(), other.start());
|
||||||
|
|
||||||
@ -79,21 +86,27 @@ public abstract class Location implements Comparable<Location> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Location of(String raw) {
|
public static Location of(String raw) {
|
||||||
var i0 = raw.lastIndexOf(':');
|
var i1 = raw.lastIndexOf(':');
|
||||||
if (i0 < 0) return Location.of(Filename.parse(raw), -1, -1);
|
if (i1 < 0) return Location.of(Filename.parse(raw), -1, -1);
|
||||||
|
|
||||||
var i1 = raw.lastIndexOf(':', i0);
|
var i0 = raw.substring(0, i1).lastIndexOf(':', i1);
|
||||||
if (i0 < 0) {
|
if (i0 < 0) {
|
||||||
try {
|
try {
|
||||||
return Location.of(Filename.parse(raw.substring(0, i0)), Integer.parseInt(raw.substring(i0 + 1)), -1);
|
return Location.of(null, Integer.parseInt(raw.substring(0, i1)) - 1, Integer.parseInt(raw.substring(i1 + 1)) - 1);
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e) {
|
catch (NumberFormatException e) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return Location.of(Filename.parse(raw.substring(0, i1)), Integer.parseInt(raw.substring(i1 + 1)) - 1, -1);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e) {}
|
||||||
|
|
||||||
return Location.of(Filename.parse(raw), -1, -1);
|
return Location.of(Filename.parse(raw), -1, -1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int start, line;
|
int start, line;
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
start = Integer.parseInt(raw.substring(i1 + 1));
|
start = Integer.parseInt(raw.substring(i1 + 1));
|
||||||
}
|
}
|
||||||
@ -105,9 +118,9 @@ public abstract class Location implements Comparable<Location> {
|
|||||||
line = Integer.parseInt(raw.substring(i0 + 1, i1));
|
line = Integer.parseInt(raw.substring(i0 + 1, i1));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e) {
|
catch (NumberFormatException e) {
|
||||||
return Location.of(Filename.parse(raw.substring(i1 + 1)), start, -1);
|
return Location.of(Filename.parse(raw.substring(0, i1)), start - 1, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Location.of(Filename.parse(raw.substring(0, i0)), start, line);
|
return Location.of(Filename.parse(raw.substring(0, i0)), line - 1, start - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
package me.topchetoeu.j2s.common;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import me.topchetoeu.j2s.common.environment.Environment;
|
||||||
|
import me.topchetoeu.j2s.common.environment.Key;
|
||||||
|
|
||||||
|
public class TestEnvironment {
|
||||||
|
private final Key<String> FOO = new Key<>();
|
||||||
|
private final Key<Void> MARKER = new Key<>();
|
||||||
|
|
||||||
|
@Test public void testShouldCreate() {
|
||||||
|
new Environment();
|
||||||
|
Environment.empty();
|
||||||
|
}
|
||||||
|
@Test public void testShouldNotExist() {
|
||||||
|
var env = new Environment();
|
||||||
|
assertEquals(env.get(FOO), null);
|
||||||
|
assertEquals(env.has(FOO), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldAdd() {
|
||||||
|
var env = new Environment();
|
||||||
|
env.add(FOO, "test");
|
||||||
|
assertEquals(env.get(FOO), "test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldGetFromParent() {
|
||||||
|
var parent = new Environment();
|
||||||
|
parent.add(FOO, "test");
|
||||||
|
var child = parent.child();
|
||||||
|
assertEquals(child.get(FOO), "test");
|
||||||
|
assertEquals(child.has(FOO), true);
|
||||||
|
}
|
||||||
|
@Test public void testShouldHideParent() {
|
||||||
|
var parent = new Environment();
|
||||||
|
parent.add(FOO, "test");
|
||||||
|
var child = parent.child();
|
||||||
|
child.remove(FOO);
|
||||||
|
assertEquals(child.get(FOO), null);
|
||||||
|
assertEquals(child.has(FOO), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldAddMarker() {
|
||||||
|
var env = new Environment();
|
||||||
|
env.add(MARKER);
|
||||||
|
assertEquals(env.has(MARKER), true);
|
||||||
|
assertEquals(env.hasNotNull(MARKER), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldInitOnce() {
|
||||||
|
var env = new Environment();
|
||||||
|
assertEquals(env.init(FOO, "a"), "a");
|
||||||
|
assertEquals(env.init(FOO, "b"), "a");
|
||||||
|
assertEquals(env.get(FOO), "a");
|
||||||
|
}
|
||||||
|
@Test public void testShouldInitOnceFrom() {
|
||||||
|
var env = new Environment();
|
||||||
|
assertEquals(env.initFrom(FOO, () -> "a"), "a");
|
||||||
|
assertEquals(env.initFrom(FOO, () -> "b"), "a");
|
||||||
|
assertEquals(env.get(FOO), "a");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldWrap() {
|
||||||
|
var env = new Environment();
|
||||||
|
assertEquals(Environment.wrap(env), env);
|
||||||
|
assertNotEquals(Environment.wrap(null), null);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package me.topchetoeu.j2s.common;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class TestFilename {
|
||||||
|
@Test public void testShouldParseFilePath() {
|
||||||
|
var filename = Filename.parse("file://hello.world");
|
||||||
|
assertEquals("file", filename.protocol);
|
||||||
|
assertEquals("hello.world", filename.path);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseNoProtocolFilename() {
|
||||||
|
var filename = Filename.parse("hello.world");
|
||||||
|
assertEquals("file", filename.protocol);
|
||||||
|
assertEquals("hello.world", filename.path);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseAdditionalSlashFilename() {
|
||||||
|
var filename = Filename.parse("test:///hello.world");
|
||||||
|
assertEquals("test", filename.protocol);
|
||||||
|
assertEquals("/hello.world", filename.path);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseOneSlashFilename() {
|
||||||
|
var filename = Filename.parse("test:/hello.world");
|
||||||
|
assertEquals("file", filename.protocol);
|
||||||
|
assertEquals("test:/hello.world", filename.path);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseMatroshkaFilename() {
|
||||||
|
var a = Filename.parse("a://b://hello.world");
|
||||||
|
assertEquals("a", a.protocol);
|
||||||
|
assertEquals("b://hello.world", a.path);
|
||||||
|
|
||||||
|
var b = Filename.parse(a.path);
|
||||||
|
assertEquals("b", b.protocol);
|
||||||
|
assertEquals("hello.world", b.path);
|
||||||
|
}
|
||||||
|
}
|
162
common/src/test/java/me/topchetoeu/j2s/common/TestLocation.java
Normal file
162
common/src/test/java/me/topchetoeu/j2s/common/TestLocation.java
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
package me.topchetoeu.j2s.common;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import me.topchetoeu.j2s.common.parsing.Filename;
|
||||||
|
import me.topchetoeu.j2s.common.parsing.Location;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class TestLocation {
|
||||||
|
@Test public void testShouldCreateLocation() {
|
||||||
|
var loc = Location.of(new Filename("file", "test.txt"), 10, 5);
|
||||||
|
assertEquals("test.txt", loc.filename().path);
|
||||||
|
assertEquals(10, loc.line());
|
||||||
|
assertEquals(5, loc.start());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldGetNextLineLocation() {
|
||||||
|
var loc = Location.of("test.txt:10:5");
|
||||||
|
var next = loc.nextLine();
|
||||||
|
assertEquals(next.filename(), new Filename("file", "test.txt"));
|
||||||
|
assertEquals(next.line(), 10);
|
||||||
|
assertEquals(next.start(), 0);
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "test.txt"));
|
||||||
|
assertEquals(loc.line(), 9);
|
||||||
|
assertEquals(loc.start(), 4);
|
||||||
|
}
|
||||||
|
@Test public void testShouldGetNextNthLineLocation() {
|
||||||
|
var loc = Location.of(new Filename("file", "test.txt"), 10, 5);
|
||||||
|
var next = loc.nextLine(5);
|
||||||
|
assertEquals(next.line(), 15);
|
||||||
|
assertEquals(next.start(), 0);
|
||||||
|
assertEquals(loc.line(), 10);
|
||||||
|
assertEquals(loc.start(), 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldGetNextLocation() {
|
||||||
|
var loc = Location.of("test:10:5");
|
||||||
|
var next = loc.add(10);
|
||||||
|
assertEquals(next.filename(), new Filename("file", "test"));
|
||||||
|
assertEquals(next.line(), 9);
|
||||||
|
assertEquals(next.start(), 14);
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "test"));
|
||||||
|
assertEquals(loc.line(), 9);
|
||||||
|
assertEquals(loc.start(), 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testShouldParseLocation() {
|
||||||
|
var loc = Location.of("test.txt:10:5");
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "test.txt"));
|
||||||
|
assertEquals(loc.line(), 9);
|
||||||
|
assertEquals(loc.start(), 4);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseComplexFilenameLocation() {
|
||||||
|
var loc = Location.of("testificate://test.txt:10:5");
|
||||||
|
assertEquals(loc.filename(), new Filename("testificate", "test.txt"));
|
||||||
|
assertEquals(loc.line(), 9);
|
||||||
|
assertEquals(loc.start(), 4);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseNoFilenameLocation() {
|
||||||
|
var loc = Location.of("10:5");
|
||||||
|
assertEquals(loc.filename(), null);
|
||||||
|
assertEquals(loc.line(), 9);
|
||||||
|
assertEquals(loc.start(), 4);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseNoStartLocationA() {
|
||||||
|
var loc = Location.of("file://10:5");
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "10"));
|
||||||
|
assertEquals(loc.line(), 4);
|
||||||
|
assertEquals(loc.start(), -1);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseNoStartLocationB() {
|
||||||
|
var loc = Location.of("file:5");
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "file"));
|
||||||
|
assertEquals(loc.line(), 4);
|
||||||
|
assertEquals(loc.start(), -1);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseOnlyFilenameLocationA() {
|
||||||
|
var loc = Location.of("http://example.org/test.txt");
|
||||||
|
assertEquals(loc.filename(), new Filename("http", "example.org/test.txt"));
|
||||||
|
assertEquals(loc.line(), -1);
|
||||||
|
assertEquals(loc.start(), -1);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseOnlyFilenameLocationB() {
|
||||||
|
var loc = Location.of("test.txt");
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "test.txt"));
|
||||||
|
assertEquals(loc.line(), -1);
|
||||||
|
assertEquals(loc.start(), -1);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseOnlyFilenameWithColonLocation() {
|
||||||
|
var loc = Location.of("my-file:bad-file");
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "my-file:bad-file"));
|
||||||
|
assertEquals(loc.line(), -1);
|
||||||
|
assertEquals(loc.start(), -1);
|
||||||
|
}
|
||||||
|
@Test public void testShouldParseOnlyFilenameWithTripleColonLocation() {
|
||||||
|
var loc = Location.of("a:my-file:bad-file");
|
||||||
|
assertEquals(loc.filename(), new Filename("file", "a:my-file:bad-file"));
|
||||||
|
assertEquals(loc.line(), -1);
|
||||||
|
assertEquals(loc.start(), -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testCompareEqualLoc() {
|
||||||
|
var locA = Location.of("test:10:5");
|
||||||
|
var locB = Location.of("test:10:5");
|
||||||
|
|
||||||
|
assertEquals(locA.compareTo(locB), 0);
|
||||||
|
assertEquals(locB.compareTo(locA), 0);
|
||||||
|
}
|
||||||
|
@Test public void testCompareNoFileLoc() {
|
||||||
|
var locA = Location.of("10:5");
|
||||||
|
var locB = Location.of("11:5");
|
||||||
|
|
||||||
|
assertEquals(locA.compareTo(locB), -1);
|
||||||
|
assertEquals(locB.compareTo(locA), 1);
|
||||||
|
}
|
||||||
|
@Test public void testCompareOneNoFileLoc() {
|
||||||
|
var locA = Location.of("10:5");
|
||||||
|
var locB = Location.of("test:10:5");
|
||||||
|
|
||||||
|
assertEquals(locA.compareTo(locB), -1);
|
||||||
|
assertEquals(locB.compareTo(locA), 1);
|
||||||
|
}
|
||||||
|
@Test public void testCompareDiffFileLoc() {
|
||||||
|
var locA = Location.of("a:10:5");
|
||||||
|
var locB = Location.of("b:10:5");
|
||||||
|
|
||||||
|
assertEquals(locA.compareTo(locB), -1);
|
||||||
|
assertEquals(locB.compareTo(locA), 1);
|
||||||
|
}
|
||||||
|
@Test public void testCompareDiffLineLoc() {
|
||||||
|
var locA = Location.of("test:10:5");
|
||||||
|
var locB = Location.of("test:11:5");
|
||||||
|
|
||||||
|
assertEquals(locA.compareTo(locB), -1);
|
||||||
|
assertEquals(locB.compareTo(locA), 1);
|
||||||
|
}
|
||||||
|
@Test public void testCompareDiffStartLoc() {
|
||||||
|
var locA = Location.of("test:10:5");
|
||||||
|
var locB = Location.of("test:10:10");
|
||||||
|
|
||||||
|
assertEquals(locA.compareTo(locB), -1);
|
||||||
|
assertEquals(locB.compareTo(locA), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test public void testToStringAll() {
|
||||||
|
var locA = Location.of("test:10:5");
|
||||||
|
assertEquals(locA.toString(), "file://test:10:5");
|
||||||
|
}
|
||||||
|
@Test public void testToStringNoFilename() {
|
||||||
|
var locA = Location.of("10:5");
|
||||||
|
assertEquals(locA.toString(), "10:5");
|
||||||
|
}
|
||||||
|
@Test public void testToStringNoStart() {
|
||||||
|
var locA = Location.of("file:5");
|
||||||
|
assertEquals(locA.toString(), "file://file:5");
|
||||||
|
}
|
||||||
|
@Test public void testToStringNoLoc() {
|
||||||
|
var locA = Location.of("file");
|
||||||
|
assertEquals(locA.toString(), "file://file");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user