InkTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEngine.TestTools;
  7. using KairoEngine.Core;
  8. using KairoEngine.StorySystem;
  9. using UniRx;
  10. namespace KairoEngine.StorySystem.EditorTests
  11. {
  12. public class StorySystemTests
  13. {
  14. private StoryController storyController;
  15. private string storyName = "TestStory";
  16. private List<string> lines;
  17. [SetUp]
  18. public void Setup()
  19. {
  20. GenericEventsStoryExtensions.list = new Dictionary<string, System.Action<StoryStepData>>();
  21. lines = new List<string>();
  22. lines.Add("Once upon a time...");
  23. lines.Add("There was a prince and a princess.");
  24. lines.Add("They lived happily ever after.");
  25. string scriptPath = "Assets/Plugins/KairoEngine/StorySystem/Tests/test-script.json";
  26. TextAsset storyJSON = AssetDatabase.LoadAssetAtPath<TextAsset>(scriptPath);
  27. if(storyJSON != null) storyController = new StoryController(storyJSON, storyName, false);
  28. else Debug.LogError($"Could not load file \"{scriptPath}\"");
  29. }
  30. [TearDown]
  31. public void TearDown()
  32. {
  33. if(storyController != null)
  34. {
  35. storyController.Stop();
  36. storyController = null;
  37. }
  38. }
  39. [Test]
  40. public void StoryController_Constructor_test()
  41. {
  42. Assert.NotNull(storyController);
  43. }
  44. [UnityTest]
  45. public IEnumerator StoryController_Start()
  46. {
  47. string result = "";
  48. bool done = false;
  49. CompositeDisposable cancel = new CompositeDisposable();
  50. StoryController.Lines().Subscribe(storyStep => {
  51. Debug.Log(storyStep.text);
  52. result = storyStep.text;
  53. done = true;
  54. cancel.Dispose();
  55. }).AddTo(cancel);
  56. storyController.Start();
  57. while(done == false)
  58. {
  59. yield return null;
  60. }
  61. Assert.AreEqual("Once upon a time...", result);
  62. }
  63. [UnityTest]
  64. public IEnumerator StoryController_Receive_Lines()
  65. {
  66. string result = "";
  67. int nextLine = 0;
  68. bool done = false;
  69. CompositeDisposable cancel = new CompositeDisposable();
  70. StoryController.Lines().Subscribe(storyStep => {
  71. if(storyStep.category != StoryStepType.Line) return;
  72. result = storyStep.text;
  73. //Debug.Log($"{nextLine}: {storyStep.text}");
  74. nextLine += 1;
  75. if(nextLine >= 2) done = true;
  76. }).AddTo(cancel);
  77. storyController.Start();
  78. while(done == false)
  79. {
  80. yield return null;
  81. }
  82. cancel.Dispose();
  83. Assert.AreEqual("There was a prince and a princess.", result);
  84. }
  85. [UnityTest]
  86. public IEnumerator StoryController_Receive_Branches()
  87. {
  88. List<string> branches = new List<string>();
  89. bool done = false;
  90. CompositeDisposable cancel = new CompositeDisposable();
  91. StoryController.Lines().Subscribe(storyStep => {
  92. if(storyStep.category != StoryStepType.Branch) return;
  93. for (int i = 0; i < storyStep.branches.Count; i++)
  94. {
  95. branches.Add(storyStep.branches[i]);
  96. }
  97. done = true;
  98. }).AddTo(cancel);
  99. storyController.Start();
  100. while(done == false)
  101. {
  102. yield return null;
  103. }
  104. cancel.Dispose();
  105. Assert.AreEqual(2, branches.Count);
  106. Assert.AreEqual("There was one choice.", branches[0]);
  107. Assert.AreEqual("or another choice.", branches[1]);
  108. }
  109. [UnityTest]
  110. public IEnumerator StoryController_Select_Path()
  111. {
  112. List<string> branches = new List<string>();
  113. string text = "";
  114. bool pathSelected = false;
  115. bool done = false;
  116. CompositeDisposable cancel = new CompositeDisposable();
  117. StoryController.Lines().Subscribe(storyStep => {
  118. if(storyStep.category == StoryStepType.Line) Debug.Log($"{storyStep.text} ({storyStep.category})");
  119. if(storyStep.category == StoryStepType.Line && pathSelected)
  120. {
  121. text = storyStep.text;
  122. done = true;
  123. }
  124. if(storyStep.category == StoryStepType.Branch)
  125. {
  126. StoryStepData newStoryStep = new StoryStepData(StoryStepType.Path, storyStep.branches[0], null, null, 0);
  127. pathSelected = true;
  128. for (int i = 0; i < storyStep.branches.Count; i++)
  129. {
  130. if(i != 0) Debug.Log($"{storyStep.branches[i]} ({storyStep.category})");
  131. else Debug.Log($"{newStoryStep.text} ({storyStep.category}/{newStoryStep.category})");
  132. }
  133. EventManager.broadcast.Trigger(storyName, newStoryStep);
  134. }
  135. }).AddTo(cancel);
  136. storyController.Start();
  137. while(done == false)
  138. {
  139. yield return null;
  140. }
  141. cancel.Dispose();
  142. Assert.AreEqual("They lived happily ever after.", text);
  143. }
  144. }
  145. }