Skip to content

Commit

Permalink
Fix bug getting patched variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
bladecoder committed Jun 22, 2020
1 parent 9af5d21 commit d20ac2b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Matching Ink v0.9.0
version=0.7.3
version=0.7.4

1 change: 1 addition & 0 deletions src/main/java/com/bladecoder/ink/runtime/StoryState.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ void pushEvaluationStack(RTObject obj) {

if (rawList.getOrigins() == null)
rawList.setOrigins(new ArrayList<ListDefinition>());

rawList.getOrigins().clear();

for (String n : rawList.getOriginNames()) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/bladecoder/ink/runtime/VariablesState.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,24 @@ RTObject getRawVariableWithName(String name, int contextIndex) throws Exception
RTObject varValue = null;
// 0 context = global
if (contextIndex == 0 || contextIndex == -1) {
if (patch != null && patch.getGlobal(name) != null)
return patch.getGlobal(name);

varValue = globalVariables.get(name);
if (varValue != null) {
return varValue;
}

// Getting variables can actually happen during globals set up since you can do
// VAR x = A_LIST_ITEM
// So _defaultGlobalVariables may be null.
// We need to do this check though in case a new global is added, so we need to
// revert to the default globals dictionary since an initial value hasn't yet
// been set.
if (defaultGlobalVariables != null && defaultGlobalVariables.containsKey(name)) {
return defaultGlobalVariables.get(name);
}

ListValue listItemValue = listDefsOrigin.findSingleItemListWithName(name);
if (listItemValue != null)
return listItemValue;
Expand Down
31 changes: 26 additions & 5 deletions src/test/java/com/bladecoder/ink/runtime/test/ListSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public void testListSaveLoad() throws Exception {
story.choosePathString("elsewhere");
// FIXME: This is the test from the C# impl. Is it correct?
// Assert.assertEquals("a, x, c, z\n", story.continueMaximally());

Assert.assertEquals("z\n", story.continueMaximally());
}

/**
* "- TestEmptyListOriginAfterAssignment"
*/
Expand All @@ -92,14 +92,35 @@ public void testEmptyListOriginAfterAssignment() throws Exception {

Assert.assertEquals("a, b, c\n", story.continueMaximally());
}
//@Test

// @Test
public void testListRange() throws Exception {

String json = TestUtils.getJsonString("inkfiles/lists/list-range.ink.json");
Story story = new Story(json);

Assert.assertEquals("Pound, Pizza, Euro, Pasta, Dollar, Curry, Paella\nEuro, Pasta, Dollar, Curry\nTwo, Three, Four, Five, Six\nPizza, Pasta\n", story.continueMaximally());
Assert.assertEquals(
"Pound, Pizza, Euro, Pasta, Dollar, Curry, Paella\nEuro, Pasta, Dollar, Curry\nTwo, Three, Four, Five, Six\nPizza, Pasta\n",
story.continueMaximally());
}

@Test
public void testBugAddingElement() throws Exception {

String json = TestUtils.getJsonString("inkfiles/lists/bug-adding-element.ink.json");
Story story = new Story(json);

String s = story.continueMaximally();
Assert.assertEquals("", s);

story.chooseChoiceIndex(0);
s = story.continueMaximally();
Assert.assertEquals("a\n", s);

story.chooseChoiceIndex(1);
s = story.continueMaximally();
Assert.assertEquals("OK\n", s);

}

}
14 changes: 14 additions & 0 deletions src/test/resources/inkfiles/lists/bug-adding-element.ink
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LIST gameState = KNOW_ALIEN_REPORT

- (init)

+ a
~ gameState += KNOW_ALIEN_REPORT
-> init

+ {gameState ? KNOW_ALIEN_REPORT} OK
-> init

+ FAIL
-> END

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"inkVersion":19,"root":[[[["ev",{"^->":"0.init.0.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":2},{"s":["^a",{"->":"$r","var":true},null]}],["ev",{"^->":"0.init.1.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str",{"VAR?":"gameState"},{"VAR?":"KNOW_ALIEN_REPORT"},"?","/ev",{"*":".^.^.c-1","flg":3},{"s":["^OK",{"->":"$r","var":true},null]}],["ev",{"^->":"0.init.2.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-2","flg":2},{"s":["^FAIL",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"0.init.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.0.s"},[{"#n":"$r2"}],"\n","ev",{"VAR?":"gameState"},{"VAR?":"KNOW_ALIEN_REPORT"},"+",{"VAR=":"gameState","re":true},"/ev",{"->":".^.^"},{"->":"0.g-0"},{"#f":5}],"c-1":["ev",{"^->":"0.init.c-1.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.1.s"},[{"#n":"$r2"}],"\n",{"->":".^.^"},{"->":"0.g-0"},{"#f":5}],"c-2":["ev",{"^->":"0.init.c-2.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.2.s"},[{"#n":"$r2"}],"\n","end",{"->":"0.g-0"},{"#f":5}],"#f":5,"#n":"init"}],{"g-0":["done",{"#f":5}]}],"done",{"global decl":["ev",{"list":{},"origins":["gameState"]},{"VAR=":"gameState"},"/ev","end",null],"#f":1}],"listDefs":{"gameState":{"KNOW_ALIEN_REPORT":1}}}

0 comments on commit d20ac2b

Please sign in to comment.