Skip to content

Commit

Permalink
Merge pull request jtwig#241 from lyncodev/fix-range-chars
Browse files Browse the repository at this point in the history
Fix range chars
  • Loading branch information
jmelo-lyncode committed Nov 29, 2014
2 parents 77caeb8 + 972b02a commit 6e8bcd4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ public void rangeStepTest() throws Exception {
when(jtwigRenders(template("{{ range(1, 3, 2) }}")));
then(theRenderedTemplate(), is(equalTo("[1, 3]")));
}
}
@Test
public void rangeCharTest() throws Exception {
when(jtwigRenders(template("{{ range('a', 'c') }}")));
then(theRenderedTemplate(), is(equalTo("[a, b, c]")));
}
@Test
public void rangeStringTest() throws Exception {
when(jtwigRenders(template("{{ range(\"AA\", \"BZ\") }}")));
then(theRenderedTemplate(), is(equalTo("[A, B]")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,50 @@ public String numberFormat (@Parameter Object number) {
}

@JtwigFunction(name = "range")
public List<Integer> range (@Parameter int start, @Parameter int end, @Parameter int step) throws FunctionException {
List<Integer> result = new ArrayList<>();

if (start == end) {
result.add(start);
return result;
}

public <T> List<T> range (@Parameter T start, @Parameter T end, @Parameter int step) throws FunctionException {
step = Math.abs(step);
if (step == 0)
throw new FunctionException("Step must not be 0");

if (start > end) {
// negate step for reversed mode, if positive input
if (step > 0) step = -step;
}
if (Math.abs(step) > (Math.abs(start - end))) {
throw new FunctionException("Step is too big");
// Determine the start value
int startInt;
int endInt;
if(start instanceof Number) {
startInt = ((Number)start).intValue();
endInt = ((Number)end).intValue();
} else if(start instanceof Character) {
startInt = ((Character)start).charValue();
endInt = ((Character)end).charValue();
} else if(start instanceof CharSequence) {
startInt = ((CharSequence)start).charAt(0);
endInt = ((CharSequence)end).charAt(0);
} else {
throw new IllegalArgumentException("range() function requires Number, Character, or CharSequence limits.");
}

// Handle negative progressions
if (startInt > endInt) {
step = -step;
}

for (int i = start; (step > 0) ? i <= end : i >= end; i += step) {
result.add(i);
// Build the list and convert if necessary
List<T> results = new ArrayList<>();
for (int i = startInt; (step > 0) ? i <= endInt : i >= endInt; i += step) {
T result = null;
if(start instanceof Number) {
result = (T)start.getClass().cast(i);
} else if(start instanceof Character) {
result = (T)Character.valueOf((char)i);
} else if(start instanceof CharSequence) {
result = (T)String.valueOf((char)i);
}
results.add(result);
}

return result;
return results;
}
@JtwigFunction(name = "range")
public List<Integer> range (@Parameter int start, @Parameter int end) throws FunctionException {
public <T> List<T> range (@Parameter T start, @Parameter T end) throws FunctionException {
return range(start, end, 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ public void rangeDefault() throws FunctionException {
assertThat(list, contains(1, 2, 3));
}
@Test
public void rangeSameLimits() throws FunctionException {
List<Integer> list = underTest.range(1,1,5);
assertThat(list, contains(1));
}
@Test
public void rangeBackward() throws FunctionException {
List<Integer> list = underTest.range(3, 1);
assertThat(list, contains(3, 2, 1));
public void rangeChars() throws FunctionException {
List<Character> list = underTest.range('A', 'D');
assertThat(list, contains('A', 'B', 'C', 'D'));
}

@Test(expected = FunctionException.class)
public void rangeInvalidStep() throws FunctionException {
underTest.range(1, 3, 0);
}

@Test(expected = FunctionException.class)
public void rangeTooBigStep() throws FunctionException {
underTest.range(3, 1, 5);
@Test
public void rangeHugeStep() throws FunctionException {
List<Integer> list = underTest.range(1, 3, 50);
assertThat(list, contains(1));
}
@Test
public void rangeNegativeStepWithPositiveProgression() throws FunctionException {
List<Integer> list = underTest.range(1, 3, -1);
assertThat(list, contains(1, 2, 3));
}
}
}

0 comments on commit 6e8bcd4

Please sign in to comment.