fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <assert.h>
  4.  
  5.  
  6. class SomeClass {
  7. public:
  8. SomeClass(const std::string &line) {
  9. if (line[0] != 'a') {
  10. // ok
  11. }
  12. else
  13. {
  14. throw std::invalid_argument("The First character is 'a'! ");
  15. }
  16. }
  17. };
  18.  
  19.  
  20. class SomeClassTest {
  21. public:
  22. void ConstructorTest() {
  23. bool hasErrorBeen = false;
  24. try
  25. {
  26. SomeClass object1("Hello");
  27. }
  28. catch(std::invalid_argument& error)
  29. {
  30. hasErrorBeen = true;
  31. }
  32. assert(hasErrorBeen == false);
  33.  
  34. hasErrorBeen = false;
  35. try
  36. {
  37. SomeClass object2("aHello");
  38. }
  39. catch(std::invalid_argument& error)
  40. {
  41. hasErrorBeen = true;
  42. }
  43. assert(hasErrorBeen == true);
  44.  
  45. // И тут еще 15 таких тестов, можно оформить в функцию
  46. }
  47. };
  48.  
  49. int main() {
  50.  
  51. SomeClassTest someClassTest;
  52. someClassTest.ConstructorTest();
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3424KB
stdin
Standard input is empty
stdout
Standard output is empty