Check-in [61998c472a]
Not logged in
Overview
SHA1 Hash:61998c472ad78728cc8416c2b76516c26f2470a1
Date: 2010-11-08 15:19:57
User: kinaba
Comment:Introduced unittest helpers (assert_eq, assert_throw, etc). Mmigration to it is not done yet.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified polemy/lex.d from [e43d60d2bdfc662b] to [783ee3b0fe58558e].

27 } 27 } 28 28 29 unittest 29 unittest 30 { 30 { 31 auto p = new LexPosition("hello.cpp", 123, 45); 31 auto p = new LexPosition("hello.cpp", 123, 45); 32 auto q = new LexPosition("hello.cpp", 123, 46); 32 auto q = new LexPosition("hello.cpp", 123, 46); 33 33 34 assert( p.filename == "hello.cpp" ); | 34 assert_eq( p.filename, "hello.cpp" ); 35 assert( p.lineno == 123 ); | 35 assert_eq( p.lineno, 123 ); 36 assert( p.column == 45 ); | 36 assert_eq( p.column, 45 ); 37 assert( to!string(p) == "hello.cpp:123:45" ); | 37 assert_eq( to!string(p), "hello.cpp:123:45" ); 38 assert( p < q ); | 38 assert_lt( p, q ); 39 assert( p != q ); | 39 assert_ne( p, q ); 40 40 41 assert( !__traits(compiles, new LexPosition) ); 41 assert( !__traits(compiles, new LexPosition) ); 42 assert( !__traits(compiles, p.filename="foo") ); 42 assert( !__traits(compiles, p.filename="foo") ); 43 assert( !__traits(compiles, p.lineno =789) ); 43 assert( !__traits(compiles, p.lineno =789) ); 44 assert( !__traits(compiles, p.column =222) ); 44 assert( !__traits(compiles, p.column =222) ); 45 } 45 } 46 46 ................................................................................................................................................................................ 63 } 63 } 64 64 65 unittest 65 unittest 66 { 66 { 67 auto p = new immutable(LexPosition)("hello.cpp", 123, 45); 67 auto p = new immutable(LexPosition)("hello.cpp", 123, 45); 68 auto t = new Token(p, "class", Token.Kind.identifier); 68 auto t = new Token(p, "class", Token.Kind.identifier); 69 69 70 assert( t.pos == p ); | 70 assert_eq( t.pos, p ); 71 assert( t.str == "class" ); | 71 assert_eq( t.str, "class" ); 72 assert( t == new Token(p, "class", Token.Kind.identifier) ); | 72 assert_eq( t, new Token(p, "class", Token.Kind.identifier) ); 73 assert( t < new Token(p, "struct", Token.Kind.identifier) ); | 73 assert_lt( t, new Token(p, "struct", Token.Kind.identifier) ); 74 74 75 assert( !__traits(compiles, new Token) ); 75 assert( !__traits(compiles, new Token) ); 76 assert( !__traits(compiles, t.pos=p) ); 76 assert( !__traits(compiles, t.pos=p) ); 77 assert( !__traits(compiles, t.str=789) ); 77 assert( !__traits(compiles, t.str=789) ); 78 } 78 } 79 79 80 /// Named Construtor for Lexer 80 /// Named Construtor for Lexer

Modified polemy/tricks.d from [b521ec130c690547] to [63ca5d91a9064dd8].

