static bool PutVar(VariableTable *table, char *var_str) { VarRef *ref = VarRefParse(var_str); Rval rval = (Rval) { var_str, RVAL_TYPE_SCALAR }; bool ret = VariableTablePut(table, ref, &rval, CF_DATA_TYPE_STRING, NULL, NULL); VarRefDestroy(ref); return ret; }
static VariableTable *ReferenceTable(void) { VariableTable *t = VariableTableNew(); assert_false(PutVar(t, "scope1.lval1")); assert_false(PutVar(t, "scope1.lval2")); assert_false(PutVar(t, "scope2.lval1")); { VarRef *ref = VarRefParse("scope1.array[one]"); Rval rval = (Rval) { "scope1.array[one]", RVAL_TYPE_SCALAR }; assert_false(VariableTablePut(t, ref, &rval, DATA_TYPE_STRING, NULL, NULL)); VarRefDestroy(ref); } { VarRef *ref = VarRefParse("scope1.array[two]"); Rval rval = (Rval) { "scope1.array[two]", RVAL_TYPE_SCALAR }; assert_false(VariableTablePut(t, ref, &rval, DATA_TYPE_STRING, NULL, NULL)); VarRefDestroy(ref); } { VarRef *ref = VarRefParse("scope1.array[two][three]"); Rval rval = (Rval) { "scope1.array[two][three]", RVAL_TYPE_SCALAR }; assert_false(VariableTablePut(t, ref, &rval, DATA_TYPE_STRING, NULL, NULL)); VarRefDestroy(ref); } { VarRef *ref = VarRefParse("scope1.array[two][four]"); Rval rval = (Rval) { "scope1.array[two][four]", RVAL_TYPE_SCALAR }; assert_false(VariableTablePut(t, ref, &rval, DATA_TYPE_STRING, NULL, NULL)); VarRefDestroy(ref); } assert_false(PutVar(t, "ns1:scope1.lval1")); assert_false(PutVar(t, "ns1:scope1.lval2")); assert_false(PutVar(t, "ns1:scope2.lval1")); return t; }
static void test_replace(void) { VariableTable *t = ReferenceTable(); VarRef *ref = VarRefParse("scope1.lval1"); TestGet(t, "scope1.lval1"); Rval rval = (Rval) { "foo", RVAL_TYPE_SCALAR }; assert_true(VariableTablePut(t, ref, &rval, CF_DATA_TYPE_STRING, NULL, NULL)); Variable *v = VariableTableGet(t, ref); assert_true(v != NULL); assert_string_equal("foo", RvalScalarValue(v->rval)); VarRefDestroy(ref); VariableTableDestroy(t); }