Hex Artifact Content
Not logged in

Artifact bb5afea9b7b1423db6e9b85852bbbbca8a2e495a:


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 69 6e 74 20 6c 6e 3d 5f  FILE__, int ln=_
0130: 5f 4c 49 4e 45 5f 5f 29 28 6c 61 7a 79 20 54 20  _LINE__)(lazy T 
0140: 74 2c 20 73 74 72 69 6e 67 20 6d 73 67 3d 22 22  t, string msg=""
0150: 29 0a 7b 0a 09 74 72 79 0a 09 09 7b 20 74 28 29  ).{..try...{ t()
0160: 3b 20 7d 0a 09 63 61 74 63 68 28 45 78 63 65 70  ; }..catch(Excep
0170: 74 69 6f 6e 54 79 70 65 29 0a 09 09 7b 20 72 65  tionType)...{ re
0180: 74 75 72 6e 3b 20 7d 0a 09 63 61 74 63 68 28 54  turn; }..catch(T
0190: 68 72 6f 77 61 62 6c 65 20 65 29 0a 09 09 7b 20  hrowable e)...{ 
01a0: 6f 6e 41 73 73 65 72 74 45 72 72 6f 72 4d 73 67  onAssertErrorMsg
01b0: 28 66 6e 2c 20 6c 6e 2c 20 6d 73 67 2e 6c 65 6e  (fn, ln, msg.len
01c0: 67 74 68 20 3f 20 6d 73 67 20 3a 20 22 65 78 63  gth ? msg : "exc
01d0: 65 70 74 69 6f 6e 20 5b 22 7e 65 2e 74 6f 53 74  eption ["~e.toSt
01e0: 72 69 6e 67 28 29 7e 22 5d 22 29 3b 20 7d 0a 09  ring()~"]"); }..
01f0: 6f 6e 41 73 73 65 72 74 45 72 72 6f 72 4d 73 67  onAssertErrorMsg
0200: 28 66 6e 2c 20 6c 6e 2c 20 6d 73 67 2e 6c 65 6e  (fn, ln, msg.len
0210: 67 74 68 20 3f 20 6d 73 67 20 3a 20 22 6e 6f 20  gth ? msg : "no 
0220: 65 78 65 63 70 74 69 6f 6e 22 29 3b 0a 7d 0a 0a  execption");.}..
0230: 2f 2f 2f 20 55 6e 69 74 74 65 73 74 20 68 65 6c  /// Unittest hel
0240: 70 65 72 20 74 68 61 74 20 61 73 73 65 72 74 73  per that asserts
0250: 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6d   an expression m
0260: 75 73 74 20 6e 6f 74 20 74 68 72 6f 77 20 61 6e  ust not throw an
0270: 79 74 68 69 6e 67 0a 0a 61 75 74 6f 20 61 73 73  ything..auto ass
0280: 65 72 74 5f 6e 6f 74 68 72 6f 77 28 54 2c 20 73  ert_nothrow(T, s
0290: 74 72 69 6e 67 20 66 6e 3d 5f 5f 46 49 4c 45 5f  tring fn=__FILE_
02a0: 5f 2c 20 69 6e 74 20 6c 6e 3d 5f 5f 4c 49 4e 45  _, int ln=__LINE
02b0: 5f 5f 29 28 6c 61 7a 79 20 54 20 74 2c 20 73 74  __)(lazy T t, st
02c0: 72 69 6e 67 20 6d 73 67 3d 22 22 29 0a 7b 0a 09  ring msg="").{..
02d0: 74 72 79 0a 09 09 7b 20 72 65 74 75 72 6e 20 74  try...{ return t
02e0: 28 29 3b 20 7d 0a 09 63 61 74 63 68 28 54 68 72  (); }..catch(Thr
02f0: 6f 77 61 62 6c 65 20 65 29 0a 09 09 7b 20 6f 6e  owable e)...{ on
0300: 41 73 73 65 72 74 45 72 72 6f 72 4d 73 67 28 66  AssertErrorMsg(f
0310: 6e 2c 20 6c 6e 2c 20 6d 73 67 2e 6c 65 6e 67 74  n, ln, msg.lengt
0320: 68 20 3f 20 6d 73 67 20 3a 20 22 65 78 63 65 70  h ? msg : "excep
0330: 74 69 6f 6e 20 5b 22 7e 65 2e 74 6f 53 74 72 69  tion ["~e.toStri
0340: 6e 67 28 29 7e 22 5d 22 29 3b 20 7d 0d 0a 09 61  ng()~"]"); }...a
0350: 73 73 65 72 74 28 66 61 6c 73 65 29 3b 0a 7d 0a  ssert(false);.}.
0360: 0a 75 6e 69 74 74 65 73 74 0a 7b 0a 09 61 75 74  .unittest.{..aut
0370: 6f 20 65 72 72 6f 72 20 3d 20 7b 74 68 72 6f 77  o error = {throw
0380: 20 6e 65 77 20 45 72 72 6f 72 28 22 68 65 6c 6c   new Error("hell
0390: 6f 22 29 3b 7d 3b 0a 09 61 75 74 6f 20 6e 6f 74  o");};..auto not
03a0: 68 69 6e 67 20 3d 20 28 29 7b 7d 3b 0a 09 61 75  hing = (){};..au
03b0: 74 6f 20 61 73 73 65 72 74 45 72 72 6f 72 20 3d  to assertError =
03c0: 20 7b 61 73 73 65 72 74 28 30 29 3b 7d 3b 0a 0a   {assert(0);};..
03d0: 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 20  .assert_nothrow 
03e0: 20 20 20 20 20 20 20 20 20 28 20 61 73 73 65 72           ( asser
03f0: 74 5f 6e 6f 74 68 72 6f 77 28 6e 6f 74 68 69 6e  t_nothrow(nothin
0400: 67 28 29 29 20 29 3b 0a 09 61 73 73 65 72 74 5f  g()) );..assert_
0410: 74 68 72 6f 77 21 41 73 73 65 72 74 45 72 72 6f  throw!AssertErro
0420: 72 28 20 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f  r( assert_nothro
0430: 77 28 65 72 72 6f 72 28 29 29 20 29 3b 0a 09 61  w(error()) );..a
0440: 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65  ssert_throw!Asse
0450: 72 74 45 72 72 6f 72 28 20 61 73 73 65 72 74 5f  rtError( assert_
0460: 6e 6f 74 68 72 6f 77 28 61 73 73 65 72 74 45 72  nothrow(assertEr
0470: 72 6f 72 28 29 29 20 29 3b 0a 0a 09 61 73 73 65  ror()) );...asse
0480: 72 74 5f 6e 6f 74 68 72 6f 77 20 20 20 20 20 20  rt_nothrow      
0490: 20 20 20 20 28 20 61 73 73 65 72 74 5f 74 68 72      ( assert_thr
04a0: 6f 77 21 45 72 72 6f 72 28 65 72 72 6f 72 28 29  ow!Error(error()
04b0: 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 74 68 72  ) );..assert_thr
04c0: 6f 77 21 41 73 73 65 72 74 45 72 72 6f 72 28 20  ow!AssertError( 
04d0: 61 73 73 65 72 74 5f 74 68 72 6f 77 21 45 72 72  assert_throw!Err
04e0: 6f 72 28 6e 6f 74 68 69 6e 67 28 29 29 20 29 3b  or(nothing()) );
04f0: 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  ..assert_nothrow
0500: 20 20 20 20 20 20 20 20 20 20 28 20 61 73 73 65            ( asse
0510: 72 74 5f 74 68 72 6f 77 21 45 72 72 6f 72 28 61  rt_throw!Error(a
0520: 73 73 65 72 74 45 72 72 6f 72 28 29 29 20 29 3b  ssertError()) );
0530: 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 41  ..assert_throw!A
0540: 73 73 65 72 74 45 72 72 6f 72 28 20 61 73 73 65  ssertError( asse
0550: 72 74 5f 74 68 72 6f 77 21 41 73 73 65 72 74 45  rt_throw!AssertE
0560: 72 72 6f 72 28 65 72 72 6f 72 28 29 29 20 29 3b  rror(error()) );
0570: 0a 7d 0a 0a 2f 2f 2f 20 55 6e 69 74 74 65 73 74  .}../// Unittest
0580: 20 68 65 6c 70 65 72 73 20 61 73 73 65 72 74 69   helpers asserti
0590: 6e 67 20 74 77 6f 20 76 61 6c 75 65 73 20 61 72  ng two values ar
05a0: 65 20 69 6e 20 73 6f 6d 65 20 72 65 6c 61 74 69  e in some relati
05b0: 6f 6e 20 3d 3d 2c 20 21 3d 2c 20 3c 2c 20 3c 3d  on ==, !=, <, <=
05c0: 2c 20 3e 2c 20 3e 3d 0a 0a 74 65 6d 70 6c 61 74  , >, >=..templat
05d0: 65 20 61 73 73 65 72 74 4f 70 28 73 74 72 69 6e  e assertOp(strin
05e0: 67 20 6f 70 29 0a 7b 0a 09 76 6f 69 64 20 61 73  g op).{..void as
05f0: 73 65 72 74 4f 70 28 41 2c 20 42 2c 20 73 74 72  sertOp(A, B, str
0600: 69 6e 67 20 66 6e 3d 5f 5f 46 49 4c 45 5f 5f 2c  ing fn=__FILE__,
0610: 20 69 6e 74 20 6c 6e 3d 5f 5f 4c 49 4e 45 5f 5f   int ln=__LINE__
0620: 29 28 41 20 61 2c 20 42 20 62 2c 20 73 74 72 69  )(A a, B b, stri
0630: 6e 67 20 6d 73 67 3d 22 22 29 0a 09 7b 0a 09 09  ng msg="")..{...
0640: 74 72 79 0a 09 09 09 7b 20 69 66 28 20 6d 69 78  try....{ if( mix
0650: 69 6e 28 22 61 22 7e 6f 70 7e 22 62 22 29 20 29  in("a"~op~"b") )
0660: 20 72 65 74 75 72 6e 3b 20 7d 0a 09 09 63 61 74   return; }...cat
0670: 63 68 28 54 68 72 6f 77 61 62 6c 65 20 65 29 0a  ch(Throwable e).
0680: 09 09 09 7b 20 6f 6e 41 73 73 65 72 74 45 72 72  ...{ onAssertErr
0690: 6f 72 4d 73 67 28 66 6e 2c 20 6c 6e 2c 20 6d 73  orMsg(fn, ln, ms
06a0: 67 2e 6c 65 6e 67 74 68 20 3f 20 6d 73 67 20 3a  g.length ? msg :
06b0: 20 22 65 78 63 65 70 74 69 6f 6e 20 5b 22 7e 65   "exception ["~e
06c0: 2e 74 6f 53 74 72 69 6e 67 28 29 7e 22 5d 22 29  .toString()~"]")
06d0: 3b 20 7d 0a 09 09 6f 6e 41 73 73 65 72 74 45 72  ; }...onAssertEr
06e0: 72 6f 72 4d 73 67 28 66 6e 2c 20 6c 6e 2c 20 6d  rorMsg(fn, ln, m
06f0: 73 67 2e 6c 65 6e 67 74 68 20 3f 20 6d 73 67 20  sg.length ? msg 
0700: 3a 20 74 6f 21 73 74 72 69 6e 67 28 61 29 7e 22  : to!string(a)~"
0710: 20 21 22 7e 6f 70 7e 74 6f 21 73 74 72 69 6e 67   !"~op~to!string
0720: 28 62 29 29 3b 0a 09 7d 0a 7d 0a 0a 61 6c 69 61  (b));..}.}..alia
0730: 73 20 61 73 73 65 72 74 4f 70 21 28 60 3d 3d 60  s assertOp!(`==`
0740: 29 20 61 73 73 65 72 74 5f 65 71 3b 0a 61 6c 69  ) assert_eq;.ali
0750: 61 73 20 61 73 73 65 72 74 4f 70 21 28 60 21 3d  as assertOp!(`!=
0760: 60 29 20 61 73 73 65 72 74 5f 6e 65 3b 0a 61 6c  `) assert_ne;.al
0770: 69 61 73 20 61 73 73 65 72 74 4f 70 21 28 60 3c  ias assertOp!(`<
0780: 60 29 20 20 61 73 73 65 72 74 5f 6c 74 3b 0a 61  `)  assert_lt;.a
0790: 6c 69 61 73 20 61 73 73 65 72 74 4f 70 21 28 60  lias assertOp!(`
07a0: 3c 3d 60 29 20 61 73 73 65 72 74 5f 6c 65 3b 0a  <=`) assert_le;.
07b0: 61 6c 69 61 73 20 61 73 73 65 72 74 4f 70 21 28  alias assertOp!(
07c0: 60 3e 60 29 20 20 61 73 73 65 72 74 5f 67 74 3b  `>`)  assert_gt;
07d0: 0a 61 6c 69 61 73 20 61 73 73 65 72 74 4f 70 21  .alias assertOp!
07e0: 28 60 3e 3d 60 29 20 61 73 73 65 72 74 5f 67 65  (`>=`) assert_ge
07f0: 3b 0a 0a 75 6e 69 74 74 65 73 74 0a 7b 0a 09 61  ;..unittest.{..a
0800: 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 61  ssert_nothrow( a
0810: 73 73 65 72 74 5f 65 71 28 31 2c 20 31 29 20 29  ssert_eq(1, 1) )
0820: 3b 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f  ;..assert_nothro
0830: 77 28 20 61 73 73 65 72 74 5f 6e 65 28 31 2c 20  w( assert_ne(1, 
0840: 30 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 6e 6f  0) );..assert_no
0850: 74 68 72 6f 77 28 20 61 73 73 65 72 74 5f 6c 74  throw( assert_lt
0860: 28 30 2c 20 31 29 20 29 3b 0a 09 61 73 73 65 72  (0, 1) );..asser
0870: 74 5f 6e 6f 74 68 72 6f 77 28 20 61 73 73 65 72  t_nothrow( asser
0880: 74 5f 6c 65 28 30 2c 20 31 29 20 29 3b 0a 09 61  t_le(0, 1) );..a
0890: 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 61  ssert_nothrow( a
08a0: 73 73 65 72 74 5f 6c 65 28 30 2c 20 30 29 20 29  ssert_le(0, 0) )
08b0: 3b 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f  ;..assert_nothro
08c0: 77 28 20 61 73 73 65 72 74 5f 67 74 28 31 2c 20  w( assert_gt(1, 
08d0: 30 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 6e 6f  0) );..assert_no
08e0: 74 68 72 6f 77 28 20 61 73 73 65 72 74 5f 67 65  throw( assert_ge
08f0: 28 31 2c 20 30 29 20 29 3b 0a 09 61 73 73 65 72  (1, 0) );..asser
0900: 74 5f 6e 6f 74 68 72 6f 77 28 20 61 73 73 65 72  t_nothrow( asser
0910: 74 5f 67 65 28 30 2c 20 30 29 20 29 3b 0a 0a 09  t_ge(0, 0) );...
0920: 61 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73  assert_throw!Ass
0930: 65 72 74 45 72 72 6f 72 28 20 61 73 73 65 72 74  ertError( assert
0940: 5f 65 71 28 31 2c 20 30 29 20 29 3b 0a 09 61 73  _eq(1, 0) );..as
0950: 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73 65 72  sert_throw!Asser
0960: 74 45 72 72 6f 72 28 20 61 73 73 65 72 74 5f 6e  tError( assert_n
0970: 65 28 31 2c 20 31 29 20 29 3b 0a 09 61 73 73 65  e(1, 1) );..asse
0980: 72 74 5f 74 68 72 6f 77 21 41 73 73 65 72 74 45  rt_throw!AssertE
0990: 72 72 6f 72 28 20 61 73 73 65 72 74 5f 6c 74 28  rror( assert_lt(
09a0: 31 2c 20 31 29 20 29 3b 0a 09 61 73 73 65 72 74  1, 1) );..assert
09b0: 5f 74 68 72 6f 77 21 41 73 73 65 72 74 45 72 72  _throw!AssertErr
09c0: 6f 72 28 20 61 73 73 65 72 74 5f 6c 74 28 31 2c  or( assert_lt(1,
09d0: 20 30 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 74   0) );..assert_t
09e0: 68 72 6f 77 21 41 73 73 65 72 74 45 72 72 6f 72  hrow!AssertError
09f0: 28 20 61 73 73 65 72 74 5f 6c 65 28 31 2c 20 30  ( assert_le(1, 0
0a00: 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 74 68 72  ) );..assert_thr
0a10: 6f 77 21 41 73 73 65 72 74 45 72 72 6f 72 28 20  ow!AssertError( 
0a20: 61 73 73 65 72 74 5f 67 74 28 30 2c 20 30 29 20  assert_gt(0, 0) 
0a30: 29 3b 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77  );..assert_throw
0a40: 21 41 73 73 65 72 74 45 72 72 6f 72 28 20 61 73  !AssertError( as
0a50: 73 65 72 74 5f 67 74 28 30 2c 20 31 29 20 29 3b  sert_gt(0, 1) );
0a60: 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 41  ..assert_throw!A
0a70: 73 73 65 72 74 45 72 72 6f 72 28 20 61 73 73 65  ssertError( asse
0a80: 72 74 5f 67 65 28 30 2c 20 31 29 20 29 3b 0a 0a  rt_ge(0, 1) );..
0a90: 09 63 6c 61 73 73 20 54 65 6d 70 20 7b 20 62 6f  .class Temp { bo
0aa0: 6f 6c 20 6f 70 45 71 75 61 6c 73 28 69 6e 74 20  ol opEquals(int 
0ab0: 78 29 7b 72 65 74 75 72 6e 20 78 2f 78 3d 3d 78  x){return x/x==x
0ac0: 3b 7d 20 7d 0a 09 61 73 73 65 72 74 5f 74 68 72  ;} }..assert_thr
0ad0: 6f 77 21 41 73 73 65 72 74 45 72 72 6f 72 28 20  ow!AssertError( 
0ae0: 61 73 73 65 72 74 5f 65 71 28 6e 65 77 20 54 65  assert_eq(new Te
0af0: 6d 70 2c 20 30 29 20 29 3b 0a 09 61 73 73 65 72  mp, 0) );..asser
0b00: 74 5f 6e 6f 74 68 72 6f 77 20 20 20 20 20 20 20  t_nothrow       
0b10: 20 20 20 28 20 61 73 73 65 72 74 5f 65 71 28 6e     ( assert_eq(n
0b20: 65 77 20 54 65 6d 70 2c 20 31 29 20 29 3b 0a 09  ew Temp, 1) );..
0b30: 61 73 73 65 72 74 5f 74 68 72 6f 77 21 41 73 73  assert_throw!Ass
0b40: 65 72 74 45 72 72 6f 72 28 20 61 73 73 65 72 74  ertError( assert
0b50: 5f 65 71 28 6e 65 77 20 54 65 6d 70 2c 20 32 29  _eq(new Temp, 2)
0b60: 20 29 3b 0a 7d 0a 0a 2f 2a 20 5b 54 6f 64 6f 5d   );.}../* [Todo]
0b70: 20 69 73 20 74 68 65 72 65 20 61 6e 79 20 77 61   is there any wa
0b80: 79 20 74 6f 20 63 6c 65 61 72 6e 6c 79 20 69 6d  y to clearnly im
0b90: 70 6c 65 6d 65 6e 74 20 22 61 73 73 65 72 74 5f  plement "assert_
0ba0: 63 6f 6d 70 69 6c 65 73 22 20 61 6e 64 20 22 61  compiles" and "a
0bb0: 73 73 65 72 74 5f 6e 6f 74 5f 63 6f 6d 70 69 6c  ssert_not_compil
0bc0: 65 22 3f 20 2a 2f 0a                             e"? */.