@@ -102,6 +102,7 @@ class RenderContentMetadataTests: XCTestCase {
102102 XCTAssertEqual ( t. rows [ 0 ] . cells. map ( renderCell) , [ " Column 1 " , " Column 2 " ] )
103103 XCTAssertEqual ( t. rows [ 1 ] . cells. map ( renderCell) , [ " Cell 1 " , " Cell 2 " ] )
104104 XCTAssertEqual ( t. rows [ 2 ] . cells. map ( renderCell) , [ " Cell 3 " , " Cell 4 " ] )
105+ XCTAssertNil ( t. alignments)
105106 default : XCTFail ( " Unexpected element " )
106107 }
107108 }
@@ -152,11 +153,127 @@ class RenderContentMetadataTests: XCTestCase {
152153 for expectedData in expectedExtendedData {
153154 XCTAssert ( t. extendedData. contains ( expectedData) )
154155 }
156+ XCTAssertNil ( t. alignments)
155157 default : XCTFail ( " Unexpected element " )
156158 }
157159
158160 try assertRoundTripCoding ( renderedTable)
159161 }
162+
163+ func testRenderingTableColumnAlignments( ) throws {
164+ let ( bundle, context) = try testBundleAndContext ( named: " TestBundle " )
165+ var renderContentCompiler = RenderContentCompiler ( context: context, bundle: bundle, identifier: ResolvedTopicReference ( bundleIdentifier: bundle. identifier, path: " /path " , fragment: nil , sourceLanguage: . swift) )
166+
167+ let source = """
168+ | one | two | three | four |
169+ | :-- | --: | :---: | ---- |
170+ | one | two | three | four |
171+ """
172+ let document = Document ( parsing: source)
173+
174+ // Verifies that a markdown table renders correctly.
175+
176+ let result = try XCTUnwrap ( renderContentCompiler. visit ( document. child ( at: 0 ) !) )
177+ let renderedTable = try XCTUnwrap ( result. first as? RenderBlockContent )
178+
179+ let renderCell : ( [ RenderBlockContent ] ) -> String = { cell in
180+ return cell. reduce ( into: " " ) { ( result, element) in
181+ switch element {
182+ case . paragraph( let p) :
183+ guard let para = p. inlineContent. first else { return }
184+ result. append ( para. plainText)
185+ default : XCTFail ( " Unexpected element " ) ; return
186+ }
187+ }
188+ }
189+
190+ switch renderedTable {
191+ case . table( let t) :
192+ XCTAssertEqual ( t. header, . row)
193+ XCTAssertEqual ( t. rows. count, 2 )
194+ guard t. rows. count == 2 else { return }
195+ XCTAssertEqual ( t. rows [ 0 ] . cells. map ( renderCell) , [ " one " , " two " , " three " , " four " ] )
196+ XCTAssertEqual ( t. rows [ 1 ] . cells. map ( renderCell) , [ " one " , " two " , " three " , " four " ] )
197+ XCTAssertEqual ( t. alignments, [ . left, . right, . center, . unset] )
198+ default : XCTFail ( " Unexpected element " )
199+ }
200+
201+ try assertRoundTripCoding ( renderedTable)
202+ }
203+
204+ /// Verifies that a table with `nil` alignments and a table with all-unset alignments still compare as equal.
205+ func testRenderedTableEquality( ) throws {
206+ let ( bundle, context) = try testBundleAndContext ( named: " TestBundle " )
207+ var renderContentCompiler = RenderContentCompiler ( context: context, bundle: bundle, identifier: ResolvedTopicReference ( bundleIdentifier: bundle. identifier, path: " /path " , fragment: nil , sourceLanguage: . swift) )
208+
209+ let source = """
210+ | Column 1 | Column 2 |
211+ | ------------- | ------------- |
212+ | Cell 1 | Cell 2 |
213+ | Cell 3 | Cell 4 |
214+ """
215+ let document = Document ( parsing: source)
216+
217+ let result = try XCTUnwrap ( renderContentCompiler. visit ( document. child ( at: 0 ) !) )
218+ let renderedTable = try XCTUnwrap ( result. first as? RenderBlockContent )
219+ guard case let . table( decodedTable) = renderedTable else {
220+ XCTFail ( " Unexpected RenderBlockContent element " )
221+ return
222+ }
223+ XCTAssertNil ( decodedTable. alignments)
224+ var modifiedTable = decodedTable
225+ modifiedTable. alignments = [ . unset, . unset]
226+
227+ XCTAssertEqual ( decodedTable, modifiedTable)
228+ }
229+
230+ /// Verifies that two tables with otherwise-identical contents but different column alignments compare as unequal.
231+ func testRenderedTableInequality( ) throws {
232+ let ( bundle, context) = try testBundleAndContext ( named: " TestBundle " )
233+ var renderContentCompiler = RenderContentCompiler ( context: context, bundle: bundle, identifier: ResolvedTopicReference ( bundleIdentifier: bundle. identifier, path: " /path " , fragment: nil , sourceLanguage: . swift) )
234+
235+ let decodedTableWithUnsetColumns : RenderBlockContent . Table
236+ do {
237+ let source = """
238+ | Column 1 | Column 2 |
239+ | ------------- | ------------- |
240+ | Cell 1 | Cell 2 |
241+ | Cell 3 | Cell 4 |
242+ """
243+ let document = Document ( parsing: source)
244+
245+ let result = try XCTUnwrap ( renderContentCompiler. visit ( document. child ( at: 0 ) !) )
246+ let renderedTable = try XCTUnwrap ( result. first as? RenderBlockContent )
247+ guard case let . table( decodedTable) = renderedTable else {
248+ XCTFail ( " Unexpected RenderBlockContent element " )
249+ return
250+ }
251+ decodedTableWithUnsetColumns = decodedTable
252+ }
253+
254+ let decodedTableWithLeftColumns : RenderBlockContent . Table
255+ do {
256+ let source = """
257+ | Column 1 | Column 2 |
258+ | :------------ | :------------ |
259+ | Cell 1 | Cell 2 |
260+ | Cell 3 | Cell 4 |
261+ """
262+ let document = Document ( parsing: source)
263+
264+ // Verifies that a markdown table renders correctly.
265+
266+ let result = try XCTUnwrap ( renderContentCompiler. visit ( document. child ( at: 0 ) !) )
267+ let renderedTable = try XCTUnwrap ( result. first as? RenderBlockContent )
268+ guard case let . table( decodedTable) = renderedTable else {
269+ XCTFail ( " Unexpected RenderBlockContent element " )
270+ return
271+ }
272+ decodedTableWithLeftColumns = decodedTable
273+ }
274+
275+ XCTAssertNotEqual ( decodedTableWithUnsetColumns, decodedTableWithLeftColumns)
276+ }
160277
161278 func testStrikethrough( ) throws {
162279 let ( bundle, context) = try testBundleAndContext ( named: " TestBundle " )
0 commit comments