Initial commit

This commit is contained in:
github-classroom[bot] 2025-02-25 16:59:47 +00:00 committed by GitHub
commit 67196c7395
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 4693 additions and 0 deletions

View file

@ -0,0 +1,65 @@
namespace PetsAndFleas.Test;
public sealed class CatTests
{
/*
private readonly Cat _cat = new();
[Fact]
public void ClimbOnTree_Simple()
{
_cat.TreesClimbed.Should().Be(0, "no trees climbed yet");
_cat.ClimbOnTree().Should().BeTrue("was not on tree, could climb");
_cat.TreesClimbed.Should().Be(1, "1 tree climbed");
}
[Fact]
public void ClimbOnTree_AlreadyOnTree()
{
_cat.TreesClimbed.Should().Be(0, "no trees climbed yet");
_cat.ClimbOnTree().Should().BeTrue("was not on tree, could climb");
_cat.ClimbOnTree().Should().BeFalse("already on tree");
_cat.TreesClimbed.Should().Be(1, "1 tree climbed");
}
[Fact]
public void ClimbDown_NotOnTree()
{
_cat.ClimbDown()
.Should().BeFalse("was not on tree, cannot climb down");
}
[Fact]
public void ClimbDown_OnTree()
{
_cat.ClimbOnTree();
_cat.ClimbDown()
.Should().BeTrue("was on tree, could climb down");
}
[Fact]
public void ClimbOnTreeAndDown_Simple()
{
_cat.TreesClimbed.Should().Be(0, "no trees climbed yet");
_cat.ClimbOnTree().Should().BeTrue("was not on tree, could climb");
_cat.ClimbDown().Should().BeTrue("was on tree, could climb down");
_cat.TreesClimbed.Should().Be(1, "1 tree climbed");
}
[Fact]
public void ClimbOnTreeAndDown_Multiple()
{
_cat.TreesClimbed.Should().Be(0, "no trees climbed yet");
_cat.ClimbOnTree();
_cat.ClimbDown();
_cat.ClimbOnTree();
_cat.ClimbDown();
_cat.TreesClimbed.Should().Be(2, "two trees climbed");
}
[Fact]
public void StringRepresentation() => new Cat().ToString().Should().Be("I'm a cat");
*/
}

View file

@ -0,0 +1,56 @@
namespace PetsAndFleas.Test;
public sealed class DogTests
{
/*
private readonly Dog _dog = new(new DateTimeProvider());
[Fact]
public void Construction()
{
_dog.HuntedAnimals.Should().Be(0, "no animals hunted yet");
_dog.RemainingBites.Should().Be(100, "set by Pet ctor");
}
[Fact]
public void HuntAnimal_Simple()
{
_dog.HuntAnimal().Should().BeTrue("has never hunted, can hunt immediately");
_dog.HuntedAnimals.Should().Be(1, "hunted one time");
}
[Fact]
public void HuntAnimal_Wait()
{
_dog.HuntAnimal().Should().BeTrue();
_dog.HuntAnimal().Should().BeFalse("cannot hunt again right away, has to wait and recover");
_dog.HuntedAnimals.Should().Be(1, "only hunted once");
}
[Fact]
public void HuntAnimal_Waited()
{
var dtp = new DateTimeProvider();
var time1 = new DateTime(2023, 02, 27, 18, 30, 00);
var time2 = new DateTime(2023, 02, 27, 18, 30, 46);
var time3 = new DateTime(2023, 02, 27, 18, 31, 10);
var dog = new Dog(dtp);
dtp.Now = time1;
dog.HuntAnimal().Should().BeTrue("can hunt");
dog.HuntedAnimals.Should().Be(1);
dtp.Now = time2;
dog.HuntAnimal().Should().BeFalse("cannot hunt again, has to wait 1 minute");
dog.HuntedAnimals.Should().Be(1);
dtp.Now = time3;
dog.HuntAnimal().Should().BeTrue("enough time has passed, can hunt again");
dog.HuntedAnimals.Should().Be(2);
}
[Fact]
public void StringRepresentation() => _dog.ToString().Should().Be("I'm a dog");
*/
}

View file

