Skip to content

Commit 817beea

Browse files
committed
test: new fixes to the tests to handle the fields refactor. add tests for getting Toc values
1 parent 8417ed8 commit 817beea

19 files changed

+1504
-353
lines changed

Assets/LDtkUnity/Runtime/Fields/LDtkField.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class LDtkField
1818
[SerializeField] private LDtkFieldType _type;
1919
[SerializeField] internal LDtkFieldElement[] _data;
2020

21-
public LDtkDefinitionObjectField Def => _def;
21+
public LDtkDefinitionObjectField Definition => _def;
2222
public string Identifier => _def.Identifier;
2323
public bool IsArray => _def.IsArray;
2424
public LDtkFieldType Type => _type;

Assets/LDtkUnity/Runtime/Fields/LDtkFieldsPublicAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public LDtkDefinitionObjectField GetDefinition(string identifier)
1414
LDtkDebug.LogError($"No field \"{identifier}\" exists in this field component for {obj.name}", obj);
1515
return null;
1616
}
17-
return field.Def;
17+
return field.Definition;
1818
}
1919

2020
#region Int

Assets/LDtkUnity/Runtime/UnityAssets/Assets/LDtkTableOfContents.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public sealed class LDtkTableOfContents : ScriptableObject
1515
{
1616
[SerializeField] internal List<LDtkTableOfContentsEntry> _entries;
1717

18+
public IReadOnlyList<LDtkTableOfContentsEntry> Entries => _entries;
19+
1820
internal void InitializeList(LdtkJson json)
1921
{
2022
_entries = new List<LDtkTableOfContentsEntry>(json.Toc.Length);

Assets/Tests/EditMode/FieldsComponent/FieldsTestBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ public abstract class FieldsTestBase
1212
protected static Dictionary<string,object> ExpectedSingleValues = FixtureConsts.ExpectedSingleValues;
1313
protected static Dictionary<string,object[]> ExpectedArrayValues = FixtureConsts.ExpectedArrayValues;
1414
protected static Dictionary<string,string> ExpectedValuesAsString = FixtureConsts.ExpectedValuesAsString;
15+
protected static Dictionary<string,string> ExpectedValuesAsStringForToc = FixtureConsts.ExpectedValuesAsStringForToc;
1516

1617
protected LDtkFields Fields;
1718
protected LDtkFields FieldsNullable;
19+
protected LDtkTableOfContents Toc;
1820

1921
[SetUp]
2022
public void Setup()
2123
{
2224
FieldsFixture.LoadComponents();
2325
Fields = FieldsFixture.Fields;
2426
FieldsNullable = FieldsFixture.FieldsNullable;
27+
Toc = FieldsFixture.Toc;
2528
}
2629
}
2730
}

