RunicTestFile class

Namespace Runic.Application.Testing in Runic.Application.Testing 0.23.0

public sealed class RunicTestFile

An in-memory file that RunicTestFileDialogs hands out: the selection of an open dialog or the destination of a save dialog. It is never on disk.

Remarks

Leases follow the rules of the real ones: a commit replaces the content only when the file still holds what the application last saw, so RunicTestFile.Write or RunicTestFile.Delete during a test acts as another writer and makes the next commit report PlatformFailureCode.Conflict.

RunicTestFile.LocalPath is a fully qualified path that does not exist; read and write the file through its lease or through this object. The type is safe to use from any thread.

Constructors

RunicTestFile

public RunicTestFile(string name, ReadOnlySpan<byte> content)

Creates a file with content, as an existing file the user selects.

Parameters

name

The display name, a filename without a path.

content

The file's bytes.

Exceptions

ArgumentException

name is empty or contains a path separator.

Properties

CommittedWrites

public IReadOnlyList<ImmutableArray<byte>> CommittedWrites { get; }

The bytes of each committed write, oldest first.

DisplayName

public string DisplayName { get; }

The display name, without a path.

Exists

public bool Exists { get; }

Whether the file exists: it was created with content or a commit or RunicTestFile.Write created it.

LocalPath

public string LocalPath { get; }

A fully qualified path that the file's document leases report; nothing exists there.

WriteUnavailable

public PlatformUnavailableReason? WriteUnavailable { get; set; }

Whether a lease of the file can begin a write: null, the default, lets it write; PlatformUnavailableReason.AtomicReplaceUnavailable acts as a read-only grant.

Remarks

ISaveFileLease.BeginWriteAsync then reports this reason.

Methods

Delete

public void Delete()

Deletes the file as another application would.

EnqueueCommitResult

public void EnqueueCommitResult(FileCommitResult result)

Scripts the outcome of the next commit that finds no conflict.

Parameters

result

NotCommitted leaves the file unchanged; CommitUnknown also leaves it unchanged (use RunicTestFile.EnqueueCommitResult for one that landed), and the lease then accepts both the previous and the staged bytes as its own, as a real lease does; Committed commits as an unscripted commit would.

EnqueueCommitResult

public void EnqueueCommitResult(FileCommitResult result, bool applied)

Scripts the outcome of the next commit that finds no conflict, and whether its write landed.

Parameters

result

The reported outcome.

applied

Whether the staged bytes replace the content. A real lease reports CommitUnknown exactly when the write may have landed, so both values are valid for it; Committed requires true and NotCommitted false.

Exceptions

ArgumentException

applied contradicts result.

FromText

public static RunicTestFile FromText(string name, string text)

Creates a file whose content is text in UTF-8, without a byte order mark.

Parameters

name

The display name, a filename without a path.

text

The file's text.

Returns

The file.

Missing

public static RunicTestFile Missing(string name)

Creates a file that does not exist yet, such as a new save destination.

Parameters

name

The display name, a filename without a path.

Returns

The absent file.

ReadAllBytes

public byte[] ReadAllBytes()

A copy of the file's current bytes.

Returns

The bytes.

Exceptions

FileNotFoundException

The file does not exist.

ReadAllText

public string ReadAllText()

The file's current content as UTF-8 text.

Returns

The text.

Exceptions

FileNotFoundException

The file does not exist.

Write

public void Write(ReadOnlySpan<byte> content)

Replaces the content as another writer would, so the next commit of a lease reports a conflict.

Parameters

content

The new bytes.

WriteText

public void WriteText(string text)

Replaces the content with UTF-8 text as another writer would.

Parameters

text

The new text.