Use typed TestData for unit tests
This commit is contained in:
parent
495288b887
commit
f9269b877c
6 changed files with 176 additions and 122 deletions
94
Tests/Lib/Converters/EmojiConverter.test.cs
Normal file
94
Tests/Lib/Converters/EmojiConverter.test.cs
Normal file
|
@ -0,0 +1,94 @@
|
|||
using Geekbot.net.Lib.Converters;
|
||||
using Xunit;
|
||||
|
||||
namespace Tests.Lib.Converters
|
||||
{
|
||||
public class EmojiConverterTest
|
||||
{
|
||||
public class NumberToEmojiTestDto
|
||||
{
|
||||
public int Number { get; set; }
|
||||
public string Expected { get; set; }
|
||||
}
|
||||
|
||||
public static TestData<NumberToEmojiTestDto> NumberToEmojiTestData =>
|
||||
new TestData<NumberToEmojiTestDto>
|
||||
{
|
||||
{
|
||||
"2",
|
||||
new NumberToEmojiTestDto
|
||||
{
|
||||
Number = 2,
|
||||
Expected = ":two:"
|
||||
}
|
||||
},
|
||||
{
|
||||
"10",
|
||||
new NumberToEmojiTestDto
|
||||
{
|
||||
Number = 10,
|
||||
Expected = "🔟"
|
||||
}
|
||||
},
|
||||
{
|
||||
"15",
|
||||
new NumberToEmojiTestDto
|
||||
{
|
||||
Number = 15,
|
||||
Expected = ":one::five:"
|
||||
}
|
||||
},
|
||||
{
|
||||
"null",
|
||||
new NumberToEmojiTestDto
|
||||
{
|
||||
Number = 0,
|
||||
Expected = ":zero:"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[Theory, MemberData(nameof(NumberToEmojiTestData))]
|
||||
public void NumberToEmoji(string testName, NumberToEmojiTestDto testData)
|
||||
{
|
||||
var emojiConverter = new EmojiConverter();
|
||||
var result = emojiConverter.NumberToEmoji(testData.Number);
|
||||
Assert.Equal(result, testData.Expected);
|
||||
}
|
||||
|
||||
public class TextToEmojiTestDto
|
||||
{
|
||||
public string Text { get; set; }
|
||||
public string Expected { get; set; }
|
||||
}
|
||||
|
||||
public static TestData<TextToEmojiTestDto> TextToEmojiTestData =>
|
||||
new TestData<TextToEmojiTestDto>
|
||||
{
|
||||
{
|
||||
"Test",
|
||||
new TextToEmojiTestDto
|
||||
{
|
||||
Text = "test",
|
||||
Expected = ":regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: "
|
||||
}
|
||||
},
|
||||
{
|
||||
"Best3+?",
|
||||
new TextToEmojiTestDto
|
||||
{
|
||||
Text = "Best3+?",
|
||||
Expected = ":b: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :three: :heavy_plus_sign: :question: "
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[Theory, MemberData(nameof(TextToEmojiTestData))]
|
||||
public void TextToEmoji(string testName, TextToEmojiTestDto testData)
|
||||
{
|
||||
var emojiConverter = new EmojiConverter();
|
||||
var result = emojiConverter.TextToEmoji(testData.Text);
|
||||
Assert.Equal(result, testData.Expected);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using Geekbot.net.Lib.Converters;
|
||||
using Xunit;
|
||||
|
||||
namespace Tests.Lib
|
||||
{
|
||||
public class EmojiConverterTest
|
||||
{
|
||||
public static IEnumerable<object[]> NumberToEmojiTestData
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
2,
|
||||
":two:"
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
10,
|
||||
"🔟"
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
15,
|
||||
":one::five:"
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
null,
|
||||
":zero:"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Theory, MemberData(nameof(NumberToEmojiTestData))]
|
||||
public void NumberToEmoji(int number, string expectedResult)
|
||||
{
|
||||
var emojiConverter = new EmojiConverter();
|
||||
var result = emojiConverter.NumberToEmoji(number);
|
||||
Assert.Equal(result, expectedResult);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> TextToEmojiTestData
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
"test",
|
||||
":regional_indicator_t: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: "
|
||||
};
|
||||
yield return new object[]
|
||||
{
|
||||
"Best3+?",
|
||||
":b: :regional_indicator_e: :regional_indicator_s: :regional_indicator_t: :three: :heavy_plus_sign: :question: "
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Theory, MemberData(nameof(TextToEmojiTestData))]
|
||||
public void TextToEmoji(string text, string expectedResult)
|
||||
{
|
||||
var emojiConverter = new EmojiConverter();
|
||||
var result = emojiConverter.TextToEmoji(text);
|
||||
Assert.Equal(result, expectedResult);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using Geekbot.net.Lib.Levels;
|
||||
using Xunit;
|
||||
|
||||
namespace Tests.Lib
|
||||
{
|
||||
public class LevelCalcTest
|
||||
{
|
||||
public static IEnumerable<object[]> LevelCalcTestData
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
500,
|
||||
13
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
41659,
|
||||
55
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
0,
|
||||
1
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
4000000,
|
||||
101
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Theory, MemberData(nameof(LevelCalcTestData))]
|
||||
public void GetLevel(int messages, int expectedResult)
|
||||
{
|
||||
var levelCalc = new LevelCalc();
|
||||
var result = levelCalc.GetLevel(messages);
|
||||
Assert.Equal(result, expectedResult);
|
||||
}
|
||||
}
|
||||
}
|
60
Tests/Lib/Levels/LevelCalc.test.cs
Normal file
60
Tests/Lib/Levels/LevelCalc.test.cs
Normal file
|
@ -0,0 +1,60 @@
|
|||
using System.Collections.Generic;
|
||||
using Geekbot.net.Lib.Levels;
|
||||
using Xunit;
|
||||
|
||||
namespace Tests.Lib.Levels
|
||||
{
|
||||
public class LevelCalcTest
|
||||
{
|
||||
public class LevelCalcTestDto
|
||||
{
|
||||
public int Messages { get; set; }
|
||||
public int ExpectedLevel { get; set; }
|
||||
}
|
||||
|
||||
public static TestData<LevelCalcTestDto> LevelCalcTestData =>
|
||||
new TestData<LevelCalcTestDto>()
|
||||
{
|
||||
{
|
||||
"500",
|
||||
new LevelCalcTestDto
|
||||
{
|
||||
Messages = 500,
|
||||
ExpectedLevel = 13
|
||||
}
|
||||
},
|
||||
{
|
||||
"41659",
|
||||
new LevelCalcTestDto
|
||||
{
|
||||
Messages = 41659,
|
||||
ExpectedLevel = 55
|
||||
}
|
||||
},
|
||||
{
|
||||
"0",
|
||||
new LevelCalcTestDto
|
||||
{
|
||||
Messages = 0,
|
||||
ExpectedLevel = 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"4000000",
|
||||
new LevelCalcTestDto
|
||||
{
|
||||
Messages = 4000000,
|
||||
ExpectedLevel = 101
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[Theory, MemberData(nameof(LevelCalcTestData))]
|
||||
public void GetLevel(string testName, LevelCalcTestDto testData)
|
||||
{
|
||||
var levelCalc = new LevelCalc();
|
||||
var result = levelCalc.GetLevel(testData.Messages);
|
||||
Assert.Equal(result, testData.ExpectedLevel);
|
||||
}
|
||||
}
|
||||
}
|
21
Tests/TestData.cs
Normal file
21
Tests/TestData.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using Xunit;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a set of data for a theory. Data can be added to the data set using the collection initializer syntax.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class TestData<T> : TheoryData
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a theory to the Test
|
||||
/// </summary>
|
||||
/// <param name="testName">Name of the Test</param>
|
||||
/// <param name="testObject">Data used in the Test</param>
|
||||
public void Add(string testName, T testObject)
|
||||
{
|
||||
AddRow(testName, testObject);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<NoWarn>NU1701</NoWarn>
|
||||
<NoWarn>xUnit1026</NoWarn>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.0" />
|
||||
|
|
Loading…
Reference in a new issue