/* Test basic use of lists */ static int ListTest(WJElement doc) { WJElement t, e; /* Every element */ if (!(t = WJEGet(doc, "digits[0]", NULL))) return(__LINE__); e = NULL; while ((e = WJEGet(doc, "digits[0,1,2,3,4,5,6,7,8,9]", e))) { if (e != t) return(__LINE__); t = t->next; } if (t) return(__LINE__); /* Every other element. Note that order in the query does not effect the order in which the results are returned. */ if (!(t = WJEGet(doc, "digits[1]", NULL))) return(__LINE__); e = NULL; while ((e = WJEGet(doc, "digits[1,7,5,3]", e))) { if (!t || e != t) return(__LINE__); t = t->next->next; } return(0); }
int main(int argc, char **argv) { WJElement doc = NULL; WJElement person = NULL; WJElement cameo = NULL; doc = WJEObject(NULL, NULL, WJE_NEW); WJEString(doc, "name", WJE_SET, "Serenity"); WJEString(doc, "class", WJE_SET, "firefly"); WJEArray(doc, "crew", WJE_SET); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Malcolm Reynolds"); WJEString(doc, "crew[-1].job", WJE_SET, "captain"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2468); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Kaywinnet Lee Fry"); WJEString(doc, "crew[-1].job", WJE_SET, "mechanic"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2494); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Jayne Cobb"); WJEString(doc, "crew[-1].job", WJE_SET, "public relations"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2485); WJEArray(doc, "cameo", WJE_SET); WJEString(doc, "cameo[$]", WJE_NEW, "Battlestar Galactica"); WJEString(doc, "cameo[$]", WJE_NEW, "Star Wars Evasive Action"); WJEString(doc, "cameo[$]", WJE_NEW, "Dr. Horrible's Sing-Along Blog"); WJEString(doc, "cameo[$]", WJE_NEW, "Ready Player One"); WJEBool(doc, "shiny", WJE_SET, TRUE); WJEInt64(doc, "crew[].born == 2468", WJE_SET, 2486); /* note: awesome! */ WJECloseDocument(WJEGet(doc, "shiny", NULL)); while((person = _WJEObject(doc, "crew[]", WJE_GET, &person))) { printf("%s (%s) is %"PRId64"\n", WJEString(person, "name", WJE_GET, ""), WJEString(person, "job", WJE_GET, ""), (2517 - WJEInt64(person, "born", WJE_GET, 0))); } while((cameo = WJEGet(doc, "cameo[]", cameo))) { printf("Cameo: %s\n", WJEString(cameo, NULL, WJE_GET, "")); } WJEDump(doc); WJECloseDocument(doc); return 0; }
/* Various methods of referencing an element by name */ static int NamesTest(WJElement doc) { WJElement e; /* The main doc can't have a name, since it has no parent */ if (doc->name) return(__LINE__); /* Various ways of referencing the same element */ if (!(e = WJEChild(doc, "one", WJE_GET))) return(__LINE__); if (e != WJEGet(doc, "one", NULL)) return(__LINE__); if (e != WJEGet(doc, "[\"one\"]", NULL)) return(__LINE__); if (e != WJEGet(doc, "[\'one\']", NULL)) return(__LINE__); return(0); }
static int PutValueTest(WJElement doc) { char *v; /* WJE_PUT will update the value if the element exists. If it does not exist then it will NOT create it. Do a WJE_PUT on values that do and do not already exist, and verify the result. Then use WJEGet() to verify that new elements where not created. */ /* Replace existing number values */ if (-1!= WJENumber(doc, "one", WJE_PUT, -1)) return(__LINE__); if (-1!= WJENumber(doc, "one", WJE_GET, 1)) return(__LINE__); if (-1!= WJENumber(doc, "two", WJE_PUT, -1)) return(__LINE__); if (-1!= WJENumber(doc, "two", WJE_GET, 2)) return(__LINE__); if (-1!= WJENumber(doc, "three", WJE_PUT, -1)) return(__LINE__); if (-1!= WJENumber(doc, "three", WJE_GET, 3)) return(__LINE__); /* Insert a new number value */ if (-1== WJENumber(doc, "four", WJE_PUT, -1)) return(__LINE__); if (WJEGet(doc, "four", NULL)) return(__LINE__); /* Replace existing boolean values */ if ( WJEBool(doc, "a.b.balloon", WJE_PUT, 0)) return(__LINE__); if (!WJEBool(doc, "a.b.aardvark", WJE_PUT, 1)) return(__LINE__); /* Insert a new boolean value */ if ( WJEBool(doc, "a.b.bull", WJE_PUT, 1)) return(__LINE__); if (WJEGet(doc, "a.b.bull", NULL)) return(__LINE__); /* Replace an existing string value */ if (!(v = WJEString(doc, "a.names[-1]", WJE_PUT, "aardvark")) || stricmp(v, "aardvark")) return(__LINE__); if (!(v = WJEString(doc, "a.names[-1]", WJE_GET, NULL)) || stricmp(v, "aardvark")) return(__LINE__); /* Insert a new string value */ if ((v = WJEString(doc, "a.x.q", WJE_PUT, "queen"))) return(__LINE__); if (WJEGet(doc, "a.x.q", NULL)) return(__LINE__); return(0); }
/* Test basic use of offsets */ static int OffsetTest(WJElement doc) { WJElement e, l; /* Get the first element */ if (!(e = WJEGet(doc, "digits[0]", NULL))) return(__LINE__); if (e->prev) return(__LINE__); if (WJEGet(doc, "digits[0]", e)) return(__LINE__); /* Get the last element */ if (!(e = WJEGet(doc, "digits[-1]", NULL))) return(__LINE__); if (e->next) return(__LINE__); if (WJEGet(doc, "digits[-1]", e)) return(__LINE__); /* Get an element in the middle */ if (!(e = WJEGet(doc, "digits[5]", NULL))) return(__LINE__); if (!e->next || !e->prev) return(__LINE__); if (WJEGet(doc, "digits[5]", e)) return(__LINE__); /* Grab the last item, then append a new item and verify the change */ if (!(e = WJEGet(doc, "digits[-1]", NULL))) return(__LINE__); l = NULL; if (-1 != _WJENumber(doc, "digits[$]", WJE_NEW, &l, -1))return(__LINE__); if (!l || l->next || l->prev != e || e->next != l) return(__LINE__); return(0); }
static int SelfTest(WJElement doc) { WJElement a, b; /* Tests methods of refering to "self" */ if (doc != WJEGet(doc, NULL, NULL)) return(__LINE__); if (doc != WJEGet(doc, "", NULL)) return(__LINE__); if (doc != WJEGet(doc, ".", NULL)) return(__LINE__); /* Often in code a WJElement is accessed like this: if ((msg->extra = WJEObject(msg->extra, NULL, WJE_NEW))) { ... The idea is that since passing a NULL refers to self that msg->extra will be returned if it exists, and created if it does not. Since this is such a common case, verify it's behavior properly. */ if (!(a = WJEObject(NULL, NULL, WJE_NEW))) return(__LINE__); if (!(b = WJEObject(a, NULL, WJE_NEW))) return(__LINE__); if (a != b) return(__LINE__); return(0); }
int main(int argc, char **argv) { WJElement doc = NULL; WJElement person = NULL; doc = WJEObject(NULL, NULL, WJE_NEW); WJEString(doc, "name", WJE_SET, "Serenity"); WJEString(doc, "class", WJE_SET, "firefly"); WJEArray(doc, "crew", WJE_SET); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Malcolm Reynolds"); WJEString(doc, "crew[-1].job", WJE_SET, "captain"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2468); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Kaywinnet Lee Fry"); WJEString(doc, "crew[-1].job", WJE_SET, "mechanic"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2494); WJEObject(doc, "crew[$]", WJE_NEW); WJEString(doc, "crew[-1].name", WJE_SET, "Jayne Cobb"); WJEString(doc, "crew[-1].job", WJE_SET, "public relations"); WJEInt64(doc, "crew[-1].born", WJE_SET, 2485); WJEBool(doc, "shiny", WJE_SET, TRUE); WJEInt64(doc, "crew[].born == 2468", WJE_SET, 2486); /* note: awesome! */ WJECloseDocument(WJEGet(doc, "shiny", NULL)); while((person = _WJEObject(doc, "crew[]", WJE_GET, &person))) { printf("%s (%s) is %d\n", WJEString(person, "name", WJE_GET, ""), WJEString(person, "job", WJE_GET, ""), 2517 - WJEInt64(person, "born", WJE_GET, 0)); } WJEDump(doc); WJECloseDocument(doc); return 0; }
static int ConditionsTest(WJElement doc) { if (!WJEGet(doc, "digits[3] == 3", NULL)) return(__LINE__); if ( WJEGet(doc, "digits[3] != 3", NULL)) return(__LINE__); if (!WJEGet(doc, "digits[3] <= 3", NULL)) return(__LINE__); if (!WJEGet(doc, "digits[3] >= 3", NULL)) return(__LINE__); if ( WJEGet(doc, "digits[3] < 2", NULL)) return(__LINE__); if ( WJEGet(doc, "digits[3] > 4", NULL)) return(__LINE__); if (!WJEGet(doc, "strings[0] == '* some *'", NULL)) return(__LINE__); if ( WJEGet(doc, "strings[0] == '* what *'", NULL)) return(__LINE__); if ( WJEGet(doc, "strings[0] != '* some *'", NULL)) return(__LINE__); if (!WJEGet(doc, "strings[0] != '* what *'", NULL)) return(__LINE__); if (!WJEGet(doc, "strings[2] == \"and this\"", NULL)) return(__LINE__); if ( WJEGet(doc, "strings[2] == \"not this\"", NULL)) return(__LINE__); if ( WJEGet(doc, "strings[2] != \"and this\"", NULL)) return(__LINE__); if (!WJEGet(doc, "strings[2] != \"not this\"", NULL)) return(__LINE__); if (!WJEGet(doc, "sender.address == 'foo*'", NULL)) return(__LINE__); return(0); }
static int PathsTest(WJElement doc) { WJElement e; /* Compare multiple methods of referencing deep elements */ if ((e = WJEGet(doc, "a.b.c.d.e.f", NULL)) == NULL) return(__LINE__); if (e != WJEGet(doc, "a[\"b\"].c.d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a['b'].c.d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a.b[\"c\"].d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a.b['c'].d.e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a['b'].c['d'].e.f", NULL)) return(__LINE__); if (e != WJEGet(doc, "a['b']['c']['d'].e.f",NULL)) return(__LINE__); /* Do the same thing with paths that don't exist yet */ if (!(e = WJEObject(doc, "z[\"y\"].x.w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z['y'].x.w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z.y[\"x\"].w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z.y['x'].w.v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z['y'].x['w'].v.u", WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); if (!(e = WJEObject(doc, "z['y']['x']['w'].v.u",WJE_NEW)) || !WJECloseDocument(e)) return(__LINE__); /* Test paths that contain spaces, and dashes */ if (!WJEGet(doc, "space balls", NULL)) return(__LINE__); if (!WJEGet(doc, "space balls.the toliet paper", NULL)) return(__LINE__); if ( WJEGet(doc, "space balls.the what ", NULL)) return(__LINE__); if (!WJEGet(doc, "space balls.the what []", NULL)) return(__LINE__); return(0); }