﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBDD.Framework.Generator;
using NBDD.Framework.Generator.CSharp;
using NBDD.Framework.Testing;
using NBDD.Tests._Assets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBDD.Tests.Framework.Generator.CSharp
{
    public partial class CSharpFeatureTestCheckersBuilderTests
    {
        [TestMethod]
        [TestProperty(TestTraits.Module, "CSharpFeatureTestCheckersBuilder.ToCode")]
        public void ShouldGenerateCodeThatWillDefineAFeatureAttributeAndImportsNamespace()
        {
            // mock
            var rootNs = "qualquer.coisa";
            var featureFile = @"unidade:\pasta1\arquivo.feature";
            var feature = new Feature() { File = featureFile, Name = "Cliente, Exclusão" };
            var scenarios = new[] { new Scenario() { Name = "Deve falhar quando cliente está vinculado à [1] produtos." } };

            // prepare
            var builder = new CSharpFeatureTestCheckersBuilder();
            builder.RootNamespace = rootNs;
            builder.Feature = feature;
            builder.FeaturePath = featureFile;
            builder.Scenarios = scenarios;

            // do
            var code = builder.ToCode();
            Console.Write(code);

            // check
            var hasNamespace = this.HasUsing(code, typeof(TestFeatureAttribute).Namespace);
            Assert.IsTrue(hasNamespace);

            var hasFeatureAttrib = this.HasFeatureAttribute(code, featureFile);
            Assert.IsTrue(hasFeatureAttrib);
        }

        #region Auxiliary

        private bool HasUsing(
            string code,
            string ns)
        {
            var usingCall = string.Concat("using ", ns);
            return code.Contains(usingCall);
        }

        private bool HasFeatureAttribute(
            string code,
            string featureFile)
        {
            var scopeTrait = string.Format("[TestFeature(@\"{0}\")]", featureFile);
            return code.Contains(scopeTrait);
        }

        #endregion
    }
}
