summaryrefslogtreecommitdiff
path: root/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs
blob: ac7bd3388925c312c364536f6faf949461c63460 (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
using System;
using SMAPI.Tests.ModApiConsumer.Interfaces;

namespace SMAPI.Tests.ModApiConsumer
{
    /// <summary>A simulated API consumer.</summary>
    public class ApiConsumer
    {
        /*********
        ** Public methods
        *********/
        /// <summary>Call the event field on the given API.</summary>
        /// <param name="api">The API to call.</param>
        /// <param name="getValues">Get the number of times the event was called and the last value received.</param>
        public void UseEventField(ISimpleApi api, out Func<(int timesCalled, int actualValue)> getValues)
        {
            // act
            int calls = 0;
            int lastValue = -1;
            api.OnEventRaised += (_, value) =>
            {
                calls++;
                lastValue = value;
            };

            getValues = () => (timesCalled: calls, actualValue: lastValue);
        }

        /// <summary>Call the event property on the given API.</summary>
        /// <param name="api">The API to call.</param>
        /// <param name="getValues">Get the number of times the event was called and the last value received.</param>
        public void UseEventProperty(ISimpleApi api, out Func<(int timesCalled, int actualValue)> getValues)
        {
            // act
            int calls = 0;
            int lastValue = -1;
            api.OnEventRaisedProperty += (_, value) =>
            {
                calls++;
                lastValue = value;
            };

            getValues = () => (timesCalled: calls, actualValue: lastValue);
        }
    }
}