Skip to content

[os_log][stdlib-private] Add tests for checking the correctness of buffer and format string construction in the new OS log APIs. #23437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions stdlib/private/OSLog/OSLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,28 @@ internal func osLog(
}
bufferMemory.deallocate()
}

/// A test helper that constructs a byte buffer and a format string from an
/// instance of `OSLogMessage` using the same logic as the function `osLog`,
/// and applies a given `assertion` to the constructed format string and
/// byte buffer. This function should be used only in tests.
/// - Parameters:
/// - message: An instance of `OSLogMessage` created from string interpolation
/// - assertion: A closure that takes a format string and a pointer to a
/// byte buffer and asserts a condition.
public // @testable
func _checkFormatStringAndBuffer(
_ message: OSLogMessage,
with assertion: (String, UnsafeBufferPointer<UInt8>) -> Void
) {
let bufferSize = message.bufferSize
let bufferMemory = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
var builder = OSLogByteBufferBuilder(bufferMemory)
message.serializeArguments(into: &builder)

assertion(
message.formatString,
UnsafeBufferPointer(start: UnsafePointer(bufferMemory), count: bufferSize))

bufferMemory.deallocate()
}
Loading