161 lines
6.5 KiB
C#
161 lines
6.5 KiB
C#
#nullable enable
|
|
|
|
using NUnit.Framework;
|
|
using System;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UVC.Data.Core;
|
|
|
|
namespace UVC.Tests.Data
|
|
{
|
|
[TestFixture]
|
|
public class DataArrayTests
|
|
{
|
|
private DataArray _dataArray = new DataArray();
|
|
private DataObject _item1 = new DataObject();
|
|
private DataObject _item2 = new DataObject();
|
|
private DataObject _item3 = new DataObject();
|
|
|
|
|
|
/// <summary>
|
|
/// 모든 테스트 메서드를 실행하는 메서드입니다.
|
|
/// </summary>
|
|
public void TestAll()
|
|
{
|
|
Debug.Log("===== DataArray 테스트 시작 =====");
|
|
|
|
SetUp();
|
|
RunTest(nameof(Add_AddsItemToList), Add_AddsItemToList);
|
|
SetUp();
|
|
RunTest(nameof(Remove_RemovesItemFromList), Remove_RemovesItemFromList);
|
|
SetUp();
|
|
RunTest(nameof(Clear_RemovesAllItems), Clear_RemovesAllItems);
|
|
SetUp();
|
|
RunTest(nameof(UpdateDifferent_TracksAddsDeletesAndModifies), UpdateDifferent_TracksAddsDeletesAndModifies);
|
|
SetUp();
|
|
RunTest(nameof(ClearTrackedChanges_ResetsAllChangeLists), ClearTrackedChanges_ResetsAllChangeLists);
|
|
|
|
Debug.Log("===== DataArray 테스트 완료 =====");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 단일 테스트 메서드를 실행하고 결과를 로그로 출력합니다.
|
|
/// </summary>
|
|
private void RunTest(string testName, Action testAction)
|
|
{
|
|
try
|
|
{
|
|
Debug.Log($"테스트 시작: {testName}");
|
|
testAction();
|
|
Debug.Log($"테스트 성공: {testName}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError($"테스트 실패: {testName}\n{ex.Message}\n{ex.StackTrace}");
|
|
}
|
|
}
|
|
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
// 테스트에 사용할 DataObject 아이템들을 초기화합니다.
|
|
// UpdateDifferent 메서드가 Id를 기준으로 동작하므로 Id를 설정합니다.
|
|
_item1 = new DataObject { { "Id", "1" }, { "value", "A" } };
|
|
_item2 = new DataObject { { "Id", "2" }, { "value", "B" } };
|
|
_item3 = new DataObject { { "Id", "3" }, { "value", "C" } };
|
|
|
|
// 각 테스트 전에 DataArray를 초기화합니다.
|
|
_dataArray = new DataArray(new[] { _item1, _item2 });
|
|
}
|
|
|
|
[Test]
|
|
public void Add_AddsItemToList()
|
|
{
|
|
// Act
|
|
_dataArray.Add(_item3);
|
|
|
|
// Assert
|
|
Assert.AreEqual(3, _dataArray.Count, "DataArray의 전체 개수는 3이어야 합니다.");
|
|
Assert.Contains(_item3, _dataArray.ToList());
|
|
Assert.AreEqual(0, _dataArray.AddedItems.Count, "AddChild 메서드는 더 이상 AddedItems를 직접 채우지 않아야 합니다.");
|
|
}
|
|
|
|
[Test]
|
|
public void Remove_RemovesItemFromList()
|
|
{
|
|
// Act
|
|
bool result = _dataArray.Remove(_item1);
|
|
|
|
// Assert
|
|
Assert.IsTrue(result, "항목 제거는 성공해야 합니다.");
|
|
Assert.AreEqual(1, _dataArray.Count, "DataArray의 전체 개수는 1이어야 합니다.");
|
|
Assert.IsFalse(_dataArray.Contains(_item1));
|
|
Assert.AreEqual(0, _dataArray.RemovedItems.Count, "RemoveChild 메서드는 더 이상 RemovedItems를 직접 채우지 않아야 합니다.");
|
|
}
|
|
|
|
[Test]
|
|
public void Clear_RemovesAllItems()
|
|
{
|
|
// Act
|
|
_dataArray.Clear();
|
|
|
|
// Assert
|
|
Assert.AreEqual(0, _dataArray.Count, "DataArray는 비어있어야 합니다.");
|
|
Assert.AreEqual(0, _dataArray.RemovedItems.Count, "Clear 메서드는 더 이상 RemovedItems를 직접 채우지 않아야 합니다.");
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateDifferent_TracksAddsDeletesAndModifies()
|
|
{
|
|
// Arrange
|
|
// 기존 항목 수정, 새 항목 추가를 포함하는 다른 DataArray 생성
|
|
var item2Modified = new DataObject { { "Id", "2" }, { "value", "B_modified" } };
|
|
var item4New = new DataObject { { "Id", "4" }, { "value", "D" } };
|
|
var otherArray = new DataArray(new[] { item2Modified, item4New });
|
|
|
|
// Act
|
|
_dataArray.UpdateDifferent(otherArray, true);
|
|
|
|
// Assert
|
|
// 제거된 항목(_item1) 확인
|
|
Assert.AreEqual(1, _dataArray.RemovedItems.Count, "제거된 항목이 1개여야 합니다.");
|
|
Assert.AreEqual("1", _dataArray.RemovedItems[0].Id);
|
|
|
|
// 추가된 항목(item4New) 확인
|
|
Assert.AreEqual(1, _dataArray.AddedItems.Count, "추가된 항목이 1개여야 합니다.");
|
|
Assert.AreEqual("4", _dataArray.AddedItems[0].Id);
|
|
|
|
// 수정된 항목(_item2) 확인
|
|
Assert.AreEqual(1, _dataArray.ModifiedList.Count, "수정된 항목이 1개여야 합니다.");
|
|
Assert.AreEqual("2", _dataArray.ModifiedList[0].Id);
|
|
|
|
// 최종 컬렉션 상태 확인
|
|
Assert.AreEqual(2, _dataArray.Count, "최종 컬렉션의 크기는 2여야 합니다.");
|
|
Assert.IsFalse(_dataArray.Any(i => i.Id == "1"), "제거된 항목이 컬렉션에 없어야 합니다.");
|
|
Assert.IsTrue(_dataArray.Any(i => i.Id == "4"), "추가된 항목이 컬렉션에 있어야 합니다.");
|
|
|
|
var updatedItem = _dataArray.First(i => i.Id == "2");
|
|
Assert.AreEqual("B_modified", updatedItem.GetString("value"), "수정된 항목의 값이 반영되어야 합니다.");
|
|
}
|
|
|
|
[Test]
|
|
public void ClearTrackedChanges_ResetsAllChangeLists()
|
|
{
|
|
// Arrange
|
|
// UpdateDifferent를 호출하여 변경 내역을 생성합니다.
|
|
var otherArray = new DataArray(new[] { _item3 });
|
|
_dataArray.UpdateDifferent(otherArray, true);
|
|
Assert.Greater(_dataArray.UpdatedCount, 0, "테스트 준비 단계에서 변경 내역이 있어야 합니다.");
|
|
|
|
// Act
|
|
_dataArray.ClearTrackedChanges();
|
|
|
|
// Assert
|
|
Assert.AreEqual(0, _dataArray.UpdatedCount, "UpdatedCount는 0이어야 합니다.");
|
|
Assert.IsEmpty(_dataArray.AddedItems, "AddedItems 리스트는 비어있어야 합니다.");
|
|
Assert.IsEmpty(_dataArray.RemovedItems, "RemovedItems 리스트는 비어있어야 합니다.");
|
|
Assert.IsEmpty(_dataArray.ModifiedList, "ModifiedList 리스트는 비어있어야 합니다.");
|
|
}
|
|
}
|
|
} |