﻿using Cbn.Lib.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cbn.Lib.Tests.Reflection
{
    [TestClass]
    public class ExpressionExtensionTests
    {
        [TestMethod]
        public void GetPropertyTest()
        {
            var prop = typeof(TestClass).GetProperty(nameof(TestClass.MyProperty));
            Assert.AreEqual(prop, new TestClass().GetProperty(x => x.MyProperty));
        }
        [TestMethod]
        public void GetByPropertyTest()
        {
            var obj = new TestClass { MyProperty = 5 };
            Assert.AreEqual(5, obj.GetByProperty<TestClass, int>(nameof(TestClass.MyProperty)));
            Assert.AreEqual(5, obj.GetByProperty<int>(obj.GetProperty(x => x.MyProperty)));
            Assert.AreEqual((object)5, obj.GetByProperty<object>(obj.GetProperty(x => x.MyProperty)));
        }
        [TestMethod]
        public void SetForPropertyTest()
        {
            var obj = new TestClass { MyProperty = 5 };
            obj.SetForProperty(nameof(TestClass.MyProperty), 4);
            Assert.AreEqual(4, obj.MyProperty);
            obj.SetForProperty(obj.GetProperty(x => x.MyProperty), 3);
            Assert.AreEqual(3, obj.MyProperty);
        }

        class TestClass
        {
            public int MyProperty { get; set; }
        }
    }
}
