1 #define SOL_CHECK_ARGUMENTS
2 
3 #include <sol.hpp>
4 #include <catch.hpp>
5 
6 TEST_CASE("operators/default", "test that generic equality operators and all sorts of equality tests can be used") {
7 	sol::state lua;
8 	lua.open_libraries(sol::lib::base);
9 
10 	struct T {};
11 	struct U {
12 		int a;
UU13 		U(int x = 20) : a(x) {}
operator ==U14 		bool operator==(const U& r) {
15 			return a == r.a;
16 		}
17 	};
18 	struct V {
19 		int a;
VV20 		V(int x = 20) : a(x) {}
operator ==V21 		bool operator==(const V& r) const {
22 			return a == r.a;
23 		}
24 	};
25 	lua.new_usertype<T>("T");
26 	lua.new_usertype<U>("U");
27 	lua.new_usertype<V>("V");
28 
29 	T t1;
30 	T& t2 = t1;
31 	T t3;
32 	U u1;
33 	U u2{ 30 };
34 	U u3;
35 	U v1;
36 	U v2{ 30 };
37 	U v3;
38 	lua["t1"] = &t1;
39 	lua["t2"] = &t2;
40 	lua["t3"] = &t3;
41 	lua["u1"] = &u1;
42 	lua["u2"] = &u2;
43 	lua["u3"] = &u3;
44 	lua["v1"] = &v1;
45 	lua["v2"] = &v2;
46 	lua["v3"] = &v3;
47 
48 	// Can only compare identity here
49 	REQUIRE_NOTHROW({
50 		lua.script("assert(t1 == t1)");
51 		lua.script("assert(t2 == t2)");
52 		lua.script("assert(t3 == t3)");
53 	});
54 	REQUIRE_NOTHROW({
55 		lua.script("assert(t1 == t2)");
56 		lua.script("assert(not (t1 == t3))");
57 		lua.script("assert(not (t2 == t3))");
58 	});
59 	// Object should compare equal to themselves
60 	// (and not invoke operator==; pointer test should be sufficient)
61 	REQUIRE_NOTHROW({
62 		lua.script("assert(u1 == u1)");
63 		lua.script("assert(u2 == u2)");
64 		lua.script("assert(u3 == u3)");
65 	});
66 	REQUIRE_NOTHROW({
67 		lua.script("assert(not (u1 == u2))");
68 		lua.script("assert(u1 == u3)");
69 		lua.script("assert(not (u2 == u3))");
70 	});
71 	// Object should compare equal to themselves
72 	// (and not invoke operator==; pointer test should be sufficient)
73 	REQUIRE_NOTHROW({
74 		lua.script("assert(v1 == v1)");
75 		lua.script("assert(v2 == v2)");
76 		lua.script("assert(v3 == v3)");
77 	});
78 	REQUIRE_NOTHROW({
79 		lua.script("assert(not (v1 == v2))");
80 		lua.script("assert(v1 == v3)");
81 		lua.script("assert(not (v2 == v3))");
82 	});
83 }