Last active
August 30, 2017 13:34
-
-
Save charlieamat/d61ac2c5f69855539b2f544b3f50ecfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
public class Goal : MonoBehaviour { | |
public Players scoresTo; | |
public IScoreEvent scoreEvent; | |
public void Construct(Players scoresTo, IScoreEvent scoreEvent) { | |
this.scoresTo = scoresTo; | |
this.scoreEvent = scoreEvent; | |
} | |
public void Awake() { | |
this.Construct(this.scoresTo, this.scoreEvent); | |
} | |
public void OnTriggerEnter2D(Collider2D other) { | |
if(other.tag == Tags.BALL) scoreEvent.Invoke(scoresTo); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.Events; | |
using UnityEngine.TestTools; | |
using NUnit.Framework; | |
using NSubstitute; | |
using System.Collections; | |
public class GoalTest { | |
public class OnTriggerEnter2D { | |
private Goal goal; | |
private IScoreEvent scoreEvent; | |
private BoxCollider2D ball; | |
[SetUp] | |
public void BeforeEachTest() { | |
goal = new GameObject().AddComponent<Goal>(); | |
scoreEvent = Substitute.For<IScoreEvent>(); | |
ball = new GameObject().AddComponent<BoxCollider2D>(); | |
goal.Construct(Players.ONE, scoreEvent); | |
} | |
[Test] | |
public void Does_Nothing_When_Collider_Tag_Is_Not_Ball() { | |
goal.OnTriggerEnter2D(ball); | |
scoreEvent.DidNotReceive().Invoke(Arg.Any<Players>()); | |
} | |
[Test] | |
public void Calls_ScoreEvent_When_Collider_Tag_Is_Ball() { | |
ball.tag = Tags.BALL; | |
goal.OnTriggerEnter2D(ball); | |
scoreEvent.Received().Invoke(Arg.Any<Players>()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IScoreEvent | |
{ | |
void Invoke(Players arg); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.Serializable] | |
public class ScoreEvent : UnityEvent<Players>, IScoreEvent | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment