Compare commits

..

No commits in common. "7a13b032f8be8e8957daca7acf06b17a5e27b2f5" and "c8a89849eeafc63a622b3d54efb2bd752c6f465e" have entirely different histories.

5 changed files with 13 additions and 302 deletions

View File

@ -63,12 +63,9 @@ public class Environment {
}
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;
}
else return get(key);
}
public <T> T initFrom(Key<T> key, Supplier<T> val) {
if (!has(key)) {
var res = val.get();

View File

@ -32,9 +32,9 @@ public abstract class Location implements Comparable<Location> {
};
}
public final Location nextLine() {
return nextLine(1);
return changeLine(1);
}
public final Location nextLine(int offset) {
public final Location changeLine(int offset) {
var self = this;
return new Location() {
@ -60,14 +60,7 @@ public abstract class Location implements Comparable<Location> {
}
@Override public int compareTo(Location other) {
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 a = filename().toString().compareTo(other.filename().toString());
int b = Integer.compare(line(), other.line());
int c = Integer.compare(start(), other.start());
@ -86,27 +79,21 @@ public abstract class Location implements Comparable<Location> {
}
public static Location of(String raw) {
var i1 = raw.lastIndexOf(':');
if (i1 < 0) return Location.of(Filename.parse(raw), -1, -1);
var i0 = raw.lastIndexOf(':');
if (i0 < 0) return Location.of(Filename.parse(raw), -1, -1);
var i0 = raw.substring(0, i1).lastIndexOf(':', i1);
var i1 = raw.lastIndexOf(':', i0);
if (i0 < 0) {
try {
return Location.of(null, Integer.parseInt(raw.substring(0, i1)) - 1, Integer.parseInt(raw.substring(i1 + 1)) - 1);
return Location.of(Filename.parse(raw.substring(0, i0)), Integer.parseInt(raw.substring(i0 + 1)), -1);
}
catch (NumberFormatException e) {}
try {
return Location.of(Filename.parse(raw.substring(0, i1)), Integer.parseInt(raw.substring(i1 + 1)) - 1, -1);
}
catch (NumberFormatException e) {}
catch (NumberFormatException e) {
return Location.of(Filename.parse(raw), -1, -1);
}
}
int start, line;
try {
start = Integer.parseInt(raw.substring(i1 + 1));
}
@ -118,9 +105,9 @@ public abstract class Location implements Comparable<Location> {
line = Integer.parseInt(raw.substring(i0 + 1, i1));
}
catch (NumberFormatException e) {
return Location.of(Filename.parse(raw.substring(0, i1)), start - 1, -1);
return Location.of(Filename.parse(raw.substring(i1 + 1)), start, -1);
}
return Location.of(Filename.parse(raw.substring(0, i0)), line - 1, start - 1);
return Location.of(Filename.parse(raw.substring(0, i0)), start, line);
}
}

View File

@ -1,72 +0,0 @@
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);
}
}

View File

@ -1,39 +0,0 @@
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);
}
}

View File

@ -1,162 +0,0 @@
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");
}
}