Facebook
From Sexy Leech, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 186
  1. using NUnit.Framework;
  2. using System;
  3. using UnityEngine;
  4.  
  5. namespace Tests
  6. {
  7.     public interface ITime
  8.     {
  9.         float delta { get; }
  10.     }
  11.  
  12.     public class TimeMock : ITime
  13.     {
  14.         public float delta { get; set; }
  15.     }
  16.  
  17.     class Timer
  18.     {
  19.         private ITime time;
  20.         public float duration { get; private set; }
  21.         public float remaining { get; private set; }
  22.         public event Action expired;
  23.         private bool active;
  24.  
  25.         public Timer(ITime time)
  26.             : this(time, 0)
  27.         {
  28.         }
  29.  
  30.         public Timer(ITime time, float duration)
  31.         {
  32.             ResetDuration(duration);
  33.             this.time = time;
  34.         }
  35.  
  36.         public void Update()
  37.         {            
  38.             remaining = Mathf.Max(remaining - time.delta, 0);
  39.             if (remaining == 0 && active)
  40.             {
  41.                 active = false;
  42.                 expired?.Invoke();
  43.             }
  44.                
  45.         }
  46.  
  47.         public void ResetDuration(float duration)
  48.         {
  49.             this.duration = duration;
  50.             remaining = duration;
  51.             active = true;
  52.         }
  53.     }
  54.  
  55.     public class CountDownTimerTest
  56.     {
  57.         const float ONE_SECOND = 1.0f;
  58.  
  59.         int callbacks;
  60.         TimeMock timeMock;
  61.         Timer timer;
  62.  
  63.         private void AssertTimeRemaining(int remaining)
  64.         {
  65.             Assert.That(timer.remaining, Is.EqualTo(remaining).Within(0.1f));
  66.         }
  67.  
  68.         private void AssertCallbacks(int times)
  69.         {
  70.             Assert.That(callbacks, Is.EqualTo(times));
  71.         }
  72.  
  73.         private void UpdateForTicks(int ticks)
  74.         {
  75.             for (int i = 0; i < ticks; ++i)
  76.                 timer.Update();
  77.         }
  78.  
  79.         [SetUp]
  80.         public void SetUp()
  81.         {
  82.             // Mock timer to make testable code.
  83.             // Implement ITime with a real implementation
  84.             // for your own implementation.
  85.             timeMock = new TimeMock();
  86.             timeMock.delta = ONE_SECOND;
  87.  
  88.             // Create 0 second timer with fixed mock delta.
  89.             timer = new Timer(timeMock);
  90.             // Every time expired is raised,
  91.             // remember how many times it's been called.
  92.             timer.expired += () => callbacks++;
  93.  
  94.             callbacks = 0;
  95.         }
  96.  
  97.         [Test]
  98.         public void DoesNotExpireOnLongDuration()
  99.         {
  100.             timer.ResetDuration(60);
  101.  
  102.             UpdateForTicks(1);
  103.  
  104.             AssertTimeRemaining(59);
  105.             AssertCallbacks(0);
  106.         }
  107.  
  108.         [Test]
  109.         public void ExpiresOnZeroDuration()
  110.         {
  111.             UpdateForTicks(1);
  112.  
  113.             AssertTimeRemaining(0);
  114.             AssertCallbacks(1);
  115.         }
  116.  
  117.         [Test]
  118.         public void ExpiresOnceOnLongRunthrough()
  119.         {
  120.             timer.ResetDuration(60);
  121.  
  122.             UpdateForTicks(61);
  123.  
  124.             AssertTimeRemaining(0);
  125.             AssertCallbacks(1);
  126.         }
  127.  
  128.         [Test]
  129.         public void ExpiresTwiceAfterResetDuration()
  130.         {
  131.             timer.ResetDuration(60);
  132.             UpdateForTicks(61);
  133.             timer.ResetDuration(60);
  134.             UpdateForTicks(61);
  135.  
  136.             AssertTimeRemaining(0);
  137.             AssertCallbacks(2);
  138.         }
  139.     }
  140. }
  141.