// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if NETFRAMEWORK
using System.Data;

using AwesomeAssertions;

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;

using Moq;

using TestFramework.ForTestingMSTest;

using ITestMethod = Microsoft.VisualStudio.TestTools.UnitTesting.ITestMethod;

namespace MSTestAdapter.PlatformServices.UnitTests.Services;

public class DesktopTestDataSourceTests : TestContainer
{
    private readonly Mock<ITestMethod> _mockTestMethodInfo;
    private readonly Mock<ITestContext> _mockTestContext;

    public DesktopTestDataSourceTests()
    {
        _mockTestMethodInfo = new Mock<ITestMethod>();
        _mockTestContext = new Mock<ITestContext>();
    }

    public void GetDataShouldReadDataFromGivenDataSource()
    {
        System.Reflection.MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PassingTest");
        DataSourceAttribute dataSourceAttribute = new(
            "Microsoft.VisualStudio.TestTools.DataSource.XML", "DataTestSourceFile.xml", "settings", DataAccessMethod.Sequential);

        _mockTestMethodInfo.Setup(ds => ds.GetAttributes<DataSourceAttribute>())
            .Returns([dataSourceAttribute]);
        _mockTestMethodInfo.Setup(ds => ds.MethodInfo).Returns(methodInfo);

        TestDataSource testDataSource = new();
        IEnumerable<object>? dataRows = testDataSource.GetData(_mockTestMethodInfo.Object, _mockTestContext.Object);

        foreach (DataRow dataRow in dataRows.Cast<DataRow>())
        {
            dataRow[3].Should().Be("v1");
        }
    }

    public void GetDataShouldSetDataConnectionInTestContextObject()
    {
        System.Reflection.MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PassingTest");
        DataSourceAttribute dataSourceAttribute = new(
            "Microsoft.VisualStudio.TestTools.DataSource.XML", "DataTestSourceFile.xml", "settings", DataAccessMethod.Sequential);

        _mockTestMethodInfo.Setup(ds => ds.GetAttributes<DataSourceAttribute>())
            .Returns([dataSourceAttribute]);
        _mockTestMethodInfo.Setup(ds => ds.MethodInfo).Returns(methodInfo);

        TestDataSource testDataSource = new();
        IEnumerable<object>? dataRows = testDataSource.GetData(_mockTestMethodInfo.Object, _mockTestContext.Object);

        _mockTestContext.Verify(tc => tc.SetDataConnection(It.IsAny<object>()), Times.Once);
    }

    #region Dummy implementation

    public class DummyTestClass
    {
        public TestContext TestContext { get; set; } = null!;

        [TestMethod]
        public void PassingTest()
        {
            TestContext.DataRow!["adapter"].ToString().Should().Be("v1");
            TestContext.DataRow["targetPlatform"].ToString().Should().Be("x86");
            TestContext.AddResultFile("C:\\temp.txt");
        }

        [TestMethod]
        public void FailingTest()
            => TestContext.DataRow!["configuration"].ToString().Should().Be("Release");
    }

    #endregion
}

#endif
