|
1 | | -package com.vladsch.flexmark.core.test.util.parser; |
2 | | - |
3 | | -import com.vladsch.flexmark.parser.MutableListOptions; |
4 | | -import com.vladsch.flexmark.parser.Parser; |
5 | | -import com.vladsch.flexmark.parser.ParserEmulationProfile; |
6 | | -import com.vladsch.flexmark.util.data.MutableDataSet; |
7 | | -import org.junit.Test; |
8 | | - |
9 | | -import java.io.ByteArrayOutputStream; |
10 | | -import java.io.InputStream; |
11 | | - |
12 | | -import static org.junit.Assert.assertArrayEquals; |
13 | | -import static org.junit.Assert.assertEquals; |
14 | | -import static org.junit.Assert.assertFalse; |
15 | | -import static org.junit.Assert.assertNotNull; |
16 | | -import static org.junit.Assert.assertNotSame; |
17 | | -import static org.junit.Assert.assertSame; |
18 | | -import static org.junit.Assert.assertTrue; |
19 | | - |
20 | | -public class ParserEmulationProfileTest { |
21 | | - private static MutableDataSet resolve(ParserEmulationProfile profile) { |
22 | | - MutableDataSet options = new MutableDataSet(); |
23 | | - profile.setIn(options); |
24 | | - return options; |
25 | | - } |
26 | | - |
27 | | - @Test |
28 | | - public void everyProfileRecordsItsOwnIdentity() { |
29 | | - for (ParserEmulationProfile profile : ParserEmulationProfile.values()) { |
30 | | - assertSame("profile " + profile + " must record itself in PARSER_EMULATION_PROFILE", |
31 | | - profile, Parser.PARSER_EMULATION_PROFILE.get(resolve(profile))); |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - @Test |
36 | | - public void everyProfileResolvesItsListOptions() { |
37 | | - for (ParserEmulationProfile profile : ParserEmulationProfile.values()) { |
38 | | - assertEquals("profile " + profile + " must resolve its list options into the data holder", |
39 | | - profile.getOptions().isEndOnDoubleBlank(), |
40 | | - Parser.LISTS_END_ON_DOUBLE_BLANK.get(resolve(profile))); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * CommonMark 0.26 removed the "two blank lines end a list" rule, so the 0.26 profile must not enable it. |
46 | | - * See the 0.25 to 0.26 specification diff at https://spec.commonmark.org/0.26/changes.html |
47 | | - */ |
48 | | - @Test |
49 | | - public void commonMark_0_26_doesNotEndListsOnDoubleBlank() { |
50 | | - assertFalse(ParserEmulationProfile.COMMONMARK_0_26.getOptions().isEndOnDoubleBlank()); |
51 | | - assertFalse(Parser.LISTS_END_ON_DOUBLE_BLANK.get(resolve(ParserEmulationProfile.COMMONMARK_0_26))); |
52 | | - } |
53 | | - |
54 | | - /** |
55 | | - * The complete set of list options written by setIn must equal the profile's own list options, not just |
56 | | - * the endOnDoubleBlank flag. |
57 | | - */ |
58 | | - @Test |
59 | | - public void everyProfileResolvesAllOfItsListOptions() { |
60 | | - for (ParserEmulationProfile profile : ParserEmulationProfile.values()) { |
61 | | - assertEquals("profile " + profile + " must resolve all of its list options into the data holder", |
62 | | - profile.getOptions(), new MutableListOptions(resolve(profile))); |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - /** |
67 | | - * The defaults of the list options keep the behaviour before CommonMark 0.29, which is enabled only by the |
68 | | - * 0.29 profile through LISTS_NO_ITEM_AT_CODE_INDENT. Except for that option, default list options equal the |
69 | | - * ones of the CommonMark latest profile. |
70 | | - */ |
71 | | - @Test |
72 | | - public void defaultListOptionsEqualTheCommonMarkLatestProfileOptions() { |
73 | | - MutableListOptions latest = ParserEmulationProfile.COMMONMARK_LATEST.getOptions(); |
74 | | - assertTrue(latest.isNoItemAtCodeIndent()); |
75 | | - assertEquals(latest, new MutableListOptions((com.vladsch.flexmark.util.data.DataHolder) null)); |
76 | | - } |
77 | | - |
78 | | - @Test |
79 | | - public void versionedCommonMarkProfilesBelongToTheCommonMarkFamily() { for (ParserEmulationProfile profile : new ParserEmulationProfile[]{ |
80 | | - ParserEmulationProfile.COMMONMARK_0_26, |
81 | | - ParserEmulationProfile.COMMONMARK_0_27, |
82 | | - ParserEmulationProfile.COMMONMARK_0_28, |
83 | | - ParserEmulationProfile.COMMONMARK_0_29, |
84 | | - ParserEmulationProfile.COMMONMARK_0_30, |
85 | | - ParserEmulationProfile.COMMONMARK_0_31_2, |
86 | | - }) { |
87 | | - assertSame(ParserEmulationProfile.COMMONMARK, profile.family); |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * COMMONMARK_LATEST must alias a concrete implemented version, never the COMMONMARK family sentinel. |
93 | | - */ |
94 | | - @Test |
95 | | - public void commonMarkLatestAliasesAConcreteVersionProfile() { |
96 | | - assertNotSame(ParserEmulationProfile.COMMONMARK, ParserEmulationProfile.COMMONMARK_LATEST); |
97 | | - assertSame(ParserEmulationProfile.COMMONMARK, ParserEmulationProfile.COMMONMARK_LATEST.family); |
98 | | - } |
99 | | - |
100 | | - @Test |
101 | | - public void commonMarkLatestIsTheDefaultEmulationProfile() { |
102 | | - assertSame(ParserEmulationProfile.COMMONMARK_LATEST, Parser.PARSER_EMULATION_PROFILE.get(null)); |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Advancing COMMONMARK_LATEST to a newer specification version is only allowed once that version's full |
107 | | - * specification test passes, and the default spec.txt resource must track it. |
108 | | - */ |
109 | | - @Test |
110 | | - public void commonMarkLatestMatchesTheDefaultSpecificationResource() throws Exception { |
111 | | - assertSame("COMMONMARK_LATEST must be advanced together with spec.txt and VERSION.md", |
112 | | - ParserEmulationProfile.COMMONMARK_0_31_2, ParserEmulationProfile.COMMONMARK_LATEST); |
113 | | - assertArrayEquals("spec.txt must be a copy of the spec file of the version COMMONMARK_LATEST points at", |
114 | | - readSpecResource("/spec.0.31.2.txt"), readSpecResource("/spec.txt")); |
115 | | - } |
116 | | - |
117 | | - private static byte[] readSpecResource(String name) throws Exception { |
118 | | - try (InputStream stream = ParserEmulationProfileTest.class.getResourceAsStream(name)) { |
119 | | - assertNotNull("missing specification resource " + name, stream); |
120 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
121 | | - byte[] buffer = new byte[8192]; |
122 | | - int read; |
123 | | - while ((read = stream.read(buffer)) != -1) out.write(buffer, 0, read); |
124 | | - return out.toByteArray(); |
125 | | - } |
126 | | - } |
127 | | -} |
| 1 | +package com.vladsch.flexmark.core.test.util.parser; |
| 2 | + |
| 3 | +import com.vladsch.flexmark.parser.MutableListOptions; |
| 4 | +import com.vladsch.flexmark.parser.Parser; |
| 5 | +import com.vladsch.flexmark.parser.ParserEmulationProfile; |
| 6 | +import com.vladsch.flexmark.util.data.MutableDataSet; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.InputStream; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertArrayEquals; |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertFalse; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertNotSame; |
| 17 | +import static org.junit.Assert.assertSame; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | + |
| 20 | +public class ParserEmulationProfileTest { |
| 21 | + private static MutableDataSet resolve(ParserEmulationProfile profile) { |
| 22 | + MutableDataSet options = new MutableDataSet(); |
| 23 | + profile.setIn(options); |
| 24 | + return options; |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void everyProfileRecordsItsOwnIdentity() { |
| 29 | + for (ParserEmulationProfile profile : ParserEmulationProfile.values()) { |
| 30 | + assertSame("profile " + profile + " must record itself in PARSER_EMULATION_PROFILE", |
| 31 | + profile, Parser.PARSER_EMULATION_PROFILE.get(resolve(profile))); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void everyProfileResolvesItsListOptions() { |
| 37 | + for (ParserEmulationProfile profile : ParserEmulationProfile.values()) { |
| 38 | + assertEquals("profile " + profile + " must resolve its list options into the data holder", |
| 39 | + profile.getOptions().isEndOnDoubleBlank(), |
| 40 | + Parser.LISTS_END_ON_DOUBLE_BLANK.get(resolve(profile))); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * CommonMark 0.26 removed the "two blank lines end a list" rule, so the 0.26 profile must not enable it. |
| 46 | + * See the 0.25 to 0.26 specification diff at https://spec.commonmark.org/0.26/changes.html |
| 47 | + */ |
| 48 | + @Test |
| 49 | + public void commonMark_0_26_doesNotEndListsOnDoubleBlank() { |
| 50 | + assertFalse(ParserEmulationProfile.COMMONMARK_0_26.getOptions().isEndOnDoubleBlank()); |
| 51 | + assertFalse(Parser.LISTS_END_ON_DOUBLE_BLANK.get(resolve(ParserEmulationProfile.COMMONMARK_0_26))); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * The complete set of list options written by setIn must equal the profile's own list options, not just |
| 56 | + * the endOnDoubleBlank flag. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void everyProfileResolvesAllOfItsListOptions() { |
| 60 | + for (ParserEmulationProfile profile : ParserEmulationProfile.values()) { |
| 61 | + assertEquals("profile " + profile + " must resolve all of its list options into the data holder", |
| 62 | + profile.getOptions(), new MutableListOptions(resolve(profile))); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * The defaults of the list options keep the behaviour before CommonMark 0.29, which is enabled only by the |
| 68 | + * 0.29 profile through LISTS_NO_ITEM_AT_CODE_INDENT. Except for that option, default list options equal the |
| 69 | + * ones of the CommonMark latest profile. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void defaultListOptionsEqualTheCommonMarkLatestProfileOptions() { |
| 73 | + MutableListOptions latest = ParserEmulationProfile.COMMONMARK_LATEST.getOptions(); |
| 74 | + assertTrue(latest.isNoItemAtCodeIndent()); |
| 75 | + assertEquals(latest, new MutableListOptions((com.vladsch.flexmark.util.data.DataHolder) null)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void versionedCommonMarkProfilesBelongToTheCommonMarkFamily() { for (ParserEmulationProfile profile : new ParserEmulationProfile[]{ |
| 80 | + ParserEmulationProfile.COMMONMARK_0_26, |
| 81 | + ParserEmulationProfile.COMMONMARK_0_27, |
| 82 | + ParserEmulationProfile.COMMONMARK_0_28, |
| 83 | + ParserEmulationProfile.COMMONMARK_0_29, |
| 84 | + ParserEmulationProfile.COMMONMARK_0_30, |
| 85 | + ParserEmulationProfile.COMMONMARK_0_31_2, |
| 86 | + }) { |
| 87 | + assertSame(ParserEmulationProfile.COMMONMARK, profile.family); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * COMMONMARK_LATEST must alias a concrete implemented version, never the COMMONMARK family sentinel. |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void commonMarkLatestAliasesAConcreteVersionProfile() { |
| 96 | + assertNotSame(ParserEmulationProfile.COMMONMARK, ParserEmulationProfile.COMMONMARK_LATEST); |
| 97 | + assertSame(ParserEmulationProfile.COMMONMARK, ParserEmulationProfile.COMMONMARK_LATEST.family); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void commonMarkLatestIsTheDefaultEmulationProfile() { |
| 102 | + assertSame(ParserEmulationProfile.COMMONMARK_LATEST, Parser.PARSER_EMULATION_PROFILE.get(null)); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Advancing COMMONMARK_LATEST to a newer specification version is only allowed once that version's full |
| 107 | + * specification test passes, and the default spec.txt resource must track it. |
| 108 | + */ |
| 109 | + @Test |
| 110 | + public void commonMarkLatestMatchesTheDefaultSpecificationResource() throws Exception { |
| 111 | + assertSame("COMMONMARK_LATEST must be advanced together with spec.txt and VERSION.md", |
| 112 | + ParserEmulationProfile.COMMONMARK_0_31_2, ParserEmulationProfile.COMMONMARK_LATEST); |
| 113 | + assertArrayEquals("spec.txt must be a copy of the spec file of the version COMMONMARK_LATEST points at", |
| 114 | + readSpecResource("/spec.0.31.2.txt"), readSpecResource("/spec.txt")); |
| 115 | + } |
| 116 | + |
| 117 | + private static byte[] readSpecResource(String name) throws Exception { |
| 118 | + try (InputStream stream = ParserEmulationProfileTest.class.getResourceAsStream(name)) { |
| 119 | + assertNotNull("missing specification resource " + name, stream); |
| 120 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 121 | + byte[] buffer = new byte[8192]; |
| 122 | + int read; |
| 123 | + while ((read = stream.read(buffer)) != -1) out.write(buffer, 0, read); |
| 124 | + return out.toByteArray(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments