Skip to content

Commit

Permalink
Improved support for item selector expressions (fix for #124)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolovos committed Oct 6, 2024
1 parent 7783fc8 commit 79d0e2e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
**********************************************************************/
package org.eclipse.epsilon.eol.dom;

import java.util.List;
import java.util.Map;

import org.eclipse.epsilon.common.module.IModule;
import org.eclipse.epsilon.common.parse.AST;
import org.eclipse.epsilon.eol.exceptions.EolRuntimeException;
Expand Down Expand Up @@ -82,6 +85,25 @@ public Object execute(IEolContext context) throws EolRuntimeException {
throw eox;
}
}
else if (targetExpression instanceof ItemSelectorExpression) {
ItemSelectorExpression ise = (ItemSelectorExpression) targetExpression;
Object source = executorFactory.execute(ise.getTargetExpression(), context);
Object index = executorFactory.execute(ise.getIndexExpression(), context);
valueExpressionResult = executorFactory.execute(valueExpression, context);
Object value = getValueEquivalent(source, valueExpressionResult, context);

if (source instanceof List) {
if (!(index instanceof Integer))
throw new EolRuntimeException("Collection index must be an integer but " + index + " was provided instead.", ise.getIndexExpression());
else ((List) source).set((Integer) index, value);
}
else if (source instanceof Map) {
((Map) source).put(index, value);
}
else {
throw new EolRuntimeException(source + " is not a list or a map.", ise.getTargetExpression());
}
}
else {
Object targetExpressionResult;
if (targetExpression instanceof NameExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.eclipse.epsilon.eol.dom;

import java.util.Collection;
import java.util.Map;

import org.eclipse.epsilon.common.module.IModule;
import org.eclipse.epsilon.common.parse.AST;
Expand Down Expand Up @@ -40,9 +41,9 @@ public Object execute(IEolContext context) throws EolRuntimeException {
throw new EolRuntimeException("Collection index must be an integer but " + index + " was provided instead.", indexExpression);
else return CollectionUtil.asList(expression).get((Integer)index);
}
//else if (expression instanceof EolMap){
// return ((EolMap) expression).get(index);
//}
else if (expression instanceof Map){
return ((Map) expression).get(index);
}

throw new EolRuntimeException(expression + " is not a collection or a map.", targetExpression);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
BreakAndContinueTests.class,
EnumResolutionTests.class,
CircularImportTests.class,
ImportCachingTests.class
ImportCachingTests.class,
ItemSelectorTests.class
})
public class EolAcceptanceTestSuite {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@test
operation testExistingIndexInSet() {
assertEquals(1, Set{1, 2}[0]);
}

@test
operation testExistingIndexInSequence() {
assertEquals(1, Sequence{1, 2}[0]);
}

@test
operation testIndexOutOfBoundsInCollection() {
assertError(Sequence{1, 2}[2]);
}

@test
operation testExistingKeyInMap() {
assertEquals(1, Map{"a" = 1}["a"]);
}

@test
operation testExistingKeyInNativeMap() {
assertEquals(1, createNativeMap()["a"]);
}

@test
operation testUnknownKeyInMap() {
assertEquals(null, Map{"a" = 1}["b"]);
}

@test
operation testAssignToListIndex() {
var s = Sequence{1, 2};
s[0] = 3;
assertEquals(3, s[0]);
}

@test
operation testAssignToExistingMapKey() {
var m = Map{"a" = 1};
m["a"] = 2;
assertEquals(2, m["a"]);
}

@test
operation testAssignToExistingNativeMapKey() {
var m = createNativeMap();
m["a"] = 2;
assertEquals(2, m["a"]);
}

@test
operation testAssignToNewMapKey() {
var m = Map{"a" = 1};
m["b"] = 2;
assertEquals(2, m["b"]);
}

@test
operation testAssignToSetIndex() {
assertError(assignToSetIndex());
}

operation assignToSetIndex() {
var s = Set{1, 2};
// This is not allowed as Set is not a java.util.List
s[0] = 3;
}

operation createNativeMap() {
var m = new Native("java.util.HashMap");
m.put("a", 1);
return m;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2008 The University of York.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* Dimitrios Kolovos - initial API and implementation
******************************************************************************/
package org.eclipse.epsilon.eol.engine.test.acceptance;

import org.eclipse.epsilon.eol.engine.test.acceptance.eunit.EUnitRunner;
import org.junit.runner.RunWith;

@RunWith(EUnitRunner.class)
public class ItemSelectorTests {

}

0 comments on commit 79d0e2e

Please sign in to comment.