test/test.sod: Abbreviate the T1 class nicknames.
[sod] / test / test.sod
index e5025b5..5779376 100644 (file)
@@ -1,10 +1,10 @@
 /* -*-sod-*- */
 
-code h : includes {
+code h: includes {
 #include "sod.h"
 }
 
-code c : includes {
+code c: includes {
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -12,7 +12,7 @@ code c : includes {
 #include "test.h"
 }
 
-code c : early_user {
+code c: early_user {
 /*----- Preliminary definitions -------------------------------------------*/
 
 /* Confuse the fragment scanner... */
@@ -44,7 +44,7 @@ static void done(int q, const char *where) { step(q, where); }
 
 }
 
-code c : (tests head)
+code c: (tests head)
   [user (tests head) tests (tests tail) main (user end)]
 {
 /*----- Test machinery ----------------------------------------------------*/
@@ -52,12 +52,12 @@ code c : (tests head)
 static void tests(void)
 LBRACE
 }
-code c : (tests tail) {
+code c: (tests tail) {
 RBRACE
 
 }
 
-code c : main {
+code c: main {
 /*----- Main program ------------------------------------------------------*/
 
 int main(void)
@@ -70,7 +70,7 @@ int main(void)
 
 /*----- Various kinds of method combinations ------------------------------*/
 
-code h : early_user {
+code h: early_user {
 struct item {
   struct item *next;
   const char *p;
@@ -78,7 +78,7 @@ struct item {
 
 }
 
-code c : early_user {
+code c: early_user {
 static void *xmalloc(size_t n)
 {
   void *p = malloc(n);
@@ -135,41 +135,66 @@ static int check_vec(struct vec *v, ...)
 
 }
 
-[link = SodObject, nick = t1base]
-class T1Base : SodObject {
-  [combination = progn] void aprogn() { STEP(1); }
-  [combination = sum] int asum() { return 1; }
-  [combination = and] int aand() { return 8; }
-  [combination = max] int amax() { return 12; }
+[link = SodObject, nick = base]
+class T1Base: SodObject {
+  [combination = progn] void aprogn();
+  [combination = sum] int asum();
+  [combination = and] int aand();
+  [combination = max] int amax();
 
   [combination = custom,
+   empty = { sod_ret = 0; },
    decls = { struct item **head = &sod_ret; },
    each = { *head = sod_val; head = &sod_val->next; },
    after = { *head = 0; }]
-  struct item *alist() { return make_item("base"); }
+  struct item *alist();
 
   [combination = custom,
    decls = { int *v; size_t i = 0; }, methty = <int>, count = n,
+   empty = { sod_ret.v = 0; sod_ret.n = 0; },
    before = { v = xmalloc(n*sizeof(int)); },
    each = { v[i++] = sod_val; },
    after = { sod_ret.v = v; sod_ret.n = n; }]
   struct vec avec();
-  int t1base.avec() { return 19; }
 }
 
-[link = T1Base, nick = t1sub]
-class T1Sub : T1Base {
-  void t1base.aprogn() { STEP(0); }
-  int t1base.asum() { return 2; }
-  int t1base.aand() { return 6; }
-  int t1base.amax() { return 17; }
-  struct item *t1base.alist() { return make_item("sub"); }
-  int t1base.avec() { return 4; }
+[link = T1Base, nick = mid]
+class T1Mid: T1Base {
+  void base.aprogn() { STEP(1); }
+  int base.asum() { return 1; }
+  int base.aand() { return 8; }
+  int base.amax() { return 12; }
+  struct item *base.alist() { return make_item("mid"); }
+  int base.avec() { return 19; }
 }
 
-code c : tests {
+[link = T1Mid, nick = sub]
+class T1Sub: T1Mid {
+  void base.aprogn() { STEP(0); }
+  int base.asum() { return 2; }
+  int base.aand() { return 6; }
+  int base.amax() { return 17; }
+  struct item *base.alist() { return make_item("sub"); }
+  int base.avec() { return 4; }
+}
+
+code c: tests {
   prepare("aggregate, base");
-  { SOD_DECL(T1Base, t1);
+  { SOD_DECL(T1Base, t1, NO_KWARGS);
+    struct item *l;
+    struct vec v;
+    STEP(0); T1Base_aprogn(t1); STEP(1);
+    if (T1Base_asum(t1) == 0) STEP(2);
+    if (T1Base_aand(t1) == 1) STEP(3);
+    if (!t1->_vt->base.amax) STEP(4);
+    l = T1Base_alist(t1);
+    if (!l) STEP(5);
+    v = T1Base_avec(t1);
+    if (!v.n) STEP(6);
+    DONE(7);
+  }
+  prepare("aggregate, mid");
+  { SOD_DECL(T1Mid, t1, NO_KWARGS);
     struct item *l;
     struct vec v;
     STEP(0); T1Base_aprogn(t1); /* 1 */
@@ -177,7 +202,7 @@ code c : tests {
     if (T1Base_aand(t1) == 8) STEP(3);
     if (T1Base_amax(t1) == 12) STEP(4);
     l = T1Base_alist(t1);
-    if (!check_list(l, "base", (const char *)0)) STEP(5);
+    if (!check_list(l, "mid", (const char *)0)) STEP(5);
     free_list(l);
     v = T1Base_avec(t1);
     if (!check_vec(&v, 19, -1)) STEP(6);
@@ -185,7 +210,7 @@ code c : tests {
     DONE(7);
   }
   prepare("aggregate, sub");
-  { SOD_DECL(T1Sub, t1);
+  { SOD_DECL(T1Sub, t1, NO_KWARGS);
     struct item *l;
     struct vec v;
     T1Base_aprogn(t1); /* 0, 1 */
@@ -193,7 +218,7 @@ code c : tests {
     if (T1Base_aand(t1) == 8) STEP(3);
     if (T1Base_amax(t1) == 17) STEP(4);
     l = T1Base_alist(t1);
-    if (!check_list(l, "sub", "base", (const char *)0)) STEP(5);
+    if (!check_list(l, "sub", "mid", (const char *)0)) STEP(5);
     free_list(l);
     v = T1Base_avec(t1);
     if (!check_vec(&v, 4, 19, -1)) STEP(6);
@@ -202,4 +227,56 @@ code c : tests {
   }
 }
 
+/*----- Slot and user initargs --------------------------------------------*/
+
+[link = SodObject, nick = t2]
+class T2: SodObject {
+  [initarg = x] int x = 0;
+
+  initarg int y = 1;
+  init { if (!y) STEP(0); }
+}
+
+code c: tests {
+  prepare("initargs, defaults");
+  { SOD_DECL(T2, t, NO_KWARGS);
+    if (t->t2.x == 0) STEP(0);
+    DONE(1);
+  }
+  prepare("initargs, explicit");
+  { SOD_DECL(T2, t, KWARGS(K(x, 42) K(y, 0)));
+    if (t->t2.x == 42) STEP(1);
+    DONE(2);
+  }
+}
+
+/*----- Keyword argument propagation --------------------------------------*/
+
+[link = SodObject, nick = base]
+class T3Base: SodObject {
+  void m0(?int x) { STEP(x); }
+  void m1(?) { }
+}
+
+[link = T3Base, nick = mid]
+class T3Mid: T3Base {
+  void base.m0(?int y) { STEP(y); CALL_NEXT_METHOD; }
+  void base.m1(?) { STEP(4); CALL_NEXT_METHOD; }
+}
+
+[link = T3Mid, nick = sub]
+class T3Sub: T3Mid {
+  void base.m0(?int z) { STEP(z); CALL_NEXT_METHOD; }
+  void base.m1(?int z) { STEP(z); CALL_NEXT_METHOD; }
+}
+
+code c: tests {
+  prepare("kwargs");
+  { SOD_DECL(T3Sub, t, NO_KWARGS);
+    T3Base_m0(t, KWARGS(K(z, 0) K(y, 1) K(x, 2)));
+    T3Base_m1(t, KWARGS(K(z, 3)));
+    DONE(5);
+  }
+}
+
 /*----- That's all, folks -------------------------------------------------*/