Skip to content

Commit

Permalink
Use more inline data for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
huguesv committed Nov 14, 2024
1 parent af0bbd4 commit bba7765
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 120 deletions.
100 changes: 44 additions & 56 deletions src/Woohoo.Agi.Engine.UnitTest/Interpreter/ParserUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,98 +12,86 @@ public class ParserUnitTest
[Fact]
public void CreateNullVocabulary()
{
// Act
Action act = () => _ = new Parser(null);

// Assert
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void ParseNullText()
{
var resource = CreateEmptyVocabulary();
// Arrange
var resource = new VocabularyBuilder().Build();
var parser = new Parser(resource);

// Act
Action act = () => parser.Parse(null);

// Assert
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void ParseSimpleWordEmptyVocabulary()
[Theory]
[InlineData("get")]
[InlineData("key")]
public void ParseEmptyVocabulary(string text)
{
var resource = CreateEmptyVocabulary();
// Arrange
var resource = new VocabularyBuilder().Build();
var parser = new Parser(resource);

var actual = parser.Parse("get");
var expected = new ParserResult[] { new("get", VocabularyResource.NoFamily) };
actual.Should().BeEquivalentTo(expected);
}
// Act
var actual = parser.Parse(text);

[Fact]
public void ParseSimpleWordsSimpleVocabulary()
{
var resource = CreateSimpleVocabulary();
var parser = new Parser(resource);

var actual = parser.Parse("get key");
var expected = new ParserResult[] { new("get", 50), new("key", 51) };
// Assert
var expected = new ParserResult[] { new(text, VocabularyResource.NoFamily) };
actual.Should().BeEquivalentTo(expected);
}

[Fact]
public void ParseSeparatorsSimpleVocabulary()
[Theory]
[InlineData("get key")]
[InlineData("get,key")]
[InlineData("ge-t key")]
public void ParseSimpleVocabulary(string text)
{
var resource = CreateSimpleVocabulary();
// Arrange
var resource = new VocabularyBuilder()
.WithFamily(50, "get")
.WithFamily(51, "key")
.Build();
var parser = new Parser(resource);

var actual = parser.Parse("get,key");
var expected = new ParserResult[] { new("get", 50), new("key", 51) };
actual.Should().BeEquivalentTo(expected);
}

[Fact]
public void ParseIllegalSeparatorsSimpleVocabulary()
{
var resource = CreateSimpleVocabulary();
var parser = new Parser(resource);
// Act
var actual = parser.Parse(text);

var actual = parser.Parse("ge-t key");
// Assert
var expected = new ParserResult[] { new("get", 50), new("key", 51) };
actual.Should().BeEquivalentTo(expected);
}

[Fact]
public void ParseComplexWordsComplexVocabulary()
[Theory]
[InlineData("get the blue key")]
[InlineData("get this blue key")]
[InlineData("get blue key")]
public void ParseComplexVocabulary(string text)
{
var resource = CreateComplexVocabulary();
var parser = new Parser(resource);

var actual = parser.Parse("get the blue key");
var expected = new ParserResult[] { new("get", 50), new("blue key", 53) };
actual.Should().BeEquivalentTo(expected);
}

private static VocabularyResource CreateEmptyVocabulary()
{
return new VocabularyBuilder().Build();
}

private static VocabularyResource CreateSimpleVocabulary()
{
return new VocabularyBuilder()
.WithFamily(50, "get")
.WithFamily(51, "key")
.Build();
}

private static VocabularyResource CreateComplexVocabulary()
{
return new VocabularyBuilder()
// Arrange
var resource = new VocabularyBuilder()
.WithFamily(50, "get")
.WithFamily(51, "blue")
.WithFamily(52, "key")
.WithFamily(53, "blue key")
.WithFamily(0, "a", "an", "the", "this")
.Build();
var parser = new Parser(resource);

// Act
var actual = parser.Parse(text);

// Assert
var expected = new ParserResult[] { new("get", 50), new("blue key", 53) };
actual.Should().BeEquivalentTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,80 +11,112 @@ public class ResourceManagerUnitTest
[Fact]
public void FindExistingLogic()
{
// Arrange
var mgr = new ResourceManager();
mgr.LogicResources.Add(new LogicResource(44, [], []));

// Act
var resource = mgr.FindLogic(44);

// Assert
resource.Should().NotBeNull();
}

[Fact]
public void FindNonExistingLogic()
{
// Arrange
var mgr = new ResourceManager();
mgr.LogicResources.Add(new LogicResource(44, [], []));

// Act
var resource = mgr.FindLogic(45);

// Assert
resource.Should().BeNull();
}

[Fact]
public void FindExistingPicture()
{
// Arrange
var mgr = new ResourceManager();
mgr.PictureResources.Add(new PictureResource(54, []));

// Act
var resource = mgr.FindPicture(54);

// Assert
resource.Should().NotBeNull();
}

[Fact]
public void FindNonExistingPicture()
{
// Arrange
var mgr = new ResourceManager();
mgr.PictureResources.Add(new PictureResource(54, []));

// Act
var resource = mgr.FindPicture(55);

// Assert
resource.Should().BeNull();
}

[Fact]
public void FindExistingSound()
{
// Arrange
var mgr = new ResourceManager();
mgr.SoundResources.Add(new SoundResource(64, [], [], [], []));

// Act
var resource = mgr.FindSound(64);

// Assert
resource.Should().NotBeNull();
}

[Fact]
public void FindNonExistingSound()
{
// Arrange
var mgr = new ResourceManager();
mgr.SoundResources.Add(new SoundResource(64, [], [], [], []));

// Act
var resource = mgr.FindSound(65);

// Assert
resource.Should().BeNull();
}

[Fact]
public void FindExistingView()
{
// Arrange
var mgr = new ResourceManager();
mgr.ViewResources.Add(new ViewResource(74, [], string.Empty, 0, 0));

// Act
var resource = mgr.FindView(74);

// Assert
resource.Should().NotBeNull();
}

[Fact]
public void FindNonExistingView()
{
// Arrange
var mgr = new ResourceManager();
mgr.ViewResources.Add(new ViewResource(74, [], string.Empty, 0, 0));

// Act
var resource = mgr.FindView(75);

// Assert
resource.Should().BeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,55 @@ public class StringUtilityUnitTest
[Fact]
public void Reverse()
{
// Arrange
var input = "Hello";

// Act
var output = StringUtility.Reverse(input);

// Assert
output.Should().Be("olleH");
}

[Fact]
public void NumberToString()
{
// Arrange
var input = 45;

// Act
var output = StringUtility.NumberToString(input);

// Assert
output.Should().Be("45");
}

[Fact]
public void NumberToHexString()
{
// Arrange
var input = 45;

// Act
var output = StringUtility.NumberToHexString(input);

// Assert
output.Should().Be("2D");
}

[Fact]
public void PadWithZeros()
[Theory]
[InlineData("45", 0, "45")]
[InlineData("45", 1, "45")]
[InlineData("45", 2, "45")]
[InlineData("45", 3, "045")]
[InlineData("45", 4, "0045")]
[InlineData("45", 5, "00045")]
public void PadWithZeros(string input, int size, string expected)
{
var input = "45";
// Act
var output = StringUtility.PadWithZeros(input, size);

var output = StringUtility.PadWithZeros(input, 5);
output.Should().Be("00045");
// Assert
output.Should().Be(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@ namespace Woohoo.Agi.Engine.UnitTest.Resources.Serialization;

public class LzwDecompressUnitTest
{
private readonly byte[] compressed = [0x00, 0x01, 0x04, 0x08, 0x00, 0x00, 0xc0, 0x01, 0x81, 0x03, 0x00, 0x10, 0x48, 0x00, 0x40, 0x80, 0x98, 0x86, 0x0f, 0x01, 0x3c, 0x8c, 0x08, 0xb1, 0x62, 0x40];
private readonly byte[] uncompressed = [0x00, 0x01, 0x01, 0x00, 0x00, 0x07, 0x00, 0x01, 0x03, 0x00, 0x04, 0x09, 0x00, 0x02, 0x62, 0x00, 0x02, 0x62, 0x00, 0x00, 0x62, 0x00, 0x62, 0x00, 0x00, 0x00, 0x02, 0x62, 0x00, 0x02, 0x62, 0x00];

[Fact]
public void DecompressNull()
{
// Arrange
var decompress = new LzwDecompress();

// Act
Action act = () => decompress.Decompress(null, 0, 0, 0);

// Assert
act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void DecompressBcV3View18()
{
Decompress(this.compressed, this.uncompressed);
}

private static void Decompress(byte[] compressed, byte[] expected)
[Theory]
[InlineData(new byte[] { 0x00, 0x01, 0x04, 0x08, 0x00, 0x00, 0xc0, 0x01, 0x81, 0x03, 0x00, 0x10, 0x48, 0x00, 0x40, 0x80, 0x98, 0x86, 0x0f, 0x01, 0x3c, 0x8c, 0x08, 0xb1, 0x62, 0x40 }, new byte[] { 0x00, 0x01, 0x01, 0x00, 0x00, 0x07, 0x00, 0x01, 0x03, 0x00, 0x04, 0x09, 0x00, 0x02, 0x62, 0x00, 0x02, 0x62, 0x00, 0x00, 0x62, 0x00, 0x62, 0x00, 0x00, 0x00, 0x02, 0x62, 0x00, 0x02, 0x62, 0x00 })] // BcV3View18
public void Decompress(byte[] compressed, byte[] expected)
{
// Arrange
var decompress = new LzwDecompress();

// Act
var uncompressed = decompress.Decompress(compressed, 0, compressed.Length, expected.Length);

// Assert
uncompressed.Should().BeEquivalentTo(expected);
}
}
Loading

0 comments on commit bba7765

Please sign in to comment.