Skip to content

Commit

Permalink
Turn on inheritance tests. Some whitespace issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Nov 28, 2023
1 parent 34ea07c commit a669942
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
58 changes: 47 additions & 11 deletions src/main/java/com/samskivert/mustache/Mustache.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,19 +431,29 @@ protected static Template.Segment[] trim (Template.Segment[] segs, boolean top)
boolean prevBlank = ((pseg == null && top) || (prev != null && prev.trailsBlank()));
boolean nextBlank = ((nseg == null && top) || (next != null && next.leadsBlank()));
// potentially trim around the open and close tags of a block segment
if (seg instanceof AbstractSectionSegment) {
AbstractSectionSegment block = (AbstractSectionSegment)seg;
if (prevBlank && block.firstLeadsBlank()) {
if (pseg != null) segs[ii-1] = prev.trimTrailBlank();
block.trimFirstBlank();
block._standaloneStart = true;
if (seg instanceof StandaloneSection) {
StandaloneSection sect = (StandaloneSection)seg;
String indent = "";
if (prevBlank && sect.firstLeadsBlank()) {
if (prev != null) {
// capture the indent before we trim
indent = prev.indent();
segs[ii-1] = prev.trimTrailBlank();
}
sect.trimFirstBlank();
sect.standaloneStart(true);
}
if (nextBlank && block.lastTrailsBlank()) {
block.trimLastBlank();
if (nseg != null) segs[ii+1] = next.trimLeadBlank();
block._standaloneEnd = true;
if (nextBlank && sect.lastTrailsBlank()) {
sect.trimLastBlank();
if (next != null) segs[ii+1] = next.trimLeadBlank();
sect.standaloneEnd(true);
}
if (sect instanceof ParentTemplateSegment && ! indent.equals("")) {
ParentTemplateSegment pt = (ParentTemplateSegment) sect;
segs[ii] = pt.indent(indent, pseg == null, nseg == null);
}
}

// we have to indent partials if there is space before
// they are also standalone...
else if (seg instanceof IncludedTemplateSegment) {
Expand Down Expand Up @@ -1211,12 +1221,32 @@ private ParentTemplateSegment (Compiler compiler, String name, Template.Segment[
blocks.put(bs._name, bs);
}
}
//return _comp.loadTemplate(_name).replaceBlocks(blocks).indent(_indent);
return super._loadTemplate().replaceBlocks(blocks);
}
@Override public boolean lastTrailsBlank () {
Template.Segment[] _segs = _segs();
int lastIdx = _segs.length-1;
//TODO this logic should probably for regular sections as well.
if (_segs.length == 0) {
return true;
}
if (!(_segs[lastIdx] instanceof StringSegment)) return false;
return ((StringSegment)_segs[lastIdx]).trailsBlank();
}
@Override public void trimLastBlank () {
Template.Segment[] _segs = _segs();
int idx = _segs.length-1;
//TODO this logic should probably for regular sections as well.
if (idx < 0) return;
_segs[idx] = ((StringSegment)_segs[idx]).trimTrailBlank();
}
@Override public Template.Segment[] _segs() { return _segs; }
@Override public boolean isStandalone() { return _standaloneEnd; }
@Override public boolean isStandaloneStart() { return _standaloneStart; }
@Override public boolean isStandaloneEnd() { return _standaloneEnd; }
@Override public void standaloneStart(boolean standaloneStart) { this._standaloneStart = standaloneStart; }
@Override public void standaloneEnd(boolean standaloneEnd) { this._standaloneEnd = standaloneEnd; }
@Override public String toString() {
return "Parent(name=" + _name + ", indent=" + _indent + ", standaloneStart=" + _standaloneStart
+ ")";
Expand Down Expand Up @@ -1285,10 +1315,11 @@ default void trimFirstBlank () {
Template.Segment[] _segs = _segs();
_segs[0] = ((StringSegment)_segs[0]).trimLeadBlank();
}

default boolean lastTrailsBlank () {
Template.Segment[] _segs = _segs();
int lastIdx = _segs.length-1;
//TODO this logic is probably wrong but for some reason passed spec before inheritance
// {{#stuff}}{{/stuff}} is standalone!
if (_segs.length == 0 || !(_segs[lastIdx] instanceof StringSegment)) return false;
return ((StringSegment)_segs[lastIdx]).trailsBlank();
}
Expand All @@ -1299,6 +1330,9 @@ default void trimLastBlank () {
}
boolean isStandaloneEnd();
boolean isStandaloneStart();
void standaloneStart(boolean standaloneStart);
void standaloneEnd(boolean standaloneEnd);

Template.Segment[] _segs();
}

Expand Down Expand Up @@ -1332,6 +1366,8 @@ protected void executeSegs (Template tmpl, Template.Context ctx, Writer out) {
@Override public boolean isStandalone() { return _standaloneEnd; }
@Override public boolean isStandaloneStart() { return _standaloneStart; }
@Override public boolean isStandaloneEnd() { return _standaloneEnd; }
@Override public void standaloneStart(boolean standaloneStart) { this._standaloneStart = standaloneStart; }
@Override public void standaloneEnd(boolean standaloneEnd) { this._standaloneEnd = standaloneEnd; }

@Override public Segment[] _segs() { return _segs; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class PartialThreadSafeTest {

@Test
public void testPartialThreadSafe() throws Exception {
long t = System.currentTimeMillis();
AtomicInteger loadCount = new AtomicInteger();
TemplateLoader loader = new TemplateLoader() {

Expand Down Expand Up @@ -61,9 +60,6 @@ public Reader getTemplate(String name) throws Exception {
}
assertTrue(q.isEmpty());
assertEquals(1, loadCount.get());
System.out.println(loadCount);
System.out.println(System.currentTimeMillis() - t);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static Collection<Object[]> data () {
"interpolation",
"inverted",
"sections",
"partials"
"partials",
"~inheritance"
};
return SpecTest.data("/specs/specs/", groups);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/samskivert/mustache/specs/Spec.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.samskivert.mustache.specs;

import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -57,6 +58,15 @@ public String toString() {
value.accept(getDescription());
label.accept("template");
value.accept(getTemplate());
if (! partials.isEmpty()) {
label.accept("partials");
sb.append("\n");
for( Entry<String, String> e : partials.entrySet()) {
sb.append("\t").append(e.getKey()).append(":\n");
value.accept(e.getValue());
}
sb.append("\n");
}
label.accept("expected");
value.accept(getExpectedOutput());
return sb.toString();
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/samskivert/mustache/specs/SpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void test () throws Exception {
// specifications, but we throw an exception (and rightfully so IMO; this is not a
// place where silent failure is helpful), so just ignore those test failures
if (!e.getMessage().contains("Invalid delimiter")) {
e.printStackTrace();
Assert.fail(
desc + "\nExpected: " + uncrlf(spec.getExpectedOutput()) + "\nError: " + e);
}
Expand Down

0 comments on commit a669942

Please sign in to comment.