def utils_unit_test(): """ Unit testing on various utility code. """ s = utils.c_escape("Hello World!") assert s == "Hello World!" s = utils.c_escape("Test \" \\ One") assert s == "Test \\\" \\\\ One" s = utils.pad_to("0123456789", 11) assert s == "0123456789 " s = utils.pad_to("0123456789", 8) assert s == "0123456789" s = utils.pad_to("0", 10, "z") assert s == "0zzzzzzzzz" s = utils.split_path_left("a/b/c") assert s == ("a", "b/c") s = utils.split_path_left("/a/b/c") assert s == ("", "a/b/c") s = utils.replace_root_name("/a", "/b", "/a/c") assert s == "/b/c" s = utils.replace_root_name("/a", "/b", "/d/e") assert s == "/d/e"
def to_c(self, var, prefix): """ Returns a list of strings you can join together to make C code for constructing the value of this expression * var - The name of the C variable we're creating. * prefix - The name of the prefix to prepend to function calls. """ str_list = [ ] if (self.type == EnvExpr.StringType): for v in self.values: str_list.append(" %s = %s_cat(%s, \"%s\");\n"%(var, prefix, var, utils.c_escape(v))) elif (self.type == EnvExpr.RefType): for v in self.values: str_list.append( " %s = %s_cat(%s, %s_lookup(\"%s\", handle));\n"%(var, prefix, var, prefix, utils.c_escape(v))) else: for v in self.values: str_list.extend(v.to_c(var, prefix)) return str_list
def get_c_subst_var(self, prefix): """ Returns the block of C to use as a substitute for ``body_impl`` in ``resources/c_env.c`` """ sorted_items = self.dependency_sort() rlist = [ ] doneOne = False for (k,v) in sorted_items: if (doneOne): rlist.append("else ") else: doneOne = True rlist.append("if (!strcmp(\"%s\", name))\n"%utils.c_escape(k) + "{ \n" + " char *rv = NULL;\n") rlist.extend(v.get_c("rv", prefix, k)) rlist.append("}\n") if (doneOne): rlist.append("else\n") rlist.append("return %s_UNKNOWN_ENV_VALUE(handle, name);\n"%(prefix.upper())) return "".join(rlist)