Upstream version 1.3.40
[profile/ivi/swig.git] / Examples / test-suite / lua / li_std_string_runme.lua
1 require("import")       -- the import fn
2 import("li_std_string") -- import lib
3
4 for k,v in pairs(li_std_string) do _G[k]=v end -- move to global
5
6 -- catch "undefined" global variables
7 setmetatable(getfenv(),{__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
8
9 -- helper to check type
10 function is_std_string(s) 
11         return type(s)=='userdata' and swig_type(s)=='std::string *'
12 end
13
14 -- std::string by value is just a Lua string
15 s=test_value("foo")
16 assert(type(s)=="string" and s=="foo")
17
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")
21
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
25
26 test_pointer(obj)       -- this wants an object
27
28 cobj=test_const_pointer_out()
29 assert(is_std_string(cobj) and cobj:c_str()=="x")       -- check type & value
30
31 test_const_pointer(cobj)
32
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!)
36
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
40
41 test_reference(robj)
42 test_reference(obj)     -- object ptr is ok
43 test_reference(cobj)    -- obj const ptr is also ok
44
45 -- throwing string
46 ok,ex=pcall(test_throw)
47 assert(ok==false and type(ex)=="string")        -- failed & threw string
48
49 ok,ex=pcall(test_const_reference_throw)
50 assert(ok==false and type(ex)=="string")        -- failed & threw string
51
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
56
57 -- ditto non const ptrs 
58 ok,ex=pcall(test_pointer_throw)
59 assert(ok==false and type(ex)=="string")        -- failed & threw object
60
61 -- testing std::string variables
62 -- Global variables
63 s = "initial string"
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")
68
69 -- Member variables
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")
75
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")
80
81
82 -- testing the structure (these are some old tests which predated the std::string variable tests above)
83 struc=Structure()
84
85 assert(type(struc.MemberString2)=="string") -- typemaps make this a string
86 assert(type(struc.ConstMemberString)=="string")
87
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())
91
92 --check type again
93 assert(type(struc.MemberString2)=="string") -- typemaps make this a string
94 assert(type(struc.ConstMemberString)=="string")
95
96 -- for static types: they are really variables, 
97 -- so we must still use the module name
98
99 -- check static type
100 assert(type(li_std_string.Structure_StaticMemberString2)=="string")
101 assert(type(li_std_string.Structure_ConstStaticMemberString)=="string")
102
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())]]
109
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")