using System.Collections; using System.Collections.Generic; using NUnit.Framework; using UnityEngine; using UnityEditor; using UnityEngine.TestTools; using KairoEngine.Core; using KairoEngine.StorySystem; using UniRx; namespace KairoEngine.StorySystem.EditorTests { public class StorySystemTests { private StoryController storyController; private string storyName = "TestStory"; private List lines; [SetUp] public void Setup() { GenericEventsStoryExtensions.list = new Dictionary>(); lines = new List(); lines.Add("Once upon a time..."); lines.Add("There was a prince and a princess."); lines.Add("They lived happily ever after."); string scriptPath = "Assets/Plugins/KairoEngine/StorySystem/Tests/test-script.json"; TextAsset storyJSON = AssetDatabase.LoadAssetAtPath(scriptPath); if(storyJSON != null) storyController = new StoryController(storyJSON, storyName, false); else Debug.LogError($"Could not load file \"{scriptPath}\""); } [TearDown] public void TearDown() { if(storyController != null) { storyController.Stop(); storyController = null; } } [Test] public void StoryController_Constructor_test() { Assert.NotNull(storyController); } [UnityTest] public IEnumerator StoryController_Start() { string result = ""; bool done = false; CompositeDisposable cancel = new CompositeDisposable(); StoryController.Lines().Subscribe(storyStep => { Debug.Log(storyStep.text); result = storyStep.text; done = true; cancel.Dispose(); }).AddTo(cancel); storyController.Start(); while(done == false) { yield return null; } Assert.AreEqual("Once upon a time...", result); } [UnityTest] public IEnumerator StoryController_Receive_Lines() { string result = ""; int nextLine = 0; bool done = false; CompositeDisposable cancel = new CompositeDisposable(); StoryController.Lines().Subscribe(storyStep => { if(storyStep.category != StoryStepType.Line) return; result = storyStep.text; //Debug.Log($"{nextLine}: {storyStep.text}"); nextLine += 1; if(nextLine >= 2) done = true; }).AddTo(cancel); storyController.Start(); while(done == false) { yield return null; } cancel.Dispose(); Assert.AreEqual("There was a prince and a princess.", result); } [UnityTest] public IEnumerator StoryController_Receive_Branches() { List branches = new List(); bool done = false; CompositeDisposable cancel = new CompositeDisposable(); StoryController.Lines().Subscribe(storyStep => { if(storyStep.category != StoryStepType.Branch) return; for (int i = 0; i < storyStep.branches.Count; i++) { branches.Add(storyStep.branches[i]); } done = true; }).AddTo(cancel); storyController.Start(); while(done == false) { yield return null; } cancel.Dispose(); Assert.AreEqual(2, branches.Count); Assert.AreEqual("There was one choice.", branches[0]); Assert.AreEqual("or another choice.", branches[1]); } [UnityTest] public IEnumerator StoryController_Select_Path() { List branches = new List(); string text = ""; bool pathSelected = false; bool done = false; CompositeDisposable cancel = new CompositeDisposable(); StoryController.Lines().Subscribe(storyStep => { if(storyStep.category == StoryStepType.Line) Debug.Log($"{storyStep.text} ({storyStep.category})"); if(storyStep.category == StoryStepType.Line && pathSelected) { text = storyStep.text; done = true; } if(storyStep.category == StoryStepType.Branch) { StoryStepData newStoryStep = new StoryStepData(StoryStepType.Path, storyStep.branches[0], null, null, 0); pathSelected = true; for (int i = 0; i < storyStep.branches.Count; i++) { if(i != 0) Debug.Log($"{storyStep.branches[i]} ({storyStep.category})"); else Debug.Log($"{newStoryStep.text} ({storyStep.category}/{newStoryStep.category})"); } EventManager.broadcast.Trigger(storyName, newStoryStep); } }).AddTo(cancel); storyController.Start(); while(done == false) { yield return null; } cancel.Dispose(); Assert.AreEqual("They lived happily ever after.", text); } } }