1 #define SOL_CHECK_ARGUMENTS
2 
3 #include <catch.hpp>
4 #include <sol.hpp>
5 
6 #include <iostream>
7 
8 struct test {};
9 template <typename T>
10 struct test_t {};
11 
12 namespace muh_namespace {
13 	struct ns_test {};
14 
15 	namespace {
16 		struct ns_anon_test {};
17 	}
18 }
19 
20 // There isn't a single library roundtripping which codecvt works on. We'll do the nitty-gritty of it later...
21 TEST_CASE("stack/strings", "test that strings can be roundtripped") {
22 	sol::state lua;
23 
24 	static const char utf8str[] = "\xF0\x9F\x8D\x8C\x20\xE6\x99\xA5\x20\x46\x6F\x6F\x20\xC2\xA9\x20\x62\x61\x72\x20\xF0\x9D\x8C\x86\x20\x62\x61\x7A\x20\xE2\x98\x83\x20\x71\x75\x78";
25 	static const char16_t utf16str[] = { 0xD83C, 0xDF4C, 0x20, 0x6665, 0x20, 0x46, 0x6F, 0x6F, 0x20, 0xA9, 0x20, 0x62, 0x61, 0x72, 0x20, 0xD834, 0xDF06, 0x20, 0x62, 0x61, 0x7A, 0x20, 0x2603, 0x20, 0x71, 0x75, 0x78, 0x00 };
26 	static const char32_t utf32str[] = { 0x1F34C, 0x0020, 0x6665, 0x0020, 0x0046, 0x006F, 0x006F, 0x0020, 0x00A9, 0x0020, 0x0062, 0x0061, 0x0072, 0x0020, 0x1D306, 0x0020, 0x0062, 0x0061, 0x007A, 0x0020, 0x2603, 0x0020, 0x0071, 0x0075, 0x0078, 0x00 };
27 #ifdef _WIN32
28 	INFO("win32 widestr");
29 	static const wchar_t widestr[] = { 0xD83C, 0xDF4C, 0x20, 0x6665, 0x20, 0x46, 0x6F, 0x6F, 0x20, 0xA9, 0x20, 0x62, 0x61, 0x72, 0x20, 0xD834, 0xDF06, 0x20, 0x62, 0x61, 0x7A, 0x20, 0x2603, 0x20, 0x71, 0x75, 0x78, 0x00 };
30 #else
31 	INFO("non-windows widestr");
32 	static const wchar_t widestr[] = { 0x1F34C, 0x0020, 0x6665, 0x0020, 0x0046, 0x006F, 0x006F, 0x0020, 0x00A9, 0x0020, 0x0062, 0x0061, 0x0072, 0x0020, 0x1D306, 0x0020, 0x0062, 0x0061, 0x007A, 0x0020, 0x2603, 0x0020, 0x0071, 0x0075, 0x0078, 0x00 };
33 #endif
34 	static const std::string utf8str_s = utf8str;
35 	static const std::u16string utf16str_s = utf16str;
36 	static const std::u32string utf32str_s = utf32str;
37 	static const std::wstring widestr_s = widestr;
38 
39 #ifdef SOL_CODECVT_SUPPORT
40 	INFO("sizeof(wchar_t): " << sizeof(wchar_t));
41 	INFO("sizeof(char16_t): " << sizeof(char16_t));
42 	INFO("sizeof(char32_t): " << sizeof(char32_t));
43 	INFO("utf8str: " << utf8str);
44 	INFO("utf8str_s: " << utf8str_s);
45 
46 	lua["utf8"] = utf8str;
47 	lua["utf16"] = utf16str;
48 	lua["utf32"] = utf32str;
49 	lua["wide"] = widestr;
50 
51 	std::string utf8_to_utf8 = lua["utf8"];
52 	std::string utf16_to_utf8 = lua["utf16"];
53 	std::string utf32_to_utf8 = lua["utf32"];
54 	std::string wide_to_utf8 = lua["wide"];
55 
56 	REQUIRE(utf8_to_utf8 == utf8str_s);
57 	REQUIRE(utf16_to_utf8 == utf8str_s);
58 	REQUIRE(utf32_to_utf8 == utf8str_s);
59 	REQUIRE(wide_to_utf8 == utf8str_s);
60 
61 	std::wstring utf8_to_wide = lua["utf8"];
62 	std::wstring utf16_to_wide = lua["utf16"];
63 	std::wstring utf32_to_wide = lua["utf32"];
64 	std::wstring wide_to_wide = lua["wide"];
65 
66 	REQUIRE(utf8_to_wide == widestr_s);
67 	REQUIRE(utf16_to_wide == widestr_s);
68 	REQUIRE(utf32_to_wide == widestr_s);
69 	REQUIRE(wide_to_wide == widestr_s);
70 
71 	std::u16string utf8_to_utf16 = lua["utf8"];
72 	std::u16string utf16_to_utf16 = lua["utf16"];
73 	std::u16string utf32_to_utf16 = lua["utf32"];
74 	std::u16string wide_to_utf16 = lua["wide"];
75 
76 	REQUIRE(utf8_to_utf16 == utf16str_s);
77 	REQUIRE(utf16_to_utf16 == utf16str_s);
78 	REQUIRE(utf32_to_utf16 == utf16str_s);
79 	REQUIRE(wide_to_utf16 == utf16str_s);
80 
81 	std::u32string utf8_to_utf32 = lua["utf8"];
82 	std::u32string utf16_to_utf32 = lua["utf16"];
83 	std::u32string utf32_to_utf32 = lua["utf32"];
84 	std::u32string wide_to_utf32 = lua["wide"];
85 
86 	REQUIRE(utf8_to_utf32 == utf32str_s);
87 	REQUIRE(utf16_to_utf32 == utf32str_s);
88 	REQUIRE(utf32_to_utf32 == utf32str_s);
89 	REQUIRE(wide_to_utf32 == utf32str_s);
90 
91 	char32_t utf8_to_char32 = lua["utf8"];
92 	char32_t utf16_to_char32 = lua["utf16"];
93 	char32_t utf32_to_char32 = lua["utf32"];
94 	char32_t wide_to_char32 = lua["wide"];
95 
96 	REQUIRE(utf8_to_char32 == utf32str[0]);
97 	REQUIRE(utf16_to_char32 == utf32str[0]);
98 	REQUIRE(utf32_to_char32 == utf32str[0]);
99 	REQUIRE(wide_to_char32 == utf32str[0]);
100 #endif // codecvt support
101 }
102 
103 TEST_CASE("detail/demangling", "test some basic demangling cases") {
104 	std::string teststr = sol::detail::short_demangle<test>();
105 	std::string nsteststr = sol::detail::short_demangle<muh_namespace::ns_test>();
106 	std::string nsateststr = sol::detail::short_demangle<muh_namespace::ns_anon_test>();
107 
108 	REQUIRE(teststr == "test");
109 	REQUIRE(nsteststr == "ns_test");
110 	REQUIRE(nsateststr == "ns_anon_test");
111 }
112 
113 TEST_CASE("object/string-pushers", "test some basic string pushers with in_place constructor") {
114 	sol::state lua;
115 
116 	sol::object ocs(lua, sol::in_place, "bark\0bark", 9);
117 	sol::object os(lua, sol::in_place<std::string>, std::string("bark\0bark", 9), 8);
118 	bool test1 = os.as<std::string>() == std::string("bark\0bar", 8);
119 	bool test2 = ocs.as<std::string>() == std::string("bark\0bark", 9);
120 	REQUIRE(test1);
121 	REQUIRE(test2);
122 }
123