Skip to content

Commit

Permalink
Add SourceLocation.fullyContainsRange(SourceLocation other) for parti…
Browse files Browse the repository at this point in the history
…al formatting.

GITHUB_BREAKING_CHANGES=NA

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=302085853
  • Loading branch information
Soy Authors authored and emspishak committed Mar 23, 2020
1 parent b67abee commit 5aef0db
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
15 changes: 15 additions & 0 deletions java/src/com/google/template/soy/base/SourceLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ public SourceLocation.Point getBeginPoint() {
return begin;
}

/** Whether this source location fully contains the given {@code range}, inclusively. */
public boolean fullyContainsRange(SourceLocation range) {
// If either location is unknown, return false.
if (!this.isKnown() || !range.isKnown()) {
return false;
}
// This source location should start before or at the same point as the given range.
boolean rangeStartsAfterOrAtBeginPoint = !this.begin.isAfter(range.begin);

// This source location should end after or at the same point as the given range.
boolean rangeEndsAfterOrAtEndPoint = !this.end.isBefore(range.end);

return rangeStartsAfterOrAtBeginPoint && rangeEndsAfterOrAtEndPoint;
}

/** Returns a new location that points to the last character of this location. */
public SourceLocation getEndLocation() {
return new SourceLocation(filePath, end, end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
public final class SourceLocationTest {

private static final Joiner JOINER = Joiner.on('\n');
private static final String FAKE_FILE_PATH = "fakefile.soy";

@Test
public void testLocationsInParsedContent() throws Exception {
Expand Down Expand Up @@ -833,6 +834,100 @@ public void testUnion() throws Exception {
assertThat(innerRange.unionWith(outerRange)).isEqualTo(outerRange);
}

@Test
public void testFullyContainsRange() throws Exception {
// One is a subset of another.
SourceLocation outerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));
SourceLocation innerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(2, 4), Point.create(5, 7));

assertThat(outerRange.fullyContainsRange(innerRange)).isTrue();
assertThat(innerRange.fullyContainsRange(outerRange)).isFalse();
}

@Test
public void testFullyContainsRange_sameStartPoint() throws Exception {
// One is a subset of another.
SourceLocation outerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));
SourceLocation innerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(5, 7));

assertThat(outerRange.fullyContainsRange(innerRange)).isTrue();
assertThat(innerRange.fullyContainsRange(outerRange)).isFalse();
}

@Test
public void testFullyContainsRange_sameEndPoint() throws Exception {
// One is a subset of another.
SourceLocation outerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));
SourceLocation innerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 5), Point.create(8, 7));

assertThat(outerRange.fullyContainsRange(innerRange)).isTrue();
assertThat(innerRange.fullyContainsRange(outerRange)).isFalse();
}

@Test
public void testFullyContainsRange_sameRange() throws Exception {
// One is a subset of another.
SourceLocation outerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));
SourceLocation innerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));

assertThat(outerRange.fullyContainsRange(innerRange)).isTrue();
assertThat(innerRange.fullyContainsRange(outerRange)).isTrue();
}

@Test
public void testFullyContainsRange_failsIfEndsAfter() throws Exception {
// One is not a subset of another.
SourceLocation outerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));
SourceLocation innerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 5), Point.create(10, 7));

assertThat(outerRange.fullyContainsRange(innerRange)).isFalse();
assertThat(innerRange.fullyContainsRange(outerRange)).isFalse();
}

@Test
public void testFullyContainsRange_failsIfBeginsBefore() throws Exception {
// One is a not subset of another.
SourceLocation outerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(8, 7));
SourceLocation innerRange =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 1), Point.create(6, 7));

assertThat(outerRange.fullyContainsRange(innerRange)).isFalse();
assertThat(innerRange.fullyContainsRange(outerRange)).isFalse();
}

@Test
public void testFullyContainsRange_failsIfBothUnknown() throws Exception {
SourceLocation range1 =
new SourceLocation(FAKE_FILE_PATH, Point.UNKNOWN_POINT, Point.UNKNOWN_POINT);
SourceLocation range2 =
new SourceLocation(FAKE_FILE_PATH, Point.UNKNOWN_POINT, Point.UNKNOWN_POINT);

assertThat(range1.fullyContainsRange(range2)).isFalse();
assertThat(range2.fullyContainsRange(range1)).isFalse();
}

@Test
public void testFullyContainsRange_failsIfOneUnknown() throws Exception {
SourceLocation range1 =
new SourceLocation(FAKE_FILE_PATH, Point.create(1, 3), Point.create(5, 8));
SourceLocation range2 =
new SourceLocation(FAKE_FILE_PATH, Point.UNKNOWN_POINT, Point.UNKNOWN_POINT);

assertThat(range1.fullyContainsRange(range2)).isFalse();
assertThat(range2.fullyContainsRange(range1)).isFalse();
}

@Test
public void testRawTextSourceLocations() throws Exception {
// RawTextNode has some special methods to calculating the source location of characters within
Expand Down

0 comments on commit 5aef0db

Please sign in to comment.