<commit_before>﻿using System;
using System.Linq;
using Dolstagis.Tests.Web.TestFeatures;
using Dolstagis.Web.Features;
using Dolstagis.Web.Features.Impl;
using Dolstagis.Web.Http;
using Moq;
using NUnit.Framework;

namespace Dolstagis.Tests.Web.Features
{
    [TestFixture]
    public class FeatureSetFixture
    {
        private IFeature alwaysEnabled = new EmptyNamedFeature("alwaysEnabled");
        private IFeature alwaysDisabled = new EmptyNamedFeature("alwaysDisabled", req => false);
        private IFeature localhostOnly = new EmptyNamedFeature("localhostOnly", req => req.Url.IsLoopback);

        private IRequest localRequest;
        private IRequest nonLocalRequest;

        private FeatureSwitchboard switchboard;

        [OneTimeSetUp]
        public void CreateFeatures()
        {
            var mockLocalRequest = new Mock<IRequest>();
            mockLocalRequest.SetupGet(x => x.Url).Returns(new Uri("http://localhost/"));
            localRequest = mockLocalRequest.Object;

            var mockNonLocalRequest = new Mock<IRequest>();
            mockNonLocalRequest.SetupGet(x => x.Url).Returns(new Uri("http://example.com"));
            nonLocalRequest = mockNonLocalRequest.Object;

            switchboard = new FeatureSwitchboard(null)
                .Add(alwaysEnabled, alwaysDisabled, localhostOnly);
        }

        [Test]
        public void VerifyMocksHaveBeenSetUpCorrectly()
        {
            Assert.IsTrue(localRequest.Url.IsLoopback);
            Assert.IsFalse(nonLocalRequest.Url.IsLoopback);
        }

        [Test]
        public void LocalRequestShouldHaveTwoFeatures()
        {
            var localFeatureSet = switchboard.GetFeatureSet(localRequest);
            Assert.AreEqual(2, localFeatureSet.Features.Count);
            Assert.AreSame(this.alwaysEnabled, localFeatureSet.Features.First(), "First feature is wrong");
            Assert.AreSame(this.localhostOnly, localFeatureSet.Features.Last(), "Second feature is wrong");
        }

        [Test]
        public void NonlocalRequestShouldHaveOneFeature()
        {
            var nonLocalFeatureSet = switchboard.GetFeatureSet(nonLocalRequest);
            Assert.AreSame(this.alwaysEnabled, nonLocalFeatureSet.Features.Single());
        }

        [Test]
        public void DuplicateRequestsShouldReturnSameFeatureSet()
        {
            var set1 = switchboard.GetFeatureSet(localRequest);
            var set2 = switchboard.GetFeatureSet(localRequest);
            Assert.AreSame(set1, set2);
        }
    }
}
<commit_msg>Fix test: switchable features always come first now.<commit_after>﻿using System;
using System.Linq;
using Dolstagis.Tests.Web.TestFeatures;
using Dolstagis.Web.Features;
using Dolstagis.Web.Features.Impl;
using Dolstagis.Web.Http;
using Moq;
using NUnit.Framework;

namespace Dolstagis.Tests.Web.Features
{
    [TestFixture]
    public class FeatureSetFixture
    {
        private IFeature alwaysEnabled = new EmptyNamedFeature("alwaysEnabled");
        private IFeature alwaysDisabled = new EmptyNamedFeature("alwaysDisabled", req => false);
        private IFeature localhostOnly = new EmptyNamedFeature("localhostOnly", req => req.Url.IsLoopback);

        private IRequest localRequest;
        private IRequest nonLocalRequest;

        private FeatureSwitchboard switchboard;

        [OneTimeSetUp]
        public void CreateFeatures()
        {
            var mockLocalRequest = new Mock<IRequest>();
            mockLocalRequest.SetupGet(x => x.Url).Returns(new Uri("http://localhost/"));
            localRequest = mockLocalRequest.Object;

            var mockNonLocalRequest = new Mock<IRequest>();
            mockNonLocalRequest.SetupGet(x => x.Url).Returns(new Uri("http://example.com"));
            nonLocalRequest = mockNonLocalRequest.Object;

            switchboard = new FeatureSwitchboard(null)
                .Add(alwaysEnabled, alwaysDisabled, localhostOnly);
        }

        [Test]
        public void VerifyMocksHaveBeenSetUpCorrectly()
        {
            Assert.IsTrue(localRequest.Url.IsLoopback);
            Assert.IsFalse(nonLocalRequest.Url.IsLoopback);
        }

        [Test]
        public void LocalRequestShouldHaveTwoFeatures()
        {
            var localFeatureSet = switchboard.GetFeatureSet(localRequest);
            Assert.AreEqual(2, localFeatureSet.Features.Count);
            // Switchable features always come first
            Assert.AreSame(this.localhostOnly, localFeatureSet.Features.First(), "First feature is wrong");
            Assert.AreSame(this.alwaysEnabled, localFeatureSet.Features.Last(), "Second feature is wrong");
        }

        [Test]
        public void NonlocalRequestShouldHaveOneFeature()
        {
            var nonLocalFeatureSet = switchboard.GetFeatureSet(nonLocalRequest);
            Assert.AreSame(this.alwaysEnabled, nonLocalFeatureSet.Features.Single());
        }

        [Test]
        public void DuplicateRequestsShouldReturnSameFeatureSet()
        {
            var set1 = switchboard.GetFeatureSet(localRequest);
            var set2 = switchboard.GetFeatureSet(localRequest);
            Assert.AreSame(set1, set2);
        }
    }
}

{'fraction_non_alphanumeric': 0.08878067053755385, 'fraction_numerical': 0.0018730099269526129, 'mean_word_length': 2.4901960784313726, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2137345', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1351, 'n_words': 264}