Guest User

Untitled

a guest
Mar 19th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #include "testy.h"
  2. using namespace std;
  3. using testy::Expect;
  4.  
  5. int main() {
  6.  
  7. // --------------------------------------------
  8. // Basic Usage
  9. // How to run a standalone test.
  10. // --------------------------------------------
  11.  
  12. //
  13. // testy::immediate(std::string name, std::function<void(testy::Expect)> lambda);
  14. // --> Runs a test function.
  15. //
  16. // name : The test name.
  17. // lambda : The test function.
  18. //
  19.  
  20. testy::immediate("Running Tests", [](Expect expect) {
  21. // Test code goes here.
  22. });
  23.  
  24. // --------------------------------------------
  25. // Creating Expectations
  26. // How to actually test things.
  27. // --------------------------------------------
  28.  
  29. //
  30. // Expect<T>(T value)
  31. // --> Creates an expectation for a value type.
  32. //
  33. // <T> : The expectation type. This can be inferred by the compiler.
  34. // value : The unknown value to assert.
  35. //
  36.  
  37. testy::immediate("Expectations", [](Expect expect) {
  38. int unknown = /* do something that returns an int */;
  39.  
  40. expect(unknown); // Does nothing yet.
  41. });
  42.  
  43. //
  44. // Expect.function(std::function<void()> function)
  45. // --> Creates an expectation for a function type.
  46. //
  47. // function : The function to assert.
  48. //
  49.  
  50. testy::immediate("Your Expectations Are Running", [](Expect expect) {
  51. int unknown = /* do something that returns an int */;
  52.  
  53. expect.function([] {
  54. // Does nothing until asserted.
  55. });
  56. });
  57.  
  58. // --------------------------------------------
  59. // Expectations
  60. // The syntactic sugar of assertions.
  61. // --------------------------------------------
  62.  
  63. //
  64. // ValueExpectation<T>.equals(T value)
  65. // --> Assert two values using the == operator.
  66. //
  67. // value : The expected value.
  68. //
  69. // NOTE -- The ==(T, T) operator must be implemented.
  70. //
  71.  
  72. testy::immediate("Equality", [](Expect expect) {
  73. int three = 3;
  74. int five = 5;
  75.  
  76. expect(three).equals(five); // Test failed.
  77. });
  78.  
  79. //
  80. // Expectation<T>.never().[...]
  81. // --> Negate an assertion.
  82. //
  83. // WARNING -- This is reset after each assertion.
  84. //
  85.  
  86. testy::immediate("Negation", [](Expect expect) {
  87. int three = 3;
  88. int five = 5;
  89.  
  90. expect(three).never().equals(five); // Passed.
  91. });
  92.  
  93. //
  94. // Expectation<T>.that(std::string description).[...]
  95. // --> Describe an assertion.
  96. //
  97. // description : The description.
  98. //
  99.  
  100. testy::immediate("Descriptions", [](Expect expect) {
  101. expect(2*2).that("Two times two is four.").equals(3);
  102. });
  103.  
  104. //
  105. // FunctionExpectation.couts(std::string value)
  106. // --> Assert that the function prints a specific string to std::cout.
  107. //
  108. // value : The expected string.
  109. //
  110.  
  111. testy::immediate("Couting", [](Expect expect) {
  112. expect.function([] {
  113. cout << "I'm sad\n";
  114. }).couts("I'm happy\n"); // Test failed.
  115. });
  116.  
  117. //
  118. // FunctionExpectation.cerrs(std::string value)
  119. // --> Assert that the function prints a specific string to std::cerr.
  120. //
  121. // value : The expected string.
  122. //
  123.  
  124. testy::immediate("Couting, With Cerr", [](Expect expect) {
  125. expect.function([] {
  126. cerr << "This went wrong.\n";
  127. }).cerrs(""); // Test failed.
  128. });
  129.  
  130. //
  131. // FunctionExpectation.throws<T>()
  132. // --> Assert that the function throws a specific exception.
  133. //
  134. // <t> : The expected exception type.
  135. // value : The expected string.
  136. //
  137.  
  138. testy::immediate("Except", [](Expect expect) {
  139. expect.function([] {
  140. throw runtime_error("Javelin");
  141. }).throws<runtime_error>(); // Test passed.
  142. });
  143.  
  144. // --------------------------------------------
  145. // Logging
  146. // Where did it go wrong?
  147. // --------------------------------------------
  148.  
  149. //
  150. // testy will tell you how things went wrong.
  151. // It's better than leaving a bunch of
  152. // std::cout << statements everywhere.
  153. //
  154.  
  155. testy::immediate("Oops", [](Expect expect) {
  156. expect(true).that("Contradiction").equals(!true);
  157.  
  158. // Oops: .................................... [failed]
  159. // ! Failure: Contradiction
  160. // ! Expected: 1
  161. // ! Received: 0
  162.  
  163. });
  164.  
  165. //
  166. // testy will automatically record everything
  167. // that went into std::cout and std::cerr. If
  168. // the test doesn't fail, you won't be bothered.
  169. //
  170.  
  171. testy::immediate("Logging", [](Expect expect) {
  172. cout << "Good." << endl;
  173. cerr << "Bad." << endl;
  174. expect(true).that("Tautology").equals(true);
  175.  
  176. // Logging: ................................. [passed]
  177. });
  178.  
  179. //
  180. // Sometimes, you have a bunch of things to test,
  181. // but you don't want 50 different tests. Thanks to
  182. // Expectation.suppress, you can test everything in one.
  183. //
  184.  
  185. testy::immediate("Suppression", [](Expect expect) {
  186. expect.suppress = true;
  187. cout << "Hello from the beginning." << endl;
  188. expect(true).that("Tautology").equals(true);
  189. expect(true).that("Contradiction").equals(false);
  190. expect(static_cast<string>("typo")).that("Spelling").equals("ytpo"); // C++ likes to default to const char* ...
  191. cerr << "Hello from the end." << endl;
  192.  
  193. // Suppression: ............................. [failed]
  194. // ! Failure: Contradiction
  195. // ! Expected: 1
  196. // ! Received: 0
  197. // ! Failure: Spelling
  198. // ! Expected: typo
  199. // ! Received: ytpo
  200. // | Hello from the beginning.
  201. // | Hello from the end.
  202.  
  203. });
  204.  
  205. return 0;
  206. }
Add Comment
Please, Sign In to add comment