/* * Copyright (C) 2014-2017 the original authors or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.sarl.lang.tests.general.compilation.general; import com.google.inject.Inject; import org.eclipse.xtext.xbase.testing.CompilationTestHelper; import org.junit.Test; import io.sarl.lang.SARLVersion; import io.sarl.lang.sarl.SarlPackage; import io.sarl.tests.api.AbstractSarlTest; /** * @author $Author: srodriguez$ * @author $Author: sgalland$ * @version $Name$ $Revision$ $Date$ * @mavengroupid $GroupId$ * @mavenartifactid $ArtifactId$ */ @SuppressWarnings("all") public class SARLMapExtensionsCompilerTest extends AbstractSarlTest { @Inject private CompilationTestHelper compiler; @Test public void operator_addMapPair_0() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map : Map<String, Integer>", "var str = \"a\"", "var num = 4", "def myaction0 : Object {", " map += str -> num", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.Objects;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pair;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map;", " ", " private String str = \"a\";", " ", " private int num = 4;", " ", " protected Object myaction0() {", " Pair<String, Integer> _mappedTo = Pair.<String, Integer>of(this.str, Integer.valueOf(this.num));", " return this.map.put(_mappedTo.getKey(), _mappedTo.getValue());", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " if (this == obj)", " return true;", " if (obj == null)", " return false;", " if (getClass() != obj.getClass())", " return false;", " A1 other = (A1) obj;", " if (!Objects.equals(this.str, other.str)) {", " return false;", " }", " if (other.num != this.num)", " return false;", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " final int prime = 31;", " result = prime * result + Objects.hashCode(this.str);", " result = prime * result + this.num;", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_addMapPair_1() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map : Map<String, Integer>", "var str = \"a\"", "var num = 4", "def myaction1 : Object {", " var p = str -> num", " map += p", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.Objects;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pair;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map;", " ", " private String str = \"a\";", " ", " private int num = 4;", " ", " protected Object myaction1() {", " Integer _xblockexpression = null;", " {", " Pair<String, Integer> p = Pair.<String, Integer>of(this.str, Integer.valueOf(this.num));", " _xblockexpression = this.map.put(p.getKey(), p.getValue());", " }", " return _xblockexpression;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " if (this == obj)", " return true;", " if (obj == null)", " return false;", " if (getClass() != obj.getClass())", " return false;", " A1 other = (A1) obj;", " if (!Objects.equals(this.str, other.str)) {", " return false;", " }", " if (other.num != this.num)", " return false;", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " final int prime = 31;", " result = prime * result + Objects.hashCode(this.str);", " result = prime * result + this.num;", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_addMapMap_0() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map1 : Map<String, Number>", "var map2 : Map<String, Integer>", "def myaction0 {", " map1 += map2", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Number> map1;", " ", " private Map<String, Integer> map2;", " ", " protected void myaction0() {", " this.map1.putAll(this.map2);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_plusMapPair_0() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map : Map<String, Integer>", "var r : Map<String, Integer>", "var str = \"a\"", "var num = 4", "def myaction0 : Object {", " r = map + (str -> num)", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.Objects;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pair;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map;", " ", " private Map<String, Integer> r;", " ", " private String str = \"a\";", " ", " private int num = 4;", " ", " protected Object myaction0() {", " Pair<String, Integer> _mappedTo = Pair.<String, Integer>of(this.str, Integer.valueOf(this.num));", " Map<String, Integer> _plus = SARLMapExtensions.union(this.map, Collections.singletonMap(_mappedTo.getKey(), _mappedTo.getValue()));", " return this.r = _plus;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " if (this == obj)", " return true;", " if (obj == null)", " return false;", " if (getClass() != obj.getClass())", " return false;", " A1 other = (A1) obj;", " if (!Objects.equals(this.str, other.str)) {", " return false;", " }", " if (other.num != this.num)", " return false;", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " final int prime = 31;", " result = prime * result + Objects.hashCode(this.str);", " result = prime * result + this.num;", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_plusMapPair_1() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map : Map<String, Integer>", "var r : Map<String, Integer>", "var str = \"a\"", "var num = 4", "def myaction0 : Object {", " var p = str -> num", " r = map + p", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.Objects;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pair;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map;", " ", " private Map<String, Integer> r;", " ", " private String str = \"a\";", " ", " private int num = 4;", " ", " protected Object myaction0() {", " Map<String, Integer> _xblockexpression = null;", " {", " Pair<String, Integer> p = Pair.<String, Integer>of(this.str, Integer.valueOf(this.num));", " Map<String, Integer> _plus = SARLMapExtensions.union(this.map, Collections.singletonMap(p.getKey(), p.getValue()));", " _xblockexpression = this.r = _plus;", " }", " return _xblockexpression;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " if (this == obj)", " return true;", " if (obj == null)", " return false;", " if (getClass() != obj.getClass())", " return false;", " A1 other = (A1) obj;", " if (!Objects.equals(this.str, other.str)) {", " return false;", " }", " if (other.num != this.num)", " return false;", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " final int prime = 31;", " result = prime * result + Objects.hashCode(this.str);", " result = prime * result + this.num;", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_plusMapMap_0() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map1 : Map<String, Integer>", "var map2 : Map<String, Integer>", "var r : Map<String, Integer>", "def myaction0 : Object {", " r = map1 + map2", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map1;", " ", " private Map<String, Integer> map2;", " ", " private Map<String, Integer> r;", " ", " protected Object myaction0() {", " Map<String, Integer> _plus = SARLMapExtensions.union(this.map1, this.map2);", " return this.r = _plus;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_plusMapMap_1() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map1 : Map<String, Integer>", "var map2 : Map<String, Integer>", "var r : Map<String, Integer>", "def myaction0 : Object {", " r = map2 + map1", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map1;", " ", " private Map<String, Integer> map2;", " ", " private Map<String, Integer> r;", " ", " protected Object myaction0() {", " Map<String, Integer> _plus = SARLMapExtensions.union(this.map2, this.map1);", " return this.r = _plus;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_plusMapMap_2() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map1 : Map<String, Integer>", "var map2 : Map<String, Number>", "var r : Map<String, Number>", "def myaction0 : Object {", " r = map2 + map1", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map1;", " ", " private Map<String, Number> map2;", " ", " private Map<String, Number> r;", " ", " protected Object myaction0() {", " Map<String, Number> _plus = SARLMapExtensions.union(this.map2, this.map1);", " return this.r = _plus;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_removeMapK_0() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map : Map<String, Integer>", "var r : Map<String, Integer>", "var str = \"a\"", "var num = 4", "def myaction0 : Object {", " map -= str", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import java.util.Map;", "import java.util.Objects;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map;", " ", " private Map<String, Integer> r;", " ", " private String str = \"a\";", " ", " private int num = 4;", " ", " protected Object myaction0() {", " return this.map.remove(this.str);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " if (this == obj)", " return true;", " if (obj == null)", " return false;", " if (getClass() != obj.getClass())", " return false;", " A1 other = (A1) obj;", " if (!Objects.equals(this.str, other.str)) {", " return false;", " }", " if (other.num != this.num)", " return false;", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " final int prime = 31;", " result = prime * result + Objects.hashCode(this.str);", " result = prime * result + this.num;", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } @Test public void operator_minusMapK_0() throws Exception { String source = multilineString( "package io.sarl.lang.tests.ste", "import java.util.Map", "agent A1 {", "var map : Map<String, Integer>", "var r : Map<String, Integer>", "var str = \"a\"", "var num = 4", "def myaction0 : Object {", " r = map - str", "}", "}" ); String expected = multilineString( "package io.sarl.lang.tests.ste;", "", "import io.sarl.lang.annotation.SarlElementType;", "import io.sarl.lang.annotation.SarlSpecification;", "import io.sarl.lang.annotation.SyntheticMember;", "import io.sarl.lang.core.Agent;", "import io.sarl.lang.core.BuiltinCapacitiesProvider;", "import io.sarl.lang.scoping.batch.SARLMapExtensions;", "import java.util.Map;", "import java.util.Objects;", "import java.util.UUID;", "import javax.inject.Inject;", "import org.eclipse.xtext.xbase.lib.Pure;", "", "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", "@SarlElementType(" + SarlPackage.SARL_AGENT + ")", "@SuppressWarnings(\"all\")", "public class A1 extends Agent {", " private Map<String, Integer> map;", " ", " private Map<String, Integer> r;", " ", " private String str = \"a\";", " ", " private int num = 4;", " ", " protected Object myaction0() {", " Map<String, Integer> _minus = SARLMapExtensions.<String, Integer>operator_minus(this.map, this.str);", " return this.r = _minus;", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public boolean equals(final Object obj) {", " if (this == obj)", " return true;", " if (obj == null)", " return false;", " if (getClass() != obj.getClass())", " return false;", " A1 other = (A1) obj;", " if (!Objects.equals(this.str, other.str)) {", " return false;", " }", " if (other.num != this.num)", " return false;", " return super.equals(obj);", " }", " ", " @Override", " @Pure", " @SyntheticMember", " public int hashCode() {", " int result = super.hashCode();", " final int prime = 31;", " result = prime * result + Objects.hashCode(this.str);", " result = prime * result + this.num;", " return result;", " }", " ", " @SyntheticMember", " public A1(final UUID arg0, final UUID arg1) {", " super(arg0, arg1);", " }", " ", " @SyntheticMember", " @Inject", " public A1(final BuiltinCapacitiesProvider arg0, final UUID arg1, final UUID arg2) {", " super(arg0, arg1, arg2);", " }", "}", "" ); this.compiler.assertCompilesTo(source, expected); } }