Skip to content

Commit

Permalink
More tests and fixed behaviour of for-range for negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
KaruroChori committed Feb 4, 2025
1 parent 4c587b9 commit 669fd6e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/vs-templ.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <variant>
Expand Down Expand Up @@ -393,7 +394,7 @@ void preprocessor::_parse(std::optional<pugi::xml_node_iterator> stop_at){
if(step>0 && to<from){/* Skip infinite loop*/}
else if(step<0 && to>from){/* Skip infinite loop*/}
else if(step==0){/* Skip potentially infinite loop*/}
else for(int i=from; i<to; i+=step){
else for(int i=from; ((step>0)?i<to:i>to); i+=step){
auto frame_guard = symbols.guard();
if(tag!=nullptr)symbols.set(tag,i);
symbols.set("$",i);
Expand Down
6 changes: 6 additions & 0 deletions test/cases/for-range.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<item>(<s:value src="{i}" />;<s:value src="{j}" />)</item>
</s:for-range>
</s:for-range>
<u>
<s:for-range tag="i" from="2" to="-2" step="-1">
<s:value src="{i}" />
</s:for-range>
</u>
</root>
</template>

Expand All @@ -25,6 +30,7 @@
<item>(7;9)</item>
<i>head</i>
<item>(9;9)</item>
<u>210-1</u>
</root>
</expects>
</test>
15 changes: 15 additions & 0 deletions test/marginal/eval-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,20 @@ int main(){
auto symbol = repl.eval("`#o` `#l` `#l` `#e` cat:*");
assert(symbol.has_value() && std::holds_alternative<std::string>(*symbol) && std::get<std::string>(*symbol) == "ello");
}
{
repl repl(pp);
auto symbol = repl.eval("`#o` `#l` `#l` `#e` `#.` join:*");
assert(symbol.has_value() && std::holds_alternative<std::string>(*symbol) && std::get<std::string>(*symbol) == "e.l.l.o");
}
{
repl repl(pp);
auto symbol = repl.eval("`1` `2` bg");
assert(symbol.has_value() && std::holds_alternative<int>(*symbol) && std::get<int>(*symbol) == true);
}
{
repl repl(pp);
auto symbol = repl.eval("`1` `2` lt");
assert(symbol.has_value() && std::holds_alternative<int>(*symbol) && std::get<int>(*symbol) == false);
}
return 0;
}

0 comments on commit 669fd6e

Please sign in to comment.