Esempio n. 1
0
        public void OnDataReceivedTest()
        {
            var processWrapper = new Mock <IProcessManager>();
            var fakeProcess    = new FakeProcessManager(processWrapper.Object);
            var writeDataList  = new List <string>();

            processWrapper.Setup(s => s.WriteData(It.IsAny <string>(), It.IsAny <object[]>()))
            .Callback <string, object[]>((s, a) => writeDataList.Add(s));
            var injector = new Mock <ISimpleInjectorWrapper>();

            var fileService = new Mock <IFileService>();

            fileService.Setup(s => s.FileExists("bot.exe")).Returns(() => true);
            fileService.Setup(s => s.PathCombine(It.IsAny <string>(), "bot.exe")).Returns(() => "bot.exe");
            injector.Setup(s => s.GetInstance <IFileService>()).Returns(() => fileService.Object);

            var processFactory = new Mock <IProcessManagerFactory>();

            processFactory.Setup(s => s.Create(It.IsAny <string>(), It.IsAny <string>())).Returns(() => fakeProcess);
            injector.Setup(s => s.GetInstance <IProcessManagerFactory>()).Returns(() => processFactory.Object);

            var confServ = new Mock <IConfigurationService>();

            injector.Setup(s => s.GetInstance <IConfigurationService>()).Returns(() => confServ.Object);

            var bot = new GnuGoBot(injector.Object, "bot.exe", "BotName");

            Assert.NotNull(fakeProcess.DataReceived);
            bot.StartGame(false);
            bot.PlaceMove(Move.Parse("A1"));
            fakeProcess.DataReceived("resign");
            Assert.Equal(2, writeDataList.Count);
            Assert.Equal("black A1", writeDataList.ElementAt(0));
            Assert.Equal("genmove white", writeDataList.ElementAt(1));
        }
Esempio n. 2
0
        public void LevelTest()
        {
            var injector    = new Mock <ISimpleInjectorWrapper>();
            var fileService = new Mock <IFileService>();

            fileService.Setup(s => s.FileExists("bot.exe")).Returns(() => true);
            fileService.Setup(s => s.PathCombine(It.IsAny <string>(), "bot.exe")).Returns(() => "bot.exe");
            injector.Setup(s => s.GetInstance <IFileService>()).Returns(() => fileService.Object);
            var processFactory = new Mock <IProcessManagerFactory>();

            processFactory.Setup(s => s.Create(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(() => new Mock <IProcessManager>().Object);
            injector.Setup(s => s.GetInstance <IProcessManagerFactory>()).Returns(() => processFactory.Object);
            var confServ = new Mock <IConfigurationService>();

            injector.Setup(s => s.GetInstance <IConfigurationService>()).Returns(() => confServ.Object);
            var bot = new GnuGoBot(injector.Object, "bot.exe", "BotName");

            Assert.Equal(-1, bot.Level);
            bot.Level = 5;
            Assert.Equal(5, bot.Level);
            bot.StartGame(true);
            try
            {
                bot.Level = 6;
                Assert.True(false, "Should fail on previous statement");
            }
            catch (Exception ex)
            {
                Assert.IsType(typeof(NotSupportedException), ex);
                Assert.Equal("Level could be set only before start of the game", ex.Message);
            }
        }
Esempio n. 3
0
        public void PlaceMoveTest()
        {
            var  writeDataList = new List <string>();
            bool disposed      = false;
            var  injector      = new Mock <ISimpleInjectorWrapper>();
            var  fileService   = new Mock <IFileService>();

            fileService.Setup(s => s.PathCombine(It.IsAny <string>(), "bot.exe")).Returns(() => "bot.exe");
            fileService.Setup(s => s.FileExists("bot.exe")).Returns(() => true);
            injector.Setup(s => s.GetInstance <IFileService>()).Returns(() => fileService.Object);
            var processWrapper = new Mock <IProcessManager>();

            processWrapper.Setup(s => s.WriteData(It.IsAny <string>(), It.IsAny <object[]>()))
            .Callback <string, object[]>((s, a) => writeDataList.Add(s));
            processWrapper.Setup(s => s.Dispose()).Callback(() => disposed = true);

            var processFactory = new Mock <IProcessManagerFactory>();

            processFactory.Setup(s => s.Create(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(() => processWrapper.Object);
            injector.Setup(s => s.GetInstance <IProcessManagerFactory>()).Returns(() => processFactory.Object);
            var confServ = new Mock <IConfigurationService>();

            injector.Setup(s => s.GetInstance <IConfigurationService>()).Returns(() => confServ.Object);
            var bot = new GnuGoBot(injector.Object, "bot.exe", "BotName");

            try
            {
                bot.PlaceMove(null);
                Assert.True(false, "Should fail on previous statement");
            }
            catch (Exception ex)
            {
                Assert.IsType(typeof(ArgumentNullException), ex);
                Assert.Equal("Value cannot be null.\r\nParameter name: move", ex.Message);
            }

            bot.Dispose();
            Assert.Equal("quit", writeDataList.FirstOrDefault());
            Assert.Equal(true, disposed);
            try
            {
                bot.PlaceMove(Move.SpecialMove(MoveType.Resign));
                Assert.True(false, "Should fail on previous statement");
            }
            catch (Exception ex)
            {
                Assert.IsType(typeof(ObjectDisposedException), ex);
                Assert.Equal("Cannot access a disposed object.\r\nObject name: 'proccess'.", ex.Message);
            }

            bot = new GnuGoBot(injector.Object, "bot.exe", "BotName");
            try
            {
                bot.PlaceMove(Move.SpecialMove(MoveType.Resign));
                Assert.True(false, "Should fail on previous statement");
            }
            catch (Exception ex)
            {
                Assert.IsType(typeof(NotSupportedException), ex);
                Assert.Equal("Invoke StartGame before placing the move", ex.Message);
            }

            bot.StartGame(false);
            bot.PlaceMove(Move.SpecialMove(MoveType.Resign));
            Assert.Equal("black Resign", writeDataList.ElementAt(1));
            Assert.Equal("genmove white", writeDataList.ElementAt(2));
        }