PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/Examples/test-suite/d/default_args_runme.2.d

#
D | 84 lines | 70 code | 10 blank | 4 comment | 30 complexity | b78ba80d30f581c03a7d8da0a2675691 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. module default_args_runme;
  2. import std.exception;
  3. import default_args.default_args;
  4. import default_args.ConstMethods;
  5. import default_args.EnumClass;
  6. import default_args.Except;
  7. import default_args.Foo;
  8. import default_args.Klass;
  9. import default_args.Statics;
  10. import default_args.Tricky;
  11. void main() {
  12. enforce(anonymous() == 7771, "anonymous (1) failed");
  13. enforce(anonymous(1234) == 1234,"anonymous (2) failed");
  14. enforce(booltest() == true, "booltest (1) failed");
  15. enforce(booltest(true) == true, "booltest (2) failed");
  16. enforce(booltest(false) == false, "booltest (3) failed");
  17. enforce((new EnumClass()).blah() == true, "EnumClass failed");
  18. enforce(casts1() == null, "casts1 failed");
  19. enforce(casts2() == "Hello", "casts2 failed");
  20. enforce(casts1("Ciao") == "Ciao", "casts1 not default failed");
  21. enforce(chartest1() == 'x', "chartest1 failed");
  22. enforce(chartest2() == '\0', "chartest2 failed");
  23. enforce(chartest1('y') == 'y', "chartest1 not default failed");
  24. enforce(chartest1('y') == 'y', "chartest1 not default failed");
  25. enforce(reftest1() == 42, "reftest1 failed");
  26. enforce(reftest1(400) == 400, "reftest1 not default failed");
  27. enforce(reftest2() == "hello", "reftest2 failed");
  28. // rename
  29. auto foo = new Foo();
  30. foo.newname();
  31. foo.newname(10);
  32. foo.renamed3arg(10, 10.0);
  33. foo.renamed2arg(10);
  34. foo.renamed1arg();
  35. // exception specifications
  36. enforceThrows( (){ exceptionspec(); }, "exceptionspec 1" );
  37. enforceThrows( (){ exceptionspec(-1); }, "exceptionspec 2" );
  38. enforceThrows( (){ exceptionspec(100); }, "exceptionspec 3" );
  39. auto ex = new Except(false);
  40. enforceThrows( (){ ex.exspec(); }, "exspec 1" );
  41. enforceThrows( (){ ex.exspec(-1); }, "exspec 2" );
  42. enforceThrows( (){ ex.exspec(100); }, "exspec 3" );
  43. enforceThrows( (){ ex = new Except(true); }, "Except constructor 1" );
  44. enforceThrows( (){ ex = new Except(true, -2); }, "Except constructor 2" );
  45. // Default parameters in static class methods
  46. enforce(Statics.staticmethod() == 10+20+30, "staticmethod 1 failed");
  47. enforce(Statics.staticmethod(100) == 100+20+30, "staticmethod 2 failed");
  48. enforce(Statics.staticmethod(100,200,300) == 100+200+300, "staticmethod 3 failed");
  49. auto tricky = new Tricky();
  50. enforce(tricky.privatedefault() == 200, "privatedefault failed");
  51. enforce(tricky.protectedint() == 2000, "protectedint failed");
  52. enforce(tricky.protecteddouble() == 987.654, "protecteddouble failed");
  53. enforce(tricky.functiondefault() == 500, "functiondefault failed");
  54. enforce(tricky.contrived() == 'X', "contrived failed");
  55. enforce(constructorcall().val == -1, "constructorcall test 1 failed");
  56. enforce(constructorcall(new Klass(2222)).val == 2222, "constructorcall test 2 failed");
  57. enforce(constructorcall(new Klass()).val == -1, "constructorcall test 3 failed");
  58. // const methods
  59. const cm = new ConstMethods();
  60. enforce(cm.coo() == 20, "coo test 1 failed");
  61. enforce(cm.coo(1.0) == 20, "coo test 2 failed");
  62. }
  63. private void enforceThrows(void delegate() dg, string errorMessage) {
  64. bool hasThrown;
  65. try {
  66. dg();
  67. } catch (Exception) {
  68. hasThrown = true;
  69. } finally {
  70. if (!hasThrown) {
  71. throw new Exception(errorMessage);
  72. }
  73. }
  74. }