Merge pull request #31 from JimmyLoveSiren/master

Fix #30 and add unit test
pull/26/head^2 1.2.0
Yoshifumi Kawai 2019-09-20 20:55:35 +09:00 committed by GitHub
commit 50ba93f951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -395,6 +395,40 @@ namespace UniRx.AsyncTests
yield return new WaitForSeconds(3);
}
[UnityTest]
public IEnumerator ToObservable() => UniTask.ToCoroutine(async () =>
{
var completedTaskObserver = new ToObservableObserver<AsyncUnit>();
completedTaskObserver.OnNextCalled.Should().BeFalse();
completedTaskObserver.OnCompletedCalled.Should().BeFalse();
completedTaskObserver.OnErrorCalled.Should().BeFalse();
UniTask.CompletedTask.ToObservable().Subscribe(completedTaskObserver);
completedTaskObserver.OnNextCalled.Should().BeTrue();
completedTaskObserver.OnCompletedCalled.Should().BeTrue();
completedTaskObserver.OnErrorCalled.Should().BeFalse();
var delayFrameTaskObserver = new ToObservableObserver<int>();
UniTask.DelayFrame(1).ToObservable().Subscribe(delayFrameTaskObserver);
delayFrameTaskObserver.OnNextCalled.Should().BeFalse();
delayFrameTaskObserver.OnCompletedCalled.Should().BeFalse();
delayFrameTaskObserver.OnErrorCalled.Should().BeFalse();
await UniTask.DelayFrame(1);
delayFrameTaskObserver.OnNextCalled.Should().BeTrue();
delayFrameTaskObserver.OnCompletedCalled.Should().BeTrue();
delayFrameTaskObserver.OnErrorCalled.Should().BeFalse();
});
class ToObservableObserver<T> : IObserver<T>
{
public bool OnNextCalled { get; private set; }
public bool OnCompletedCalled { get; private set; }
public bool OnErrorCalled { get; private set; }
public void OnNext(T value) => OnNextCalled = true;
public void OnCompleted() => OnCompletedCalled = true;
public void OnError(Exception error) => OnErrorCalled = true;
}
#endif
#endif
}

View File

@ -257,6 +257,7 @@ namespace UniRx.Async
public IDisposable Subscribe(IObserver<T> observer)
{
observer.OnNext(value);
observer.OnCompleted();
return EmptyDisposable.Instance;
}
}