<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Shielded;
using Shielded.ProxyGen;
using ShieldedTests.ProxyTestEntities;

namespace ShieldedTests
{
    [TestFixture]
    public class OldStateTests
    {
        [Test]
        public void BasicTest()
        {
            var a = new Shielded<int>(10);
            Shield.InTransaction(() => {
                a.Value = 20;
                Assert.AreEqual(20, a);
                Shield.ReadOldState(() => {
                    Assert.AreEqual(10, a);
                    a.Value = 30;
                    Assert.AreEqual(10, a);
                    a.Modify((ref int x) =>
                        Assert.AreEqual(30, x));
                });
                Assert.AreEqual(30, a.Value);
            });
            Assert.AreEqual(30, a.Value);
        }

        [Test]
        public void DictionaryTest()
        {
            var dict = Shield.InTransaction(() => new ShieldedDict<int, int>() {
                { 1, 1 },
                { 2, 2 },
                { 3, 3 },
            });
            Shield.InTransaction(() => {
                dict[2] = 102;
                Assert.AreEqual(102, dict[2]);
                Shield.ReadOldState(() => {
                    Assert.AreEqual(2, dict[2]);
                    var values = dict.Values;
                    Assert.AreEqual(3, values.Count);
                    Assert.IsFalse(values.Except(new[] { 1, 2, 3 }).Any());
                    dict[2] = 22;
                    Assert.AreEqual(2, dict[2]);
                });
                Assert.AreEqual(22, dict[2]);
            });
            Assert.AreEqual(22, dict[2]);
        }

        [Test]
        public void WhenCommittingWithProxy()
        {
            var a = Factory.NewShielded<Entity1>();
            var guid1 = Guid.NewGuid();
            Shield.InTransaction(() => {
                a.Id = guid1;
                a.Name = "Test";
            });

            var guid2 = Guid.NewGuid();
            using (Shield.WhenCommitting<Entity1>(es => {
                Shield.ReadOldState(() => {
                    Assert.AreEqual(guid1, a.Id);
                    Assert.AreEqual("Test", a.Name);
                });
                Assert.AreEqual(guid2, a.Id);
                Assert.AreEqual("New name", a.Name);
            }))
            {
                Shield.InTransaction(() => {
                    a.Id = guid2;
                    a.Name = "New name";
                });
            }
        }
    }
}

<commit_msg>Small extra check in an old state test<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Shielded;
using Shielded.ProxyGen;
using ShieldedTests.ProxyTestEntities;

namespace ShieldedTests
{
    [TestFixture]
    public class OldStateTests
    {
        [Test]
        public void BasicTest()
        {
            var a = new Shielded<int>(10);
            Shield.InTransaction(() => {
                a.Value = 20;
                Assert.AreEqual(20, a);
                Shield.ReadOldState(() => {
                    Assert.AreEqual(10, a);
                    a.Value = 30;
                    Assert.AreEqual(10, a);
                    a.Modify((ref int x) =>
                        Assert.AreEqual(30, x));
                });
                Assert.AreEqual(30, a.Value);
            });
            Assert.AreEqual(30, a.Value);
        }

        [Test]
        public void DictionaryTest()
        {
            var dict = Shield.InTransaction(() => new ShieldedDict<int, int>() {
                { 1, 1 },
                { 2, 2 },
                { 3, 3 },
            });
            Shield.InTransaction(() => {
                Assert.AreEqual(1, dict[1]);
                dict[2] = 102;
                Assert.AreEqual(102, dict[2]);
                Shield.ReadOldState(() => {
                    Assert.AreEqual(1, dict[1]);
                    Assert.AreEqual(2, dict[2]);
                    var values = dict.Values;
                    Assert.AreEqual(3, values.Count);
                    Assert.IsFalse(values.Except(new[] { 1, 2, 3 }).Any());
                    dict[2] = 22;
                    Assert.AreEqual(2, dict[2]);
                });
                Assert.AreEqual(1, dict[1]);
                Assert.AreEqual(22, dict[2]);
            });
            Assert.AreEqual(1, dict[1]);
            Assert.AreEqual(22, dict[2]);
        }

        [Test]
        public void WhenCommittingWithProxy()
        {
            var a = Factory.NewShielded<Entity1>();
            var guid1 = Guid.NewGuid();
            Shield.InTransaction(() => {
                a.Id = guid1;
                a.Name = "Test";
            });

            var guid2 = Guid.NewGuid();
            using (Shield.WhenCommitting<Entity1>(es => {
                Shield.ReadOldState(() => {
                    Assert.AreEqual(guid1, a.Id);
                    Assert.AreEqual("Test", a.Name);
                });
                Assert.AreEqual(guid2, a.Id);
                Assert.AreEqual("New name", a.Name);
            }))
            {
                Shield.InTransaction(() => {
                    a.Id = guid2;
                    a.Name = "New name";
                });
            }
        }
    }
}


{'fraction_non_alphanumeric': 0.12903225806451613, 'fraction_numerical': 0.022748461681894462, 'mean_word_length': 1.1370517928286852, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, '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': '5299448', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1416, 'n_words': 269}