3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 * 4 * 5 * Common tricks and utilities for programming in D. 5 * Common tricks and utilities for programming in D. 6 */ 6 */ 7 module polemy.tricks; 7 module polemy.tricks; 8 static import std.array; 8 static import std.array; 9 static import std.format; 9 static import std.format; > 10 static import core.exception; 10 11 11 /// Simple Wrapper for std.format.doFormat 12 /// Simple Wrapper for std.format.doFormat 12 13 13 string sprintf(string fmt, T...)(T params) 14 string sprintf(string fmt, T...)(T params) 14 { 15 { 15 auto writer = std.array.appender!string(); 16 auto writer = std.array.appender!string(); 16 std.format.formattedWrite(writer, fmt, params); 17 std.format.formattedWrite(writer, fmt, params); ................................................................................................................................................................................ 19 20 20 unittest 21 unittest 21 { 22 { 22 assert( sprintf!"%s == %d"("1+2", 3) == "1+2 == 3" ); 23 assert( sprintf!"%s == %d"("1+2", 3) == "1+2 == 3" ); 23 assert( sprintf!"%s == %04d"("1+2", 3) == "1+2 == 0003" ); 24 assert( sprintf!"%s == %04d"("1+2", 3) == "1+2 == 0003" ); 24 } 25 } 25 26 > 27 /// Unittest helpers asserting two values are in some relation ==, !=, <, <=, >, > 28 > 29 template assertOp(string op) > 30 { > 31 void assertOp(A, B, string fn=__FILE__, int ln=__LINE__)(A a, B b, strin > 32 { > 33 try { > 34 if( mixin("a"~op~"b") ) return; > 35 } catch(Throwable e) { > 36 core.exception.onAssertErrorMsg(fn, ln, msg.length ? msg > 37 } > 38 core.exception.onAssertErrorMsg(fn, ln, msg.length ? msg : sprin > 39 } > 40 } > 41 > 42 alias assertOp!(`==`) assert_eq; > 43 alias assertOp!(`!=`) assert_ne; > 44 alias assertOp!(`<`) assert_lt; > 45 alias assertOp!(`<=`) assert_le; > 46 alias assertOp!(`>`) assert_gt; > 47 alias assertOp!(`>=`) assert_ge; > 48 > 49 /// Unittest helper that asserts an expression must throw something > 50 > 51 void assert_throw(ExceptionType, T, string fn=__FILE__, int ln=__LINE__)(lazy T > 52 { > 53 try { > 54 t(); > 55 } catch(ExceptionType) { > 56 return; > 57 } catch(Throwable e) { > 58 core.exception.onAssertErrorMsg(fn, ln, msg.length ? msg : sprin > 59 } > 60 core.exception.onAssertErrorMsg(fn, ln, msg.length ? msg : "no execption > 61 } > 62 > 63 /// Unittest helper that asserts an expression must not throw anything > 64 > 65 void assert_nothrow(ExceptionType, T, string fn=__FILE__, int ln=__LINE__)(lazy > 66 { > 67 try { > 68 t(); > 69 } catch(Throwable e) { > 70 core.exception.onAssertErrorMsg(fn, ln, msg.length ? msg : sprin > 71 } > 72 } > 73 > 74 /* [Todo] is there any way to clearnly implement "assert_compiles" and "assert_n > 75 26 /// Mixing-in the bean constructor for a class 76 /// Mixing-in the bean constructor for a class 27 77 28 /*mixin*/ template SimpleConstructor() | 78 template SimpleConstructor() 29 { 79 { 30 static if( is(typeof(super) == Object) || super.tupleof.length==0 ) 80 static if( is(typeof(super) == Object) || super.tupleof.length==0 ) 31 this( typeof(this.tupleof) params ) 81 this( typeof(this.tupleof) params ) 32 { 82 { 33 static if(this.tupleof.length>0) 83 static if(this.tupleof.length>0) 34 this.tupleof = params; 84 this.tupleof = params; 35 } 85 } 36 else 86 else 37 // this parameter list is not always desirable but should work f < 38 this( typeof(super.tupleof) ps, typeof(this.tupleof) params ) 87 this( typeof(super.tupleof) ps, typeof(this.tupleof) params ) 39 { 88 { > 89 // including (only) the direct super class members > 90 // may not always be a desirable choice, but should work 40 super(ps); 91 super(ps); 41 static if(this.tupleof.length>0) 92 static if(this.tupleof.length>0) 42 this.tupleof = params; 93 this.tupleof = params; 43 } 94 } 44 } 95 } > 96 > 97 unittest > 98 { > 99 class Temp > 100 { > 101 int x; > 102 string y; > 103 mixin SimpleConstructor; > 104 } > 105 assert_eq( (new Temp(1,"foo")).x, 1 ); > 106 assert_eq( (new Temp(1,"foo")).y, "foo" ); > 107 assert( !__traits(compiles, new Temp) ); > 108 assert( !__traits(compiles, new Temp(1)) ); > 109 assert( !__traits(compiles, new Temp("foo",1)) ); > 110 } 45 111 46 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class 112 /// Mixing-in the MOST-DERIVED-member-wise comparator for a class 47 113 48 /*mixin*/ template SimpleCompare() | 114 template SimpleCompare() 49 { 115 { 50 override bool opEquals(Object rhs_) const 116 override bool opEquals(Object rhs_) const 51 { 117 { 52 if( auto rhs = cast(typeof(this))rhs_ ) 118 if( auto rhs = cast(typeof(this))rhs_ ) 53 { 119 { 54 foreach(i,_; this.tupleof) 120 foreach(i,_; this.tupleof) 55 if( this.tupleof[i] != rhs.tupleof[i] ) 121 if( this.tupleof[i] != rhs.tupleof[i] ) ................................................................................................................................................................................ 85 class Temp 151 class Temp 86 { 152 { 87 int x; 153 int x; 88 string y; 154 string y; 89 mixin SimpleConstructor; 155 mixin SimpleConstructor; 90 mixin SimpleCompare; 156 mixin SimpleCompare; 91 } 157 } 92 assert( (new Temp(1,"foo")).x == 1 ); < 93 assert( (new Temp(1,"foo")).y == "foo" ); < 94 assert( !__traits(compiles, new Temp) ); < 95 assert( !__traits(compiles, new Temp(1)) ); < 96 assert( !__traits(compiles, new Temp("foo",1)) ); < 97 assert( new Temp(1,"foo") == new Temp(1,"foo") ); | 158 assert_eq( new Temp(1,"foo"), new Temp(1,"foo") ); 98 assert( (new Temp(1,"foo")).toHash == (new Temp(1,"foo")).toHash ); | 159 assert_eq( (new Temp(1,"foo")).toHash, (new Temp(1,"foo")).toHash ); 99 assert( new Temp(1,"foo") != new Temp(2,"foo") ); | 160 assert_ne( new Temp(1,"foo"), new Temp(2,"foo") ); 100 assert( new Temp(1,"foo") != new Temp(1,"bar") ); | 161 assert_ne( new Temp(1,"foo"), new Temp(1,"bar") ); 101 assert( new Temp(1,"foo") > new Temp(1,"bar") ); | 162 assert_gt( new Temp(1,"foo"), new Temp(1,"bar") ); 102 assert( new Temp(1,"foo") < new Temp(2,"bar") ); | 163 assert_lt( new Temp(1,"foo"), new Temp(2,"bar") ); 103 } 164 }