浏览代码

Add tests

Tigran 4 年之前
父节点
当前提交
fff7cdece2

+ 6 - 0
MafiaTelegramBot.sln

@@ -2,6 +2,8 @@
 Microsoft Visual Studio Solution File, Format Version 12.00
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MafiaTelegramBot", "MafiaTelegramBot\MafiaTelegramBot.csproj", "{2447C2FC-E9CD-4212-8949-90B6C6B4A8D5}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MafiaTelegramBotTests", "MafiaTelegramBotTests\MafiaTelegramBotTests.csproj", "{BB1857E4-418E-4AE5-8437-43EBD1D903FB}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
 		{2447C2FC-E9CD-4212-8949-90B6C6B4A8D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2447C2FC-E9CD-4212-8949-90B6C6B4A8D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{2447C2FC-E9CD-4212-8949-90B6C6B4A8D5}.Release|Any CPU.Build.0 = Release|Any CPU
+		{BB1857E4-418E-4AE5-8437-43EBD1D903FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{BB1857E4-418E-4AE5-8437-43EBD1D903FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{BB1857E4-418E-4AE5-8437-43EBD1D903FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{BB1857E4-418E-4AE5-8437-43EBD1D903FB}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 EndGlobal

+ 10 - 0
MafiaTelegramBot.sln.DotSettings.user

@@ -1,4 +1,14 @@
 <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+	<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=0ea868fa_002D7cf8_002D466b_002Dbf25_002Dc540fc960a6b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Killing_Player_In_Summing_Up_Phase #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
+  &lt;TestAncestor&gt;
+    &lt;TestId&gt;xUnit::BB1857E4-418E-4AE5-8437-43EBD1D903FB::net5.0::MafiaTelegramBotTests.Game.Tests.GameRooms.Tests.GameRoomTests&lt;/TestId&gt;
+  &lt;/TestAncestor&gt;
+&lt;/SessionState&gt;</s:String>
+	<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=fbf9be8e_002Dabe2_002D485d_002Dbae0_002D5fd4fbd8c901/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Killing_Player_In_Summing_Up_Phase" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
+  &lt;TestAncestor&gt;
+    &lt;TestId&gt;xUnit::BB1857E4-418E-4AE5-8437-43EBD1D903FB::net5.0::MafiaTelegramBotTests.Game.Tests.GameRooms.Tests.GameRoomTests.Killing_Player_In_Summing_Up_Phase&lt;/TestId&gt;
+  &lt;/TestAncestor&gt;
+&lt;/SessionState&gt;</s:String>
 	<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=MafiaTelegramBot_002Fappsettings/@EntryIndexedValue">True</s:Boolean>
 	
 	<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=MafiaTelegramBot_002FResources_002Fkeyboard/@EntryIndexedValue">False</s:Boolean>

+ 1 - 1
MafiaTelegramBot/Game/GameRoles/MafiaRole.cs

@@ -32,7 +32,7 @@ namespace MafiaTelegramBot.Game.GameRoles
         public override async Task<Message> SetNightTarget(long userId)
         {
             NightTargetId = userId;
-            return await Bot.Get().EditMessageTextAsync(Player.ChatId, MessageId, $"{strings.you_choose_target} {Room.Players[userId].NickName}");
+            return await Bot.SendWithMarkdown2(Player.ChatId, $"{strings.you_choose_target} {Room.Players[userId].NickName}");
         }
 
         public MafiaRole(GameRoom room, Player player) : base(room, player) { }

+ 1 - 1
MafiaTelegramBot/Game/GameRooms/ExtendedGameRoom.cs

@@ -8,7 +8,7 @@ namespace MafiaTelegramBot.Game.GameRooms
     {
         public override bool IsExtended { get; protected set; } = true;
 
-        protected override Dictionary<Roles, List<Player>> PlayersRole { get; } = new()
+        public override Dictionary<Roles, List<Player>> PlayersRole { get; set; } = new()
         {
             //passive roles
             [Roles.Elder] = new List<Player>(),

+ 14 - 18
MafiaTelegramBot/Game/GameRooms/GameRoom.GameProcess.cs

@@ -145,41 +145,37 @@ namespace MafiaTelegramBot.Game.GameRooms
             });
         }
 
-        private async Task SummingUpPhase()
+        public async Task SummingUpPhase()
         {
             await Task.Run(async () =>
             {
-                Player killedPlayer = null;
+                long killedPlayerId = -1;
                 foreach (var (role, players) in PlayersRole)
                 {
-                    if (players.Count == 1)
-                    {
-                        var player = players[0];
-                        if (role != Roles.Doctor) player.CanBeHealed = true;
-                        if (player.IsAlive) await player.CurrentRole.ApplyNightActionResult();
-                    }
-                    else if (role is Roles.Mafia)
+                    if (role is Roles.Mafia)
                     {
                         foreach (var mafia in players) mafia.IsSpeaker = false;
                         var votes = players
-                            .GroupBy(player => player.CurrentRole.GetNightTarget())
+                            .GroupBy(p => p.CurrentRole.GetNightTarget())
                             .Select(item => new {id = item.Key, count = item.Count()})
                             .ToList();
-                        foreach (var vote in votes)
-                        {
-                            Console.WriteLine(vote.id);
-                            Console.WriteLine(vote.count);
-                        }
-                        if (votes.Count == players.Count) continue;
                         var max = votes.Max(item => item.count);
                         var maxCount = votes.Count(item => item.count == max);
                         if (maxCount != 1) continue;
                         var selected = votes.First(item => item.count == max);
-                        if (selected.id != -1) killedPlayer = Players[selected.id];
+                        if (selected.id == -1) continue;
+                        killedPlayerId = selected.id;
+                        Players[killedPlayerId].IsAlive = false;
+                    }
+                    else if (players.Count == 1)
+                    {
+                        var player = players[0];
+                        if (role != Roles.Doctor) player.CanBeHealed = true;
+                        await player.CurrentRole.ApplyNightActionResult();
                     }
                 }
                 var message = strings.city_wakes_up;
-                if (killedPlayer is {IsAlive: false}) message += $"{strings.at_this_night} {killedPlayer.NickName}";
+                if (killedPlayerId != -1 && !Players[killedPlayerId].IsAlive) message += $"{strings.at_this_night} {Players[killedPlayerId].NickName}";
                 else message += strings.everyone_survived;
                 await PlayersCh.SendSticker(Stickers.Sticker["Day"]);
                 await PlayersCh.Send(message);

+ 1 - 1
MafiaTelegramBot/Game/GameRooms/GameRoom.Structure.cs

@@ -31,7 +31,7 @@ namespace MafiaTelegramBot.Game.GameRooms
 
         public readonly Dictionary<Roles, int> Settings = new();
 
-        protected abstract Dictionary<Roles, List<Player>> PlayersRole { get; }
+        public abstract Dictionary<Roles, List<Player>> PlayersRole { get; set; }
 
         public GameRoom()
         {

+ 1 - 1
MafiaTelegramBot/Game/GameRooms/NormalGameRoom.cs

@@ -8,7 +8,7 @@ namespace MafiaTelegramBot.Game.GameRooms
     {
         public override bool IsExtended { get; protected set; } = false;
 
-        protected override Dictionary<Roles, List<Player>> PlayersRole { get; } = new()
+        public override Dictionary<Roles, List<Player>> PlayersRole { get; set;} = new()
         {
             //passive roles
             [Roles.Villager] = new List<Player>(),

+ 9 - 8
MafiaTelegramBot/Models/Bot.cs

@@ -81,26 +81,27 @@ namespace MafiaTelegramBot.Models
         private static void InitReplies()
         {
             //TODO fill inline keyboard array
-            _repliesList = new List<Reply>
-            {
-                
-            };
+            _repliesList = new List<Reply>();
         }
 
         public static async Task SendStickerAsync(long chatId, string fileId)
         {
-            await Get().SendStickerAsync(chatId, fileId, disableNotification: true);
+            if (chatId > 0)
+                await Get().SendStickerAsync(chatId, fileId, disableNotification: true);
         }
 
         public static async Task SendHyperLink(long chatId, string message)
         {
-            await Get().SendTextMessageAsync(chatId, message, ParseMode.Html, disableNotification: true);
+            if (chatId > 0)
+                await Get().SendTextMessageAsync(chatId, message, ParseMode.Html, disableNotification: true);
         }
         
         public static async Task<Message> SendWithMarkdown2(long chatId, string message, IReplyMarkup replyMarkup = null)
         {
-            return await Get().SendTextMessageAsync(chatId, await Utilities.ToMarkdownString(message),
-                ParseMode.MarkdownV2, replyMarkup: replyMarkup, disableNotification: true);
+            if (chatId > 0)
+                return await Get().SendTextMessageAsync(chatId, await Utilities.ToMarkdownString(message),
+                    ParseMode.MarkdownV2, replyMarkup: replyMarkup, disableNotification: true);
+            return new Message();
         }
     }
 }

+ 40 - 0
MafiaTelegramBotTests/Game.Tests/GameRooms.Tests/GameRoom.GameProcess.Tests.cs

@@ -0,0 +1,40 @@
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using MafiaTelegramBot.Game;
+using MafiaTelegramBot.Game.GameRoles;
+using MafiaTelegramBot.Game.GameRooms;
+using MafiaTelegramBot.Resources;
+using Xunit;
+
+namespace MafiaTelegramBotTests.Game.Tests.GameRooms.Tests
+{
+    public class GameRoomTests
+    {
+        [Fact]
+        public async Task Killing_Player_In_Summing_Up_Phase()
+        {
+            // arrange
+            var gameRoom = new NormalGameRoom();
+            var mafia = new Player {Id = -1, ChatId = -1};
+            mafia.CurrentRole = new MafiaRole(gameRoom, mafia);
+            var actual = new Player {Id = -2, ChatId = -2};
+            actual.CurrentRole = new VillagerRole(gameRoom, actual);
+            gameRoom.Players.Add(-1, mafia);
+            gameRoom.Players.Add(-2, actual);
+            await mafia.CurrentRole.SetNightTarget(-2);
+            gameRoom.PlayersRole = new Dictionary<Roles, List<Player>>
+            {
+                [Roles.Mafia] = new() {mafia},
+                [Roles.Villager] = new() {actual}
+            };
+            
+            var expected = new Player {Id = -2, ChatId = -2, IsAlive = false};
+            expected.CurrentRole = new VillagerRole(gameRoom, expected);
+            
+            // actual
+            await gameRoom.SummingUpPhase();
+            // assert
+            Assert.Equal(expected.IsAlive, gameRoom.Players[-2].IsAlive);
+        }
+    }
+}

+ 38 - 0
MafiaTelegramBotTests/MafiaTelegramBotTests.csproj

@@ -0,0 +1,38 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>net5.0</TargetFramework>
+
+        <IsPackable>false</IsPackable>
+    </PropertyGroup>
+
+    <ItemGroup>
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
+        <PackageReference Include="xunit" Version="2.4.1" />
+        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
+            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+            <PrivateAssets>all</PrivateAssets>
+        </PackageReference>
+        <PackageReference Include="coverlet.collector" Version="3.0.2">
+            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+            <PrivateAssets>all</PrivateAssets>
+        </PackageReference>
+    </ItemGroup>
+
+    <ItemGroup>
+      <ProjectReference Include="..\MafiaTelegramBot\MafiaTelegramBot.csproj" />
+    </ItemGroup>
+
+    <ItemGroup>
+      <Folder Include="Controllers.Tests" />
+      <Folder Include="CustomCollections.Tests\Extensions.Tests" />
+      <Folder Include="DataBase.Tests\Entity.Tests" />
+      <Folder Include="DataBase.Tests\EntityDao.Tests" />
+      <Folder Include="Game.Tests\GameRoles.Tests" />
+      <Folder Include="Models.Tests\Commands.Tests" />
+      <Folder Include="Models.Tests\Inlines.Tests" />
+      <Folder Include="Models.Tests\Replies.Tests" />
+      <Folder Include="Resources.Tests" />
+    </ItemGroup>
+
+</Project>

+ 13 - 0
MafiaTelegramBotTests/MainClassTests.cs

@@ -0,0 +1,13 @@
+using System;
+using Xunit;
+
+namespace MafiaTelegramBotTests
+{
+    public class MainClassTests
+    {
+        [Fact]
+        public void Test1()
+        {
+        }
+    }
+}