<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DotVVM.Framework.Tests.Common.ViewModel
{
    [TestClass]
    public class ValidationErrorFactoryTests
    {
        [TestMethod]
        public void ValidationErrorFactory_GetPathFromExpression_WithPrimitiveLocal()
        {
            var index = 1;
            var expression = ValidationErrorFactory.GetPathFromExpression(
                DotvvmTestHelper.DefaultConfig,
                (Expression<Func<TestViewModel, int>>)(vm => vm.Numbers[index]));
            Assert.AreEqual("Numbers()[1]", expression);
        }

        [TestMethod]
        public void ValidationErrorFactory_GetPathFromExpression_WithComplexLocal()
        {
            var sample = new Sample { Index = 42 };
            var expression = ValidationErrorFactory.GetPathFromExpression(
                DotvvmTestHelper.DefaultConfig,
                (Expression<Func<TestViewModel, int>>)(vm => vm.Numbers[sample.Index]));
            Assert.AreEqual("Numbers()[42]", expression);
        }

        private class Sample
        {
            public int Index { get; set; }
        }

        private class TestViewModel
        {
            public int[] Numbers { get; set; } = new[] { 0, 1, 1, 2, 3, 5 };
        }
    }
}<commit_msg>Added (failing) tests for cache in ValidationErrorFactory<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.ViewModel.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DotVVM.Framework.Tests.Common.ViewModel
{
    [TestClass]
    public class ValidationErrorFactoryTests
    {

        [TestMethod]
        public void ValidationErrorFactory_GetPathFromExpression_WithPrimitiveLocal()
        {
            var index = 1;
            var expression = ValidationErrorFactory.GetPathFromExpression(
                DotvvmTestHelper.DefaultConfig,
                (Expression<Func<TestViewModel, int>>)(vm => vm.Numbers[index]));
            Assert.AreEqual("Numbers()[1]", expression);
        }

        [TestMethod]
        public void ValidationErrorFactory_GetPathFromExpression_WithComplexLocal()
        {
            var sample = new Sample { Index = 42 };
            var expression = ValidationErrorFactory.GetPathFromExpression(
                DotvvmTestHelper.DefaultConfig,
                (Expression<Func<TestViewModel, int>>)(vm => vm.Numbers[sample.Index]));
            Assert.AreEqual("Numbers()[42]", expression);
        }


        string GetNumbers(int index) =>
            ValidationErrorFactory.GetPathFromExpression(
                DotvvmTestHelper.DefaultConfig,
                (Expression<Func<TestViewModel, int>>)(vm => vm.Numbers[index])
            );

        [TestMethod]
        public void TestMultipleValues()
        {
            // test that the cache built into ValidationErrorFactory does not leak

            Assert.AreEqual("Numbers()[1]", GetNumbers(1));
            Assert.AreEqual("Numbers()[2]", GetNumbers(2));
            Assert.AreEqual("Numbers()[3]", GetNumbers(3));
        }

        [TestMethod]
        public void TestValidationErrorCache()
        {
            // test that the cache built into ValidationErrorFactory actually works

            var expr1 = GetNumbers(1);
            var expr2 = GetNumbers(1);

            // the expression must be referentially equal if they are from the cache
            Assert.IsTrue((object)expr1 == expr2);
        }

        private class Sample
        {
            public int Index { get; set; }
        }

        private class TestViewModel
        {
            public int[] Numbers { get; set; } = new[] { 0, 1, 1, 2, 3, 5 };
        }
    }
}
{'fraction_non_alphanumeric': 0.09715284715284715, 'fraction_numerical': 0.008991008991008992, 'mean_word_length': 2.365546218487395, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '4629290', 'n_tokens_mistral': 1081, 'n_tokens_neox': 987, 'n_words': 227}