1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "sol_test.hpp"
#include <catch2/catch_all.hpp>
#include <iostream>
struct test { };
template <typename T>
struct test_t { };
namespace muh_namespace {
struct ns_test { };
namespace {
struct ns_anon_test { };
} // namespace
} // namespace muh_namespace
TEST_CASE("stack/strings", "test that strings can be roundtripped") {
sol::state lua;
sol::stack_guard luasg(lua);
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";
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 };
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 };
#ifdef _WIN32
INFO("win32 widestr");
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 };
#else
INFO("non-windows widestr");
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 };
#endif
static const std::string utf8str_s = utf8str;
static const std::u16string utf16str_s = utf16str;
static const std::u32string utf32str_s = utf32str;
static const std::wstring widestr_s = widestr;
INFO("sizeof(wchar_t): " << sizeof(wchar_t));
INFO("sizeof(char16_t): " << sizeof(char16_t));
INFO("sizeof(char32_t): " << sizeof(char32_t));
INFO("utf8str: " << utf8str);
INFO("utf8str_s: " << utf8str_s);
lua["utf8"] = utf8str;
lua["utf16"] = utf16str;
lua["utf32"] = utf32str;
lua["wide"] = widestr;
SECTION("to_utf8") {
std::string utf8_to_utf8 = lua["utf8"];
std::string utf16_to_utf8 = lua["utf16"];
std::string utf32_to_utf8 = lua["utf32"];
std::string wide_to_utf8 = lua["wide"];
REQUIRE(utf8_to_utf8 == utf8str_s);
REQUIRE(utf16_to_utf8 == utf8str_s);
REQUIRE(utf32_to_utf8 == utf8str_s);
REQUIRE(wide_to_utf8 == utf8str_s);
}
SECTION("to_wide") {
std::wstring utf8_to_wide = lua["utf8"];
std::wstring utf16_to_wide = lua["utf16"];
std::wstring utf32_to_wide = lua["utf32"];
std::wstring wide_to_wide = lua["wide"];
REQUIRE(utf8_to_wide == widestr_s);
REQUIRE(utf16_to_wide == widestr_s);
REQUIRE(utf32_to_wide == widestr_s);
REQUIRE(wide_to_wide == widestr_s);
}
SECTION("to_utf16") {
std::u16string utf8_to_utf16 = lua["utf8"];
std::u16string utf16_to_utf16 = lua["utf16"];
std::u16string utf32_to_utf16 = lua["utf32"];
std::u16string wide_to_utf16 = lua["wide"];
REQUIRE(utf8_to_utf16 == utf16str_s);
REQUIRE(utf16_to_utf16 == utf16str_s);
REQUIRE(utf32_to_utf16 == utf16str_s);
REQUIRE(wide_to_utf16 == utf16str_s);
}
SECTION("to_utf32") {
std::u32string utf8_to_utf32 = lua["utf8"];
std::u32string utf16_to_utf32 = lua["utf16"];
std::u32string utf32_to_utf32 = lua["utf32"];
std::u32string wide_to_utf32 = lua["wide"];
REQUIRE(utf8_to_utf32 == utf32str_s);
REQUIRE(utf16_to_utf32 == utf32str_s);
REQUIRE(utf32_to_utf32 == utf32str_s);
REQUIRE(wide_to_utf32 == utf32str_s);
}
SECTION("to_char32_t") {
char32_t utf8_to_char32 = lua["utf8"];
char32_t utf16_to_char32 = lua["utf16"];
char32_t utf32_to_char32 = lua["utf32"];
char32_t wide_to_char32 = lua["wide"];
REQUIRE(utf8_to_char32 == utf32str[0]);
REQUIRE(utf16_to_char32 == utf32str[0]);
REQUIRE(utf32_to_char32 == utf32str[0]);
REQUIRE(wide_to_char32 == utf32str[0]);
}
SECTION("to_char16_t") {
char16_t utf8_to_char16 = lua["utf8"];
char16_t utf16_to_char16 = lua["utf16"];
char16_t utf32_to_char16 = lua["utf32"];
char16_t wide_to_char16 = lua["wide"];
REQUIRE(utf8_to_char16 == utf16str[0]);
REQUIRE(utf16_to_char16 == utf16str[0]);
REQUIRE(utf32_to_char16 == utf16str[0]);
REQUIRE(wide_to_char16 == utf16str[0]);
}
SECTION("to_char") {
char utf8_to_char = lua["utf8"].get<char>();
char utf16_to_char = lua["utf16"].get<char>();
char utf32_to_char = lua["utf32"].get<char>();
char wide_to_char = lua["wide"].get<char>();
REQUIRE(utf8_to_char == utf8str[0]);
REQUIRE(utf16_to_char == utf8str[0]);
REQUIRE(utf32_to_char == utf8str[0]);
REQUIRE(wide_to_char == utf8str[0]);
}
}
TEST_CASE("detail/demangling", "test some basic demangling cases") {
std::string teststr = sol::detail::short_demangle<test>();
std::string nsteststr = sol::detail::short_demangle<muh_namespace::ns_test>();
std::string nsateststr = sol::detail::short_demangle<muh_namespace::ns_anon_test>();
REQUIRE(teststr == "test");
REQUIRE(nsteststr == "ns_test");
REQUIRE(nsateststr == "ns_anon_test");
}
TEST_CASE("object/string-pushers", "test some basic string pushers with in_place constructor") {
sol::state lua;
sol::object ocs(lua, sol::in_place, "bark\0bark", 9u);
sol::object os(lua, sol::in_place_type<std::string>, std::string("bark\0bark", 9), 8u);
sol::object osv(lua, sol::in_place_type<sol::string_view>, sol::string_view("woofwoof", 8), 8u);
bool test1 = ocs.as<std::string>() == std::string("bark\0bark", 9);
bool test2 = os.as<std::string>() == std::string("bark\0bar", 8);
bool test3 = osv.as<std::string>() == std::string("woofwoof", 8);
REQUIRE(ocs.get_type() == sol::type::string);
REQUIRE(ocs.is<std::string>());
REQUIRE(ocs.is<sol::string_view>());
REQUIRE(os.get_type() == sol::type::string);
REQUIRE(os.is<std::string>());
REQUIRE(os.is<sol::string_view>());
REQUIRE(osv.get_type() == sol::type::string);
REQUIRE(osv.is<std::string>());
REQUIRE(osv.is<sol::string_view>());
REQUIRE(test1);
REQUIRE(test2);
REQUIRE(test3);
}
TEST_CASE("strings/non const c strings", "push non const qualified c strings as strings") {
sol::state lua;
char cbark[] = "bark";
char* bark = cbark;
lua["bark"] = bark;
sol::type t = lua["bark"].get_type();
std::string lbark = lua["bark"];
REQUIRE((t == sol::type::string));
REQUIRE((bark == std::string("bark")));
}
|