|
| 1 | +#include "ada.h" |
| 2 | + |
| 3 | +#include <cstdlib> |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | +#define TEST_START() \ |
| 7 | + do { \ |
| 8 | + std::cout << "> Running " << __func__ << " ..." << std::endl; \ |
| 9 | + } while (0); |
| 10 | +#define RUN_TEST(ACTUAL) \ |
| 11 | + do { \ |
| 12 | + if (!(ACTUAL)) { \ |
| 13 | + return false; \ |
| 14 | + } \ |
| 15 | + } while (0); |
| 16 | +#define TEST_FAIL(MESSAGE) \ |
| 17 | + do { \ |
| 18 | + std::cerr << "FAIL: " << (MESSAGE) << std::endl; \ |
| 19 | + return false; \ |
| 20 | + } while (0); |
| 21 | +#define TEST_SUCCEED() \ |
| 22 | + do { \ |
| 23 | + return true; \ |
| 24 | + } while (0); |
| 25 | +#define TEST_ASSERT(LHS, RHS, MESSAGE) \ |
| 26 | + do { \ |
| 27 | + if (LHS != RHS) { \ |
| 28 | + std::cerr << "Mismatch: '" << LHS << "' - '" << RHS << "'" << std::endl; \ |
| 29 | + TEST_FAIL(MESSAGE); \ |
| 30 | + } \ |
| 31 | + } while (0); \ |
| 32 | + |
| 33 | + |
| 34 | +bool set_host_should_return_false_sometimes() { |
| 35 | + TEST_START() |
| 36 | + ada::result r = ada::parse("mailto:a@b.com"); |
| 37 | + bool b = r->set_host("something"); |
| 38 | + TEST_ASSERT(b, false, "set_host should return false") |
| 39 | + TEST_SUCCEED() |
| 40 | +} |
| 41 | + |
| 42 | +bool set_host_should_return_true_sometimes() { |
| 43 | + TEST_START() |
| 44 | + ada::result r = ada::parse("https://www.google.com"); |
| 45 | + bool b = r->set_host("something"); |
| 46 | + TEST_ASSERT(b, true, "set_host should return true") |
| 47 | + TEST_SUCCEED() |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +bool set_hostname_should_return_false_sometimes() { |
| 52 | + TEST_START() |
| 53 | + ada::result r = ada::parse("mailto:a@b.com"); |
| 54 | + bool b = r->set_hostname("something"); |
| 55 | + TEST_ASSERT(b, false, "set_hostname should return false") |
| 56 | + TEST_SUCCEED() |
| 57 | +} |
| 58 | + |
| 59 | +bool set_hostname_should_return_true_sometimes() { |
| 60 | + TEST_START() |
| 61 | + ada::result r = ada::parse("https://www.google.com"); |
| 62 | + bool b = r->set_hostname("something"); |
| 63 | + TEST_ASSERT(b, true, "set_hostname should return true") |
| 64 | + TEST_SUCCEED() |
| 65 | +} |
| 66 | + |
| 67 | +bool readme1() { |
| 68 | + TEST_START() |
| 69 | + ada::result url = ada::parse("https://www.google.com"); |
| 70 | + TEST_ASSERT(bool(url), true, "URL is valid") |
| 71 | + TEST_SUCCEED() |
| 72 | +} |
| 73 | + |
| 74 | +bool readme2() { |
| 75 | + TEST_START() |
| 76 | + ada::result url = ada::parse("https://www.google.com"); |
| 77 | + url->set_username("username"); |
| 78 | + url->set_password("password"); |
| 79 | + TEST_ASSERT(url->get_href(), "https://username:password@www.google.com/", "href returned bad result") |
| 80 | + TEST_SUCCEED() |
| 81 | +} |
| 82 | + |
| 83 | +bool readme3() { |
| 84 | + TEST_START() |
| 85 | + ada::result url = ada::parse("https://www.google.com"); |
| 86 | + url->set_protocol("wss"); |
| 87 | + TEST_ASSERT(url->get_protocol(), "wss:", "get_protocol returned bad result") |
| 88 | + TEST_ASSERT(url->get_href(), "wss://www.google.com/", "get_href returned bad result") |
| 89 | + |
| 90 | + TEST_SUCCEED() |
| 91 | +} |
| 92 | + |
| 93 | +bool readme4() { |
| 94 | + TEST_START() |
| 95 | + ada::result url = ada::parse("https://www.google.com"); |
| 96 | + url->set_host("github.com"); |
| 97 | + TEST_ASSERT(url->get_host(), "github.com", "get_host returned bad result") |
| 98 | + TEST_SUCCEED() |
| 99 | +} |
| 100 | + |
| 101 | +bool readme5() { |
| 102 | + TEST_START() |
| 103 | + ada::result url = ada::parse("https://www.google.com"); |
| 104 | + url->set_port("8080"); |
| 105 | + TEST_ASSERT(url->get_port(), "8080", "get_port returned bad result") |
| 106 | + TEST_SUCCEED() |
| 107 | +} |
| 108 | + |
| 109 | +bool readme6() { |
| 110 | + TEST_START() |
| 111 | + ada::result url = ada::parse("https://www.google.com"); |
| 112 | + url->set_pathname("/my-super-long-path"); |
| 113 | + TEST_ASSERT(url->get_pathname(), "/my-super-long-path", "get_pathname returned bad result") |
| 114 | + TEST_SUCCEED() |
| 115 | +} |
| 116 | + |
| 117 | +bool readme7() { |
| 118 | + TEST_START() |
| 119 | + ada::result url = ada::parse("https://www.google.com"); |
| 120 | + url->set_search("target=self"); |
| 121 | + TEST_ASSERT(url->get_search(), "?target=self", "get_pathname returned bad result"); |
| 122 | + TEST_SUCCEED() |
| 123 | +} |
| 124 | + |
| 125 | +bool readme8() { |
| 126 | + TEST_START() |
| 127 | + ada::result url = ada::parse("https://www.google.com"); |
| 128 | + url->set_hash("is-this-the-real-life"); |
| 129 | + TEST_ASSERT(url->get_hash(), "#is-this-the-real-life", "get_hash returned bad result"); |
| 130 | + TEST_SUCCEED() |
| 131 | +} |
| 132 | + |
| 133 | +int main() { |
| 134 | + bool success = set_host_should_return_false_sometimes() |
| 135 | + && set_host_should_return_true_sometimes() |
| 136 | + && set_hostname_should_return_false_sometimes() |
| 137 | + && set_hostname_should_return_true_sometimes() |
| 138 | + && readme1() && readme2() && readme3() |
| 139 | + && readme4() && readme5() && readme6() |
| 140 | + && readme7(); |
| 141 | + if(success) { return EXIT_SUCCESS; } |
| 142 | + return EXIT_FAILURE; |
| 143 | +} |
0 commit comments