1 module ctpg.for_unittest; 2 3 debug(ctpg) version(unittest): 4 5 import std.typetuple : TypeTuple; 6 import std.traits : Unqual, isArray; 7 import std.range : ElementEncodingType; 8 9 import ctpg.caller : Caller; 10 import ctpg.input : Input; 11 import ctpg.macro_ : MAKE_RESULT; 12 import ctpg.parse_result : ParseResult; 13 14 15 template TestParser(alias value) 16 { 17 template build(alias kind, SrcType) 18 { 19 mixin MAKE_RESULT!q{ typeof(value) }; 20 21 Result parse(Input!SrcType input, in Caller caller) 22 { 23 Result result; 24 25 result.match = true; 26 result.nextInput = input; 27 28 static if(kind.hasValue) result.value = value; 29 30 return result; 31 } 32 } 33 } 34 35 36 auto rangize(T)(T source) if(isArray!T) 37 { 38 static struct Range 39 { 40 T source; 41 42 const pure @safe nothrow @property 43 Unqual!(ElementEncodingType!T) front(){ return source[0]; } 44 45 pure @safe nothrow @property 46 void popFront(){ source = source[1..$]; } 47 48 const pure @safe nothrow @property 49 bool empty(){ return source.length == 0; } 50 51 const pure @safe nothrow @property 52 Range save(){ return this; } 53 54 const pure @safe nothrow @property 55 size_t length(){ return source.length; } 56 57 pure @safe nothrow 58 Range opSlice(size_t begin, size_t end){ return Range(source[begin .. end]); } 59 60 const pure @safe nothrow 61 equals_t opEquals(Range rhs){ return source == rhs.source; } 62 } 63 64 return Range(source); 65 } 66 67 68 template toString (alias input) { enum toString = cast( string)input; } 69 template toWstring (alias input) { enum toWstring = cast(wstring)input; } 70 template toDstring (alias input) { enum toDstring = cast(dstring)input; } 71 template toCharRange (alias input) { enum toCharRange = (cast( string)input).rangize(); } 72 template toWcharRange(alias input) { enum toWcharRange = (cast(wstring)input).rangize(); } 73 template toDcharRange(alias input) { enum toDcharRange = (cast(dstring)input).rangize(); } 74 75 alias convs = TypeTuple!(toString, toWstring, toDstring, toCharRange, toWcharRange, toDcharRange);