summaryrefslogtreecommitdiff
path: root/src/SMAPI.Tests/Core/AssumptionTests.cs
blob: efc9da3f7d16faa55f642a5473527099a7284ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using FluentAssertions.Execution;
using NUnit.Framework;
using StardewModdingAPI.Framework.Models;

namespace SMAPI.Tests.Core
{
    /// <summary>Unit tests which validate assumptions about .NET used in the SMAPI implementation.</summary>
    [TestFixture]
    internal class AssumptionTests
    {
        /*********
        ** Unit tests
        *********/
        /****
        ** Constructor
        ****/
        [Test(Description = $"Assert that {nameof(HashSet<string>)} maintains insertion order when no elements are removed. If this fails, we'll need to change the implementation for the {nameof(SConfig.ModsToLoadEarly)} and {nameof(SConfig.ModsToLoadLate)} options.")]
        [TestCase("construct from array")]
        [TestCase("add incrementally")]
        public void HashSet_MaintainsInsertionOrderWhenNoElementsAreRemoved(string populateMethod)
        {
            // arrange
            string[] inserted = Enumerable.Range(0, 1000)
                .Select(_ => Guid.NewGuid().ToString("N"))
                .ToArray();

            // act
            HashSet<string> set;
            switch (populateMethod)
            {
                case "construct from array":
                    set = new(inserted, StringComparer.OrdinalIgnoreCase);
                    break;

                case "add incrementally":
                    set = new(StringComparer.OrdinalIgnoreCase);
                    foreach (string value in inserted)
                        set.Add(value);
                    break;

                default:
                    throw new AssertionFailedException($"Unknown populate method '{populateMethod}'.");
            }

            // assert
            string[] actualOrder = set.ToArray();
            actualOrder.Should().HaveCount(inserted.Length);
            for (int i = 0; i < inserted.Length; i++)
            {
                string expected = inserted[i];
                string actual = actualOrder[i];

                if (actual != expected)
                    throw new AssertionFailedException($"The hash set differed at index {i}: expected {expected}, but found {actual} instead.");
            }
        }
    }
}