@ -0,0 +1,122 @@
namespace PetsAndFleas.Test;
public sealed class FleaTests
{
/*
private readonly Flea _flea = new();
[Fact]
public void JumpOnPet()
{
Pet pet = new Cat();
_flea.CurrentPet.Should().BeNull("did not yet jump on a pet");
_flea.JumpOnPet(pet);
_flea.CurrentPet.Should().NotBeNull().And.BeSameAs(pet);
_flea.TotalBites.Should().Be(0, "no bites yet");
}
[Fact]
public void JumpOnPet_Leave()
{
Pet pet = new Cat();
_flea.JumpOnPet(pet);
_flea.CurrentPet.Should().NotBeNull().And.BeSameAs(pet);
_flea.JumpOnPet(null);
_flea.CurrentPet.Should().BeNull("flea left pet");
}
[Fact]
public void BitePet_Simple()
{
const int BiteAmount = 15;
Pet pet = new Dog(new DateTimeProvider());
_flea.JumpOnPet(pet);
_flea.BitePet(BiteAmount)
.Should().Be(BiteAmount, "flea sits on pet and pet has sufficient 'free bites'");
_flea.TotalBites.Should().Be(BiteAmount);
pet.RemainingBites.Should().Be(85, "100 - 15 = 85");
}
[Fact]
public void BitePet_NoPet()
{
_flea.BitePet(10).Should().Be(0, "flea not sitting on a pet");
}
[Fact]
public void BitePet_NegativeBites()
{
Pet pet = new Cat();
_flea.JumpOnPet(pet);
_flea.BitePet(-10)
.Should().Be(0, "negative bites are not executed");
_flea.TotalBites.Should().Be(0, "no bites performed");
pet.RemainingBites.Should().Be(100, "no bites occurred");
}
[Fact]
public void BitePet_NotEnoughAvailableBites()
{
Pet pet = new Cat();
_flea.JumpOnPet(pet);
_flea.BitePet(60);
_flea.BitePet(50)
.Should().Be(40, "there were only 40 'free bites' remaining");
_flea.TotalBites.Should().Be(100, "60 + 40");
}
[Fact]
public void BitePet_MultiplePets()
{
Pet pet1 = new Dog(new DateTimeProvider());
Pet pet2 = new Cat();
_flea.JumpOnPet(pet1);
_flea.BitePet(20);
_flea.BitePet(15);
_flea.JumpOnPet(pet2);
_flea.BitePet(40);
_flea.JumpOnPet(pet1);
_flea.BitePet(10);
_flea.JumpOnPet(null);
_flea.TotalBites.Should().Be(85, "spread across two pets");
pet1.RemainingBites.Should().Be(55);
pet2.RemainingBites.Should().Be(60);
_flea.CurrentPet.Should().BeNull();
}
[Fact]
public void StringRepresentation() => new Flea().ToString().Should().Be("I'm a flea");
#region you wouldn't normally write such tests, only for school context
[Fact]
public void IsSealed()
{
typeof(Flea).IsSealed
.Should().BeTrue("nothing inherits from Flea");
}
[Fact]
public void InheritsDirectlyFromObject()
{
typeof(Flea).BaseType.Should().Be<object>("does not inherit from anything except object");
}
#endregion
*/
}

View file

@ -0,0 +1,90 @@
namespace PetsAndFleas.Test;
public sealed class PetTests
{
/*
[Fact]
public void PetId()
{
int initialId = Pet.NextPetId;
for (var i = 1; i < 5; i++)
{
int expectedId = initialId + i;
Pet pet = i % 2 == 0
? new Dog(new DateTimeProvider())
: new Cat();
pet.PetId.Should().Be(expectedId, "each pet gets a unique id by incrementing a static counter");
}
}
[Fact]
public void RemainingBites_Initial()
{
Pet pet = new Cat();
pet.RemainingBites
.Should().Be(100, "initial value set by Pet ctor");
}
[Theory]
[MemberData(nameof(GetBittenData))]
public void GetBitten(Pet pet, int amountBites, int expectedActualBites, int expectedRemaining,
string reasonActualBites, string reasonRemainingBites)
{
pet.GetBitten(amountBites)
.Should().Be(expectedActualBites, reasonActualBites);
pet.RemainingBites
.Should().Be(expectedRemaining, reasonRemainingBites);
}
public static TheoryData<Pet, int, int, int, string, string> GetBittenData()
{
Pet pet = new Dog(new DateTimeProvider());
return new TheoryData<Pet, int, int, int, string, string>
{
{ pet, 40, 40, 60, "40 < 100, can perform all bites", "100 - 40 = 60" },
{ pet, 70, 60, 0, "70 > 60, can only perform 60 bytes", "no more bites possible" },
{ new Cat(), 200, 100, 0, "200 > 100, performing 100 bites", "all bites 'spent'" },
{ new Dog(new DateTimeProvider()), -50, 0, 100, "negative amount, ignored", "no bites have happened yet" }
};
}
#region you wouldn't normally write such tests, only for school context
[Fact]
public void IsAbstract()
{
typeof(Pet).IsAbstract
.Should().BeTrue("Pet has to be abstract");
}
[Fact]
public void InheritsDirectlyFromObject()
{
typeof(Pet).BaseType.Should().Be<object>("does not inherit from anything except object");
}
[Fact]
public void HasInheritors()
{
var dogType = typeof(Dog);
var catType = typeof(Cat);
var petType = typeof(Pet);
petType.IsAssignableFrom(dogType)
.Should().BeTrue("Pet is base class of Dog");
petType.IsAssignableFrom(catType)
.Should().BeTrue("Pet is base class of Cat");
dogType.IsSealed
.Should().BeTrue("nothing inherits from Dog");
catType.IsSealed
.Should().BeTrue("nothing inherits from Cat");
}
#endregion
*/
}

View file

@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Using Include="FluentAssertions" />
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AwesomeAssertions" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PetsAndFleas\PetsAndFleas.csproj" />
</ItemGroup>
</Project>