Browse Source

Added some Activator tests.

pull/4/head
Steven Kirk 12 years ago
parent
commit
3f4f96ab91
  1. 3
      Perspex.UnitTests/Perspex.UnitTests.csproj
  2. 109
      Perspex.UnitTests/Styling/ActivatorTests.cs
  3. 65
      Perspex.UnitTests/TestObserver.cs
  4. 60
      Perspex.UnitTests/TestSubject.cs

3
Perspex.UnitTests/Perspex.UnitTests.csproj

@ -72,11 +72,14 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PerspexObjectTests.cs" />
<Compile Include="StyleTests.cs" />
<Compile Include="Styling\ActivatorTests.cs" />
<Compile Include="Styling\SelectorTests_Class.cs" />
<Compile Include="Styling\SelectorTests_Id.cs" />
<Compile Include="Styling\SelectorTests_OfType.cs" />
<Compile Include="Styling\SelectorTests_Template.cs" />
<Compile Include="Styling\TestControlBase.cs" />
<Compile Include="TestSubject.cs" />
<Compile Include="TestObserver.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Perspex\Perspex.csproj">

109
Perspex.UnitTests/Styling/ActivatorTests.cs

@ -0,0 +1,109 @@
namespace Perspex.UnitTests.Styling
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Perspex.Styling;
using Activator = Perspex.Styling.Activator;
[TestClass]
public class ActivatorTests
{
[TestMethod]
public void Activator_Should_Follow_Single_Input()
{
var inputs = new[] { new TestSubject<bool>(false) };
var target = new Activator(inputs);
var result = new TestObserver<bool>();
target.Subscribe(result);
Assert.IsFalse(result.GetValue());
inputs[0].OnNext(true);
Assert.IsTrue(result.GetValue());
inputs[0].OnNext(false);
Assert.IsFalse(result.GetValue());
inputs[0].OnNext(true);
Assert.IsTrue(result.GetValue());
Assert.AreEqual(1, inputs[0].SubscriberCount);
}
[TestMethod]
public void Activator_Should_AND_Multiple_Inputs()
{
var inputs = new[]
{
new TestSubject<bool>(false),
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs);
var result = new TestObserver<bool>();
target.Subscribe(result);
Assert.IsFalse(result.GetValue());
inputs[0].OnNext(true);
inputs[1].OnNext(true);
Assert.IsTrue(result.GetValue());
inputs[0].OnNext(false);
Assert.IsFalse(result.GetValue());
Assert.AreEqual(1, inputs[0].SubscriberCount);
Assert.AreEqual(1, inputs[1].SubscriberCount);
Assert.AreEqual(1, inputs[2].SubscriberCount);
}
[TestMethod]
public void Activator_Should_Unsubscribe_All_When_Input_Completes_On_False()
{
var inputs = new[]
{
new TestSubject<bool>(false),
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs);
var result = new TestObserver<bool>();
target.Subscribe(result);
Assert.IsFalse(result.GetValue());
inputs[0].OnNext(true);
inputs[1].OnNext(true);
Assert.IsTrue(result.GetValue());
inputs[0].OnNext(false);
Assert.IsFalse(result.GetValue());
inputs[0].OnCompleted();
Assert.AreEqual(0, inputs[0].SubscriberCount);
Assert.AreEqual(0, inputs[1].SubscriberCount);
Assert.AreEqual(0, inputs[2].SubscriberCount);
}
[TestMethod]
public void Activator_Should_Not_Unsubscribe_All_When_Input_Completes_On_False()
{
var inputs = new[]
{
new TestSubject<bool>(false),
new TestSubject<bool>(false),
new TestSubject<bool>(true),
};
var target = new Activator(inputs);
var result = new TestObserver<bool>();
target.Subscribe(result);
Assert.IsFalse(result.GetValue());
inputs[0].OnNext(true);
inputs[0].OnCompleted();
Assert.AreEqual(1, inputs[0].SubscriberCount);
Assert.AreEqual(1, inputs[1].SubscriberCount);
Assert.AreEqual(1, inputs[2].SubscriberCount);
}
}
}

65
Perspex.UnitTests/TestObserver.cs

@ -0,0 +1,65 @@
// -----------------------------------------------------------------------
// <copyright file="TestObserver.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.UnitTests
{
using System;
internal class TestObserver<T> : IObserver<T>
{
private bool hasValue;
private T value;
public bool Completed { get; private set; }
public Exception Error { get; private set; }
public T GetValue()
{
if (!this.hasValue)
{
throw new Exception("Observable provided no value.");
}
if (this.Completed)
{
throw new Exception("Observable completed unexpectedly.");
}
if (this.Error != null)
{
throw new Exception("Observable errored unexpectedly.");
}
this.hasValue = false;
return value;
}
public void OnCompleted()
{
this.Completed = true;
}
public void OnError(Exception error)
{
this.Error = error;
}
public void OnNext(T value)
{
if (!this.hasValue)
{
this.value = value;
this.hasValue = true;
}
else
{
throw new Exception("Observable pushed more than one value.");
}
}
}
}

60
Perspex.UnitTests/TestSubject.cs

@ -0,0 +1,60 @@
// -----------------------------------------------------------------------
// <copyright file="TestSubject.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.UnitTests
{
using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
internal class TestSubject<T> : IObserver<T>, IObservable<T>
{
private T initial;
private List<IObserver<T>> subscribers = new List<IObserver<T>>();
public TestSubject(T initial)
{
this.initial = initial;
}
public int SubscriberCount
{
get { return this.subscribers.Count; }
}
public void OnCompleted()
{
foreach (IObserver<T> subscriber in this.subscribers.ToArray())
{
subscriber.OnCompleted();
}
}
public void OnError(Exception error)
{
foreach (IObserver<T> subscriber in this.subscribers.ToArray())
{
subscriber.OnError(error);
}
}
public void OnNext(T value)
{
foreach (IObserver<T> subscriber in this.subscribers.ToArray())
{
subscriber.OnNext(value);
}
}
public IDisposable Subscribe(IObserver<T> observer)
{
this.subscribers.Add(observer);
observer.OnNext(initial);
return Disposable.Create(() => this.subscribers.Remove(observer));
}
}
}
Loading…
Cancel
Save