1 module ut.conv.misc;
2 
3 import test;
4 import xlld.conv.misc;
5 import xlld.conv.to: toXlOper;
6 
7 @("isUserStruct")
8 @safe pure unittest {
9     import std.datetime: DateTime;
10     import std.typecons: Tuple;
11     import xlld.any: Any;
12 
13     static struct Foo {}
14 
15     static assert( isUserStruct!Foo);
16     static assert(!isUserStruct!Any);
17     static assert(!isUserStruct!DateTime);
18     static assert(!isUserStruct!(Tuple!(int, int)));
19     static assert(!isUserStruct!(Tuple!(int, string)));
20     static assert(!isUserStruct!(Tuple!(int, Foo, double)));
21 }
22 
23 ///
24 @("dup")
25 @safe unittest {
26     auto int_ = 42.toXlOper(theGC);
27     int_.dup(theGC).shouldEqualDlang(42);
28 
29     auto double_ = (33.3).toXlOper(theGC);
30     double_.dup(theGC).shouldEqualDlang(33.3);
31 
32     auto string_ = "foobar".toXlOper(theGC);
33     string_.dup(theGC).shouldEqualDlang("foobar");
34 
35     auto array = () @trusted {
36         return [
37             ["foo", "bar", "baz"],
38             ["quux", "toto", "brzz"]
39         ]
40         .toXlOper(theGC);
41     }();
42 
43     array.dup(theGC).shouldEqualDlang(
44         [
45             ["foo", "bar", "baz"],
46             ["quux", "toto", "brzz"],
47         ]
48     );
49 }
50 
51 @("dup string allocator fails")
52 @safe unittest {
53     auto allocator = FailingAllocator();
54     "foo".toXlOper(theGC).dup(allocator).shouldThrowWithMessage("Failed to allocate memory in dup");
55 }
56 
57 @("dup multi allocator fails")
58 @safe unittest {
59     auto allocator = FailingAllocator();
60     auto oper = () @trusted { return [33.3].toXlOper(theGC); }();
61     oper.dup(allocator).shouldThrowWithMessage("Failed to allocate memory in dup");
62 }
63 
64 ///
65 @("operStringLength")
66 unittest {
67     import std.experimental.allocator.mallocator: Mallocator;
68     auto oper = "foobar".toXlOper(theGC);
69     const length = () @nogc { return operStringLength(oper); }();
70     length.shouldEqual(6);
71 }
72 
73 
74 ///
75 @("multi")
76 @safe unittest {
77     auto allocator = FailingAllocator();
78     multi(2, 3, allocator).shouldThrowWithMessage("Failed to allocate memory for multi oper");
79 }