Skip to content

Commit

Permalink
Various test improvements to increase coverage and speed things up (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach authored May 25, 2023
1 parent 3268139 commit b33ae47
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Interval
Arguments:
- start: The start of the interval.
- end: The end of the interval.
Between (start : Bound.Bound) (end : Bound.Bound)
Between (start : Bound) (end : Bound)

## Checks if the interval contains `that`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ type Match_Iterator
## PRIVATE
type Match_Iterator_Value
## PRIVATE
Next (filler : Span) (match : Match) (next_iterator : Match_Iterator)
Next (filler : Utf_16_Span) (match : Match) (next_iterator : Match_Iterator)

## PRIVATE
Last (filler : Span)
Last (filler : Utf_16_Span)

## PRIVATE
Convert the polyglot map to a Map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Table
- connection: The connection with which the table is associated.
- internal_columns: The internal representation of the table columns.
- context: The context associated with this table.
Value name:Text connection:Connection (internal_columns:(Vector Internal_Column)) context:IR.Context
Value name:Text connection:Connection (internal_columns:(Vector Internal_Column)) context:Context

## PRIVATE
ADVANCED
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.enso.interpreter.test;

import org.enso.interpreter.test.ValuesGenerator.Language;
import org.graalvm.polyglot.Context;

public final class MetaIsAPolyglotTest extends MetaIsATest {
public MetaIsAPolyglotTest() {}

@Override
protected ValuesGenerator createGenerator(Context ctx) {
return ValuesGenerator.create(ctx, Language.values());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.enso.interpreter.test;

import org.enso.interpreter.test.ValuesGenerator.Language;
import org.graalvm.polyglot.Context;

public final class MetaObjectPolyglotTest extends MetaObjectTest {
public MetaObjectPolyglotTest() {}

@Override
protected ValuesGenerator createGenerator(Context ctx) {
return ValuesGenerator.create(ctx, Language.values());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.enso.compiler;

import com.oracle.truffle.api.source.Source;
import java.io.IOException;

import org.enso.compiler.core.IR;
import org.junit.AfterClass;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Ignore;

import com.oracle.truffle.api.source.Source;

public class EnsoCompilerTest {
private static EnsoCompiler ensoCompiler;
Expand Down Expand Up @@ -947,7 +948,6 @@ public void testCaseWithComment() throws Exception {
}

@Test
@Ignore // Crashes old parser
public void testAlternationTypes() throws Exception {
parseTest("""
foo : [Integer | Text] -> (Integer | Text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public static void disposeContext() {
private static Object[] fetchAllUnwrappedValues() {
var valGenerator =
ValuesGenerator.create(
context,
ValuesGenerator.Language.ENSO,
ValuesGenerator.Language.JAVA,
ValuesGenerator.Language.JAVASCRIPT,
ValuesGenerator.Language.PYTHON);
context, ValuesGenerator.Language.ENSO, ValuesGenerator.Language.JAVA);
List<Value> values = new ArrayList<>();
values.addAll(valGenerator.numbers());
values.addAll(valGenerator.booleans());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ private static Object[] fetchAllUnwrappedValues() {
var valGenerator = ValuesGenerator.create(
context,
ValuesGenerator.Language.ENSO,
ValuesGenerator.Language.JAVA,
ValuesGenerator.Language.JAVASCRIPT,
ValuesGenerator.Language.PYTHON
ValuesGenerator.Language.JAVA
);
List<Value> values = new ArrayList<>();
values.addAll(valGenerator.numbers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.enso.interpreter.test.ValuesGenerator.Language;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
Expand All @@ -12,16 +14,17 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class MetaIsATest extends TestBase {
private Context ctx;
private Value isACheck;
private static Context ctx;
private static Value isACheck;
private static ValuesGenerator generator;

@Before
public void prepareCtx() throws Exception {
@BeforeClass
public static void prepareCtx() throws Exception {
ctx = createDefaultContext();
final URI uri = new URI("memory://choose.enso");
final Source src = Source.newBuilder("enso", """
Expand All @@ -37,98 +40,104 @@ public void prepareCtx() throws Exception {
assertTrue("it is a function", isACheck.canExecute());
}

@After
public void disposeCtx() {
@AfterClass
public static void disposeCtx() {
ctx.close();
}

/** Override to create different values generator.
*
* @param context the context to allocate values in
* @return an instance of values generator
*/
ValuesGenerator createGenerator(Context context) {
return ValuesGenerator.create(context, Language.ENSO, Language.JAVA);
}

private ValuesGenerator generator() {
if (generator == null) {
generator = createGenerator(ctx);
}
return generator;
}

@Test
public void checkNumbersAreNumber() {
var g = ValuesGenerator.create(ctx);
var typeNumber = g.typeNumber();
for (var v : g.numbers()) {
var typeNumber = generator().typeNumber();
for (var v : generator().numbers()) {
var r = isACheck.execute(v, typeNumber);
assertTrue("Value " + v + " is a number, type: " + v.getMetaObject(), r.asBoolean());
}
}

@Test
public void checkValuesAreAny() throws Exception {
var g = ValuesGenerator.create(ctx);
var typeAny = g.typeAny();
for (var v : g.allValues()) {
var typeAny = generator().typeAny();
for (var v : generator().allValues()) {
var r = isACheck.execute(v, typeAny);
assertTrue("Value " + v + " is any", r.asBoolean());
}
}

@Test
public void checkNumbersAreNotText() {
var g = ValuesGenerator.create(ctx);
for (var v : g.numbers()) {
var r = isACheck.execute(v, g.typeText());
for (var v : generator().numbers()) {
var r = isACheck.execute(v, generator().typeText());
assertFalse("Value " + v + " is not a string", r.asBoolean());
}
}

@Test
public void checkTextsAreText() {
var g = ValuesGenerator.create(ctx);
for (var v : g.textual()) {
var r = isACheck.execute(v, g.typeText());
for (var v : generator().textual()) {
var r = isACheck.execute(v, generator().typeText());
assertTrue("Value " + v + " is a string", r.asBoolean());
}
}

@Test
public void checkIntegerIsNotInstanceOfInteger() {
var g = ValuesGenerator.create(ctx);
var t = g.typeInteger();
var t = generator().typeInteger();
var r = isACheck.execute(t, t);
assertFalse("Integer is not instance of Integer", r.asBoolean());
}

@Test
public void checkNumberIsNotInstanceOfNumber() {
var g = ValuesGenerator.create(ctx);
var t = g.typeNumber();
var t = generator().typeNumber();
var r = isACheck.execute(t, t);
assertFalse("Number is not instance of Number", r.asBoolean());
}

@Test
public void checkAnyIsInstanceOfAny() {
var g = ValuesGenerator.create(ctx);
var t = g.typeAny();
var t = generator().typeAny();
var r = isACheck.execute(t, t);
assertTrue("Everything is instance of Any even Any", r.asBoolean());
}

@Test
public void checkTextsAreNotNumbers() {
var g = ValuesGenerator.create(ctx);
for (var v : g.textual()) {
var r = isACheck.execute(v, g.typeNumber());
for (var v : generator().textual()) {
var r = isACheck.execute(v, generator().typeNumber());
assertFalse("Value " + v + " is not a number", r.asBoolean());
}
}

@Test
public void checkArraysAreArrays() {
var g = ValuesGenerator.create(ctx);
for (var v : g.arrayLike()) {
var isVector = isACheck.execute(v, g.typeVector());
var isArray = isACheck.execute(v, g.typeArray());
for (var v : generator().arrayLike()) {
var isVector = isACheck.execute(v, generator().typeVector());
var isArray = isACheck.execute(v, generator().typeArray());
assertTrue("Value " + v + " of type " + v.getMetaObject() + " should either be array (" + isArray + ") or vector (" + isVector + ")", isArray.asBoolean() ^ isVector.asBoolean());
}
}

@Test
public void valuesAreNotInstancesOfThemselves() throws Exception {
var g = ValuesGenerator.create(ctx);
for (var v : g.allValues()) {
for (var v : generator().allValues()) {
var r = isACheck.execute(v, v);
if (v.equals(g.typeNothing())) {
if (v.equals(generator().typeNothing())) {
assertTrue("Nothing is instance of itself", r.asBoolean());
} else {
assertFalse("Value " + v + " shall not be instance of itself", r.isBoolean() && r.asBoolean());
Expand All @@ -138,12 +147,11 @@ public void valuesAreNotInstancesOfThemselves() throws Exception {

@Test
public void constructorVariants() throws Exception {
var g = ValuesGenerator.create(ctx);
var found = new HashMap<Value, Value>();
final List<Value> values = g.constructorsAndValuesAndSumType();
final List<Value> values = generator().constructorsAndValuesAndSumType();
for (var v1 : values) {
for (var v2 : values) {
assertTypeWithCheck(g, v1, v2, found);
assertTypeWithCheck(generator, v1, v2, found);
}
}
assertEquals("Just one: " + found, 1, found.size());
Expand All @@ -170,14 +178,13 @@ private void assertTypeWithCheck(ValuesGenerator g, Value type, Value value, Map

@Test
public void typesAreNotInstancesOfThemselves() throws Exception {
var g = ValuesGenerator.create(ctx);
var f = new StringBuilder();
for (var v : g.allTypes()) {
if (v.equals(g.typeAny())) {
for (var v : generator().allTypes()) {
if (v.equals(generator().typeAny())) {
continue;
}
var r = isACheck.execute(v, v);
if (v.equals(g.typeNothing())) {
if (v.equals(generator().typeNothing())) {
assertTrue("Nothing is instance of itself", r.asBoolean());
} else {
if (r.asBoolean()) {
Expand All @@ -190,13 +197,12 @@ public void typesAreNotInstancesOfThemselves() throws Exception {

@Test
public void consistencyWithCase() throws Exception {
var g = ValuesGenerator.create(ctx);
var f = new StringBuilder();
for (var t : g.allTypes()) {
var typeCaseOf = g.withType(t);
for (var t : generator().allTypes()) {
var typeCaseOf = generator().withType(t);

for (var v : g.allValues()) {
assertTypeAndValue(typeCaseOf, v, t, f, g);
for (var v : generator().allValues()) {
assertTypeAndValue(typeCaseOf, v, t, f, generator);
}
}
if (f.length() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.enso.interpreter.runtime.type.ConstantsGen;
import org.enso.interpreter.test.ValuesGenerator.Language;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
Expand All @@ -22,6 +24,7 @@

public class MetaObjectTest extends TestBase {
private static Context ctx;
private static ValuesGenerator generator;

@BeforeClass
public static void prepareCtx() {
Expand All @@ -33,6 +36,22 @@ public static void disposeCtx() {
ctx.close();
}

/** Override to create different values generator.
*
* @param context the context to allocate values in
* @return an instance of values generator
*/
ValuesGenerator createGenerator(Context context) {
return ValuesGenerator.create(context, Language.ENSO, Language.JAVA);
}

private ValuesGenerator generator() {
if (generator == null) {
generator = createGenerator(ctx);
}
return generator;
}

@Test
public void checkingAtomMetaObject() throws Exception {
final URI uri = new URI("memory://callback.enso");
Expand Down Expand Up @@ -68,7 +87,7 @@ public void checkingAtomMetaObject() throws Exception {

@Test
public void checkAllConstantGenValuesArePresent() throws Exception {
var g = ValuesGenerator.create(ctx);
var g = generator();
var expecting = new HashSet<String>();
for (var f : ConstantsGen.class.getFields()) {
if (!f.getName().endsWith("_BUILTIN")) {
Expand Down Expand Up @@ -135,7 +154,7 @@ public void checkArraysAreArrays() {
}

private void checkAllTypesSatisfy(Check check) throws Exception {
var g = ValuesGenerator.create(ctx);
var g = generator();
var expecting = new LinkedHashSet<Value>();
for (var t : g.allTypes()) {
if (t.isNull()) {
Expand Down
Loading

0 comments on commit b33ae47

Please sign in to comment.