chiark / gitweb /
Static instance support.
[sod] / test / test.sod
index 47e85452b7eb7a01ed7f2d1fe3d84b707b5efdda..5a029cd918771149dbe5e1c4a5fef36b2b7be2bb 100644 (file)
@@ -161,10 +161,13 @@ static int check_vec(struct vec *v, ...)
 
 [link = SodObject, nick = base]
 class T1Base: SodObject {
+  int plain(int x) { STEP(x); return (x + 1); }
+
   [combination = progn] void aprogn();
   [combination = sum] int asum();
   [combination = and] int aand();
   [combination = max] int amax();
+  [role = around] int base.asum() { return (CALL_NEXT_METHOD); }
 
   [combination = custom,
    empty = { sod_ret = 0; },
@@ -184,6 +187,7 @@ class T1Base: SodObject {
 
 [link = T1Base, nick = mid]
 class T1Mid: T1Base {
+  int base.plain(int x) { STEP(x - 1); return (CALL_NEXT_METHOD); }
   void base.aprogn() { STEP(1); }
   int base.asum() { return 1; }
   int base.aand() { return 8; }
@@ -215,7 +219,8 @@ code c: tests {
     if (!l) STEP(5);
     v = T1Base_avec(t1);
     if (!v.n) STEP(6);
-    DONE(7);
+    STEP(T1Base_plain(t1, 7)); /* 7, 8 */
+    DONE(9);
   }
   prepare("aggregate, mid");
   { SOD_DECL(T1Mid, t1, NO_KWARGS);
@@ -231,7 +236,8 @@ code c: tests {
     v = T1Base_avec(t1);
     if (!check_vec(&v, 19, -1)) STEP(6);
     free_vec(&v);
-    DONE(7);
+    STEP(T1Base_plain(t1, 8)); /* 7, 8, 9 */
+    DONE(10);
   }
   prepare("aggregate, sub");
   { SOD_DECL(T1Sub, t1, NO_KWARGS);
@@ -256,11 +262,20 @@ code c: tests {
 [link = SodObject, nick = t2]
 class T2: SodObject {
   [initarg = x] int x = 0;
+  [initarg = z] t2.x;
 
   initarg int y = 1;
   init { if (!y) STEP(0); }
 }
 
+[link = T2]
+class T2Sub: T2 {
+  [initarg = a] t2.x;
+  [initarg = b] t2.x;
+  [initarg = x] t2.x;
+  [initarg = c] t2.x;
+}
+
 code c: tests {
   prepare("initargs, defaults");
   { SOD_DECL(T2, t, NO_KWARGS);
@@ -272,6 +287,21 @@ code c: tests {
     if (t->t2.x == 42) STEP(1);
     DONE(2);
   }
+  prepare("initargs, inheritance");
+  { SOD_DECL(T2Sub, t, KWARGS(K(c, 1) K(z, 2)));
+    if (t->t2.x == 1) STEP(0);
+    DONE(1);
+  }
+  prepare("initargs, ordering");
+  { SOD_DECL(T2Sub, t, KWARGS(K(a, 1) K(b, 2)));
+    if (t->t2.x == 1) STEP(0);
+    DONE(1);
+  }
+  prepare("initargs, reprioritizing");
+  { SOD_DECL(T2Sub, t, KWARGS(K(x, 1) K(c, 2)));
+    if (t->t2.x == 1) STEP(0);
+    DONE(1);
+  }
 }
 
 /*----- Keyword argument propagation --------------------------------------*/
@@ -310,7 +340,7 @@ class MyClass: SodClass {
   int x = -1, y, z = 2;
 }
 
-[link = SodObject, nick = myobj, metaclass = MyClass]
+[nick = myobj, metaclass = MyClass]
 class MyObject: SodObject {
   class mycls.x = 0, mycls.y = 1;
 }
@@ -323,4 +353,26 @@ code c: tests {
   DONE(3);
 }
 
+/*----- Static instances --------------------------------------------------*/
+
+[link = SodObject, nick = st]
+class StaticObject: SodObject {
+  int x, y = 2, z = 3;
+  [readonly = t] void step() { STEP(me->st.x); }
+}
+
+[extern = t] instance StaticObject mystatic: st.x = 0, st.z = 69;
+[const = nil] instance StaticObject otherstatic: st.x = 3, st.y = 42;
+
+code c: tests {
+  prepare("static instance");
+  StaticObject_step(mystatic);
+  if (mystatic->st.y == 2) STEP(1);
+  if (mystatic->st.z == 69) STEP(2);
+  StaticObject_step(otherstatic);
+  if (otherstatic->st.y == 42) STEP(4);
+  if (otherstatic->st.z == 3) STEP(5);
+  DONE(6);
+}
+
 /*----- That's all, folks -------------------------------------------------*/