Skip to content

Commit 70f073d

Browse files
committed
Added test for inspect expression with multiple types
1 parent e1d0c05 commit 70f073d

4 files changed

+105
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
main: () -> int = {
2+
v: std::variant<int, double> = 42.0;
3+
a: std::any = "xyzzy" as std::string;
4+
o: std::optional<int> = ();
5+
6+
test_generic(3.14);
7+
test_generic(v);
8+
test_generic(a);
9+
test_generic(o);
10+
11+
v = 1;
12+
a = 2;
13+
o = 3;
14+
test_generic(42);
15+
test_generic(v);
16+
test_generic(a);
17+
test_generic(o);
18+
}
19+
20+
test_generic: ( x: _ ) = {
21+
std::cout
22+
<< std::setw(30) << typeid(x).name()
23+
<< " value is "
24+
<< inspect x -> std::string {
25+
is int = "integer " + std::to_string(x as int);
26+
is std::string = '"' + x as std::string + '"';
27+
is _ = "not an int or string";
28+
}
29+
<< "\n";
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ----- Cpp2 support -----
2+
#define CPP2_USE_MODULES Yes
3+
#include "cpp2util.h"
4+
5+
6+
#line 1 "pure2-inspect-expression-in-generic-function-multiple-types.cpp2"
7+
[[nodiscard]] auto main() -> int;
8+
#line 20 "pure2-inspect-expression-in-generic-function-multiple-types.cpp2"
9+
auto test_generic(auto const& x) -> void;
10+
11+
//=== Cpp2 definitions ==========================================================
12+
13+
#line 1 "pure2-inspect-expression-in-generic-function-multiple-types.cpp2"
14+
[[nodiscard]] auto main() -> int{
15+
std::variant<int,double> v { 42.0 };
16+
std::any a { cpp2::as<std::string>("xyzzy") };
17+
std::optional<int> o { };
18+
19+
test_generic(3.14);
20+
test_generic(v);
21+
test_generic(a);
22+
test_generic(o);
23+
24+
v = 1;
25+
a = 2;
26+
o = 3;
27+
test_generic(42);
28+
test_generic(v);
29+
test_generic(a);
30+
test_generic(o);
31+
}
32+
33+
auto test_generic(auto const& x) -> void{
34+
std::cout
35+
<< std::setw(30) << typeid(x).name()
36+
<< " value is "
37+
<< [&] () -> std::string { auto&& __expr = x;
38+
if (cpp2::is<int>(__expr)) { if constexpr( requires{"integer " + std::to_string(cpp2::as<int>(x));} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF("integer " + std::to_string(cpp2::as<int>(x))),std::string> ) return "integer " + std::to_string(cpp2::as<int>(x)); else return std::string{}; else return std::string{}; }
39+
else if (cpp2::is<std::string>(__expr)) { if constexpr( requires{'"' + cpp2::as<std::string>(x) + '"';} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF('"' + cpp2::as<std::string>(x) + '"'),std::string> ) return '"' + cpp2::as<std::string>(x) + '"'; else return std::string{}; else return std::string{}; }
40+
else return "not an int or string"; }
41+
()
42+
<< "\n";
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
main: () -> int = {
2+
v: std::variant<int, double> = 42.0;
3+
a: std::any = "xyzzy" as std::string;
4+
o: std::optional<int> = ();
5+
6+
test_generic(3.14);
7+
test_generic(v);
8+
test_generic(a);
9+
test_generic(o);
10+
11+
v = 1;
12+
a = 2;
13+
o = 3;
14+
test_generic(42);
15+
test_generic(v);
16+
test_generic(a);
17+
test_generic(o);
18+
}
19+
20+
test_generic: ( x: _ ) = {
21+
std::cout
22+
<< std::setw(30) << typeid(x).name()
23+
<< " value is "
24+
<< inspect x -> std::string {
25+
is int = "integer " + std::to_string(x as int);
26+
is std::string = '"' + x as std::string + '"';
27+
is _ = "not an int or string";
28+
}
29+
<< "\n";
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-inspect-expression-in-generic-function-multiple-types.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)