@@ -161,5 +161,103 @@ class SampleDownloadTests: XCTestCase {
161
161
162
162
XCTAssertEqual ( origIdent, decodedIdent)
163
163
}
164
-
164
+
165
+ func testSampleDownloadRelativeURL( ) throws {
166
+ let ( bundle, context) = try testBundleAndContext ( named: " SampleBundle " )
167
+ let reference = ResolvedTopicReference (
168
+ bundleIdentifier: bundle. identifier,
169
+ path: " /documentation/SampleBundle/RelativeURLSample " ,
170
+ sourceLanguage: . swift
171
+ )
172
+ let article = try XCTUnwrap ( context. entity ( with: reference) . semantic as? Article )
173
+ var translator = RenderNodeTranslator (
174
+ context: context,
175
+ bundle: bundle,
176
+ identifier: reference,
177
+ source: nil
178
+ )
179
+ let renderNode = try XCTUnwrap ( translator. visitArticle ( article) as? RenderNode )
180
+ let sampleCodeDownload = try XCTUnwrap ( renderNode. sampleDownload)
181
+ guard case . reference( identifier: let ident, isActive: true , overridingTitle: " Download " , overridingTitleInlineContent: nil ) = sampleCodeDownload. action else {
182
+ XCTFail ( " Unexpected action in callToAction " )
183
+ return
184
+ }
185
+ XCTAssertEqual ( ident. identifier, " files/ExternalSample.zip " )
186
+
187
+ // Ensure that the encoded URL still references the entered URL
188
+ let downloadReference = try XCTUnwrap ( renderNode. references [ ident. identifier] as? ExternalLocationReference )
189
+
190
+ let encoder = JSONEncoder ( )
191
+ let decoder = JSONDecoder ( )
192
+
193
+ let encodedReference = try encoder. encode ( downloadReference)
194
+ let decodedReference = try decoder. decode ( DownloadReference . self, from: encodedReference)
195
+
196
+ XCTAssertEqual ( decodedReference. url. description, " files/ExternalSample.zip " )
197
+ }
198
+
199
+ func testExternalLocationRoundtrip( ) throws {
200
+ let ( bundle, context) = try testBundleAndContext ( named: " SampleBundle " )
201
+ let reference = ResolvedTopicReference (
202
+ bundleIdentifier: bundle. identifier,
203
+ path: " /documentation/SampleBundle/RelativeURLSample " ,
204
+ sourceLanguage: . swift
205
+ )
206
+ let article = try XCTUnwrap ( context. entity ( with: reference) . semantic as? Article )
207
+ var translator = RenderNodeTranslator (
208
+ context: context,
209
+ bundle: bundle,
210
+ identifier: reference,
211
+ source: nil
212
+ )
213
+ let renderNode = try XCTUnwrap ( translator. visitArticle ( article) as? RenderNode )
214
+ let sampleCodeDownload = try XCTUnwrap ( renderNode. sampleDownload)
215
+ guard case . reference( identifier: let ident, isActive: true , overridingTitle: " Download " , overridingTitleInlineContent: nil ) = sampleCodeDownload. action else {
216
+ XCTFail ( " Unexpected action in callToAction " )
217
+ return
218
+ }
219
+ XCTAssertEqual ( ident. identifier, " files/ExternalSample.zip " )
220
+
221
+ // Make sure that the ExternalLocationReference we get can round-trip as itself as well as through a DownloadReference
222
+ let downloadReference = try XCTUnwrap ( renderNode. references [ ident. identifier] as? ExternalLocationReference )
223
+
224
+ let encoder = JSONEncoder ( )
225
+ encoder. outputFormatting. insert ( . sortedKeys)
226
+ let decoder = JSONDecoder ( )
227
+
228
+ let encodedReference = try encoder. encode ( downloadReference)
229
+
230
+ // ExternalLocationReference -> ExternalLocationReference
231
+ // The encoded JSON should be the same before and after re-encoding.
232
+ do {
233
+ let decodedReference = try decoder. decode ( ExternalLocationReference . self, from: encodedReference)
234
+ let reEncodedReference = try encoder. encode ( decodedReference)
235
+
236
+ let firstJson = String ( data: encodedReference, encoding: . utf8)
237
+ let finalJson = String ( data: reEncodedReference, encoding: . utf8)
238
+
239
+ XCTAssertEqual ( firstJson, finalJson)
240
+ }
241
+
242
+ // ExternalLocationReference -> DownloadReference -> ExternalLocationReference
243
+ // The reference identifier should be the same all throughout, and the final ExternalLocationReference
244
+ // should encode to the same JSON as the initial reference.
245
+ do {
246
+ let decodedReference = try decoder. decode ( DownloadReference . self, from: encodedReference)
247
+
248
+ XCTAssertEqual ( decodedReference. identifier, downloadReference. identifier)
249
+
250
+ let encodedDownload = try encoder. encode ( decodedReference)
251
+ let reDecodedReference = try decoder. decode ( ExternalLocationReference . self, from: encodedDownload)
252
+
253
+ XCTAssertEqual ( reDecodedReference. identifier, downloadReference. identifier)
254
+
255
+ let reEncodedReference = try encoder. encode ( reDecodedReference)
256
+
257
+ let firstJson = String ( data: encodedReference, encoding: . utf8)
258
+ let finalJson = String ( data: reEncodedReference, encoding: . utf8)
259
+
260
+ XCTAssertEqual ( firstJson, finalJson)
261
+ }
262
+ }
165
263
}
0 commit comments