Skip to content

Commit

Permalink
Extra tests and better handling for query-as return
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Mar 14, 2024
1 parent 2e02797 commit 59a2806
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
16 changes: 2 additions & 14 deletions convex-core/src/main/java/convex/core/lang/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ public Context evalAs(Address address, ACell form) {
}

/**
* Executes code as if run in the specified account, but always discarding state changes.
* Executes code as if run in the specified account, but always rolling back state changes.
* @param address Address of Account in which to execute the query
* @param form Code to execute.
* @return Context updated with only query result and juice consumed
Expand All @@ -1372,19 +1372,7 @@ public Context queryAs(Address address, ACell form) {
if (cs==null) return withError(ErrorCodes.NOBODY,"Address does not exist: "+address);
Context ctx=Context.create(cs, juice,juiceLimit, EMPTY_BINDINGS, null, depth,log,null);
ctx=ctx.eval(form);
return handleQueryResult(ctx);
}

/**
* Just take result and juice from query. Log and state not kept.
* @param ctx
* @return
*/
protected Context handleQueryResult(Context ctx) {
// Copy juice used
this.juice=ctx.juice;
// Copy over result (may be exceptional)
return this.withValue(ctx.getValue());
return handleStateResults(ctx,true);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions convex-core/src/test/java/convex/core/init/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import convex.core.data.Refs;
import convex.core.data.prim.CVMLong;
import convex.core.lang.ACVMTest;
import convex.core.lang.Core;
import convex.core.lang.RT;

/**
* Tests for Init functionality
Expand Down Expand Up @@ -93,6 +95,12 @@ public void testVILLAIN() {
assertNotEquals(HERO,VILLAIN);
}

// Regression
@Test
public void testPrintCore() {
RT.print(STATE.getAccount(Core.CORE_ADDRESS).getEnvironment());
}

@Test
public void testInitRef() {
State s=STATE;
Expand Down
5 changes: 5 additions & 0 deletions convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4296,6 +4296,9 @@ public void testQueryAs() {
Context c3=exec(context(),"(query-as *address* 1)");
Context c4=exec(context(),"(eval-as *address* 1)");
assertEquals(c3.getJuiceUsed(),c4.getJuiceUsed());

// Query as halts should act as return
assertCVMEquals(666,eval("(do (query-as #8 '(halt 777)) 666)"));

assertNobodyError(step("(query-as #8888 '1)"));
assertCastError(step("(query-as nil '1)"));
Expand All @@ -4306,6 +4309,8 @@ public void testQueryAs() {
// Failures within query
assertCastError(step("(query-as *address* (count 1))"));
assertTrustError(step("(query-as #0 '(eval-as #8 1))"));


}

@Test
Expand Down
1 change: 0 additions & 1 deletion convex-core/src/test/java/convex/core/lang/RTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void testPrint() {
assertEquals(Strings.create("\"'\""),RT.print(Strings.create("'"))); // See #407
}


@Test
public void testSequence() {
AVector<CVMLong> v = Vectors.of(1L, 2L, 3L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void handleResult(Result r) {
}

protected void handleResult(ACell m) {
String resultString=RT.print(m).toString();
String resultString=RT.print(m,10000).toString();
int start=outputArea.getDocument().getLength();
addOutput(outputArea," => " + resultString + "\n");
int end=outputArea.getDocument().getLength();
Expand Down

0 comments on commit 59a2806

Please sign in to comment.