Skip to content

Commit 7741928

Browse files
committed
Unit test: added new test methods to check if saving diagnostic data works fine.
1 parent e4538dc commit 7741928

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

DataTest/DataTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,20 @@ public void getTableDataTest()
4040
Assert.AreEqual (width, tableData.Width);
4141
Assert.AreEqual(height , tableData.Height);
4242
}
43+
44+
[Test]
45+
public async Task LogDiagnosticDataAsyncTest()
46+
{
47+
var mockDataAPI = new MockDataAPI();
48+
string testData = "{\"Event\":\"Test\"}";
49+
50+
await mockDataAPI.LogDiagnosticDataAsync(testData);
51+
52+
List<string> loggedData = mockDataAPI.GetLoggedData();
53+
54+
Assert.IsNotNull(loggedData);
55+
Assert.AreEqual(1, loggedData.Count);
56+
Assert.AreEqual(testData, loggedData.First());
57+
}
4358
}
4459
}

DataTest/MockDataAPI.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Data;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace DataTest
9+
{
10+
public class MockDataAPI : DataAPI
11+
{
12+
private readonly List<string> _loggedData = new List<string>();
13+
private readonly SemaphoreSlim _fileSemaphore = new SemaphoreSlim(1, 1);
14+
15+
public override async Task LogDiagnosticDataAsync(string data)
16+
{
17+
await _fileSemaphore.WaitAsync();
18+
try
19+
{
20+
_loggedData.Add(data);
21+
}
22+
finally
23+
{
24+
_fileSemaphore.Release();
25+
}
26+
}
27+
28+
public List<string> GetLoggedData() => _loggedData;
29+
}
30+
}

Logic/LogicAPI.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ public class LogicAPI : LogicAbstractAPI
1616
public override Table table { get; }
1717
public override ObservableCollection<BallLogic> balls { get; } = new ObservableCollection<BallLogic>();
1818

19-
public LogicAPI()
19+
public LogicAPI() : this(DataAPI.createDataAPI()) { }
20+
21+
public LogicAPI(DataAPI dataAPI)
2022
{
21-
_dataAPI = DataAPI.createDataAPI();
23+
_dataAPI = dataAPI;
2224
table = _dataAPI.getTableData(500, 500);
23-
BallLogic.SetTable(table);
25+
BallLogic.SetTable(table);
2426
}
2527

2628
public override void RunSimulation()

LogicTest/LogicTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Data;
33
using System.Collections.ObjectModel;
44
using System.Numerics;
5+
using DataTest;
56

67
namespace LogicTest
78
{
@@ -86,5 +87,21 @@ public void createDeleteBallsTest()
8687
_logicAPI.deleteBalls();
8788
Assert.AreEqual(0, _logicAPI.balls.Count);
8889
}
90+
91+
[Test]
92+
public async Task RunSimulation_LogDataTest()
93+
{
94+
var mockDataAPI = new MockDataAPI();
95+
var logicAPI = new LogicAPI(mockDataAPI);
96+
logicAPI.createBalls(2, 10);
97+
98+
logicAPI.RunSimulation();
99+
await Task.Delay(100);
100+
logicAPI.StopSimulation();
101+
102+
List<string> loggedData = mockDataAPI.GetLoggedData();
103+
104+
Assert.IsNotNull(loggedData);
105+
}
89106
}
90107
}

LogicTest/LogicTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21+
<ProjectReference Include="..\DataTest\DataTest.csproj" />
2122
<ProjectReference Include="..\Data\Data.csproj" />
2223
<ProjectReference Include="..\Logic\Logic.csproj" />
2324
</ItemGroup>

0 commit comments

Comments
 (0)