-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathUnityDataToolPlayerDataTests.cs
More file actions
122 lines (97 loc) · 4.13 KB
/
UnityDataToolPlayerDataTests.cs
File metadata and controls
122 lines (97 loc) · 4.13 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using Microsoft.Data.Sqlite;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityDataTools.TestCommon;
namespace UnityDataTools.UnityDataTool.Tests;
#pragma warning disable NUnit2005, NUnit2006
public class UnityDataToolPlayerDataTests : PlayerDataTestFixture
{
private string m_TestOutputFolder;
public UnityDataToolPlayerDataTests(Context context) : base(context)
{
}
[OneTimeSetUp]
public void OneTimeSetup()
{
m_TestOutputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "test_folder");
Directory.CreateDirectory(m_TestOutputFolder);
Directory.SetCurrentDirectory(m_TestOutputFolder);
}
[TearDown]
public void Teardown()
{
SqliteConnection.ClearAllPools();
foreach (var file in new DirectoryInfo(m_TestOutputFolder).EnumerateFiles())
{
file.Delete();
}
}
[Test]
public async Task Analyze_PlayerData_DatabaseCorrect()
{
var databasePath = SQLTestHelper.GetDatabasePath(m_TestOutputFolder);
var analyzePath = Path.Combine(Context.UnityDataFolder);
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", analyzePath, "-p", "*." }));
using var db = SQLTestHelper.OpenDatabase(databasePath);
using var cmd = db.CreateCommand();
cmd.CommandText =
@"SELECT
(SELECT COUNT(*) FROM asset_bundles),
(SELECT COUNT(*) FROM assets),
(SELECT COUNT(*) FROM objects),
(SELECT COUNT(*) FROM refs),
(SELECT COUNT(*) FROM serialized_files)";
using var reader = cmd.ExecuteReader();
reader.Read();
Assert.AreEqual(0, reader.GetInt32(0));
Assert.AreEqual(0, reader.GetInt32(1));
Assert.Greater(reader.GetInt32(2), 0);
Assert.Greater(reader.GetInt32(3), 0);
Assert.AreEqual(1, reader.GetInt32(4));
}
[Test]
public async Task DumpText_PlayerData_TextFileCreatedCorrectly()
{
var path = Path.Combine(Context.UnityDataFolder, "level0");
var outputFile = Path.Combine(m_TestOutputFolder, "level0.txt");
Assert.AreEqual(0, await Program.Main(new string[] { "dump", path }));
Assert.IsTrue(File.Exists(outputFile));
var content = File.ReadAllText(outputFile);
var expected = File.ReadAllText(Path.Combine(Context.ExpectedDataFolder, "level0.txt"));
// Normalize line endings.
content = Regex.Replace(content, @"\r\n|\n\r|\r", "\n");
expected = Regex.Replace(expected, @"\r\n|\n\r|\r", "\n");
Assert.AreEqual(expected, content);
}
[Test]
public async Task Analyze_PlayerDataNoTypeTree_ReportsFailureCorrectly()
{
// Test for issue #48: Files that fail to process should be counted as failures, not successes
var testDataFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", "PlayerNoTypeTree");
using var swOut = new StringWriter();
using var swErr = new StringWriter();
var currentOut = Console.Out;
var currentErr = Console.Error;
try
{
Console.SetOut(swOut);
Console.SetError(swErr);
// Analyze should return 0 even if files fail (non-zero would be a critical error)
Assert.AreEqual(0, await Program.Main(new string[] { "analyze", testDataFolder, "-p", "level0" }));
var output = swOut.ToString() + swErr.ToString();
// Check that the filename appears in the error output
Assert.That(output, Does.Contain("level0"), "Expected 'level0' to appear in error output");
// Check that the summary line correctly reports the failure
Assert.That(output, Does.Contain("Failed files: 1"), "Expected 'Failed files: 1' in summary");
Assert.That(output, Does.Contain("Successfully processed files: 0"), "Expected 'Successfully processed files: 0' in summary");
}
finally
{
Console.SetOut(currentOut);
Console.SetError(currentErr);
}
}
}