forked from parse-community/Parse-SDK-dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigTests.cs
97 lines (81 loc) · 2.89 KB
/
ConfigTests.cs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Moq;
using NUnit.Framework;
using Parse;
using Parse.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace ParseTest {
[TestFixture]
public class ConfigTests {
private IParseConfigController MockedConfigController {
get {
var mockedConfigController = new Mock<IParseConfigController>();
var mockedCurrentConfigController = new Mock<IParseCurrentConfigController>();
ParseConfig theConfig = new ParseConfig(new Dictionary<string, object> {{
"params", new Dictionary<string, object> {{
"testKey", "testValue"
}}
}});
mockedCurrentConfigController.Setup(
obj => obj.GetCurrentConfigAsync()
).Returns(Task.FromResult(theConfig));
mockedConfigController.Setup(obj => obj.CurrentConfigController)
.Returns(mockedCurrentConfigController.Object);
var tcs = new TaskCompletionSource<ParseConfig>();
tcs.TrySetCanceled();
mockedConfigController.Setup(obj => obj.FetchConfigAsync(It.IsAny<string>(),
It.Is<CancellationToken>(ct => ct.IsCancellationRequested))).Returns(tcs.Task);
mockedConfigController.Setup(obj => obj.FetchConfigAsync(It.IsAny<string>(),
It.Is<CancellationToken>(ct => !ct.IsCancellationRequested))).Returns(Task.FromResult(theConfig));
return mockedConfigController.Object;
}
}
[SetUp]
public void SetUp() {
ParseCorePlugins.Instance.ConfigController = MockedConfigController;
ParseCorePlugins.Instance.CurrentUserController = new Mock<IParseCurrentUserController>().Object;
}
[TearDown]
public void TearDown() {
ParseCorePlugins.Instance.ConfigController = null;
ParseCorePlugins.Instance.CurrentUserController = null;
}
[Test]
public void TestCurrentConfig() {
ParseConfig config = ParseConfig.CurrentConfig;
Assert.AreEqual("testValue", config["testKey"]);
Assert.AreEqual("testValue", config.Get<string>("testKey"));
}
[Test]
public void TestToJSON() {
ParseConfig config1 = ParseConfig.CurrentConfig;
IDictionary<string, object> expectedJson = new Dictionary<string, object> {{
"params", new Dictionary<string, object> {{
"testKey", "testValue"
}}
}};
Assert.AreEqual(((IJsonConvertible)config1).ToJSON(), expectedJson);
}
[Test]
[AsyncStateMachine(typeof(ConfigTests))]
public Task TestGetConfig() {
return ParseConfig.GetAsync().ContinueWith(t => {
Assert.AreEqual("testValue", t.Result["testKey"]);
Assert.AreEqual("testValue", t.Result.Get<string>("testKey"));
});
}
[Test]
[AsyncStateMachine(typeof(ConfigTests))]
public Task TestGetConfigCancel() {
CancellationTokenSource tokenSource = new CancellationTokenSource();
tokenSource.Cancel();
return ParseConfig.GetAsync(tokenSource.Token).ContinueWith(t => {
Assert.True(t.IsCanceled);
});
}
}
}