Seraphina/Seraphina.Tests/SkillRepoTest.cs

140 lines
3.5 KiB
C#

using FsCheck;
using FsCheck.Xunit;
using Seraphina.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace Seraphina.Tests
{
public class SkillRepoTest
{
[Fact]
public void Ctor_CanAcceptNoParams()
{
var repo = new SkillRepo();
Assert.Empty(repo.Skills);
}
[Fact]
public void Ctor_CanAcceptEmptyEnumerable()
{
var repo = new SkillRepo(Enumerable.Empty<SkillInfo>());
Assert.Empty(repo.Skills);
}
private static readonly IEnumerable<SkillPoint> validSkillPointCosts = new[] { 1, 2, 3, 4 }.Select(x => new SkillPoint(x));
private static SkillInfo MakeSkill(int id) => new SkillInfo($"id_{id}", $"name_{id}", validSkillPointCosts);
private static readonly List<SkillInfo> validSkills = new List<SkillInfo>
{
MakeSkill(1),
MakeSkill(2),
MakeSkill(3)
};
[Fact]
public void Ctor_SavesSkills()
{
var repo = new SkillRepo(validSkills);
Assert.Equal(3, repo.Skills.Count);
}
[Fact]
public void Ctor_OverwritesSkillsWithSameId()
{
var skills = new List<SkillInfo>
{
new SkillInfo("id1", "name1", validSkillPointCosts),
new SkillInfo("id2", "name2", validSkillPointCosts),
new SkillInfo("id1", "name3", validSkillPointCosts),
};
var repo = new SkillRepo(skills);
Assert.Equal(2, repo.Skills.Count);
Assert.Equal("name3", repo["id1"].Name);
}
[Fact]
public void AddSkill_ThrowsExceptionIfSkillAlreadyExists()
{
// NOTE: May want to remove this later, depending on future needs.
// For instance, overwriting, or skipping.
var repo = new SkillRepo(validSkills);
Assert.Throws<InvalidOperationException>(() => repo.AddSkill(MakeSkill(1)));
}
[Fact]
public void AddSkill_AddsSkillToRepo()
{
var repo = new SkillRepo();
repo.AddSkill(MakeSkill(1));
Assert.Single(repo.Skills);
repo.AddSkill(MakeSkill(2));
Assert.Equal(2, repo.Skills.Count);
}
[Fact]
public void AddSkill_TriggersSkillAddedEvent()
{
var repo = new SkillRepo();
var counter = 0;
repo.SkillAdded += (sender, e) => counter++;
repo.AddSkill(MakeSkill(1));
Assert.Equal(1, counter);
repo.AddSkill(MakeSkill(2));
Assert.Equal(2, counter);
}
[Fact]
public void AddSkill_DoesNotTriggerSkillAddedEventIfInvalidParameter()
{
var repo = new SkillRepo();
var counter = 0;
repo.SkillAdded += (sender, e) => counter++;
repo.AddSkill(MakeSkill(1));
Assert.Equal(1, counter);
try
{
repo.AddSkill(MakeSkill(1));
}
catch (InvalidOperationException) { }
Assert.Equal(1, counter);
}
[Fact]
public void SkillAdded_GivesAddedSkill()
{
var repo = new SkillRepo();
var skill = MakeSkill(1);
var str = "";
repo.SkillAdded += (obj, e) => str = e;
repo.AddSkill(skill);
Assert.Equal(skill.Id, str);
}
}
}