0% found this document useful (0 votes)
28 views5 pages

Task2.3P CodeV2

The document contains code for an IdentifiableObject class that can identify objects by string identifiers and for unit tests of that class using the NUnit framework. The IdentifiableObject class allows adding, checking for and retrieving identifiers as strings. The unit tests create IdentifiableObjects and assert their behavior.

Uploaded by

L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Task2.3P CodeV2

The document contains code for an IdentifiableObject class that can identify objects by string identifiers and for unit tests of that class using the NUnit framework. The IdentifiableObject class allows adding, checking for and retrieving identifiers as strings. The unit tests create IdentifiableObjects and assert their behavior.

Uploaded by

L
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SwinAdventure

IdentifiableObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SwinAdventure
{
public class IdentifiableObject
{
List<string> _identifiers;

public IdentifiableObject(string [] idents)


{
_identifiers = new List<string>(idents);
}

public bool AreYou(string id)


{
for (int j = 0; j < _identifiers.Count; j++)
{
if (_identifiers[j].ToLower().Equals(id.ToLower()))
{
return true;
}
}
return false;
}

public string FirstID()


{
if(_identifiers.Count == 0)
{
return "";
}
return _identifiers[0];
}

public void AddIdentifier(string id)


{
_identifiers.Add(id.ToLower());
}
}
}
Program.cs
using System;

namespace SwinAdventure
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
UnitTest

UnitTest1.cs
/*
* File: NunitTemplate.cs
* Author: Joshua Wright
* Date: 16/08/2019
* Unit: COS20007 Object Oriented Programming
* Institution: Swinburne University of Technology
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework; //Don't forget this.
using SwinAdventure;

namespace UnitTest1 //Rename this to the namespace of your project.


{
[TestFixture]
public class UnitTest1 //Rename this appropriately.
{
/// <summary>
/// FIELDS
/// Declare the fields you are going to use to access the objects you are
testing here.
/// </summary>
//For Example:
private Object _testableObject;
private string _testableString;

/// <summary>
/// SETUP
/// Use this method to setup the objects you are going to use for each test.
/// This method will be executed before each test is run, "resetting" your
objects/data.
/// </summary>
//For Example:
[SetUp]
public void SetUp()
{
_testableObject = new object();
_testableString = "Some string.";
}

/// <summary>
/// TESTS
/// Use these methods to run tests with only one scenario.
/// Remember to name your test methods appropriately.
/// </summary>
//For Example:
[Test]
public void TestAreYou()
{
IdentifiableObject id = new IdentifiableObject(new string[] { "Ray",
"James" });
bool result = id.AreYou("Ray");
Assert.IsTrue(result);
}
[Test]
public void TestAreYouNot()
{
IdentifiableObject id = new IdentifiableObject(new string[] { "Ray",
"James" });
bool result = id.AreYou("Allen");
Assert.IsFalse(result);
}

[Test]
public void TestAreYouCaseSensitive()
{
IdentifiableObject id = new IdentifiableObject(new string[] { "Ray",
"James" });
bool result = id.AreYou("RAY");
Assert.IsTrue(result);
}

[Test]
public void TestAddID()
{
IdentifiableObject id = new IdentifiableObject(new string[] { "Ray",
"James" });
id.AddIdentifier("Allen");
bool result = id.AreYou("Allen");
Assert.IsTrue(result);
}

public void TestObjectExists()


{
/* ASSERT STATEMENTS
* Assert statements are how you test your data.
* If the data is what you expect, the assert statements pass and so do
your tests.
* If the data does not match what you expect, the test will fail.
*/
Assert.IsNotNull(_testableObject); //For example
/* Most assert statments take 3 arguments:
* - expected: The value/data you are expecting to get.
* - actual: The actual data you have generated in your test.
* - message (optional): A short message informing you (the programmer)
what the
* assert statement is testing. For your own benefit when you are
debugging later.
* For example:
*/
StringAssert.AreEqualIgnoringCase
(
"some string.", //expected
_testableString, //actual
"Testing that the string is indeed some string" //message
);
/* There are many different types of assert statements.
* Don't forget to use the most appropriate assert statement for the data
you are
* testing.
* For Example:
* - Assert.IsTrue/IsFalse to test a boolean.
* - Assert.AreEqual to compare two values.
* - Assert.AreSame to compare two objects.
* - StringAssert statements to test strings.
* - Many other assert statements for different scenarios...
*/
}

/// <summary>
/// TESTCASES
/// Use these methods to run tests when you have multiple scenarios you want
to test.
/// Create parameters to allow you to pass in the unique data you will be
testing
/// against for each scenario.
/// Pass your unique data for each scenario as arugments in each TestCase.
/// Please Note: The data you pass in must be static and cannot be a pointer
to an
/// object instance.
/// The test will run once per TestCase.
/// </summary>
//For Example:
[TestCase("Some string.")] //Scenario 1: Exact match.
[TestCase("SOME sTrInG.")] //Scenario 2: Contains uppercase characters.
[TestCase("some string.")] //Scenario 3: All lowercase characters.
public void TestString(string toTest)
{
StringAssert.AreEqualIgnoringCase(toTest, _testableString, "Testing the
strings are actually the same.");
}

/*
* Don't forget to run the tests using the test explorer.
* How you do this differs slightly depending on what version of VS you are
running
* and what OS you have.
* If you are unsure how to do this, have a google or seek help in the
discussion
* board or at the help desk.
* Happy Testing! :)
*/
}
}

Program.cs
using System;

namespace UnitTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

You might also like