Пример #1
0
        public void ArgTest()
        {
            var logger = new MockLogger();

            logger.Reset();
            ArgTestExtensions.Method1(logger);
            Assert.Null(logger.LastException);
            Assert.Equal("M1", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method2(logger, "arg1");
            Assert.Null(logger.LastException);
            Assert.Equal("M2", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method3(logger, "arg1", 2);
            Assert.Null(logger.LastException);
            Assert.Equal("M3 arg1 2", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method4(logger, new InvalidOperationException("A"));
            Assert.Equal("A", logger.LastException !.Message);
            Assert.Equal("M4", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method5(logger, new InvalidOperationException("A"), new InvalidOperationException("B"));
            Assert.Equal("A", logger.LastException !.Message);
            Assert.Equal("M5", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method6(logger, new InvalidOperationException("A"), 2);
            Assert.Equal("A", logger.LastException !.Message);
            Assert.Equal("M6", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method7(logger, 1, new InvalidOperationException("B"));
            Assert.Equal("B", logger.LastException !.Message);
            Assert.Equal("M7", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method8(logger, 1, 2, 3, 4, 5, 6, 7);
            Assert.Equal("M8", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);

            logger.Reset();
            ArgTestExtensions.Method9(logger, 1, 2, 3, 4, 5, 6, 7);
            Assert.Equal("M9 1 2 3 4 5 6 7", logger.LastFormattedString);
            Assert.Equal(1, logger.CallCount);
        }
Пример #2
0
        public void ReadOnlyListTest()
        {
            var logger = new MockLogger();

            logger.Reset();
            ArgTestExtensions.Method1(logger);
            var rol = logger.LastState as IReadOnlyList <KeyValuePair <string, object> >;

            Assert.Equal(0, rol !.Count);
            Assert.Empty(rol);
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = rol[0]);

            logger.Reset();
            ArgTestExtensions.Method2(logger, "arg1");
            rol = logger.LastState as IReadOnlyList <KeyValuePair <string, object> >;
            Assert.Equal(1, rol !.Count);
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
            Assert.Equal(1, rol.LongCount());
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
            Assert.Equal("p1", (string)rol[0].Key);
            Assert.Equal("arg1", (string)rol[0].Value);
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = rol[1]);

            logger.Reset();
            ArgTestExtensions.Method3(logger, "arg1", 2);
            rol = logger.LastState as IReadOnlyList <KeyValuePair <string, object> >;
            Assert.Equal(2, rol !.Count);
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
            Assert.Equal(2, rol.LongCount());
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
            Assert.Equal("p1", (string)rol[0].Key);
            Assert.Equal("arg1", (string)rol[0].Value);
            Assert.Equal("p2", (string)rol[1].Key);
            Assert.Equal(2, (int)rol[1].Value);
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = rol[2]);
        }