summaryrefslogtreecommitdiff
path: root/tools/test_cinit.lua
diff options
context:
space:
mode:
authorJosh Haberman <haberman@google.com>2013-02-15 16:27:18 -0800
committerJosh Haberman <haberman@google.com>2013-02-15 16:27:18 -0800
commit7d3e2bd2c4cfd1296d1d6f996d7548de26540d41 (patch)
treeb4b35967b3322c65cfb1a32220e8718de09d85fc /tools/test_cinit.lua
parentea198bdcf947ba4bd51474bdd4f7b82b5e4cf41d (diff)
Sync with 8 months of Google-internal development.
Many things have changed and been simplified. The memory-management story for upb_def and upb_handlers is much more robust; upb_def and upb_handlers should be fairly stable interfaces now. There is still much work to do for the runtime component (upb_sink).
Diffstat (limited to 'tools/test_cinit.lua')
-rw-r--r--tools/test_cinit.lua78
1 files changed, 78 insertions, 0 deletions
diff --git a/tools/test_cinit.lua b/tools/test_cinit.lua
new file mode 100644
index 0000000..bb7977f
--- /dev/null
+++ b/tools/test_cinit.lua
@@ -0,0 +1,78 @@
+--[[
+
+ upb - a minimalist implementation of protocol buffers.
+
+ Copyright (c) 2012 Google Inc. See LICENSE for details.
+ Author: Josh Haberman <jhaberman@gmail.com>
+
+ Tests for dump_cinit.lua. Runs first in a mode that generates
+ some C code for an extension. The C code is compiled and then
+ loaded by a second invocation of the test which checks that the
+ generated defs are as expected.
+
+--]]
+
+local dump_cinit = require "dump_cinit"
+local upb = require "upb"
+
+-- Once APIs for loading descriptors are fleshed out, we should replace this
+-- with a descriptor for a meaty protobuf like descriptor.proto.
+local symtab = upb.SymbolTable{
+ upb.EnumDef{full_name = "MyEnum",
+ values = {
+ {"FOO", 1},
+ {"BAR", 77}
+ }
+ },
+ upb.MessageDef{full_name = "MyMessage",
+ fields = {
+ upb.FieldDef{label = upb.LABEL_REQUIRED, name = "field1", number = 1,
+ type = upb.TYPE_INT32},
+ upb.FieldDef{label = upb.LABEL_REPEATED, name = "field2", number = 2,
+ type = upb.TYPE_ENUM, subdef_name = ".MyEnum"},
+ upb.FieldDef{name = "field3", number = 3, type = upb.TYPE_MESSAGE,
+ subdef_name = ".MyMessage"}
+ }
+ }
+}
+
+if arg[1] == "generate" then
+ local f = assert(io.open(arg[2], "w"))
+ local f_h = assert(io.open(arg[2] .. ".h", "w"))
+ local appendc = dump_cinit.file_appender(f)
+ local appendh = dump_cinit.file_appender(f_h)
+ f:write('#include "lua.h"\n')
+ f:write('#define ELEMENTS(array) (sizeof(array)/sizeof(*array))\n')
+ f:write('#include "bindings/lua/upb.h"\n')
+ dump_cinit.dump_defs(symtab, "test", appendh, appendc)
+ f:write([[int luaopen_staticdefs(lua_State *L) {
+ lua_newtable(L);
+ for (int i = 0; i < ELEMENTS(test_msgs); i++) {
+ lupb_def_pushnewrapper(L, upb_upcast(&test_msgs[i]), NULL);
+ lua_rawseti(L, -2, i + 1);
+ }
+ for (int i = 0; i < ELEMENTS(test_enums); i++) {
+ lupb_def_pushnewrapper(L, upb_upcast(&test_enums[i]), NULL);
+ lua_rawseti(L, -2, ELEMENTS(test_msgs) + i + 1);
+ }
+ return 1;
+ }]])
+ f_h:close()
+ f:close()
+elseif arg[1] == "test" then
+ local staticdefs = require "staticdefs"
+
+ local msg = assert(staticdefs[1])
+ local enum = assert(staticdefs[2])
+ local f2 = assert(msg:field("field2"))
+ assert(msg:def_type() == upb.DEF_MSG)
+ assert(msg:full_name() == "MyMessage")
+ assert(enum:def_type() == upb.DEF_ENUM)
+ assert(enum:full_name() == "MyEnum")
+ assert(enum:value("FOO") == 1)
+ assert(f2:name() == "field2")
+ assert(f2:msgdef() == msg)
+ assert(f2:subdef() == enum)
+else
+ error("Unknown operation " .. arg[1])
+end
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback