public async Task TestCreateFromTemporaryStorage() { var textFactory = CreateMockTextFactoryService(); var temporaryStorageService = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory); var text = Text.SourceText.From("Hello, World!"); // Create a temporary storage location using (var temporaryStorage = temporaryStorageService.CreateTemporaryTextStorage(System.Threading.CancellationToken.None)) { // Write text into it await temporaryStorage.WriteTextAsync(text); // Read text back from it var text2 = await temporaryStorage.ReadTextAsync(); Assert.NotSame(text, text2); Assert.Equal(text.ToString(), text2.ToString()); Assert.Equal(text2.Encoding, null); } }
public void TestTemporaryTextStorageExceptions() { var textFactory = new TextFactoryService(); var service = new TemporaryStorageServiceFactory.TemporaryStorageService(textFactory); var storage = service.CreateTemporaryTextStorage(CancellationToken.None); // Nothing has been written yet Assert.Throws<InvalidOperationException>(() => storage.ReadText()); Assert.Throws<AggregateException>(() => storage.ReadTextAsync().Result); // write a normal string var text = SourceText.From(new string(' ', 4096) + "public class A {}"); storage.WriteTextAsync(text).Wait(); // Writing multiple times is not allowed Assert.Throws<InvalidOperationException>(() => storage.WriteText(text)); Assert.Throws<AggregateException>(() => storage.WriteTextAsync(text).Wait()); }