public void Adapt_ReturnsFalseForTypeNotMatching()
        {
            // Arrange
            var adapter = new ProxyDiagnosticSourceMethodAdapter();

            var listener = new Listener1();
            var method   = GetMethodInfo <Listener1>(l => l.Listen());

            // Act
            var func = adapter.Adapt(method, new { }.GetType());

            // Assert
            Assert.False(func(listener, "hello"));
        }
        public void Adapt_SplatsParameters()
        {
            // Arrange
            var adapter = new ProxyDiagnosticSourceMethodAdapter();

            var listener = new Listener2();
            var value    = new { id = 17, name = "Bill" };
            var method   = GetMethodInfo <Listener2>(l => l.Listen(0, ""));

            // Act
            var func = adapter.Adapt(method, value.GetType());

            // Assert
            Assert.True(func(listener, value));
            Assert.Equal(17, listener.Id);
            Assert.Equal("Bill", listener.Name);
        }
        public void Adapt_SplatsParameters_ExtraParametersGetDefaultValues()
        {
            // Arrange
            var adapter = new ProxyDiagnosticSourceMethodAdapter();

            var listener = new Listener2();
            var value    = new { };
            var method   = GetMethodInfo <Listener2>(l => l.Listen(0, ""));

            // Act
            var func = adapter.Adapt(method, value.GetType());

            // Assert
            Assert.True(func(listener, value));
            Assert.Equal(0, listener.Id);
            Assert.Null(listener.Name);
        }
        public void CanCreateProxyMethodForBasicTypeWithUpperCasing()
        {
            // Arrange
            var target = new Listener6();
            var source = new { Name = "John", Age = 1234 };

            var targetMethodInfo = target.GetType().GetMethod(nameof(Listener6.Listen));

            // Act
            var adapter  = new ProxyDiagnosticSourceMethodAdapter();
            var callback = adapter.Adapt(targetMethodInfo, source.GetType());

            var result = callback(target, source);

            // Assert
            Assert.True(result);
            Assert.Equal(target.SafeName, source.Name);
            Assert.Equal(target.SafeAge, source.Age);
        }
        public void Adapt_SplatsParameters_CamelCase()
        {
            // Arrange
            var adapter = new ProxyDiagnosticSourceMethodAdapter();

            var listener = new Listener4();
            var value    = new { Id = 17, Person = new Person()
                                 {
                                     Name = "Bill"
                                 } };
            var method = GetMethodInfo <Listener4>(l => l.Listen(0, null));

            // Act
            var func = adapter.Adapt(method, value.GetType());

            // Assert
            Assert.True(func(listener, value));
            Assert.Equal(17, listener.Id);
            Assert.Equal("Bill", listener.Name);
        }
        public void Adapt_Throws_ForSameNamedPropertiesWithDifferentCasing()
        {
            // Arrange
            var adapter = new ProxyDiagnosticSourceMethodAdapter();

            var listener = new Listener1();
            var method   = GetMethodInfo <Listener2>(l => l.Listen(5, "joey"));

            var value = new { Id = 5, id = 17 };
            var type  = value.GetType();

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => adapter.Adapt(method, type));

            // Assert
            Assert.Equal(
                $"Proxy method generation doesn't support types with properties that vary only by case. " +
                $"The type '{type.FullName}' defines multiple properties named 'id' that vary only by case.",
                ex.Message);
        }