public void MoveNextAsync_runs_initializer() { var mockShaper = Core.Objects.MockHelper.CreateShaperMock <int>(); mockShaper .Setup(m => m.GetEnumerator()) .Returns(() => new DbEnumeratorShim <int>(Enumerable.Range(1, 1).GetEnumerator())); var mockObjectResult = new Mock <ObjectResult <int> >(mockShaper.Object, null, null) { CallBase = true }; var initialized = false; var enumerator = new LazyAsyncEnumerator <int>( ct => { initialized = true; return(Task.FromResult(mockObjectResult.Object)); }); Assert.True(enumerator.MoveNextAsync(CancellationToken.None).Result); Assert.True(initialized); Assert.Equal(1, enumerator.Current); }
public void MoveNextAsync_passes_users_cancellationToken() { var cancellationToken = new CancellationTokenSource().Token; var mockAsyncEnumerator = new Mock <IDbAsyncEnumerator <int> >(); mockAsyncEnumerator .Setup(e => e.MoveNextAsync(It.IsAny <CancellationToken>())) .Returns( (CancellationToken token) => { Assert.Equal(cancellationToken, token); return(Task.FromResult(false)); }); var lazyEnumerator = new LazyAsyncEnumerator <int>( token => { Assert.Equal(cancellationToken, token); return(Task.FromResult(mockAsyncEnumerator.Object)); }); lazyEnumerator .MoveNextAsync(cancellationToken) .GetAwaiter() .GetResult(); }
public void Constructor_doesnt_run_initializer() { var initialized = false; var _ = new LazyAsyncEnumerator <object>( ct => { initialized = true; return(null); }); Assert.False(initialized); }
public void Current_runs_initializer() { var initialized = false; var enumerator = new LazyAsyncEnumerator <object>( ct => { initialized = true; return(Task.FromResult(new Mock <IDbAsyncEnumerator <object> >().Object)); }); var _ = enumerator.Current; Assert.True(initialized); }
public void MoveNextAsync_runs_initializer() { var initialized = false; var enumerator = new LazyAsyncEnumerator <int>( ct => { initialized = true; return(Task.FromResult( (IDbAsyncEnumerator <int>) new DbEnumeratorShim <int>( ((IEnumerable <int>) new[] { 1 }).GetEnumerator()))); }); Assert.True(enumerator.MoveNextAsync(CancellationToken.None).Result); Assert.True(initialized); Assert.Equal(1, enumerator.Current); }
public void MoveNextAsync_passes_users_cancellationToken() { var cancellationToken = new CancellationTokenSource().Token; var mockEnumerator = new Mock <DbEnumeratorShim <int> >(Enumerable.Empty <int>().GetEnumerator()); mockEnumerator .As <IDbAsyncEnumerator <int> >() .Setup(e => e.MoveNextAsync(It.IsAny <CancellationToken>())) .Returns((CancellationToken token) => Task.FromResult(false)); var mockShaper = Core.Objects.MockHelper.CreateShaperMock <int>(); mockShaper .Setup(m => m.GetEnumerator()) .Returns(() => mockEnumerator.Object); var mockObjectResult = new Mock <ObjectResult <int> >(mockShaper.Object, null, null) { CallBase = true }; var lazyEnumerator = new LazyAsyncEnumerator <int>( token => { Assert.Equal(cancellationToken, token); return(Task.FromResult(mockObjectResult.Object)); }); Assert.False( lazyEnumerator .MoveNextAsync(cancellationToken) .GetAwaiter() .GetResult()); mockEnumerator .As <IDbAsyncEnumerator <int> >() .Verify(e => e.MoveNextAsync(cancellationToken), Times.Once()); }