ArrowMotionTests.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using UnityEngine;
  6. namespace DiamondArrow.Tests
  7. {
  8. public sealed class ArrowMotionTests
  9. {
  10. [Test] public void BentArrowTailFollowsOriginalTrackWhileHeadLeavesStraight()
  11. {
  12. var path=new ArrowMotionPath(new[]{Vector2.zero,new Vector2(0,2),new Vector2(2,2),new Vector2(2,4)});
  13. var line=new List<Vector2>();path.Window(1,line);
  14. Assert.That(line[0],Is.EqualTo(new Vector2(0,1)));Assert.That(line[line.Count-1],Is.EqualTo(new Vector2(2,5)));
  15. Assert.That(line,Does.Contain(new Vector2(0,2)));Assert.That(line,Does.Contain(new Vector2(2,2)));
  16. path.Window(3,line);Assert.That(line[0],Is.EqualTo(new Vector2(1,2)));CollectionAssert.DoesNotContain(line,new Vector2(0,2));
  17. Assert.That(line,Does.Contain(new Vector2(2,2)));Assert.That(line[line.Count-1],Is.EqualTo(new Vector2(2,7)));
  18. }
  19. [Test] public void EveryArrowKeepsItsLengthAtEveryPartOfItsExit()
  20. {
  21. var book=LevelBook.Parse(File.ReadAllText("Assets/DiamondArrow/Data/Levels.json"));var line=new List<Vector2>();
  22. foreach(var level in book.levels)foreach(var arrow in level.arrows)
  23. {
  24. var path=new ArrowMotionPath(arrow.cells.Select(c=>new Vector2(c.x,c.y)).ToArray());
  25. for(int i=0;i<=30;i++){float travel=(path.Length+level.size)*i/30f;path.Window(travel,line);float length=0;for(int k=1;k<line.Count;k++)length+=Vector2.Distance(line[k-1],line[k]);Assert.That(length,Is.EqualTo(path.Length).Within(.003f),"Level "+level.number);Assert.That(Vector2.Distance(line[line.Count-1],path.Points.Last()+path.Direction*travel),Is.LessThan(.003f));}
  26. }
  27. }
  28. [Test] public void ExitedArrowBecomesStraightOnlyAfterTailPassesLastBend()
  29. {
  30. var path=new ArrowMotionPath(new[]{Vector2.zero,Vector2.up,Vector2.one});var line=new List<Vector2>();path.Window(3,line);
  31. Assert.That(line.Count,Is.EqualTo(2));Assert.That(line[1]-line[0],Is.EqualTo(Vector2.right*2));
  32. }
  33. [Test] public void BlockedArrowAdvancesHoldsAndReturnsWithoutOvershoot()
  34. {
  35. Assert.That(ArrowMotionPath.Bump(0,30),Is.Zero);Assert.That(ArrowMotionPath.Bump(.11f,30),Is.EqualTo(30));Assert.That(ArrowMotionPath.Bump(.16f,30),Is.EqualTo(30));
  36. Assert.That(ArrowMotionPath.Bump(.28f,30),Is.InRange(0f,30f));Assert.That(ArrowMotionPath.Bump(.38f,30),Is.Zero);Assert.That(ArrowMotionPath.Bump(-1,30),Is.Zero);
  37. for(int i=0;i<60;i++)Assert.That(ArrowMotionPath.Bump(i/100f,30),Is.InRange(0f,30f));
  38. }
  39. [Test] public void ExitMotionAcceleratesAndEndsExactlyAtTarget()
  40. {
  41. float prev=0;for(int i=0;i<=100;i++){float now=ArrowMotionPath.Travel(i/100f,.8f,900);Assert.That(now,Is.GreaterThanOrEqualTo(prev));prev=now;}
  42. Assert.That(prev,Is.EqualTo(900));Assert.That(ArrowMotionPath.Travel(-1,.8f,900),Is.Zero);
  43. float early=ArrowMotionPath.Travel(.2f,.8f,900)-ArrowMotionPath.Travel(.1f,.8f,900);float late=ArrowMotionPath.Travel(.7f,.8f,900)-ArrowMotionPath.Travel(.6f,.8f,900);Assert.That(late,Is.GreaterThan(early));
  44. }
  45. }
  46. }