Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loop's statements copy #131

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/main/java/org/mozilla/zest/core/v1/ZestLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,15 @@ public void onControlNext(){
* @return the list of copied statements
*/
public List<ZestStatement> copyStatements() {
if (this.getStatements() != null) {
List<ZestStatement> statements = new LinkedList<>();
for (ZestStatement stmt : this.getStatements()) {
statements.add(stmt.deepCopy());
}
List<ZestStatement> copiedStatements = new LinkedList<>();
if (this.getStatements() == null) {
return copiedStatements;
}

for (ZestStatement stmt : getStatements()) {
copiedStatements.add(stmt.deepCopy());
}
return statements;
return copiedStatements;
}

/**
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/mozilla/zest/test/v1/ZestLoopUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.zest.test.v1;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;
import org.mozilla.zest.core.v1.ZestComment;
import org.mozilla.zest.core.v1.ZestLoop;
import org.mozilla.zest.core.v1.ZestLoopTokenSet;
import org.mozilla.zest.core.v1.ZestStatement;

/**
* Unit test for {@link ZestLoop}.
*/
public class ZestLoopUnitTest {

@Test
public void shouldReturnEmptyListIfNoStatementsWhenCopyingStatements() throws Exception {
// Given
ZestLoop<String> loop = new ZestLoopTest();
// When
List<ZestStatement> copiedStatements = loop.copyStatements();
// Then
assertTrue(copiedStatements.isEmpty());
}

@Test
public void shouldReturnADifferentListAndStatementsWhenCopyingStatements() throws Exception {
// Given
ZestLoop<String> loop = new ZestLoopTest();
loop.addStatement(new ZestComment());
loop.addStatement(new ZestComment());
// When
List<ZestStatement> copiedStatements = loop.copyStatements();
// Then
assertTrue(copiedStatements != loop.getStatements());
assertTrue(copiedStatements.size() == loop.getStatements().size());
assertTrue(copiedStatements.get(0) != loop.getStatements().get(0));
assertTrue(copiedStatements.get(1) != loop.getStatements().get(1));
}

private static class ZestLoopTest extends ZestLoop<String> {

@Override
public ZestLoopTokenSet<String> getSet() {
return null;
}

@Override
public ZestLoop<String> deepCopy() {
return null;
}

@Override
public boolean isLastState() {
return false;
}

@Override
protected void increase() {
}

@Override
public void toLastState() {
}

}

}