Skip to content

Commit 469ede0

Browse files
committed
Add tests for every new overload added
1 parent afe2d35 commit 469ede0

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

Tests/SystemTests/SystemStringTests.swift

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,165 @@ final class SystemStringTest: XCTestCase {
252252

253253
}
254254

255+
extension SystemStringTest {
256+
func test_String_initWithArrayConversion() {
257+
let source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
258+
let str = String(platformString: source)
259+
source.withUnsafeBufferPointer {
260+
XCTAssertEqual(str, String(platformString: $0.baseAddress!))
261+
}
262+
}
263+
264+
@available(*, deprecated) // silence the warning for using a deprecated api
265+
func test_String_initWithStringConversion() {
266+
let source = "ab\0c"
267+
var str: String
268+
str = String(platformString: source)
269+
source.withPlatformString {
270+
XCTAssertEqual(str, String(platformString: $0))
271+
}
272+
str = String(platformString: "")
273+
XCTAssertEqual(str.isEmpty, true)
274+
}
275+
276+
@available(*, deprecated) // silence the warning for using a deprecated api
277+
func test_String_initWithInoutConversion() {
278+
var c: CInterop.PlatformChar = 0
279+
let str = String(platformString: &c)
280+
// Any other value of `c` would violate the null-terminated precondition
281+
XCTAssertEqual(str.isEmpty, true)
282+
}
283+
284+
func test_String_validatingPlatformStringWithArrayConversion() {
285+
var source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
286+
var str: String?
287+
str = String(validatingPlatformString: source)
288+
source.withUnsafeBufferPointer {
289+
XCTAssertEqual(str, String(validatingPlatformString: $0.baseAddress!))
290+
}
291+
source[1] = CInterop.PlatformChar(truncatingIfNeeded: 0xffff)
292+
str = String(validatingPlatformString: source)
293+
XCTAssertNil(str)
294+
}
295+
296+
@available(*, deprecated) // silence the warning for using a deprecated api
297+
func test_String_validatingPlatformStringWithStringConversion() {
298+
let source = "ab\0c"
299+
var str: String?
300+
str = String(validatingPlatformString: source)
301+
XCTAssertNotNil(str)
302+
source.withPlatformString {
303+
XCTAssertEqual(str, String.init(validatingPlatformString: $0))
304+
}
305+
str = String(validatingPlatformString: "")
306+
XCTAssertNotNil(str)
307+
XCTAssertEqual(str?.isEmpty, true)
308+
}
309+
310+
@available(*, deprecated) // silence the warning for using a deprecated api
311+
func test_String_validatingPlatformStringWithInoutConversion() {
312+
var c: CInterop.PlatformChar = 0
313+
let str = String(validatingPlatformString: &c)
314+
// Any other value of `c` would violate the null-terminated precondition
315+
XCTAssertNotNil(str)
316+
XCTAssertEqual(str?.isEmpty, true)
317+
}
318+
319+
func test_FilePath_initWithArrayConversion() {
320+
let source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
321+
let path = FilePath(platformString: source)
322+
source.withUnsafeBufferPointer {
323+
XCTAssertEqual(path, FilePath(platformString: $0.baseAddress!))
324+
}
325+
}
326+
327+
@available(*, deprecated) // silence the warning for using a deprecated api
328+
func test_FilePath_initWithStringConversion() {
329+
let source = "ab\0c"
330+
var path: FilePath
331+
path = FilePath(platformString: source)
332+
source.withPlatformString {
333+
XCTAssertEqual(path, FilePath(platformString: $0))
334+
}
335+
path = FilePath(platformString: "")
336+
XCTAssertEqual(path.string.isEmpty, true)
337+
}
338+
339+
@available(*, deprecated) // silence the warning for using a deprecated api
340+
func test_FilePath_initWithInoutConversion() {
341+
var c: CInterop.PlatformChar = 0
342+
let path = FilePath(platformString: &c)
343+
// Any other value of `c` would violate the null-terminated precondition
344+
XCTAssertEqual(path.string.isEmpty, true)
345+
}
346+
347+
func test_FilePathComponent_initWithArrayConversion() {
348+
var source: [CInterop.PlatformChar] = [0x61, 0x62, 0, 0x63]
349+
var component: FilePath.Component?
350+
component = FilePath.Component(platformString: source)
351+
source.withUnsafeBufferPointer {
352+
XCTAssertEqual(component, .init(platformString: $0.baseAddress!))
353+
}
354+
source[1] = CInterop.PlatformChar(truncatingIfNeeded: 0xffff)
355+
component = FilePath.Component(platformString: source)
356+
source.withUnsafeBufferPointer {
357+
XCTAssertEqual(component, .init(platformString: $0.baseAddress!))
358+
}
359+
}
360+
361+
@available(*, deprecated) // silence the warning for using a deprecated api
362+
func test_FilePathComponent_initWithStringConversion() {
363+
let source = "ab\0c"
364+
var component: FilePath.Component?
365+
component = FilePath.Component(platformString: source)
366+
source.withPlatformString {
367+
XCTAssertEqual(component, FilePath.Component(platformString: $0))
368+
}
369+
component = FilePath.Component(platformString: "")
370+
XCTAssertNil(component)
371+
}
372+
373+
@available(*, deprecated) // silence the warning for using a deprecated api
374+
func test_FilePathComponent_initWithInoutConversion() {
375+
var c: CInterop.PlatformChar = 0
376+
let component = FilePath.Component(platformString: &c)
377+
XCTAssertNil(component)
378+
}
379+
380+
func test_FilePathRoot_initWithArrayConversion() {
381+
let source: [CInterop.PlatformChar]
382+
#if os(Windows)
383+
source = [0x41, 0x3a, 0x5c, 0, 0x7f]
384+
#else // unix
385+
source = [0x2f, 0, 0x7f]
386+
#endif
387+
var root: FilePath.Root?
388+
root = FilePath.Root(platformString: source)
389+
source.withUnsafeBufferPointer {
390+
XCTAssertEqual(root, FilePath.Root(platformString: $0.baseAddress!))
391+
}
392+
}
393+
394+
@available(*, deprecated) // silence the warning for using a deprecated api
395+
func test_FilePathRoot_initWithStringConversion() {
396+
#if os(Windows)
397+
let source = "C:\\\0 and the rest"
398+
#else // unix
399+
let source = "/\0 and the rest"
400+
#endif
401+
var root: FilePath.Root?
402+
root = FilePath.Root(platformString: source)
403+
source.withPlatformString {
404+
XCTAssertEqual(root, FilePath.Root(platformString: $0))
405+
}
406+
root = FilePath.Root(platformString: "")
407+
XCTAssertNil(root)
408+
}
409+
410+
@available(*, deprecated) // silence the warning for using a deprecated api
411+
func test_FilePathRoot_initWithInoutConversion() {
412+
var c: CInterop.PlatformChar = 0
413+
let root = FilePath.Root(platformString: &c)
414+
XCTAssertNil(root)
415+
}
416+
}

0 commit comments

Comments
 (0)