Hex Artifact Content
Not logged in

Artifact de7560f1ecccee67725f81e1dbeac6773bb97779:


0000: 2f 2a 2a 0a 20 2a 20 41 75 74 68 6f 72 73 3a 20  /**. * Authors: 
0010: 6b 2e 69 6e 61 62 61 0a 20 2a 20 4c 69 63 65 6e  k.inaba. * Licen
0020: 73 65 3a 20 4e 59 53 4c 20 30 2e 39 39 38 32 20  se: NYSL 0.9982 
0030: 68 74 74 70 3a 2f 2f 77 77 77 2e 6b 6d 6f 6e 6f  http://www.kmono
0040: 73 2e 6e 65 74 2f 6e 79 73 6c 2f 0a 20 2a 0a 20  s.net/nysl/. *. 
0050: 2a 20 55 6e 69 74 74 65 73 74 20 68 65 6c 70 65  * Unittest helpe
0060: 72 73 2e 0a 20 2a 2f 0a 6d 6f 64 75 6c 65 20 74  rs.. */.module t
0070: 72 69 63 6b 73 2e 74 65 73 74 3b 0d 0a 69 6d 70  ricks.test;..imp
0080: 6f 72 74 20 73 74 64 2e 63 6f 6e 76 20 3a 20 74  ort std.conv : t
0090: 6f 3b 0a 69 6d 70 6f 72 74 20 63 6f 72 65 2e 65  o;.import core.e
00a0: 78 63 65 70 74 69 6f 6e 3b 0a 0a 2f 2f 2f 20 55  xception;../// U
00b0: 6e 69 74 74 65 73 74 20 68 65 6c 70 65 72 20 74  nittest helper t
00c0: 68 61 74 20 61 73 73 65 72 74 73 20 61 6e 20 65  hat asserts an e
00d0: 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 74  xpression must t
00e0: 68 72 6f 77 20 73 6f 6d 65 74 68 69 6e 67 0a 0a  hrow something..
00f0: 76 6f 69 64 20 61 73 73 65 72 74 5f 74 68 72 6f  void assert_thro
0100: 77 28 45 78 63 65 70 74 69 6f 6e 54 79 70 65 2c  w(ExceptionType,
0110: 20 54 2c 20 73 74 72 69 6e 67 20 66 6e 3d 5f 5f   T, string fn=__
0120: 46 49 4c 45 5f 5f 2c 20 73 69 7a 65 5f 74 20 6c  FILE__, size_t l
0130: 6e 3d 5f 5f 4c 49 4e 45 5f 5f 29 28 6c 61 7a 79  n=__LINE__)(lazy
0140: 20 54 20 74 2c 20 73 74 72 69 6e 67 20 6d 73 67   T t, string msg
0150: 3d 22 22 29 0a 7b 0a 09 74 72 79 0a 09 09 7b 20  ="").{..try...{ 
0160: 74 28 29 3b 20 7d 0a 09 63 61 74 63 68 28 45 78  t(); }..catch(Ex
0170: 63 65 70 74 69 6f 6e 54 79 70 65 29 0a 09 09 7b  ceptionType)...{
0180: 20 72 65 74 75 72 6e 3b 20 7d 0a 09 63 61 74 63   return; }..catc
0190: 68 28 54 68 72 6f 77 61 62 6c 65 20 65 29 0a 09  h(Throwable e)..
01a0: 09 7b 20 6f 6e 41 73 73 65 72 74 45 72 72 6f 72  .{ onAssertError
01b0: 4d 73 67 28 66 6e 2c 20 6c 6e 2c 20 6d 73 67 2e  Msg(fn, ln, msg.
01c0: 6c 65 6e 67 74 68 20 3f 20 6d 73 67 20 3a 20 22  length ? msg : "
01d0: 65 78 63 65 70 74 69 6f 6e 20 5b 22 7e 65 2e 74  exception ["~e.t
01e0: 6f 53 74 72 69 6e 67 28 29 7e 22 5d 22 29 3b 20  oString()~"]"); 
01f0: 7d 0a 09 6f 6e 41 73 73 65 72 74 45 72 72 6f 72  }..onAssertError
0200: 4d 73 67 28 66 6e 2c 20 6c 6e 2c 20 6d 73 67 2e  Msg(fn, ln, msg.
0210: 6c 65 6e 67 74 68 20 3f 20 6d 73 67 20 3a 20 22  length ? msg : "
0220: 6e 6f 20 65 78 65 63 70 74 69 6f 6e 22 29 3b 0a  no execption");.
0230: 7d 0a 0a 2f 2f 2f 20 55 6e 69 74 74 65 73 74 20  }../// Unittest 
0240: 68 65 6c 70 65 72 20 74 68 61 74 20 61 73 73 65  helper that asse
0250: 72 74 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f  rts an expressio
0260: 6e 20 6d 75 73 74 20 6e 6f 74 20 74 68 72 6f 77  n must not throw
0270: 20 61 6e 79 74 68 69 6e 67 0a 0a 61 75 74 6f 20   anything..auto 
0280: 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 54  assert_nothrow(T
0290: 2c 20 73 74 72 69 6e 67 20 66 6e 3d 5f 5f 46 49  , string fn=__FI
02a0: 4c 45 5f 5f 2c 20 73 69 7a 65 5f 74 20 6c 6e 3d  LE__, size_t ln=
02b0: 5f 5f 4c 49 4e 45 5f 5f 29 28 6c 61 7a 79 20 54  __LINE__)(lazy T
02c0: 20 74 2c 20 73 74 72 69 6e 67 20 6d 73 67 3d 22   t, string msg="
02d0: 22 29 0a 7b 0a 09 74 72 79 0a 09 09 7b 20 72 65  ").{..try...{ re
02e0: 74 75 72 6e 20 74 28 29 3b 20 7d 0a 09 63 61 74  turn t(); }..cat
02f0: 63 68 28 54 68 72 6f 77 61 62 6c 65 20 65 29 0a  ch(Throwable e).
0300: 09 09 7b 20 6f 6e 41 73 73 65 72 74 45 72 72 6f  ..{ onAssertErro
0310: 72 4d 73 67 28 66 6e 2c 20 6c 6e 2c 20 6d 73 67  rMsg(fn, ln, msg
0320: 2e 6c 65 6e 67 74 68 20 3f 20 6d 73 67 20 3a 20  .length ? msg : 
0330: 22 65 78 63 65 70 74 69 6f 6e 20 5b 22 7e 65 2e  "exception ["~e.
0340: 74 6f 53 74 72 69 6e 67 28 29 7e 22 5d 22 29 3b  toString()~"]");
0350: 20 7d 0d 0a 09 61 73 73 65 72 74 28 66 61 6c 73   }...assert(fals
0360: 65 29 3b 0a 7d 0a 0a 75 6e 69 74 74 65 73 74 0a  e);.}..unittest.
0370: 7b 0a 09 61 75 74 6f 20 65 72 72 6f 72 20 3d 20  {..auto error = 
0380: 7b 74 68 72 6f 77 20 6e 65 77 20 45 72 72 6f 72  {throw new Error
0390: 28 22 68 65 6c 6c 6f 22 29 3b 7d 3b 0a 09 61 75  ("hello");};..au
03a0: 74 6f 20 6e 6f 74 68 69 6e 67 20 3d 20 28 29 7b  to nothing = (){
03b0: 7d 3b 0a 09 61 75 74 6f 20 61 73 73 65 72 74 45  };..auto assertE
03c0: 72 72 6f 72 20 3d 20 7b 61 73 73 65 72 74 28 30  rror = {assert(0
03d0: 29 3b 7d 3b 0a 0a 09 61 73 73 65 72 74 5f 6e 6f  );};...assert_no
03e0: 74 68 72 6f 77 20 20 20 20 20 20 20 20 20 20 28  throw          (
03f0: 20 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28   assert_nothrow(
0400: 6e 6f 74 68 69 6e 67 28 29 29 20 29 3b 0a 09 61  nothing()) );..a
0410: 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65  ssert_throw!Asse
0420: 72 74 45 72 72 6f 72 28 20 61 73 73 65 72 74 5f  rtError( assert_
0430: 6e 6f 74 68 72 6f 77 28 65 72 72 6f 72 28 29 29  nothrow(error())
0440: 20 29 3b 0a 09 61 73 73 65 72 74 5f 74 68 72 6f   );..assert_thro
0450: 77 21 41 73 73 65 72 74 45 72 72 6f 72 28 20 61  w!AssertError( a
0460: 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 61 73  ssert_nothrow(as
0470: 73 65 72 74 45 72 72 6f 72 28 29 29 20 29 3b 0a  sertError()) );.
0480: 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  ..assert_nothrow
0490: 20 20 20 20 20 20 20 20 20 20 28 20 61 73 73 65            ( asse
04a0: 72 74 5f 74 68 72 6f 77 21 45 72 72 6f 72 28 65  rt_throw!Error(e
04b0: 72 72 6f 72 28 29 29 20 29 3b 0a 09 61 73 73 65  rror()) );..asse
04c0: 72 74 5f 74 68 72 6f 77 21 41 73 73 65 72 74 45  rt_throw!AssertE
04d0: 72 72 6f 72 28 20 61 73 73 65 72 74 5f 74 68 72  rror( assert_thr
04e0: 6f 77 21 45 72 72 6f 72 28 6e 6f 74 68 69 6e 67  ow!Error(nothing
04f0: 28 29 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 6e  ()) );..assert_n
0500: 6f 74 68 72 6f 77 20 20 20 20 20 20 20 20 20 20  othrow          
0510: 28 20 61 73 73 65 72 74 5f 74 68 72 6f 77 21 45  ( assert_throw!E
0520: 72 72 6f 72 28 61 73 73 65 72 74 45 72 72 6f 72  rror(assertError
0530: 28 29 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 74  ()) );..assert_t
0540: 68 72 6f 77 21 41 73 73 65 72 74 45 72 72 6f 72  hrow!AssertError
0550: 28 20 61 73 73 65 72 74 5f 74 68 72 6f 77 21 41  ( assert_throw!A
0560: 73 73 65 72 74 45 72 72 6f 72 28 65 72 72 6f 72  ssertError(error
0570: 28 29 29 20 29 3b 0a 7d 0a 0a 2f 2f 2f 20 55 6e  ()) );.}../// Un
0580: 69 74 74 65 73 74 20 68 65 6c 70 65 72 73 20 61  ittest helpers a
0590: 73 73 65 72 74 69 6e 67 20 74 77 6f 20 76 61 6c  sserting two val
05a0: 75 65 73 20 61 72 65 20 69 6e 20 73 6f 6d 65 20  ues are in some 
05b0: 72 65 6c 61 74 69 6f 6e 20 3d 3d 2c 20 21 3d 2c  relation ==, !=,
05c0: 20 3c 2c 20 3c 3d 2c 20 3e 2c 20 3e 3d 0a 0a 74   <, <=, >, >=..t
05d0: 65 6d 70 6c 61 74 65 20 61 73 73 65 72 74 4f 70  emplate assertOp
05e0: 28 73 74 72 69 6e 67 20 6f 70 29 0a 7b 0a 09 76  (string op).{..v
05f0: 6f 69 64 20 61 73 73 65 72 74 4f 70 28 41 2c 20  oid assertOp(A, 
0600: 42 2c 20 73 74 72 69 6e 67 20 66 6e 3d 5f 5f 46  B, string fn=__F
0610: 49 4c 45 5f 5f 2c 20 73 69 7a 65 5f 74 20 6c 6e  ILE__, size_t ln
0620: 3d 5f 5f 4c 49 4e 45 5f 5f 29 28 41 20 61 2c 20  =__LINE__)(A a, 
0630: 42 20 62 2c 20 73 74 72 69 6e 67 20 6d 73 67 3d  B b, string msg=
0640: 22 22 29 0a 09 7b 0a 09 09 74 72 79 0a 09 09 09  "")..{...try....
0650: 7b 20 69 66 28 20 6d 69 78 69 6e 28 22 61 22 7e  { if( mixin("a"~
0660: 6f 70 7e 22 62 22 29 20 29 20 72 65 74 75 72 6e  op~"b") ) return
0670: 3b 20 7d 0a 09 09 63 61 74 63 68 28 54 68 72 6f  ; }...catch(Thro
0680: 77 61 62 6c 65 20 65 29 0a 09 09 09 7b 20 6f 6e  wable e)....{ on
0690: 41 73 73 65 72 74 45 72 72 6f 72 4d 73 67 28 66  AssertErrorMsg(f
06a0: 6e 2c 20 6c 6e 2c 20 6d 73 67 2e 6c 65 6e 67 74  n, ln, msg.lengt
06b0: 68 20 3f 20 6d 73 67 20 3a 20 22 65 78 63 65 70  h ? msg : "excep
06c0: 74 69 6f 6e 20 5b 22 7e 65 2e 74 6f 53 74 72 69  tion ["~e.toStri
06d0: 6e 67 28 29 7e 22 5d 22 29 3b 20 7d 0a 09 09 6f  ng()~"]"); }...o
06e0: 6e 41 73 73 65 72 74 45 72 72 6f 72 4d 73 67 28  nAssertErrorMsg(
06f0: 66 6e 2c 20 6c 6e 2c 20 6d 73 67 2e 6c 65 6e 67  fn, ln, msg.leng
0700: 74 68 20 3f 20 6d 73 67 20 3a 20 74 6f 21 73 74  th ? msg : to!st
0710: 72 69 6e 67 28 61 29 7e 22 20 21 22 7e 6f 70 7e  ring(a)~" !"~op~
0720: 74 6f 21 73 74 72 69 6e 67 28 62 29 29 3b 0a 09  to!string(b));..
0730: 7d 0a 7d 0a 0a 61 6c 69 61 73 20 61 73 73 65 72  }.}..alias asser
0740: 74 4f 70 21 28 60 3d 3d 60 29 20 61 73 73 65 72  tOp!(`==`) asser
0750: 74 5f 65 71 3b 0a 61 6c 69 61 73 20 61 73 73 65  t_eq;.alias asse
0760: 72 74 4f 70 21 28 60 21 3d 60 29 20 61 73 73 65  rtOp!(`!=`) asse
0770: 72 74 5f 6e 65 3b 0a 61 6c 69 61 73 20 61 73 73  rt_ne;.alias ass
0780: 65 72 74 4f 70 21 28 60 3c 60 29 20 20 61 73 73  ertOp!(`<`)  ass
0790: 65 72 74 5f 6c 74 3b 0a 61 6c 69 61 73 20 61 73  ert_lt;.alias as
07a0: 73 65 72 74 4f 70 21 28 60 3c 3d 60 29 20 61 73  sertOp!(`<=`) as
07b0: 73 65 72 74 5f 6c 65 3b 0a 61 6c 69 61 73 20 61  sert_le;.alias a
07c0: 73 73 65 72 74 4f 70 21 28 60 3e 60 29 20 20 61  ssertOp!(`>`)  a
07d0: 73 73 65 72 74 5f 67 74 3b 0a 61 6c 69 61 73 20  ssert_gt;.alias 
07e0: 61 73 73 65 72 74 4f 70 21 28 60 3e 3d 60 29 20  assertOp!(`>=`) 
07f0: 61 73 73 65 72 74 5f 67 65 3b 0a 0a 75 6e 69 74  assert_ge;..unit
0800: 74 65 73 74 0a 7b 0a 09 61 73 73 65 72 74 5f 6e  test.{..assert_n
0810: 6f 74 68 72 6f 77 28 20 61 73 73 65 72 74 5f 65  othrow( assert_e
0820: 71 28 31 2c 20 31 29 20 29 3b 0a 09 61 73 73 65  q(1, 1) );..asse
0830: 72 74 5f 6e 6f 74 68 72 6f 77 28 20 61 73 73 65  rt_nothrow( asse
0840: 72 74 5f 6e 65 28 31 2c 20 30 29 20 29 3b 0a 09  rt_ne(1, 0) );..
0850: 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20  assert_nothrow( 
0860: 61 73 73 65 72 74 5f 6c 74 28 30 2c 20 31 29 20  assert_lt(0, 1) 
0870: 29 3b 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72  );..assert_nothr
0880: 6f 77 28 20 61 73 73 65 72 74 5f 6c 65 28 30 2c  ow( assert_le(0,
0890: 20 31 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 6e   1) );..assert_n
08a0: 6f 74 68 72 6f 77 28 20 61 73 73 65 72 74 5f 6c  othrow( assert_l
08b0: 65 28 30 2c 20 30 29 20 29 3b 0a 09 61 73 73 65  e(0, 0) );..asse
08c0: 72 74 5f 6e 6f 74 68 72 6f 77 28 20 61 73 73 65  rt_nothrow( asse
08d0: 72 74 5f 67 74 28 31 2c 20 30 29 20 29 3b 0a 09  rt_gt(1, 0) );..
08e0: 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20  assert_nothrow( 
08f0: 61 73 73 65 72 74 5f 67 65 28 31 2c 20 30 29 20  assert_ge(1, 0) 
0900: 29 3b 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72  );..assert_nothr
0910: 6f 77 28 20 61 73 73 65 72 74 5f 67 65 28 30 2c  ow( assert_ge(0,
0920: 20 30 29 20 29 3b 0a 0a 09 61 73 73 65 72 74 5f   0) );...assert_
0930: 74 68 72 6f 77 21 41 73 73 65 72 74 45 72 72 6f  throw!AssertErro
0940: 72 28 20 61 73 73 65 72 74 5f 65 71 28 31 2c 20  r( assert_eq(1, 
0950: 30 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 74 68  0) );..assert_th
0960: 72 6f 77 21 41 73 73 65 72 74 45 72 72 6f 72 28  row!AssertError(
0970: 20 61 73 73 65 72 74 5f 6e 65 28 31 2c 20 31 29   assert_ne(1, 1)
0980: 20 29 3b 0a 09 61 73 73 65 72 74 5f 74 68 72 6f   );..assert_thro
0990: 77 21 41 73 73 65 72 74 45 72 72 6f 72 28 20 61  w!AssertError( a
09a0: 73 73 65 72 74 5f 6c 74 28 31 2c 20 31 29 20 29  ssert_lt(1, 1) )
09b0: 3b 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21  ;..assert_throw!
09c0: 41 73 73 65 72 74 45 72 72 6f 72 28 20 61 73 73  AssertError( ass
09d0: 65 72 74 5f 6c 74 28 31 2c 20 30 29 20 29 3b 0a  ert_lt(1, 0) );.
09e0: 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73  .assert_throw!As
09f0: 73 65 72 74 45 72 72 6f 72 28 20 61 73 73 65 72  sertError( asser
0a00: 74 5f 6c 65 28 31 2c 20 30 29 20 29 3b 0a 09 61  t_le(1, 0) );..a
0a10: 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65  ssert_throw!Asse
0a20: 72 74 45 72 72 6f 72 28 20 61 73 73 65 72 74 5f  rtError( assert_
0a30: 67 74 28 30 2c 20 30 29 20 29 3b 0a 09 61 73 73  gt(0, 0) );..ass
0a40: 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65 72 74  ert_throw!Assert
0a50: 45 72 72 6f 72 28 20 61 73 73 65 72 74 5f 67 74  Error( assert_gt
0a60: 28 30 2c 20 31 29 20 29 3b 0a 09 61 73 73 65 72  (0, 1) );..asser
0a70: 74 5f 74 68 72 6f 77 21 41 73 73 65 72 74 45 72  t_throw!AssertEr
0a80: 72 6f 72 28 20 61 73 73 65 72 74 5f 67 65 28 30  ror( assert_ge(0
0a90: 2c 20 31 29 20 29 3b 0a 0a 09 63 6c 61 73 73 20  , 1) );...class 
0aa0: 54 65 6d 70 20 7b 20 62 6f 6f 6c 20 6f 70 45 71  Temp { bool opEq
0ab0: 75 61 6c 73 28 69 6e 74 20 78 29 7b 72 65 74 75  uals(int x){retu
0ac0: 72 6e 20 78 2f 78 3d 3d 78 3b 7d 20 7d 0a 09 61  rn x/x==x;} }..a
0ad0: 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65  ssert_throw!Asse
0ae0: 72 74 45 72 72 6f 72 28 20 61 73 73 65 72 74 5f  rtError( assert_
0af0: 65 71 28 6e 65 77 20 54 65 6d 70 2c 20 30 29 20  eq(new Temp, 0) 
0b00: 29 3b 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72  );..assert_nothr
0b10: 6f 77 20 20 20 20 20 20 20 20 20 20 28 20 61 73  ow          ( as
0b20: 73 65 72 74 5f 65 71 28 6e 65 77 20 54 65 6d 70  sert_eq(new Temp
0b30: 2c 20 31 29 20 29 3b 0a 09 61 73 73 65 72 74 5f  , 1) );..assert_
0b40: 74 68 72 6f 77 21 41 73 73 65 72 74 45 72 72 6f  throw!AssertErro
0b50: 72 28 20 61 73 73 65 72 74 5f 65 71 28 6e 65 77  r( assert_eq(new
0b60: 20 54 65 6d 70 2c 20 32 29 20 29 3b 0a 7d 0a 0a   Temp, 2) );.}..
0b70: 2f 2a 20 5b 54 6f 64 6f 5d 20 69 73 20 74 68 65  /* [Todo] is the
0b80: 72 65 20 61 6e 79 20 77 61 79 20 74 6f 20 63 6c  re any way to cl
0b90: 65 61 72 6e 6c 79 20 69 6d 70 6c 65 6d 65 6e 74  earnly implement
0ba0: 20 22 61 73 73 65 72 74 5f 63 6f 6d 70 69 6c 65   "assert_compile
0bb0: 73 22 20 61 6e 64 20 22 61 73 73 65 72 74 5f 6e  s" and "assert_n
0bc0: 6f 74 5f 63 6f 6d 70 69 6c 65 22 3f 20 2a 2f 0a  ot_compile"? */.