Browse Source
Thread synchronization is not currently implemented. Task tests fail because of this: putting a Thread.Sleep before the Assert makes them pass. Need to work out how we're going to do async unit testing.pull/237/head
4 changed files with 168 additions and 4 deletions
@ -0,0 +1,61 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Reactive.Linq; |
|||
using System.Reactive.Subjects; |
|||
using Perspex.Markup.Binding; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.Markup.UnitTests.Binding |
|||
{ |
|||
public class ExpressionObserverTests_Observable |
|||
{ |
|||
[Fact] |
|||
public void Should_Get_Simple_Observable_Value() |
|||
{ |
|||
var source = new BehaviorSubject<string>("foo"); |
|||
var data = new { Foo = source }; |
|||
var target = new ExpressionObserver(data, "Foo"); |
|||
var result = new List<object>(); |
|||
|
|||
var sub = target.Subscribe(x => result.Add(x.Value)); |
|||
source.OnNext("bar"); |
|||
|
|||
Assert.Equal(new[] { "foo", "bar" }, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Property_Value_From_Observable() |
|||
{ |
|||
var data = new Class1(); |
|||
var target = new ExpressionObserver(data, "Next.Foo"); |
|||
var result = new List<object>(); |
|||
|
|||
var sub = target.Subscribe(x => result.Add(x.Value)); |
|||
data.Next.OnNext(new Class2("foo")); |
|||
|
|||
Assert.Equal(new[] { null, "foo" }, result); |
|||
|
|||
sub.Dispose(); |
|||
|
|||
Assert.Equal(0, data.SubscriptionCount); |
|||
} |
|||
|
|||
private class Class1 : NotifyingBase |
|||
{ |
|||
public Subject<Class2> Next { get; } = new Subject<Class2>(); |
|||
} |
|||
|
|||
private class Class2 : NotifyingBase |
|||
{ |
|||
public Class2(string foo) |
|||
{ |
|||
Foo = foo; |
|||
} |
|||
|
|||
public string Foo { get; } |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Reactive.Linq; |
|||
using System.Threading.Tasks; |
|||
using Perspex.Markup.Binding; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.Markup.UnitTests.Binding |
|||
{ |
|||
public class ExpressionObserverTests_Task |
|||
{ |
|||
[Fact] |
|||
public void Should_Get_Simple_Task_Value() |
|||
{ |
|||
var tcs = new TaskCompletionSource<string>(); |
|||
var data = new { Foo = tcs.Task }; |
|||
var target = new ExpressionObserver(data, "Foo"); |
|||
var result = new List<object>(); |
|||
|
|||
var sub = target.Subscribe(x => result.Add(x.Value)); |
|||
tcs.SetResult("foo"); |
|||
|
|||
Assert.Equal(new object[] { null, "foo" }, result.ToArray()); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Get_Property_Value_From_Task() |
|||
{ |
|||
var tcs = new TaskCompletionSource<Class2>(); |
|||
var data = new Class1(tcs.Task); |
|||
var target = new ExpressionObserver(data, "Next.Foo"); |
|||
var result = new List<object>(); |
|||
|
|||
var sub = target.Subscribe(x => result.Add(x.Value)); |
|||
tcs.SetResult(new Class2("foo")); |
|||
|
|||
Assert.Equal(new object[] { null, "foo" }, result.ToArray()); |
|||
} |
|||
|
|||
private class Class1 : NotifyingBase |
|||
{ |
|||
public Class1(Task<Class2> next) |
|||
{ |
|||
Next = next; |
|||
} |
|||
|
|||
public Task<Class2> Next { get; } |
|||
} |
|||
|
|||
private class Class2 : NotifyingBase |
|||
{ |
|||
public Class2(string foo) |
|||
{ |
|||
Foo = foo; |
|||
} |
|||
|
|||
public string Foo { get; } |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue