123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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<string> lines;
- [SetUp]
- public void Setup()
- {
- GenericEventsStoryExtensions.list = new Dictionary<string, System.Action<StoryStepData>>();
- lines = new List<string>();
- 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<TextAsset>(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<string> branches = new List<string>();
- 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<string> branches = new List<string>();
- 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);
- }
- }
- }
|