Skip to content

Commit

Permalink
Add some warnings test
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Nov 18, 2024
1 parent 9f33d74 commit 92722b8
Showing 1 changed file with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.oracle.truffle.api.interop.InteropLibrary;
import java.util.List;
import org.enso.common.LanguageInfo;
import org.enso.common.MethodNames;
import org.enso.interpreter.node.expression.builtin.interop.syntax.HostValueToEnsoNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.hash.EnsoHashMap;
import org.enso.interpreter.runtime.data.hash.HashMapGetNode;
import org.enso.interpreter.runtime.data.hash.HashMapInsertNode;
import org.enso.interpreter.runtime.data.hash.HashMapSizeNode;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.data.vector.ArrayLikeHelpers;
import org.enso.interpreter.runtime.warning.AppendWarningNode;
import org.enso.interpreter.runtime.warning.Warning;
import org.enso.interpreter.runtime.warning.WarningsLibrary;
import org.enso.interpreter.runtime.warning.WithWarnings;
import org.enso.test.utils.ContextUtils;
import org.graalvm.polyglot.Context;
Expand Down Expand Up @@ -194,4 +206,92 @@ public void warningOnAnError() throws Exception {
assertEquals(
"Standard.Base.Error.Error", errorWithWarning.getMetaObject().getMetaQualifiedName());
}

@Test
public void warningsArray_readViaInterop_shouldNotRemoveWarnings() {
ContextUtils.executeInContext(
ctx,
() -> {
var warn1 = Warning.create(ensoContext, 1L, null);
var warn2 = Warning.create(ensoContext, 2L, null);
var arr = ArrayLikeHelpers.wrapEnsoObjects(warn1, warn2);
var interop = InteropLibrary.getUncached();
var warn1FromArr = interop.readArrayElement(arr, 0);
assertThat(
"warn1 and warn1FromArr should be the same reference",
warn1,
is(sameInstance(warn1FromArr)));
var warn2FromArr = interop.readArrayElement(arr, 1);
assertThat(
"warn2 and warn2FromArr should be the same reference",
warn2,
is(sameInstance(warn2FromArr)));
return null;
});
}

@Test
public void warningsArray_collectWarningsViaWarningsLibrary() {
ContextUtils.executeInContext(
ctx,
() -> {
var appendWarnNode = AppendWarningNode.getUncached();
var warnsLib = WarningsLibrary.getUncached();
var hashMapSizeNode = HashMapSizeNode.getUncached();
var hashMapGetNode = HashMapGetNode.getUncached();

var warn1 = Warning.create(ensoContext, 1L, null);
var warn2 = Warning.create(ensoContext, 2L, null);
var warnsMap = createWarningsMap(List.of(warn1, warn2));
var text1 = Text.create("1");
var text2 = Text.create("2");
var arr = ArrayLikeHelpers.wrapEnsoObjects(text1, text2);
var arrWithWarns = appendWarnNode.executeAppend(null, arr, warnsMap);
assertThat(warnsLib.hasWarnings(arrWithWarns), is(true));
var gatheredWarns = warnsLib.getWarnings(arrWithWarns, false);
assertThat("Hash size should be 2", hashMapSizeNode.execute(gatheredWarns), is(2L));
var warn1FromMap =
hashMapGetNode.execute(null, null, gatheredWarns, warn1.getSequenceId(), null);
assertThat(
"Original warning and warning gathered via WarningsLibrary should be the same object",
warn1 == warn1FromMap,
is(true));
return null;
});
}

@Test
public void nothingWithWarn_IsNotRemovedByHostValueToEnsoNode() {
ContextUtils.executeInContext(
ctx,
() -> {
var hostValueToEnsoNode = HostValueToEnsoNode.getUncached();
var warn = Warning.create(ensoContext, ensoContext.getNothing(), null);
var converted = hostValueToEnsoNode.execute(warn);
assertThat(converted, is(sameInstance(warn)));
return null;
});
}

@Test
public void nothingWithWarn_FromMapToArray() {
ContextUtils.executeInContext(
ctx,
() -> {
var warn = Warning.create(ensoContext, ensoContext.getNothing(), null);
var warnsMap = createWarningsMap(List.of(warn));
var warns = Warning.fromMapToArray(warnsMap);
assertThat(warns.length, is(1));
return null;
});
}

private EnsoHashMap createWarningsMap(List<Warning> warns) {
var map = EnsoHashMap.empty();
var mapInsertNode = HashMapInsertNode.getUncached();
for (var warn : warns) {
map = mapInsertNode.execute(null, map, warn.getSequenceId(), warn);
}
return map;
}
}

0 comments on commit 92722b8

Please sign in to comment.