Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jan 29, 2019
1 parent 14dfc06 commit adee5b1
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 75 deletions.
22 changes: 11 additions & 11 deletions src/taglibs/migrate/for-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ function migrateForLoop(elNode, context) {
varNamePrefix = parsed.statusVarName.name;
replaceStatusVarInBody(elNode, context, parsed);
}
}

if (parsed.separator) {
needsParams = true;
elNode.appendChild(
builder.htmlElement(
"if",
undefined,
[builder.text(parsed.separator, false, true)],
`${varNamePrefix}Index !== ${varNamePrefix}All.length - 1`
)
);
}
if (parsed.separator) {
needsParams = true;
elNode.appendChild(
builder.htmlElement(
"if",
undefined,
[builder.text(parsed.separator, false, true)],
`${varNamePrefix}Index !== ${varNamePrefix}All.length - 1`
)
);
}

if (needsParams) {
Expand Down
118 changes: 86 additions & 32 deletions test/migrate/fixtures/legacy-for-syntax/snapshot-expected.marko
Original file line number Diff line number Diff line change
@@ -1,21 +1,105 @@
<!-- test/migrate/fixtures/legacy-for-syntax/template.marko -->

<!-- Object -->
<!-- Array -->
<for|color| of=colors>
<li>${color}</li>
</for>
<!-- Array | iterator -->
$ var iteratorColors = [];
$ input.iterator(colors, function(item) {
iteratorColors.push(item);
});
<for|color| of=iteratorColors>
<li>${color}</li>
</for>
<!-- Array | separator -->
<for|color, loopIndex, loopAll| of=colors>
<li>${color}</li>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- Array | status-var -->
<for|color, loopIndex, loopAll| of=colors>
${color}${loopIndex + 1}) of ${loopAll.length}<if(loopIndex === 0)>
- FIRST
</if>
<if(loopIndex === loopAll.length - 1)>- LAST</if>
</for>
<!-- Array | iterator separator -->
$ var iteratorColors = [];
$ input.iterator(colors, function(item) {
iteratorColors.push(item);
});
<for|color, loopIndex, loopAll| of=iteratorColors>
<li>${color}</li>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- Array | status-var iterator -->
<for|color, loop| of=input.iterator.bind(null, colors)>
${color}${loop.getIndex() + 1}) of ${loop.getLength()}<if(loop.isFirst())>
- FIRST
</if>
<if(loop.isLast())>- LAST</if>
</for>
<!-- Array | status-var separator -->
<for|color, loopIndex, loopAll| of=colors>
${color}${loopIndex + 1}) of ${loopAll.length}<if(loopIndex === 0)>
- FIRST
</if>
<if(loopIndex === loopAll.length - 1)>- LAST</if>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- Array | status-var iterator separator -->
<for|color, loop, loopIndex, loopAll| of=input.iterator.bind(null, colors)>
${color}${loop.getIndex() + 1}) of ${loop.getLength()}<if(loop.isFirst())>
- FIRST
</if>
<if(loop.isLast())>- LAST</if>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- Properties -->
<for|color, code| in=colors>
<li>${color}: #${code}</li>
</for>
<!-- Properties | separator -->
<for|color, loopIndex, loopAll| of=Object.keys(colors)>
$ var code = colors[color];
<li>${color}: #${code}</li>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- Properties | status-var -->
<for|color, loopIndex, loopAll| of=Object.keys(colors)>
$ var code = colors[color];
<li>${color}: #${code}</li>
${loopIndex + 1}
) of
${loopAll.length}
<if(loopIndex === 0)>- FIRST</if>
<if(loopIndex === loopAll.length - 1)>- LAST</if>
</for>
<!-- Properties | status-var separator -->
<for|color, loopIndex, loopAll| of=Object.keys(colors)>
$ var code = colors[color];
<li>${color}: #${code}</li>
${loopIndex + 1}
) of
${loopAll.length}
<if(loopIndex === 0)>- FIRST</if>
<if(loopIndex === loopAll.length - 1)>- LAST</if>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- Range -->
<for|i| from=0 to=10>
<li>${i}</li>
</for>
<!-- Regular -->
<for|i| from=0 to=(list.length - 1)>${i}</for>
<for|i| from=0 to=listSize step=2>${i}</for>
<!-- Stange: backwards -->
$ var i = 0;
<while(list.length >= i)>
${i}
$ i++;
</while>
<for|i| from=0 to=listSize step=2>${i}</for>
$ var i = list.length;
<while(i >= 0)>
${i}
Expand Down Expand Up @@ -55,33 +139,3 @@ $ var i = 0;
</while>
<!-- Strange: missing parts -->
<while(i--)>${i}</while>
<!-- modifier: status-var -->
<for|color, loopIndex, loopAll| of=colors>
<li>
${color}${loopIndex + 1}) of ${loopAll.length}<if(loopIndex === 0)>
- FIRST
</if>
<if(loopIndex === loopAll.length - 1)>- LAST</if>
</li>
</for>
<for|color, loopIndex, loopAll| of=colors>
<li>
${color}${loopIndex + 1}) of ${loopAll.length}<if(loopIndex === 0)>
- FIRST
</if>
<if(loopIndex === loopAll.length - 1)>- LAST</if>
</li>
</for>
<!-- modifier: separator -->
<for|color, loopIndex, loopAll| of=colors>
<li>${color}</li>
<if(loopIndex !== loopAll.length - 1)>,</if>
</for>
<!-- modifier: iterator -->
$ var reverseIteratorColors = [];
$ reverseIterator(colors, function(item) {
reverseIteratorColors.push(item);
});
<for|color| of=reverseIteratorColors>
<li>${color}</li>
</for>
110 changes: 78 additions & 32 deletions test/migrate/fixtures/legacy-for-syntax/template.marko
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
<!-- Object -->
<!-- Array -->
<for(color in colors)>
<li>${color}</li>
</for>

