<commit_before>﻿using System;
using CSF.Zpt.Rendering;
using Moq;
using NUnit.Framework;
using CSF.Zpt;
using CSF.Zpt.Metal;
using Ploeh.AutoFixture;
using Test.CSF.Zpt.Util.Autofixture;
using Test.CSF.Zpt.Util;

namespace Test.CSF.Zpt.Metal
{
  [TestFixture]
  public class TestMacroFinder
  {
    #region fields

    private IFixture _fixture;

    #endregion

    #region setup

    [SetUp]
    public void Setup()
    {
      _fixture = new Fixture();
      new RenderingContextCustomisation().Customize(_fixture);
    }

    #endregion

    #region tests

    [Test]
    public void TestGetUsedMacro()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      var referencedElement = Mock.Of<ZptElement>(x => x.Clone() == x);

      var model = _fixture.Create<DummyModel>();
      _fixture.Inject(model);

      var context = _fixture.Create<RenderingContext>();
      Mock.Get(context)
        .Setup(x => x.GetAttribute(ZptConstants.Metal.Namespace, ZptConstants.Metal.UseMacroAttribute))
        .Returns(attribute);
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Returns(new ExpressionResult(referencedElement));

      var sut = new MacroFinder();

      // Act
      var result = sut.GetUsedMacro(context);

      // Assert
      Assert.AreSame(referencedElement, result);
    }

    [Test]
    public void TestGetUsedMacroEvaluationFailure()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      ZptElement
        originalElement = Mock.Of<ZptElement>(x => x.GetAttribute(ZptConstants.Metal.Namespace,
                                                                  ZptConstants.Metal.UseMacroAttribute) == attribute);

      _fixture.Inject(originalElement);

      var model = _fixture.Create<DummyModel>();
      var context = _fixture.Create<RenderingContext>();
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Throws<Util.IntendedTestingException>();

      var sut = new MacroFinder();

      // Act
      var result = sut.GetUsedMacro(context);

      // Assert
      Assert.IsNull(result);
    }

    [Test]
    public void TestGetUsedMacroNoAttribute()
    {
      // Arrange
      ZptElement
        originalElement = Mock.Of<ZptElement>(x => x.GetAttribute(ZptConstants.Metal.Namespace,
                                                                  ZptConstants.Metal.UseMacroAttribute) == (global::CSF.Zpt.Rendering.ZptAttribute) null);
      _fixture.Inject(originalElement);

      var context = _fixture.Create<RenderingContext>();

      var sut = new MacroFinder();

      // Act
      var result = sut.GetUsedMacro(context);

      // Assert
      Assert.IsNull(result);
    }

    [Test]
    public void TestGetExtendedMacro()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      var referencedElement = Mock.Of<ZptElement>(x => x.Clone() == x);

      var model = _fixture.Create<DummyModel>();
      _fixture.Inject(model);

      var context = _fixture.Create<RenderingContext>();
      Mock.Get(context)
        .Setup(x => x.GetAttribute(ZptConstants.Metal.Namespace, ZptConstants.Metal.ExtendMacroAttribute))
        .Returns(attribute);
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Returns(new ExpressionResult(referencedElement));

      var sut = new MacroFinder();

      // Act
      var result = sut.GetExtendedMacro(context);

      // Assert
      Assert.AreSame(referencedElement, result);
    }

    [Test]
    public void TestGetExtendedMacroEvaluationFailure()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      ZptElement
        originalElement = Mock.Of<ZptElement>(x => x.GetAttribute(ZptConstants.Metal.Namespace,
                                                                  ZptConstants.Metal.ExtendMacroAttribute) == attribute);

      _fixture.Inject(originalElement);
      var model = _fixture.Create<DummyModel>();
      _fixture.Inject(model);

      var context = _fixture.Create<RenderingContext>();
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Throws<Util.IntendedTestingException>();

      var sut = new MacroFinder();

      // Act
      var result = sut.GetExtendedMacro(context);

      // Assert
      Assert.IsNull(result);
    }

    [Test]
    public void TestGetExtendedMacroNoAttribute()
    {
      // Arrange
      ZptElement
        originalElement = Mock.Of<ZptElement>(x => x.GetAttribute(ZptConstants.Metal.Namespace,
                                                                  ZptConstants.Metal.ExtendMacroAttribute) == (global::CSF.Zpt.Rendering.ZptAttribute) null);
      _fixture.Inject(originalElement);

      var context = _fixture.Create<RenderingContext>();

      var sut = new MacroFinder();

      // Act<commit_msg>WIP #43 - Fix lambda expression usage in unit test<commit_after>﻿using System;
using CSF.Zpt.Rendering;
using Moq;
using NUnit.Framework;
using CSF.Zpt;
using CSF.Zpt.Metal;
using Ploeh.AutoFixture;
using Test.CSF.Zpt.Util.Autofixture;
using Test.CSF.Zpt.Util;

namespace Test.CSF.Zpt.Metal
{
  [TestFixture]
  public class TestMacroFinder
  {
    #region fields

    private IFixture _fixture;

    #endregion

    #region setup

    [SetUp]
    public void Setup()
    {
      _fixture = new Fixture();
      new RenderingContextCustomisation().Customize(_fixture);
    }

    #endregion

    #region tests

    [Test]
    public void TestGetUsedMacro()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      var referencedElement = Mock.Of<ZptElement>(x => x.Clone() == x);

      var model = _fixture.Create<DummyModel>();
      _fixture.Inject(model);

      var context = _fixture.Create<RenderingContext>();
      Mock.Get(context)
        .Setup(x => x.GetAttribute(ZptConstants.Metal.Namespace, ZptConstants.Metal.UseMacroAttribute))
        .Returns(attribute);
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Returns(new ExpressionResult(referencedElement));

      var sut = new MacroFinder();

      // Act
      var result = sut.GetUsedMacro(context);

      // Assert
      Assert.AreSame(referencedElement, result);
    }

    [Test]
    public void TestGetUsedMacroEvaluationFailure()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      ZptElement
        originalElement = Mock.Of<ZptElement>(x => x.GetAttribute(ZptConstants.Metal.Namespace,
                                                                  ZptConstants.Metal.UseMacroAttribute) == attribute);

      _fixture.Inject(originalElement);

      var model = _fixture.Create<DummyModel>();
      var context = _fixture.Create<RenderingContext>();
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Throws<Util.IntendedTestingException>();

      var sut = new MacroFinder();

      // Act
      var result = sut.GetUsedMacro(context);

      // Assert
      Assert.IsNull(result);
    }

    [Test]
    public void TestGetUsedMacroNoAttribute()
    {
      // Arrange
      var mockOriginalElement = new Mock<ZptElement>();
      mockOriginalElement
        .Setup(x => x.GetAttribute(ZptConstants.Metal.Namespace, ZptConstants.Metal.UseMacroAttribute))
        .Returns((global::CSF.Zpt.Rendering.ZptAttribute)null);
      
      ZptElement originalElement = mockOriginalElement.Object;

      var context = _fixture.Create<RenderingContext>();

      var sut = new MacroFinder();

      // Act
      var result = sut.GetUsedMacro(context);

      // Assert
      Assert.IsNull(result);
    }

    [Test]
    public void TestGetExtendedMacro()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      var referencedElement = Mock.Of<ZptElement>(x => x.Clone() == x);

      var model = _fixture.Create<DummyModel>();
      _fixture.Inject(model);

      var context = _fixture.Create<RenderingContext>();
      Mock.Get(context)
        .Setup(x => x.GetAttribute(ZptConstants.Metal.Namespace, ZptConstants.Metal.ExtendMacroAttribute))
        .Returns(attribute);
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Returns(new ExpressionResult(referencedElement));

      var sut = new MacroFinder();

      // Act
      var result = sut.GetExtendedMacro(context);

      // Assert
      Assert.AreSame(referencedElement, result);
    }

    [Test]
    public void TestGetExtendedMacroEvaluationFailure()
    {
      // Arrange
      var attribute = Mock.Of<global::CSF.Zpt.Rendering.ZptAttribute>();
      ZptElement
        originalElement = Mock.Of<ZptElement>(x => x.GetAttribute(ZptConstants.Metal.Namespace,
                                                                  ZptConstants.Metal.ExtendMacroAttribute) == attribute);

      _fixture.Inject(originalElement);
      var model = _fixture.Create<DummyModel>();
      _fixture.Inject(model);

      var context = _fixture.Create<RenderingContext>();
      Mock.Get(model)
        .Setup(x => x.Evaluate(It.IsAny<string>(), context))
        .Throws<Util.IntendedTestingException>();

      var sut = new MacroFinder();

      // Act
      var result = sut.GetExtendedMacro(context);

      // Assert
      Assert.IsNull(result);
    }

    [Test]
    public void TestGetExtendedMacroNoAttribute()
    {
      // Arrange
      var mockOriginalElement = new Mock<ZptElement>();
      mockOriginalElement
        .Setup(x => x.GetAttribute(ZptConstants.Metal.Namespace, ZptConstants.Metal.ExtendMacroAttribute))
        .Returns((global::CSF.Zpt.Rendering.ZptAttribute) null);
      
      ZptElement originalElement = mockOriginalElement.Object;
      _fixture.Inject(originalElement);

      var context = _fixture.Create<RenderingContext>();

      var sut = new MacroFinder();

      // Act
{'fraction_non_alphanumeric': 0.11869436201780416, 'fraction_numerical': 0.00019782393669634025, 'mean_word_length': 2.5010387811634347, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5200166', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2969, 'n_words': 513}