Skip to content

Commit

Permalink
fix(for_block): make ranges inclusive.
Browse files Browse the repository at this point in the history
For loops with ranges should include the last value.
  • Loading branch information
Goncalerta committed Oct 16, 2018
1 parent 620e92a commit 42055c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
40 changes: 23 additions & 17 deletions src/tags/for_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Range {
Range::Counted(ref start_arg, ref stop_arg) => {
let start = int_argument(start_arg, context, "start")?;
let stop = int_argument(stop_arg, context, "end")?;
let range = start..stop;
let range = start..=stop;
range.map(|x| Value::scalar(x as i32)).collect()
}
};
Expand Down Expand Up @@ -369,7 +369,7 @@ mod test {
let output = for_tag.render(&mut data).unwrap();
assert_eq!(
output,
"#1 test 42 | #2 test 43 | #3 test 44 | #4 test 45 | "
"#1 test 42 | #2 test 43 | #3 test 44 | #4 test 45 | #5 test 46 | "
);
}

Expand All @@ -393,7 +393,10 @@ mod test {
.stack_mut()
.set_global("omega", Value::scalar(46i32));
let output = template.render(&mut context).unwrap();
assert_eq!(output, "#1 test 42, #2 test 43, #3 test 44, #4 test 45, ");
assert_eq!(
output,
"#1 test 42, #2 test 43, #3 test 44, #4 test 45, #5 test 46, "
);
}

#[test]
Expand All @@ -420,10 +423,11 @@ mod test {
assert_eq!(
output,
concat!(
">>0:1>>1:0:6,1:1:7,1:2:8,1:3:9,>>1>>\n",
">>1:2>>2:0:6,2:1:7,2:2:8,2:3:9,>>2>>\n",
">>2:3>>3:0:6,3:1:7,3:2:8,3:3:9,>>3>>\n",
">>3:4>>4:0:6,4:1:7,4:2:8,4:3:9,>>4>>\n"
">>0:1>>1:0:6,1:1:7,1:2:8,1:3:9,1:4:10,>>1>>\n",
">>1:2>>2:0:6,2:1:7,2:2:8,2:3:9,2:4:10,>>2>>\n",
">>2:3>>3:0:6,3:1:7,3:2:8,3:3:9,3:4:10,>>3>>\n",
">>3:4>>4:0:6,4:1:7,4:2:8,4:3:9,4:4:10,>>4>>\n",
">>4:5>>5:0:6,5:1:7,5:2:8,5:3:9,5:4:10,>>5>>\n",
)
);
}
Expand All @@ -432,8 +436,8 @@ mod test {
fn nested_forloops_with_else() {
// test that nested for loops parse their `else` blocks correctly
let text = concat!(
"{% for x in (0..i) %}",
"{% for y in (0..j) %}inner{% else %}empty inner{% endfor %}",
"{% for x in i %}",
"{% for y in j %}inner{% else %}empty inner{% endfor %}",
"{% else %}",
"empty outer",
"{% endfor %}"
Expand All @@ -444,13 +448,15 @@ mod test {
.unwrap();

let mut context = Context::new();
context.stack_mut().set_global("i", Value::scalar(0i32));
context.stack_mut().set_global("j", Value::scalar(0i32));
context.stack_mut().set_global("i", Value::Array(vec![]));
context.stack_mut().set_global("j", Value::Array(vec![]));
let output = template.render(&mut context).unwrap();
assert_eq!(output, "empty outer");

context.stack_mut().set_global("i", Value::scalar(1i32));
context.stack_mut().set_global("j", Value::scalar(0i32));
context
.stack_mut()
.set_global("i", Value::Array(vec![Value::scalar(1i32)]));
context.stack_mut().set_global("j", Value::Array(vec![]));
let output = template.render(&mut context).unwrap();
assert_eq!(output, "empty inner");
}
Expand Down Expand Up @@ -501,7 +507,7 @@ mod test {

let mut context = Context::new();
let output = template.render(&mut context).unwrap();
assert_eq!(output, "5 6 7 8 9 ");
assert_eq!(output, "5 6 7 8 9 10 ");
}

#[test]
Expand Down Expand Up @@ -535,7 +541,7 @@ mod test {

let mut context = Context::new();
let output = template.render(&mut context).unwrap();
assert_eq!(output, "9 8 7 6 5 4 3 2 1 ");
assert_eq!(output, "10 9 8 7 6 5 4 3 2 1 ");
}

#[test]
Expand Down Expand Up @@ -584,7 +590,7 @@ mod test {

let mut context = Context::new();
let output = template.render(&mut context).unwrap();
assert_eq!(output, "1 2 3 4 ");
assert_eq!(output, "1 2 3 4 5 ");
}

#[test]
Expand All @@ -597,7 +603,7 @@ mod test {
Token::OpenRound,
Token::IntegerLiteral(100i32),
Token::DotDot,
Token::IntegerLiteral(103i32),
Token::IntegerLiteral(102i32),
Token::CloseRound,
],
&compiler::tokenize(concat!(
Expand Down
13 changes: 8 additions & 5 deletions src/tags/interrupt_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ mod test {
concat!(
"enter-0; 6, 7, break, exit-0\n",
"enter-1; 6, 7, break, exit-1\n",
"enter-2; 6, 7, break, exit-2\n"
"enter-2; 6, 7, break, exit-2\n",
"enter-3; 6, 7, break, exit-3\n",
)
);
}
Expand Down Expand Up @@ -151,7 +152,8 @@ mod test {
"enter-1;exit-1\n",
"enter-2;continue-2\n",
"enter-3;exit-3\n",
"enter-4;exit-4\n"
"enter-4;exit-4\n",
"enter-5;exit-5\n",
)
);
}
Expand Down Expand Up @@ -179,9 +181,10 @@ mod test {
assert_eq!(
output,
concat!(
"enter-0; 6, 7, continue, 9, exit-0\n",
"enter-1; 6, 7, continue, 9, exit-1\n",
"enter-2; 6, 7, continue, 9, exit-2\n"
"enter-0; 6, 7, continue, 9, 10, exit-0\n",
"enter-1; 6, 7, continue, 9, 10, exit-1\n",
"enter-2; 6, 7, continue, 9, 10, exit-2\n",
"enter-3; 6, 7, continue, 9, 10, exit-3\n",
)
);
}
Expand Down

0 comments on commit 42055c3

Please sign in to comment.