Commit 25a3d60c authored by 16이상민's avatar 16이상민

Write tests for "JudgeManager"

parent b7d28703
This diff is collapsed.
fileFormatVersion: 2
guid: 3e4992bf17dae8e44b8880d8867b91ea
timeCreated: 1518892677
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: b5f132dad262fcc43a19502f6a0ee9e3
timeCreated: 1518923184
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -35,19 +35,17 @@ namespace JudgeModule
public bool IsBreak { get; private set; }
public Color Color { get; private set; }
public static readonly Judge BAD = JudgeList[2];
public static readonly Judge MISS = JudgeList.Last();
public static readonly Judge BAD = JudgeList[2];
public static readonly Judge MISS = JudgeList[3];
public static readonly Judge Perfect = JudgeList[0];
public static readonly
float MaxButtonTimingRange = BadTime;
public static Judge TestJudge(Note note, float elapsedTime, bool end = false, bool test = false)
public static Judge TestJudge(Note note, float elapsedTime, bool end = false)
{
float timing = end ? note.EndTiming : note.StartTiming;
float difference = elapsedTime - timing;
if (test)
Debug.Log(difference);
var result = JudgeList.Where(x => Mathf.Abs(difference) < x.ButtonTimingRange).ToList();
return result.Count == 0 ? JudgeList.Last() : result[0];
......@@ -67,9 +65,18 @@ namespace JudgeModule
public static bool IsNoteEnd(Note note, float elapsedTime)
{
var timing = elapsedTime - note.EndTiming;
return timing <= BadTime &&
timing >= 0;
var difference = elapsedTime - note.EndTiming;
return difference <= BadTime &&
difference >= 0;
}
public static bool IsNoteProgress(Note note, float elapsedTime, float interval)
{
var timing = note.StartTiming + (interval * (note.JudgeCount + 1));
return Mathf.Abs(elapsedTime - timing) < PerfectTime &&
Mathf.Abs(elapsedTime - timing) <
Mathf.Abs(elapsedTime - (timing - interval));
}
}
}
\ No newline at end of file
......@@ -39,16 +39,13 @@ namespace JudgeModule
DeactivateNote(note);
}
public void ContinuingNoteProc(Note note, float timing)
public void ContinuingNoteProc(Note note, float timing, float interval)
{
var isEnd = Judge.IsNoteEnd(note, timing);
var judge = Judge.TestJudge(note, timing, isEnd);
SetJudge(judge);
if (!isEnd &&
judge.IsBreak)
DeactivateNote(note);
if (Judge.IsNoteProgress(note, timing, interval))
{
SetJudge(Judge.Perfect);
++note.JudgeCount;
}
}
public void CorrectlyStoppedNoteProc(Note note, float timing)
......
......@@ -7,9 +7,6 @@ namespace JudgeModule
{
public class NoteManager
{
private float baseTime;
private bool IsInitTime;
private GameObject offset;
private GameObject noteobj;
private GameObject deactives;
......@@ -49,16 +46,8 @@ namespace JudgeModule
GetLastNote = getlastnote;
}
public void Controll(float current)
public void Controll(float timing)
{
if (!IsInitTime)
{
baseTime = current;
IsInitTime = true;
}
float timing = current - baseTime;
offset.transform.position = Vector3.left * timing * ScrollSpeed;
RelocateControllers(timing);
......
......@@ -9,9 +9,7 @@ public class Controller : MonoBehaviour
private readonly float minAlpha = 0.3f;
private readonly float maxAlpha = 0.7f;
public int id;
// Update is called once per frame
void Update()
{
......
......@@ -168,7 +168,7 @@ public class InGameManager : MonoBehaviour {
if (condition.IsLongNoteStartCorrectly(note))
judger.EnteredNoteProc(note, timing);
else if (condition.IsLongNoteHoldCorrectly(note))
judger.ContinuingNoteProc(note, timing);
judger.ContinuingNoteProc(note, timing, 0);
else if (condition.IsLongNoteFinishCorrectly(note, timing))
judger.CorrectlyStoppedNoteProc(note, timing);
else if (condition.IsLongNoteFinishIncorrectly(note, timing))
......
This diff is collapsed.
......@@ -2,7 +2,7 @@
{
public Controller Component = null;
public int id;
public int JudgeCount;
public float StartTiming { get; private set; }
public float EndTiming { get; private set; }
......
using UnityEngine.TestTools;
using System.Collections;
using UnityEngine;
using JudgeModule;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using TestSupport;
using UnityEngine.UI;
using MotionAnalysis;
public class JudgeManagerTests
{
[UnityTest]
public IEnumerator Judge_Miss_And_Note_Deactive_When_Wrong_Input_Button()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("SMO", 0);
controller.Instance.Component = controller;
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 0);
var expected = true;
var actual = (objects["judgetext"].GetComponent<Text>().text == Judge.MISS.Name &&
controller.transform.parent.name == "Deactives");
Assert.AreEqual(expected, actual, "Judge is miss and note is deactive when wrong input with button");
}
[UnityTest]
public IEnumerator Judge_Miss_And_Note_Deactive_When_Wrong_Input_Motion()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("SBT", 0);
controller.Instance.Component = controller;
yield return null;
input.CurrentMotionState = MotionState.CLAP_PREPARE;
yield return null;
input.CurrentMotionState = MotionState.CLAP_DONE;
manager.JudgeNote(controller.Instance, 0);
var expected = true;
var actual = (objects["judgetext"].GetComponent<Text>().text == Judge.MISS.Name &&
controller.transform.parent.name == "Deactives");
Assert.AreEqual(expected, actual, "Judge is miss and note is deactive when wrong input with motion");
}
[UnityTest]
public IEnumerator Judge_Valid_When_Short()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
bool isCorrect = true;
new Dictionary<float, Judge>
{
{ 0, Judge.JudgeList[0] },
{ Judge.JudgeList[0].ButtonTimingRange - 1, Judge.JudgeList[0] },
{ Judge.JudgeList[0].ButtonTimingRange, Judge.JudgeList[1] },
{ Judge.JudgeList[1].ButtonTimingRange - 1, Judge.JudgeList[1] },
{ Judge.JudgeList[1].ButtonTimingRange, Judge.JudgeList[2] },
{ Judge.JudgeList[2].ButtonTimingRange - 1, Judge.JudgeList[2] },
{ Judge.JudgeList[2].ButtonTimingRange, Judge.JudgeList[3] }
}
.ToList()
.ForEach(x =>
{
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("SBT", 0);
controller.Instance.Component = controller;
manager.JudgeNote(controller.Instance, x.Key);
if (objects["judgetext"].GetComponent<Text>().text != x.Value.Name)
isCorrect = false;
})
;
manager.JudgeNote(controller.Instance, 0);
var expected = true;
var actual = isCorrect;
Assert.AreEqual(expected, actual, "Judge is valid when short note");
}
[UnityTest]
public IEnumerator Judge_Valid_When_Long()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
bool isCorrect = true;
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("LBT", 0, 2000);
controller.Instance.Component = controller;
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 0);
if (controller.Instance.Activated == false)
isCorrect = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 1000);
if (controller.Instance.JudgeCount == 0)
isCorrect = false;
yield return null;
input.IsButtonDown = false;
manager.JudgeNote(controller.Instance, 2000);
if (controller.Instance.JudgeCount != 1 ||
objects["judgetext"].GetComponent<Text>().text == Judge.MISS.Name)
isCorrect = false;
var expected = true;
var actual = isCorrect;
Assert.AreEqual(expected, actual, "Judge is valid when long note");
}
[UnityTest]
public IEnumerator Judge_Invalid_When_Long()
{
var objects = MakeObjects();
var input = MakeInput();
var manager = new JudgeManager(input, () => { }, objects, 60);
var maker = new TestNoteMaker();
var controller = objects["appear"].transform
.GetChild(0)
.GetComponent<Controller>();
bool isCorrect = true;
controller.transform.SetParent(objects["noteobj"].transform);
controller.Instance = maker.MakeNote("LBT", 0, 2000);
controller.Instance.Component = controller;
yield return null;
input.IsButtonDown = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 0);
if (controller.Instance.Activated == false)
isCorrect = false;
yield return null;
input.IsButtonDown = true;
manager.JudgeNote(controller.Instance, 1000);
if (controller.Instance.JudgeCount == 0)
isCorrect = false;
yield return null;
input.IsButtonDown = false;
manager.JudgeNote(controller.Instance, 1800);
if (controller.Instance.JudgeCount != 1 ||
objects["judgetext"].GetComponent<Text>().text != Judge.MISS.Name)
isCorrect = false;
var expected = true;
var actual = isCorrect;
Assert.AreEqual(expected, actual, "Judge is invalid when long note");
}
private Dictionary<string, GameObject> MakeObjects()
{
var offset = new GameObject("Offset");
var noteobj = new GameObject("NoteObj");
var deactives = new GameObject("Deactives");
var appear = new GameObject("AppearNote");
var disappear = new GameObject("DisappearNote");
var judgetext = new GameObject("Judge");
offset .transform.position = Vector3.zero;
noteobj .transform.position = Vector3.zero;
deactives.transform.position = Vector3.zero;
appear .transform.position = Vector3.right * 1000;
disappear.transform.position = Vector3.left * 1000;
noteobj .transform.SetParent(offset.transform);
deactives.transform.SetParent(offset.transform);
Enumerable.Range(0, 20).ToList().ForEach(x =>
{
var obj = new GameObject();
obj.transform.SetParent(appear.transform);
var controller = obj.AddComponent<Controller>();
controller.Instance = new Note(0, 0);
});
judgetext.AddComponent<Text>();
return new Dictionary<string, GameObject>
{
{ "offset", offset },
{ "noteobj", noteobj },
{ "deactives", deactives },
{ "appear", appear },
{ "disappear", disappear },
{ "judgetext", judgetext }
};
}
private InputManager MakeInput()
{
var obj = new GameObject();
return obj.AddComponent<InputManager>();
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 141ae0ccc58281c4a8177fb1b037e44f
timeCreated: 1518879354
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -166,9 +166,9 @@ public class NoteJudgerTests
}
[UnityTest]
public IEnumerator ContinuingNoteProc_Should_Deactive_When_Not_End_And_Break()
public IEnumerator ContinuingNoteProc_Should_Judge_Perfect_When_Next_Timeing_In_Interval()
{
var settings = new TestSettings("LBT", 0, Judge.BAD.ButtonTimingRange + 1);
var settings = new TestSettings("LBT", 0, 1000);
var note = settings.note;
......@@ -181,60 +181,18 @@ public class NoteJudgerTests
yield return null;
judger.ContinuingNoteProc(note, -(Judge.BAD.ButtonTimingRange + 1));
note.Activated = true;
var expected = "Deactives";
var actual = note.Component.transform.parent.name;
Assert.AreEqual(expected, actual, "ContinuingNoteProc should deactive when not end and break");
}
[UnityTest]
public IEnumerator ContinuingNoteProc_Should_Not_Deactive_When_End()
{
var settings = new TestSettings("LBT", 0, 1);
var note = settings.note;
var judger = new NoteJudger(new Dictionary<string, GameObject>
{
{ "deactives", settings.deactives },
{ "judgetext", settings.judge }
},
() => { });
yield return null;
judger.ContinuingNoteProc(note, note.EndTiming);
float interval = 60f;
float frame = 10f;
var expected = "NoteObj";
var actual = note.Component.transform.parent.name;
Assert.AreEqual(expected, actual, "ContinuingNoteProc should not deactive when end");
}
[UnityTest]
public IEnumerator ContinuingNoteProc_Should_Not_Deactive_When_Not_Break()
{
var settings = new TestSettings("LBT", 0, Judge.BAD.ButtonTimingRange + 1);
var note = settings.note;
for (float t = note.StartTiming + frame; t < note.EndTiming; t += frame)
judger.ContinuingNoteProc(note, t, interval);
var judger = new NoteJudger(new Dictionary<string, GameObject>
{
{ "deactives", settings.deactives },
{ "judgetext", settings.judge }
},
() => { });
yield return null;
judger.ContinuingNoteProc(note, note.StartTiming);
var expected = "NoteObj";
var actual = note.Component.transform.parent.name;
var expected = (int)(note.EndTiming / interval);
var actual = note.JudgeCount;
Assert.AreEqual(expected, actual, "ContinuingNoteProc should not deactive when not break");
Assert.AreEqual(expected, actual, "ContinuingNoteProc should judge perfect when next timing is included in interval");
}
class TestSettings
......
......@@ -113,7 +113,7 @@ public class NoteManagerTests
return Mathf.Abs(a - b) >= errorLimit;
}
public Dictionary<string, GameObject> MakeObjects()
private Dictionary<string, GameObject> MakeObjects()
{
var offset = new GameObject();
var noteobj = new GameObject();
......@@ -134,6 +134,7 @@ public class NoteManagerTests
{
var obj = new GameObject();
obj.transform.SetParent(appear.transform);
var controller = obj.AddComponent<Controller>();
controller.Instance = new Note(0, 0);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment