<commit_before><commit_msg>WIP #258 - Add some test coverage<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture.NUnit3;
using Moq;
using NUnit.Framework;
using ZptSharp.Autofixture;
using ZptSharp.Dom;
using ZptSharp.Expressions;
using ZptSharp.Metal;
using ZptSharp.Rendering;

namespace ZptSharp.Tal
{
    [TestFixture,Parallelizable]
    public class RecordMetalMacroNameDecoratorTests
    {
        [Test, AutoMoqData]
        public async Task ProcessContextAsync_records_macroname_when_there_is_a_define_macro_attribute([Frozen] IProcessesExpressionContext wrapped,
                                                                                                       [Frozen] IGetsMetalAttributeSpecs metalSpecProvider,
                                                                                                       RecordMetalMacroNameDecorator sut,
                                                                                                       [StubDom] ExpressionContext context,
                                                                                                       [StubDom] IAttribute attribute,
                                                                                                       AttributeSpec spec,
                                                                                                       string macroName)
        {
            Mock.Get(metalSpecProvider).SetupGet(x => x.DefineMacro).Returns(spec);
            Mock.Get(attribute).Setup(x => x.Matches(spec)).Returns(true);
            context.CurrentElement.Attributes.Add(attribute);
            Mock.Get(wrapped)
                .Setup(x => x.ProcessContextAsync(context, CancellationToken.None))
                .Returns(() => Task.FromResult(ExpressionContextProcessingResult.Noop));
            attribute.Value = macroName;

            await sut.ProcessContextAsync(context);

            Assert.That(context.LocalDefinitions,
                        Has.One.Matches<KeyValuePair<string, object>>(x => x.Key == "macroname" && Equals(x.Value, macroName)));
        }

        [Test, AutoMoqData]
        public async Task ProcessContextAsync_does_not_record_macroname_when_there_is_no_define_macro_attribute([Frozen] IProcessesExpressionContext wrapped,
                                                                                                                [Frozen] IGetsMetalAttributeSpecs metalSpecProvider,
                                                                                                                RecordMetalMacroNameDecorator sut,
                                                                                                                [StubDom] ExpressionContext context,
                                                                                                                AttributeSpec spec)
        {
            Mock.Get(metalSpecProvider).SetupGet(x => x.DefineMacro).Returns(spec);
            Mock.Get(wrapped)
                .Setup(x => x.ProcessContextAsync(context, CancellationToken.None))
                .Returns(() => Task.FromResult(ExpressionContextProcessingResult.Noop));
            context.CurrentElement.Attributes.Clear();

            await sut.ProcessContextAsync(context);

            Assert.That(context.LocalDefinitions,
                        Has.None.Matches<KeyValuePair<string, object>>(x => x.Key == "macroname"));
        }
    }
}
{'fraction_non_alphanumeric': 0.06866346425543939, 'fraction_numerical': 0.0011302627860977678, 'mean_word_length': 1.2069825436408979, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2365592', 'n_tokens_mistral': 724, 'n_tokens_neox': 702, 'n_words': 114}