Skip to content

Commit

Permalink
More test simplification with exec
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 22, 2023
1 parent 27dc730 commit 68b7d3a
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 79 deletions.
8 changes: 3 additions & 5 deletions convex-core/src/main/cvx/convex/core.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,9 @@

[[sym count] & body]

(let [n (long count)
sym (if (symbol? (unsyntax sym))
sym
(fail :CAST
"`dotimes` requires a symbol for loop binding"))]
(let [n (long count)]
(or (symbol? (unsyntax sym))
(fail :CAST "`dotimes` requires a symbol for loop binding"))
`(loop [~sym 0]
(if (< ~sym
~n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import convex.core.init.InitTest;
import convex.core.lang.ACVMTest;
import convex.core.lang.Context;
import convex.core.util.Utils;

import static convex.test.Assertions.*;

public class CurationMarketTest extends ACVMTest {

Expand Down
3 changes: 1 addition & 2 deletions convex-core/src/test/java/convex/actors/RegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public void testRegistryCNSUpdate() throws IOException {
Context c=ctx.forkWithAddress(VILLAIN);

// VILLAIN shouldn't be able to use update on existing CNS mapping
c=step(c,"(call *registry* (cns-update 'convex.villain *address*))");
assertNotError(c);
c=exec(c,"(call *registry* (cns-update 'convex.villain *address*))");

// original mapping should be held
assertEquals(VILLAIN,eval(c,"(call *registry* (cns-resolve 'convex.villain))"));
Expand Down
1 change: 0 additions & 1 deletion convex-core/src/test/java/convex/actors/TorusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static convex.test.Assertions.assertCVMEquals;
import static convex.test.Assertions.assertError;
import static convex.test.Assertions.assertNotError;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/test/java/convex/core/lang/ACVMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected Context step(String source) {
* @param source Code to execute
* @return
*/
protected Context exec(Context ctx, String source) {
protected static Context exec(Context ctx, String source) {
ctx=step(ctx, source);
assertNotError(ctx);
return ctx;
Expand Down
18 changes: 12 additions & 6 deletions convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2365,22 +2365,22 @@ public void testImport() {

@Test
public void testImportCore() {
Context ctx = step("(import convex.core :as cc)");
Context ctx = exec(context(),"(import convex.core :as cc)");
assertNotError(ctx);
assertEquals(eval(ctx,"count"),eval(ctx,"cc/count"));
}

@Test
public void testImportStatic() {
Context ctx = step("(import convex.core :as cc)");
assertNotError(ctx);
Context ctx = context();
ctx=exec(ctx,"(import convex.core :as cc)");

if (Constants.OPT_STATIC) {
ACell va=eval(ctx,"(compile 'cc/actor?)");
assertEquals(Lookup.create(Init.CORE_ADDRESS, Symbols.ACTOR_Q),va);

ACell vc=eval(ctx,"(compile 'cc/count)");
assertEquals(Constant.of(Core.COUNT),vc);

}
}

Expand Down Expand Up @@ -2607,8 +2607,14 @@ public void testCreateAccount() {
assertTrue(evalB("(= (query (create-account *key*)) (query (create-account *key*)))"));

// Check multiple create-accounts in same transaction
assertNotError(step("(dotimes [i 100] (create-account *key*))"));

{
Context c=ctx;
long numAccounts=c.getState().getAccounts().count();
c=exec(c,"(dotimes [i 100] (create-account *key*))");
long resultNum=c.getState().getAccounts().count();
assertEquals(numAccounts+100, resultNum);
}

assertCastError(step("(create-account :foo)"));
assertCastError(step("(create-account 1)"));
assertCastError(step("(create-account nil)"));
Expand Down
31 changes: 10 additions & 21 deletions convex-core/src/test/java/convex/lib/ShareTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,19 @@ protected ShareTest() {
}

@Override protected Context buildContext(Context ctx) {
String importS="(import asset.multi-token :as mt)";
ctx=step(ctx,importS);
assertNotError(ctx);

String importS2="(import asset.share :as share)";
ctx=step(ctx,importS2);
assertNotError(ctx);

ctx=step(ctx,"(import convex.asset :as asset)");
assertNotError(ctx);
ctx=step(ctx,"(import convex.fungible :as fungible)");
assertNotError(ctx);
ctx=step(ctx,"(import convex.trust :as trust)");
assertNotError(ctx);

// Unerlying
ctx=step(ctx,"(def underlying [mt (call mt (create :USD))])");
assertNotError(ctx);
ctx=exec(ctx,"(import asset.multi-token :as mt)");
ctx=step(ctx,"(import asset.share :as share)");
ctx=exec(ctx,"(import convex.asset :as asset)");
ctx=exec(ctx,"(import convex.fungible :as fungible)");
ctx=exec(ctx,"(import convex.trust :as trust)");

// Underlying asset
ctx=exec(ctx,"(def underlying [mt (call mt (create :USD))])");

ctx=step(ctx,"(call underlying (mint 1000000))");
ctx=exec(ctx,"(call underlying (mint 1000000))");
assertCVMEquals(1000000,ctx.getResult());

ctx=step(ctx,"(asset/transfer "+InitTest.VILLAIN+" [underlying 400])");
assertNotError(ctx);
ctx=exec(ctx,"(asset/transfer "+InitTest.VILLAIN+" [underlying 400])");

return ctx;
}
Expand Down
8 changes: 3 additions & 5 deletions convex-core/src/test/java/convex/lib/SimpleNFTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ protected SimpleNFTTest() {
private static State createState() {
Context ctx=TestState.CONTEXT.fork();


String importS = "(import asset.nft.simple :as nft)";
ctx=step(ctx,importS);

ctx=step(ctx,"(import convex.asset :as asset)");
ctx=exec(ctx,importS);
ctx=exec(ctx,"(import convex.asset :as asset)");
return ctx.getState();
}

Expand All @@ -48,7 +46,7 @@ private static State createState() {
@SuppressWarnings("unchecked")
@Test public void testAssetAPI() {
Context ctx=context();
ctx=step(ctx,"(def total (map (fn [v] (call nft (create))) [1 2 3 4]))");
ctx=exec(ctx,"(def total (map (fn [v] (call nft (create))) [1 2 3 4]))");
AVector<CVMLong> v=(AVector<CVMLong>) ctx.getResult();
assertEquals(4,v.count());
CVMLong b1=v.get(0);
Expand Down
13 changes: 5 additions & 8 deletions convex-core/src/test/java/convex/lib/TrustActorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ protected TrustActorTest() throws IOException {

@Override protected Context buildContext(Context ctx) {
String importS = "(import convex.trust :as trust)";
ctx = step(ctx, importS);
assertNotError(ctx);
ctx = exec(ctx, importS);
trusted = (Address)ctx.getResult();
return ctx;
}

@Test
public void testDelegate() {
Context ctx=step(context(),"(import convex.trust.delegate :as del)");
assertNotError(ctx);
Context ctx=exec(context(),"(import convex.trust.delegate :as del)");
Address del=ctx.getResult();

// Unscoped usage
Expand All @@ -47,20 +45,19 @@ public void testDelegate() {
assertFalse(evalB(ctx,"(trust/trusted? [del -1] *address*)"));

// Get a monitor
ctx=step(ctx,"(call del (create nil))");
ctx=exec(ctx,"(call del (create nil))");
ACell id = ctx.getResult();
AVector<ACell> mon=Vectors.of(del,id);
assertFalse(evalB(ctx,"(trust/trusted? "+mon+" *address*)"));

// Update monitor to own address
ctx=step(ctx,"(call "+mon+" (update *address*))");
assertNotError(ctx);
ctx=exec(ctx,"(call "+mon+" (update *address*))");

assertFalse(evalB(ctx,"(trust/trusted? "+mon+" #0)"));
assertTrue(evalB(ctx,"(trust/trusted? "+mon+" *address*)"));

// Get another monitor
ctx=step(ctx,"(call del (create nil))");
ctx=exec(ctx,"(call del (create nil))");
ACell newid = ctx.getResult();
assertNotEquals(id,newid);

Expand Down
12 changes: 5 additions & 7 deletions convex-core/src/test/java/convex/lib/TrustTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ protected TrustTest() throws IOException {

@Override protected Context buildContext(Context ctx) {
String importS = "(import convex.trust :as trust)";
ctx = step(ctx, importS);
assertNotError(ctx);
ctx = exec(ctx, importS);
trusted = (Address)ctx.getResult();
return ctx;
}
Expand Down Expand Up @@ -61,19 +60,19 @@ public void testUpgrade() {
Context ctx = CONTEXT.fork();

// deploy an actor with upgradable capability
ctx = step(ctx, "(def wlist (deploy (trust/add-trusted-upgrade nil)))");
ctx = exec(ctx, "(def wlist (deploy (trust/add-trusted-upgrade nil)))");
Address wl = (Address) ctx.getResult();
assertNotNull(wl);

// do an upgrade that edits the actor
ctx = step(ctx, "(call wlist (upgrade '(def foo 2)))");
ctx = exec(ctx, "(call wlist (upgrade '(def foo 2)))");

{
// check our villain cannot upgrade the actor!
Address a1 = VILLAIN;

Context c = ctx.forkWithAddress(a1);
c = step(c, "(do (import " + trusted + " :as trust) (def wlist " + wl + "))");
c = exec(c, "(do (import " + trusted + " :as trust) (def wlist " + wl + "))");

assertTrustError(step(c, "(call wlist (upgrade '(def foo 3)))"));
}
Expand All @@ -84,8 +83,7 @@ public void testUpgrade() {
testChangeControl(ctx,wl);

// check we can permanently remove upgradability
ctx = step(ctx, "(trust/remove-upgradability! wlist)");
assertNotError(ctx);
ctx = exec(ctx, "(trust/remove-upgradability! wlist)");
assertStateError(step(ctx, "(call wlist (upgrade '(do :foo)))"));

// actor functionality should still work otherwise
Expand Down
30 changes: 10 additions & 20 deletions convex-core/src/test/java/convex/lib/WrappedCVXTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,10 @@ protected WrappedCVXTest() {
private static State buildState() {
Context ctx=TestState.CONTEXT.fork();
String importS="(import convex.fungible :as fungible)";
ctx=step(ctx,importS);
assertNotError(ctx);
ctx=step(ctx,"(import convex.asset :as asset)");
assertNotError(ctx);

ctx=step(ctx,"(import asset.wrap.convex :as wcvx)");
assertNotError(ctx);

ctx=step(ctx,"(def token wcvx)");
ctx=exec(ctx,importS);
ctx=exec(ctx,"(import convex.asset :as asset)");
ctx=exec(ctx,"(import asset.wrap.convex :as wcvx)");
ctx=exec(ctx,"(def token wcvx)");

return ctx.getState();
}
Expand All @@ -56,11 +51,8 @@ private static State buildState() {
assertEquals(1000000L,evalL(ctx,"(asset/balance token *address*)"));
assertEquals(0L,evalL(ctx,"(asset/balance token *registry*)"));

ctx=step(ctx,"(asset/offer "+VILLAIN+" [token 1000])");
assertNotError(ctx);

ctx=step(ctx,"(asset/transfer "+VILLAIN+" [token 2000])");
assertNotError(ctx);
ctx=exec(ctx,"(asset/offer "+VILLAIN+" [token 1000])");
ctx=exec(ctx,"(asset/transfer "+VILLAIN+" [token 2000])");

assertEquals(998000L,evalL(ctx,"(asset/balance token *address*)"));
assertEquals(2000L,evalL(ctx,"(asset/balance token "+VILLAIN+")"));
Expand All @@ -83,12 +75,12 @@ private static State buildState() {
assertFalse(evalB(ctx,"(asset/owns? "+VILLAIN+" [token 2001])"));

// transfer using map argument
ctx=step(ctx,"(asset/transfer "+VILLAIN+" {token 100})");
ctx=exec(ctx,"(asset/transfer "+VILLAIN+" {token 100})");
assertTrue(ctx.getResult() instanceof AMap);
assertTrue(evalB(ctx,"(asset/owns? "+VILLAIN+" [token 2100])"));

// test offer
ctx=step(ctx,"(asset/offer "+VILLAIN+" [token 1337])");
ctx=exec(ctx,"(asset/offer "+VILLAIN+" [token 1337])");
assertEquals(1337L,evalL(ctx,"(asset/get-offer token *address* "+VILLAIN+")"));


Expand All @@ -98,15 +90,13 @@ private static State buildState() {
Context ctx = context();
long BAL= evalL(ctx,"*balance*");

ctx=step(ctx,"(wcvx/wrap 1000000)");
assertNotError(ctx);
ctx=exec(ctx,"(wcvx/wrap 1000000)");

assertEquals(1000000,evalL(ctx,"(asset/balance wcvx *address*)"));
long NBAL= evalL(ctx,"*balance*");
assertEquals(BAL-1000000,NBAL);

ctx=step(ctx,"(wcvx/unwrap 50000)");
assertNotError(ctx);
ctx=exec(ctx,"(wcvx/unwrap 50000)");
assertEquals(950000,evalL(ctx,"(fungible/balance wcvx *address*)"));

assertEquals(NBAL+50000,evalL(ctx,"*balance*"));
Expand Down

0 comments on commit 68b7d3a

Please sign in to comment.