|
| 1 | +#include <cmark.h> |
| 2 | +#include "test_utils.h" |
| 3 | + |
| 4 | +int test_link_basic() { |
| 5 | + return test_xml( |
| 6 | + "This is a [link](http://example.com)", |
| 7 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 8 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 9 | + "<document xmlns=\"http://commonmark.org/xml/1.0\">\n" |
| 10 | + " <paragraph>\n" |
| 11 | + " <text xml:space=\"preserve\">This is a </text>\n" |
| 12 | + " <link destination=\"http://example.com\">\n" |
| 13 | + " <text xml:space=\"preserve\">link</text>\n" |
| 14 | + " </link>\n" |
| 15 | + " </paragraph>\n" |
| 16 | + "</document>\n", |
| 17 | + CMARK_OPT_DEFAULT); |
| 18 | +} |
| 19 | + |
| 20 | +int test_link_with_title() { |
| 21 | + return test_xml( |
| 22 | + "Here's a [link](http://example.com \"Example Site\")", |
| 23 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 24 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 25 | + "<document xmlns=\"http://commonmark.org/xml/1.0\">\n" |
| 26 | + " <paragraph>\n" |
| 27 | + " <text xml:space=\"preserve\">Here's a </text>\n" |
| 28 | + " <link destination=\"http://example.com\" title=\"Example Site\">\n" |
| 29 | + " <text xml:space=\"preserve\">link</text>\n" |
| 30 | + " </link>\n" |
| 31 | + " </paragraph>\n" |
| 32 | + "</document>\n", |
| 33 | + CMARK_OPT_DEFAULT); |
| 34 | +} |
| 35 | + |
| 36 | +int test_reference_link() { |
| 37 | + return test_xml( |
| 38 | + "This is a [reference link][1]\n\n[1]: http://example.com", |
| 39 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 40 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 41 | + "<document xmlns=\"http://commonmark.org/xml/1.0\">\n" |
| 42 | + " <paragraph>\n" |
| 43 | + " <text xml:space=\"preserve\">This is a </text>\n" |
| 44 | + " <link destination=\"http://example.com\">\n" |
| 45 | + " <text xml:space=\"preserve\">reference link</text>\n" |
| 46 | + " </link>\n" |
| 47 | + " </paragraph>\n" |
| 48 | + "</document>\n", |
| 49 | + CMARK_OPT_DEFAULT); |
| 50 | +} |
| 51 | + |
| 52 | +int test_link_with_escaped_brackets() { |
| 53 | + return test_xml( |
| 54 | + "This link has \\[escaped brackets\\]: [link](http://example.com)", |
| 55 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 56 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 57 | + "<document xmlns=\"http://commonmark.org/xml/1.0\">\n" |
| 58 | + " <paragraph>\n" |
| 59 | + " <text xml:space=\"preserve\">This link has [escaped brackets]: </text>\n" |
| 60 | + " <link destination=\"http://example.com\">\n" |
| 61 | + " <text xml:space=\"preserve\">link</text>\n" |
| 62 | + " </link>\n" |
| 63 | + " </paragraph>\n" |
| 64 | + "</document>\n", |
| 65 | + CMARK_OPT_DEFAULT); |
| 66 | +} |
| 67 | + |
| 68 | +int test_link_invalid() { |
| 69 | + return test_xml( |
| 70 | + "This is not a [link(http://example.com)", |
| 71 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 72 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 73 | + "<document xmlns=\"http://commonmark.org/xml/1.0\">\n" |
| 74 | + " <paragraph>\n" |
| 75 | + " <text xml:space=\"preserve\">This is not a [link(http://example.com)</text>\n" |
| 76 | + " </paragraph>\n" |
| 77 | + "</document>\n", |
| 78 | + CMARK_OPT_DEFAULT); |
| 79 | +} |
| 80 | + |
| 81 | +int test_reference_definitions_only() { |
| 82 | + return test_xml( |
| 83 | + "[ref]: http://example.com\n" |
| 84 | + "[other]: http://example.org", |
| 85 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 86 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 87 | + "<document xmlns=\"http://commonmark.org/xml/1.0\" />\n", |
| 88 | + CMARK_OPT_DEFAULT); |
| 89 | +} |
| 90 | + |
| 91 | +int test_reference_definitions_with_text() { |
| 92 | + return test_xml( |
| 93 | + "[ref]: http://example.com\n\n" |
| 94 | + "This is a [ref] link\n\n" |
| 95 | + "[other]: http://example.org\n\n" |
| 96 | + "And [other] link", |
| 97 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 98 | + "<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n" |
| 99 | + "<document xmlns=\"http://commonmark.org/xml/1.0\">\n" |
| 100 | + " <paragraph>\n" |
| 101 | + " <text xml:space=\"preserve\">This is a </text>\n" |
| 102 | + " <link destination=\"http://example.com\">\n" |
| 103 | + " <text xml:space=\"preserve\">ref</text>\n" |
| 104 | + " </link>\n" |
| 105 | + " <text xml:space=\"preserve\"> link</text>\n" |
| 106 | + " </paragraph>\n" |
| 107 | + " <paragraph>\n" |
| 108 | + " <text xml:space=\"preserve\">And </text>\n" |
| 109 | + " <link destination=\"http://example.org\">\n" |
| 110 | + " <text xml:space=\"preserve\">other</text>\n" |
| 111 | + " </link>\n" |
| 112 | + " <text xml:space=\"preserve\"> link</text>\n" |
| 113 | + " </paragraph>\n" |
| 114 | + "</document>\n", |
| 115 | + CMARK_OPT_DEFAULT); |
| 116 | +} |
| 117 | + |
| 118 | +int main() { |
| 119 | + CASE(test_link_basic); |
| 120 | + CASE(test_link_with_title); |
| 121 | + CASE(test_reference_link); |
| 122 | + CASE(test_link_with_escaped_brackets); |
| 123 | + CASE(test_link_invalid); |
| 124 | + CASE(test_reference_definitions_only); |
| 125 | + CASE(test_reference_definitions_with_text); |
| 126 | + return 0; |
| 127 | +} |
0 commit comments