1 require("import") -- the import fn
2 import("li_std_string") -- import lib
4 for k,v in pairs(li_std_string) do _G[k]=v end -- move to global
6 -- catch "undefined" global variables
7 setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
9 -- helper to check type
10 function is_std_string(s)
11 return type(s)=='userdata' and swig_type(s)=='std::string *'
14 -- std::string by value is just a Lua string
16 assert(type(s)=="string" and s=="foo")
18 -- std::string by const ref is also just a Lua string
19 s=test_const_reference("foo")
20 assert(type(s)=="string" and s=="foo")
22 -- std:string* is an object
23 obj=test_pointer_out()
24 assert(is_std_string(obj) and obj:c_str()=="x") -- check type & value
26 test_pointer(obj) -- this wants an object
28 cobj=test_const_pointer_out()
29 assert(is_std_string(cobj) and cobj:c_str()=="x") -- check type & value
31 test_const_pointer(cobj)
33 -- this shouldnt work, but it does
34 -- swig doesnt appear to diff between const object ptrs & object ptrs very well
35 test_pointer(cobj) -- this wants an non const object (give it a const one!)
37 -- refs are also wrappered as ptrs (unless the correct typemaps are applied)
38 robj=test_reference_out()
39 assert(is_std_string(robj) and robj:c_str()=="test_reference_out message") -- check type & value
42 test_reference(obj) -- object ptr is ok
43 test_reference(cobj) -- obj const ptr is also ok
46 ok,ex=pcall(test_throw)
47 assert(ok==false and type(ex)=="string") -- failed & threw string
49 ok,ex=pcall(test_const_reference_throw)
50 assert(ok==false and type(ex)=="string") -- failed & threw string
52 -- const ptrs are now converted to lua strings
53 -- they used to be std::string*
54 ok,ex=pcall(test_const_pointer_throw)
55 assert(ok==false and type(ex)=="string") -- failed & threw object
57 -- ditto non const ptrs
58 ok,ex=pcall(test_pointer_throw)
59 assert(ok==false and type(ex)=="string") -- failed & threw object
61 -- testing std::string variables
64 assert (li_std_string.GlobalString2 == "global string 2")
65 li_std_string.GlobalString2 = s
66 assert (li_std_string.GlobalString2 == s)
67 assert (li_std_string.ConstGlobalString == "const global string")
70 myStructure = Structure()
71 assert(myStructure.MemberString2 == "member string 2")
72 myStructure.MemberString2 = s
73 assert (myStructure.MemberString2 == s)
74 assert (myStructure.ConstMemberString == "const member string")
76 assert (li_std_string.Structure_StaticMemberString2 == "static member string 2")
77 li_std_string.Structure_StaticMemberString2 = s
78 assert (li_std_string.Structure_StaticMemberString2 == s)
79 assert (li_std_string.Structure_ConstStaticMemberString == "const static member string")
82 -- testing the structure (these are some old tests which predated the std::string variable tests above)
85 assert(type(struc.MemberString2)=="string") -- typemaps make this a string
86 assert(type(struc.ConstMemberString)=="string")
88 -- set a const (should fail with error)
89 assert(pcall(function () struc.ConstMemberString="c" end)==false)
90 --print(struc.MemberString:data(),struc.MemberString2,struc.ConstMemberString:data())
93 assert(type(struc.MemberString2)=="string") -- typemaps make this a string
94 assert(type(struc.ConstMemberString)=="string")
96 -- for static types: they are really variables,
97 -- so we must still use the module name
100 assert(type(li_std_string.Structure_StaticMemberString2)=="string")
101 assert(type(li_std_string.Structure_ConstStaticMemberString)=="string")
103 -- try setting (should fail with error)
104 --li_std_string.Structure_StaticMemberString2='e'
105 assert(pcall(function () li_std_string.Structure_ConstStaticMemberString='f' end)==false)
106 --[[print(li_std_string.Structure_StaticMemberString:data(),
107 li_std_string.Structure_StaticMemberString2,
108 li_std_string.Structure_ConstStaticMemberString:data())]]
110 -- check static type again
111 assert(type(li_std_string.Structure_StaticMemberString)=="string")
112 assert(type(li_std_string.Structure_StaticMemberString2)=="string")
113 assert(type(li_std_string.Structure_ConstStaticMemberString)=="string")