Implemented Tests (All Green), Fixed some names and documentation
This commit is contained in:
parent
d731193948
commit
bf79b07ea4
@ -18,7 +18,7 @@ namespace NEG.Utils.Achievments
|
||||
}
|
||||
}
|
||||
|
||||
public class AchievmentTypeExcetion : AchievmentException
|
||||
public class AchievmentTypeException : AchievmentException
|
||||
{
|
||||
/// <summary>
|
||||
/// Expected achievment type under <see cref="AchievmentException.Id"/>
|
||||
@ -34,7 +34,7 @@ namespace NEG.Utils.Achievments
|
||||
/// <seealso cref="AchievmentTypes.AchievmentDefinition"/>
|
||||
public Type Actual { get; private set; }
|
||||
|
||||
public AchievmentTypeExcetion(string message, string achievmentId, Type expectedType, Type actualType) : base(message, achievmentId)
|
||||
public AchievmentTypeException(string message, string achievmentId, Type expectedType, Type actualType) : base(message, achievmentId)
|
||||
{
|
||||
Expected = expectedType;
|
||||
Actual = actualType;
|
||||
|
||||
@ -164,6 +164,7 @@ namespace NEG.Utils.Achievments
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id</remarks>
|
||||
public bool IsCompleted(string id)
|
||||
{
|
||||
return GetAchievmentForId(id).IsCompleted;
|
||||
@ -172,9 +173,9 @@ namespace NEG.Utils.Achievments
|
||||
#region Toggle
|
||||
/// <summary>
|
||||
/// Sets a <see cref="ToggleAchievmentData"/> as completed, after which sends <see cref="AchievmentStateChanged"/>, also if the achievment is completed sends a <see cref="AchievmentCompleted"/>. <br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="ToggleAchievmentDefinition"/>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public void SetToggleAchivment(string id)
|
||||
{
|
||||
ManipulateAchievment<ToggleAchievmentData>(id, (achievment) => achievment.CompletionState = true);
|
||||
@ -182,9 +183,9 @@ namespace NEG.Utils.Achievments
|
||||
|
||||
/// <summary>
|
||||
/// Gets a completion state from a <see cref="ToggleAchievmentData"/>.<br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="ToggleAchievmentDefinition"/>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public bool GetToggleState(string id)
|
||||
{
|
||||
return GetAchievmentForId<ToggleAchievmentData>(id).CompletionState;
|
||||
@ -194,9 +195,9 @@ namespace NEG.Utils.Achievments
|
||||
#region Int
|
||||
/// <summary>
|
||||
/// Sets progress of a given <see cref="IntAchievmentData"/> to <paramref name="progress"/>, after which sends <see cref="AchievmentStateChanged"/>, also if the achievment is completed sends a <see cref="AchievmentCompleted"/>. <br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="IntAchievmentDefinition"/>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public void SetIntProgress(string id, int progress)
|
||||
{
|
||||
ManipulateAchievment<IntAchievmentData>(id, (achievment) => achievment.CurrentProgress = progress);
|
||||
@ -204,19 +205,19 @@ namespace NEG.Utils.Achievments
|
||||
|
||||
/// <summary>
|
||||
/// Changes progress of a given <see cref="IntAchievmentData"/> by <paramref name="delta"/>, after which sends <see cref="AchievmentStateChanged"/>, also if the achievment is completed sends a <see cref="AchievmentCompleted"/>. <br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="IntAchievmentDefinition"/>
|
||||
public void CangeIntProgress(string id, int delta)
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public void ChangeIntProgress(string id, int delta)
|
||||
{
|
||||
ManipulateAchievment<IntAchievmentData>(id, (achievment) => achievment.CurrentProgress += delta);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets current progress from a <see cref="IntAchievmentData"/>.<br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="ToggleAchievmentDefinition"/>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public int GetIntProgress(string id)
|
||||
{
|
||||
return GetAchievmentForId<IntAchievmentData>(id).CurrentProgress;
|
||||
@ -226,9 +227,9 @@ namespace NEG.Utils.Achievments
|
||||
#region Float
|
||||
/// <summary>
|
||||
/// Sets progress of a given <see cref="FloatAchievmentData"/> to <paramref name="progress"/>, after which sends <see cref="AchievmentStateChanged"/>, also if the achievment is completed sends a <see cref="AchievmentCompleted"/>. <br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="FloatAchievmentDefinition"/>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public void SetFloatProgress(string id, float progress)
|
||||
{
|
||||
ManipulateAchievment<FloatAchievmentData>(id, (achievment) => achievment.CurrentProgress = progress);
|
||||
@ -236,19 +237,19 @@ namespace NEG.Utils.Achievments
|
||||
|
||||
/// <summary>
|
||||
/// Changes progress of a given <see cref="FloatAchievmentData"/> by <paramref name="delta"/>, after which sends <see cref="AchievmentStateChanged"/>, also if the achievment is completed sends a <see cref="AchievmentCompleted"/>. <br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="FloatAchievmentDefinition"/>
|
||||
public void CangeFloatProgress(string id, float delta)
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public void ChangeFloatProgress(string id, float delta)
|
||||
{
|
||||
ManipulateAchievment<FloatAchievmentData>(id, (achievment) => achievment.CurrentProgress += delta);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets current progress from a <see cref="FloatAchievmentData"/>.<br/>
|
||||
/// If there is no achievment at a given id throws an <see cref="ApplicationException"/>. If achievment at a given id is of a wrong tupe throws an <see cref="ApplicationException"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="FloatAchievmentDefinition"/>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public float GetFloatProgress(string id)
|
||||
{
|
||||
return GetAchievmentForId<FloatAchievmentData>(id).CurrentProgress;
|
||||
@ -261,7 +262,7 @@ namespace NEG.Utils.Achievments
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the achievment</typeparam>
|
||||
/// <param name="id">Id of requested achievment</param>
|
||||
/// <remarks>throws an <see cref="ApplicationException"/> if either there is no achievment under id or achievment under id is of a different type</remarks>
|
||||
/// <remarks>throws an <see cref="AchievmentException"/> if there is no achievment under id or an <see cref="AchievmentTypeException"/> if achievment under id is of a different type</remarks>
|
||||
public T GetAchievmentForId<T>(string id) where T : AchievmentData
|
||||
{
|
||||
return ValidateAchievmentType<T>(GetAchievmentForId(id));
|
||||
@ -289,7 +290,7 @@ namespace NEG.Utils.Achievments
|
||||
{
|
||||
if (data is not T convetred)
|
||||
{
|
||||
throw new AchievmentTypeExcetion($"Attempting to perform an operation on an invalid achievment type. Expected {typeof(T)} got {data.GetType()}", data.Achivment.Id, typeof(T), data.GetType());
|
||||
throw new AchievmentTypeException($"Attempting to perform an operation on an invalid achievment type. Expected {typeof(T)} got {data.GetType()}", data.Achivment.Id, typeof(T), data.GetType());
|
||||
}
|
||||
return convetred;
|
||||
}
|
||||
|
||||
@ -1,90 +1,380 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using NEG.Utils.Achivments;
|
||||
|
||||
public class ConfigTests
|
||||
namespace NEG.Utils.Achievments.Tests
|
||||
{
|
||||
public const string AchivmentDefOnlyLabel = "TestAchivments";
|
||||
public const string AchivmentSetLabel = "TestAchivments";
|
||||
public const string AchivmentProgressLabel = "TestAchivments";
|
||||
using AchievmentTypes;
|
||||
using static Internal.Extensions;
|
||||
|
||||
public const string NoProgressAchivment = "NO_PROGRESS";
|
||||
public const string WithProgressAchivment = "WITH_PROGRESS";
|
||||
|
||||
[Test]
|
||||
public void AchivmentDefOnly()
|
||||
public class ConfigTests
|
||||
{
|
||||
AchivmentManager manager = AchivmentManager.Builder.FromLabeledConfig(AchivmentDefOnlyLabel).Build();
|
||||
public const string AchievmentsLabel = "TestAchivments";
|
||||
|
||||
//Shoud not throw
|
||||
manager.SetAchivment(NoProgressAchivment);
|
||||
//Shoud not throw
|
||||
manager.SetAchivment(WithProgressAchivment);
|
||||
public const string AchievmentIdToggle = "TOGGLE";
|
||||
public const string AchievmentIdInt = "INT";
|
||||
public const string AchievmentIdFloat = "FLOAT";
|
||||
public const string AchievmentIdInvalid = "huptyrz";
|
||||
public const int RandomProgress = 15;
|
||||
public const int ProgressChangeDelta = 15;
|
||||
public const int CompletedProgress = 100;
|
||||
public const int OvershootProgress = 150;
|
||||
|
||||
#region Id And Types
|
||||
[Test]
|
||||
public void AchivmentInvalidId()
|
||||
{
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel).Build();
|
||||
|
||||
TestInvalidId(AchievmentIdInvalid, (id) => manager.GetAchievmentForId(id), "Get");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentInvalidType()
|
||||
{
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel).Build();
|
||||
|
||||
manager.TestInvalidType<IntAchievmentData, ToggleAchievmentData>(AchievmentIdToggle, "Toggle");
|
||||
manager.TestInvalidType<FloatAchievmentData, IntAchievmentData>(AchievmentIdInt, "Int");
|
||||
manager.TestInvalidType<ToggleAchievmentData, FloatAchievmentData>(AchievmentIdFloat, "Float");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentCorrectIdAndType()
|
||||
{
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel).Build();
|
||||
|
||||
manager.TestValidIdAndType<ToggleAchievmentData>(AchievmentIdToggle, "Toggle");
|
||||
manager.TestValidIdAndType<IntAchievmentData>(AchievmentIdInt, "Int");
|
||||
manager.TestValidIdAndType<FloatAchievmentData>(AchievmentIdFloat, "Float");
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Toggle
|
||||
[Test]
|
||||
public void AchivmentToggleSet()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
manager.SetToggleAchivment(AchievmentIdToggle);
|
||||
callbackTester.TestCompleted<ToggleAchievmentData>(AchievmentIdToggle);
|
||||
callbackTester.Reset();
|
||||
|
||||
manager.SetToggleAchivment(AchievmentIdToggle);
|
||||
callbackTester.TestNoChanges();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Int
|
||||
[Test]
|
||||
public void AchivmentIntSetLess()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Set progress to some value progress
|
||||
manager.SetIntProgress(AchievmentIdInt, RandomProgress);
|
||||
var data = callbackTester.GetChanged<IntAchievmentData>();
|
||||
|
||||
Assert.AreEqual(RandomProgress, data.CurrentProgress);
|
||||
Assert.IsFalse(data.IsCompleted);
|
||||
callbackTester.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentIntSetEqual()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Set progress to some value equal to required value
|
||||
manager.SetIntProgress(AchievmentIdInt, CompletedProgress);
|
||||
var data = callbackTester.GetChanged<IntAchievmentData>();
|
||||
Assert.AreEqual(CompletedProgress, data.CurrentProgress);
|
||||
callbackTester.TestCompleted<IntAchievmentData>(AchievmentIdInt);
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, this time nothing sould change
|
||||
manager.SetIntProgress(AchievmentIdInt, CompletedProgress);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentIntSetGreater()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Set progress to some value greater than required
|
||||
manager.SetIntProgress(AchievmentIdInt, OvershootProgress);
|
||||
var data = callbackTester.GetChanged<IntAchievmentData>();
|
||||
//Testing against completed progress, should be clamped down
|
||||
Assert.AreEqual(CompletedProgress, data.CurrentProgress);
|
||||
callbackTester.TestCompleted<IntAchievmentData>(AchievmentIdInt);
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, this time nothing sould change
|
||||
manager.SetIntProgress(AchievmentIdInt, OvershootProgress);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentIntChangeCompletion()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Add progresss one interval below completion
|
||||
for (int i = 0; i < CompletedProgress / ProgressChangeDelta; i++)
|
||||
{
|
||||
manager.ChangeIntProgress(AchievmentIdInt, ProgressChangeDelta);
|
||||
callbackTester.TestNotCompleted();
|
||||
var changed = callbackTester.GetChanged<IntAchievmentData>();
|
||||
|
||||
Assert.AreEqual((i + 1) * ProgressChangeDelta, changed.CurrentProgress);
|
||||
callbackTester.Reset();
|
||||
}
|
||||
|
||||
//Add progress one more time, should now be completed
|
||||
manager.ChangeIntProgress(AchievmentIdInt, ProgressChangeDelta);
|
||||
var changed1 = callbackTester.GetChanged<IntAchievmentData>();
|
||||
Assert.AreEqual(CompletedProgress, changed1.CurrentProgress);
|
||||
callbackTester.TestCompleted<IntAchievmentData>(AchievmentIdInt);
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, this time nothing should change
|
||||
manager.ChangeIntProgress(AchievmentIdInt, ProgressChangeDelta);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, but down this time also nothing should change
|
||||
manager.ChangeIntProgress(AchievmentIdInt, -ProgressChangeDelta);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Float
|
||||
[Test]
|
||||
public void AchivmentFloatSetLess()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Set progress to some value progress
|
||||
manager.SetFloatProgress(AchievmentIdFloat, RandomProgress);
|
||||
var data = callbackTester.GetChanged<FloatAchievmentData>();
|
||||
|
||||
Assert.AreEqual((float)RandomProgress, data.CurrentProgress);
|
||||
Assert.IsFalse(data.IsCompleted);
|
||||
callbackTester.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentFloatSetEqual()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Set progress to some value equal to required value
|
||||
manager.SetFloatProgress(AchievmentIdFloat, CompletedProgress);
|
||||
var data = callbackTester.GetChanged<FloatAchievmentData>();
|
||||
Assert.AreEqual((float)CompletedProgress, data.CurrentProgress);
|
||||
callbackTester.TestCompleted<FloatAchievmentData>(AchievmentIdFloat);
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, this time nothing sould change
|
||||
manager.SetFloatProgress(AchievmentIdFloat, CompletedProgress);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentFloatSetGreater()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Set progress to some value greater than required
|
||||
manager.SetFloatProgress(AchievmentIdFloat, OvershootProgress);
|
||||
var data = callbackTester.GetChanged<FloatAchievmentData>();
|
||||
//Testing against completed progress, should be clamped down
|
||||
Assert.AreEqual((float)CompletedProgress, data.CurrentProgress);
|
||||
callbackTester.TestCompleted<FloatAchievmentData>(AchievmentIdFloat);
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, this time nothing sould change
|
||||
manager.SetFloatProgress(AchievmentIdFloat, OvershootProgress);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentFloatChangeCompletion()
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
|
||||
AchievmentManager manager = AchievmentManager.Builder.FromLabeledConfig(AchievmentsLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
|
||||
//Add progresss one interval below completion
|
||||
for (int i = 0; i < CompletedProgress / ProgressChangeDelta; i++)
|
||||
{
|
||||
manager.ChangeFloatProgress(AchievmentIdFloat, ProgressChangeDelta);
|
||||
callbackTester.TestNotCompleted();
|
||||
var changed = callbackTester.GetChanged<FloatAchievmentData>();
|
||||
|
||||
Assert.AreEqual((i + 1) * ProgressChangeDelta, changed.CurrentProgress, 0.0f);
|
||||
callbackTester.Reset();
|
||||
}
|
||||
|
||||
//Add progress one more time, should now be completed
|
||||
manager.ChangeFloatProgress(AchievmentIdFloat, ProgressChangeDelta);
|
||||
var changed1 = callbackTester.GetChanged<FloatAchievmentData>();
|
||||
Assert.AreEqual((float)CompletedProgress, changed1.CurrentProgress);
|
||||
callbackTester.TestCompleted<FloatAchievmentData>(AchievmentIdFloat);
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, this time nothing should change
|
||||
manager.ChangeFloatProgress(AchievmentIdFloat, ProgressChangeDelta);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
|
||||
//Do that again, but down this time also nothing should change
|
||||
manager.ChangeFloatProgress(AchievmentIdFloat, -ProgressChangeDelta);
|
||||
callbackTester.TestNoChanges();
|
||||
callbackTester.Reset();
|
||||
}
|
||||
#endregion
|
||||
|
||||
private class TestCallbackRereiver : IAchievmentCallbackReciever
|
||||
{
|
||||
public AchievmentData LastDataUpdated { get; private set; } = null;
|
||||
public Type LastTypeSet { get; private set; } = null;
|
||||
public string LastIdSet { get; private set; } = null;
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
LastDataUpdated = null;
|
||||
LastTypeSet = null;
|
||||
LastIdSet = null;
|
||||
}
|
||||
|
||||
public T GetChanged<T>() where T : AchievmentData
|
||||
{
|
||||
Assert.IsInstanceOf(typeof(T), LastDataUpdated);
|
||||
|
||||
return (T)LastDataUpdated;
|
||||
}
|
||||
|
||||
public void TestNoChanges()
|
||||
{
|
||||
Assert.IsNull(LastDataUpdated);
|
||||
Assert.IsNull(LastTypeSet);
|
||||
Assert.IsNull(LastIdSet);
|
||||
}
|
||||
|
||||
public void TestNotCompleted()
|
||||
{
|
||||
//No need to also check LastTypeSet, they are both set or bot null
|
||||
Assert.IsNull(LastIdSet);
|
||||
}
|
||||
|
||||
public void TestCompleted<Type>(string id)
|
||||
{
|
||||
Assert.AreEqual(typeof(Type), LastTypeSet);
|
||||
Assert.AreEqual(id, LastIdSet);
|
||||
//Shold not be null: if we axpect achievment to be completed it must have also been updated
|
||||
Assert.IsTrue(LastDataUpdated.IsCompleted);
|
||||
}
|
||||
|
||||
public void AchievmentCompleted(AchievmentData achievment)
|
||||
{
|
||||
LastTypeSet = achievment.GetType();
|
||||
LastIdSet = achievment.Achivment.Id;
|
||||
}
|
||||
|
||||
public void AchievmentStateChanged(AchievmentData achievment)
|
||||
{
|
||||
LastDataUpdated = achievment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AchivmentSet()
|
||||
namespace Internal
|
||||
{
|
||||
var callbackTester = new TestCallbackRereiver();
|
||||
public static class Extensions
|
||||
{
|
||||
public static void TestValidIdAndType<Type>(this AchievmentManager manager, string id, string testName) where Type : AchievmentData
|
||||
{
|
||||
TestValidIdAndType(id, (id) => manager.GetAchievmentForId<Type>(id), testName);
|
||||
}
|
||||
|
||||
AchivmentManager manager = AchivmentManager.Builder.FromLabeledConfig(AchivmentDefOnlyLabel)
|
||||
.WithCallbackReciever(callbackTester)
|
||||
.Build();
|
||||
public static void TestValidIdAndType(string id, Action<string> manipulation, string testName)
|
||||
{
|
||||
Assert.DoesNotThrow(() => manipulation(id), $"{testName}: Invalid type or id");
|
||||
}
|
||||
|
||||
//Shoud not throw
|
||||
manager.SetAchivment(NoProgressAchivment);
|
||||
public static void TestInvalidId(this AchievmentManager manager, string id, string testName)
|
||||
{
|
||||
TestInvalidId(id, (id) => manager.GetAchievmentForId(id), testName);
|
||||
}
|
||||
|
||||
//Shoud not throw
|
||||
manager.SetAchivment(WithProgressAchivment);
|
||||
}
|
||||
public static void TestInvalidId(string id, Action<string> manipulation, string testName)
|
||||
{
|
||||
var exception = Assert.Throws<AchievmentException>(() => manipulation(id), $"Expected to fail with {typeof(AchievmentTypeException)}");
|
||||
|
||||
[Test]
|
||||
public void AchivmentProgress()
|
||||
{
|
||||
AchivmentManager manager = AchivmentManager.Builder.FromLabeledConfig(AchivmentProgressLabel).Build();
|
||||
}
|
||||
Assert.AreEqual(exception.Id, id, $"{testName}: Achievment id does not match");
|
||||
}
|
||||
|
||||
private class TestCallbackRereiver : IAchivmentCallbackReciever
|
||||
{
|
||||
public int LastProgressUpdated { get; private set; } = -1;
|
||||
public string LastIdUpdated { get; private set; } = null;
|
||||
public string LastIdSet { get; private set; } = null;
|
||||
public static void TestInvalidType<Expected, Actual>(this AchievmentManager manager, string id, string testName) where Expected : AchievmentData where Actual : AchievmentData
|
||||
{
|
||||
TestInvalidType<Expected, Actual>(id, (id) => manager.GetAchievmentForId<Expected>(id), testName);
|
||||
}
|
||||
|
||||
public static void TestInvalidType<Expected, Actual>(string id, Action<string> manipulation, string testName) where Expected : AchievmentData where Actual : AchievmentData
|
||||
{
|
||||
var exception = Assert.Throws<AchievmentTypeException>(() => manipulation(id), $"Expected to fail with {typeof(AchievmentTypeException)}");
|
||||
|
||||
public void SetAchivment(AchivmentData achivment)
|
||||
{
|
||||
LastIdSet = achivment.Achivment.Id;
|
||||
}
|
||||
Assert.AreEqual(id, exception.Id, $"{testName}: Achievment id does not match");
|
||||
Assert.AreSame(typeof(Expected), exception.Expected, $"{testName}: Target achievment type does not match");
|
||||
Assert.AreSame(typeof(Actual), exception.Actual, $"{testName}: Actual achievment type does not match");
|
||||
}
|
||||
|
||||
public void SetAchivmentProgress(AchivmentData achivment, int progress)
|
||||
{
|
||||
LastIdUpdated = achivment.Achivment.Id;
|
||||
LastProgressUpdated = progress;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
LastIdSet = null;
|
||||
LastIdUpdated = null;
|
||||
LastProgressUpdated = -1;
|
||||
}
|
||||
|
||||
public void TestSetCallbackst(AchivmentManager manager, string id)
|
||||
{
|
||||
manager.SetAchivment(id);
|
||||
Assert.AreEqual(id, LastIdSet);
|
||||
Assert.AreEqual(null, LastIdUpdated);
|
||||
Assert.AreEqual(-1, LastProgressUpdated);
|
||||
}
|
||||
|
||||
public void TestProgressCallback(AchivmentManager manager, string id, int progress, int expectedProgress, bool shouldSet)
|
||||
{
|
||||
manager.AddProgress(id, progress);
|
||||
//Assert.AreEqual(, LastIdSet);
|
||||
Assert.AreEqual(null, LastIdUpdated);
|
||||
Assert.AreEqual(-1, LastProgressUpdated);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,5 +13,6 @@ MonoBehaviour:
|
||||
m_Name: AchivmentCollection
|
||||
m_EditorClassIdentifier:
|
||||
<Achivments>k__BackingField:
|
||||
- {fileID: 11400000, guid: 553d34779f9a31d4891686d6d34e8033, type: 2}
|
||||
- {fileID: 11400000, guid: adbb4fd3d7472eb49933ac9958d499ff, type: 2}
|
||||
- {fileID: 11400000, guid: 7734df2e5d4033346aac56f0a2b2a836, type: 2}
|
||||
- {fileID: 11400000, guid: c704b1ea2247ad540842a9caff628211, type: 2}
|
||||
- {fileID: 11400000, guid: c71840de74e747e45afc82ecf8922dcd, type: 2}
|
||||
|
||||
18
Achievments/Tests/TestAssets/Float.asset
Normal file
18
Achievments/Tests/TestAssets/Float.asset
Normal file
@ -0,0 +1,18 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!114 &11400000
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 2d7270d5452c9b04ca07ef43a491a18d, type: 3}
|
||||
m_Name: Float
|
||||
m_EditorClassIdentifier:
|
||||
<Id>k__BackingField: FLOAT
|
||||
<ProgressRequired>k__BackingField: 100
|
||||
<InitialProgress>k__BackingField: 0
|
||||
<Clamped>k__BackingField: 0
|
||||
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 553d34779f9a31d4891686d6d34e8033
|
||||
guid: 7734df2e5d4033346aac56f0a2b2a836
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
@ -9,9 +9,10 @@ MonoBehaviour:
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: f9bd42dd58472044b8ecc0d69caa7da8, type: 3}
|
||||
m_Name: WithProgress
|
||||
m_Script: {fileID: 11500000, guid: 5318fea685aa56646a3310c38a9a9bac, type: 3}
|
||||
m_Name: Int
|
||||
m_EditorClassIdentifier:
|
||||
<Id>k__BackingField: WITH_PROGRESS
|
||||
<Id>k__BackingField: INT
|
||||
<ProgressRequired>k__BackingField: 100
|
||||
additionalDataInitializer:
|
||||
<InitialProgress>k__BackingField: 0
|
||||
<Clamped>k__BackingField: 0
|
||||
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: adbb4fd3d7472eb49933ac9958d499ff
|
||||
guid: c704b1ea2247ad540842a9caff628211
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
@ -9,9 +9,7 @@ MonoBehaviour:
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: f9bd42dd58472044b8ecc0d69caa7da8, type: 3}
|
||||
m_Name: NoProgress
|
||||
m_Script: {fileID: 11500000, guid: 608c7e921b8b16b42919fc6f55b67fcb, type: 3}
|
||||
m_Name: Toggle
|
||||
m_EditorClassIdentifier:
|
||||
<Id>k__BackingField: NO_PROGRESS
|
||||
<ProgressRequired>k__BackingField: 1
|
||||
additionalDataInitializer:
|
||||
<Id>k__BackingField: TOGGLE
|
||||
8
Achievments/Tests/TestAssets/Toggle.asset.meta
Normal file
8
Achievments/Tests/TestAssets/Toggle.asset.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c71840de74e747e45afc82ecf8922dcd
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user