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