<!-- Array | iterator -->
<for(color in colors | iterator=input.iterator)>
<li>${color}</li>
</for>

<!-- Array | separator -->
<for(color in colors | separator=", ")>
<li>${color}</li>
</for>

<!-- Array | status-var -->
<for(color in colors | status-var=loop)>
${color}
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</for>

<!-- Array | iterator separator -->
<for(color in colors | iterator=input.iterator separator=", ")>
<li>${color}</li>
</for>

<!-- Array | status-var iterator -->
<for(color in colors | status-var=loop iterator=input.iterator)>
${color}
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</for>

<!-- Array | status-var separator -->
<for(color in colors | status-var=loop separator=", ")>
${color}
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</for>

<!-- Array | status-var iterator separator -->
<for(color in colors | status-var=loop iterator=input.iterator separator=", ")>
${color}
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</for>


<!-- Properties -->
<for(color, code in colors)>
<li>${color}: #${code}</li>
</for>

<!-- Properties | separator -->
<for(color, code in colors | separator=", ")>
<li>${color}: #${code}</li>
</for>

<!-- Properties | status-var -->
<for(color, code in colors | status-var=loop)>
<li>${color}: #${code}</li>
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</for>

<!-- Properties | status-var separator -->
<for(color, code in colors | status-var=loop separator=", ")>
<li>${color}: #${code}</li>
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</for>

<!-- Range -->
<for(i from 0 to 10)>
<li>${i}</li>
Expand All @@ -13,11 +87,12 @@
${i}
</for>

<for(var i = 0; list.length >= i; i++)>
<for(var i = 0; i <= listSize; i += 2)>
${i}
</for>

<for(var i = 0; i <= listSize; i += 2)>
<!-- Stange: backwards -->
<for(var i = 0; list.length >= i; i++)>
${i}
</for>

Expand Down Expand Up @@ -53,32 +128,3 @@
<for(;i--;)>
${i}
</for>

<!-- modifier: status-var -->
<for(color in colors | status-var=loop)>
<li>
${color}
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</li>
</for>

<for(color in colors | status-var=loop)>
<li>
${color}
${loop.getIndex()+1}) of ${loop.getLength()}
<if(loop.isFirst())> - FIRST</if>
<if(loop.isLast())> - LAST</if>
</li>
</for>

<!-- modifier: separator -->
<for(color in colors | separator=", ")>
<li>${color}</li>
</for>

<!-- modifier: iterator -->
<for(color in colors | iterator=reverseIterator)>
<li>${color}</li>
</for>

0 comments on commit adee5b1

Please sign in to comment.