Assets/Tests/EditMode/FieldsComponent/FieldsTestErrorHandling.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace LDtkUnity.Tests
77
{
88
public class TestErrorHandling : FieldsTestBase
99
{
10-
const string identifier = "String";
10+
const string identifier = "string";
1111

1212
[Test]
1313
public void NonExistentField()
@@ -43,7 +43,7 @@ public void OutOfBounds()
4343
public void MismatchType()
4444
{
4545
Assert.NotNull(Fields, "Fields != null");
46-
Assert.True(Fields.ContainsField(identifier));
46+
Assert.True(Fields.ContainsField(identifier), $"Fields component doesnt contain {identifier}");
4747

4848
if (!Fields.TryGetString(identifier, out string value))
4949
{

Assets/Tests/EditMode/FieldsComponent/FieldsValuesAsString.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,28 @@ public class FieldsValuesAsString : FieldsTestBase
1010
{
1111
#region ExpectIncorrectValues
1212

13+
/*
1314
[Test, TestCaseSource(nameof(Singles))]
1415
public void GetValueAsString_ExpectIncorrectValue(string s)
1516
{
1617
var value = Fields.GetValueAsString(s);
18+
1719
Assert.AreNotEqual(value, s, $"{value} != {s}");
1820
}
21+
*/
1922

20-
[Test, TestCaseSource(nameof(Arrays))]
23+
/*[Test, TestCaseSource(nameof(Arrays))]
2124
public void GetValuesAsStrings_ExpectIncorrectValue(string s)
2225
{
2326
var value = Fields.GetValuesAsStrings(s);
2427
Assert.AreNotEqual(value, s, $"{value} != {s}");
25-
}
28+
}*/
2629

27-
[Test, TestCaseSource(nameof(Singles))]
30+
/*[Test, TestCaseSource(nameof(Singles))]
2831
public void TryGetValueAsString_ExpectIncorrectValue(string s)
2932
{
3033
Assert.True(Fields.TryGetValueAsString(s, out var value));
34+
3135
Assert.AreNotEqual(value, s, $"{value} != {s}");
3236
}
3337
@@ -36,7 +40,7 @@ public void TryGetValuesAsStrings_ExpectIncorrectValue(string s)
3640
{
3741
Assert.True(Fields.TryGetValuesAsStrings(s, out var value));
3842
Assert.AreNotEqual(value, s, $"{value} != {s}");
39-
}
43+
}*/
4044
#endregion
4145

4246
#region ExpectFailWhenGettingMismatchedSingleOrArray
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
4+
namespace LDtkUnity.Tests
5+
{
6+
internal sealed class TestLoadTocFieldValues : FieldsTestBase
7+
{
8+
//public const string FieldsProjectPath = "Assets/Tests/Misc/OtherTechTests/TestAllFieldsLevels.ldtk";
9+
10+
/*[Test]
11+
public void TestTocJson()
12+
{
13+
string jsonText = TestJsonLoader.LoadTextAsset(FieldsProjectPath);
14+
LdtkJson project = Utf8Json.JsonSerializer.Deserialize<LdtkJson>(jsonText);
15+
LdtkTableOfContentEntry[] toc = project.Toc;
16+
LdtkTableOfContentEntry entry = toc[0];
17+
LdtkTocInstanceData data = entry.InstancesData[0];
18+
var fields = data.Fields;
19+
20+
foreach (var key in fields.Keys)
21+
{
22+
object value = fields[key];
23+
24+
Debug.Log($"{key}: {value}");
25+
}
26+
27+
double intValue = (double)fields["integer"];
28+
double floatValue = (double)fields["float"];
29+
string stringValue = (string)fields["string"];
30+
31+
32+
Debug.Assert(intValue == 12345);
33+
}*/
34+
35+
[Test]
36+
public void TestTocLength()
37+
{
38+
LDtkTableOfContentsEntry entry = Toc.GetEntry("TestAllFields");
39+
LDtkTableOfContentsEntryData firstEntity = entry.Entries[0];
40+
Assert.IsTrue(All.Length == firstEntity.Fields.Length);
41+
}
42+
43+
[Test, TestCaseSource(nameof(All))]
44+
public void TestTocValues(string fieldIdentifier)
45+
{
46+
LDtkTableOfContentsEntry entry = Toc.Entries[0];
47+
Debug.Assert(entry.Definition.Identifier == "TestAllFields");
48+
49+
LDtkTableOfContentsEntryData entity = entry.Entries[0];
50+
LDtkField field = entity.GetField(fieldIdentifier);
51+
52+
string expected = ExpectedValuesAsStringForToc[fieldIdentifier];
53+
string actual;
54+
55+
56+
if (field.Definition.IsArray)
57+
{
58+
string[] values = field.GetValuesAsStrings();
59+
actual = string.Join(", ", values);
60+
}
61+
else
62+
{
63+
actual = field.GetValueAsString();
64+
}
65+
66+
Assert.AreEqual(expected, actual);
67+
}
68+
}
69+
}

Assets/Tests/EditMode/FieldsComponent/TestLoadTocFieldValues.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)