Exemple #1
0
        public void Constructor_shall_not_call_within_short_timeframe_to_generate_unique_information()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var seeds = new HashSet <int>();
                PRandom.ConstructorInt32().Body = (@this, seed) =>
                {
                    IndirectionsContext.ExecuteOriginal(() =>
                    {
                        var ctor = typeof(Random).GetConstructor(new[] { typeof(int) });
                        ctor.Invoke(@this, new object[] { seed });
                    });
                    seeds.Add(seed);
                };
                new Random();  // preparing JIT
                seeds.Clear();


                // Act
                var vil1 = new Village();
                Thread.Sleep(TimeSpan.FromSeconds(1));
                var vil2 = new Village();


                // Assert
                Assert.AreEqual(2, seeds.Count);
            }
        }
Exemple #2
0
        public void GetShortestRoute_should_consider_routes_in_order_from_small_distance()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var slot                 = 0;
                var numAndDistances      = new[] { 4, 2, 4, 3, 1, 6, 7 };
                PRandom.NextInt32().Body = (@this, maxValue) => numAndDistances[slot++];

                var vil = new Village();

                var considerations = new List <RicePaddy>();
                PList <RicePaddy> .AddT().Body = (@this, item) =>
                {
                    IndirectionsContext.ExecuteOriginal(() =>
                    {
                        considerations.Add(item);
                        @this.Add(item);
                    });
                };


                // Act
                var result = vil.GetShortestRoute(vil.RicePaddies.ElementAt(2), vil.RicePaddies.ElementAt(0));


                // Assert
                Assert.AreEqual(3, result.TotalDistance);
                Assert.AreEqual(4, considerations.Count);
                Assert.AreEqual(2, considerations[0].Identifier);
                Assert.AreEqual(1, considerations[1].Identifier);
                Assert.AreEqual(0, considerations[2].Identifier);
                Assert.AreEqual(3, considerations[3].Identifier);
            }
        }
Exemple #3
0
        public void BeginRead_should_be_callable_indirectly()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                PStream.BeginReadByteArrayInt32Int32AsyncCallbackObject().Body = (@this, _buffer, offset, count, callback, state) =>
                                                                                 IndirectionsContext.ExecuteOriginal(() => @this.BeginRead(_buffer, offset, 42, callback, state));

                var buffer = new byte[256];
                for (int i = 0; i < buffer.Length; i++)
                {
                    buffer[i] = (byte)i;
                }


                using (var ms = new MemoryStream(buffer))
                {
                    // Act
                    var _buffer = new byte[1024];
                    var ar      = ms.BeginRead(_buffer, 0, _buffer.Length, null, null);
                    var actual  = ms.EndRead(ar);

                    // Assert
                    Assert.AreEqual(42, actual);
                }
            }
        }
Exemple #4
0
        public void AddOnDisposed_should_be_callable_indirectly()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var handler = default(ULSharedMemory.DisposedEventHandler);

                PULSharedMemory.AddOnDisposedDisposedEventHandler().Body = (@this, value) => handler += value;
                PULSharedMemory.Dispose().Body = @this =>
                {
                    handler(true);
                    IndirectionsContext.ExecuteOriginal(() => @this.Dispose());
                };

                var called = false;

                using (var sm = new ULSharedMemory())
                {
                    // Act
                    sm.OnDisposed += disposing => called = disposing;
                }


                // Assert
                Assert.IsTrue(called);
            }
        }
Exemple #5
0
        public void PProxyListDefaultBehavior_can_apply_default_behavior_to_throw_FallthroughException_against_one_instance()
        {
            // Arrange
            IndirectionsContext.
            ExcludeGeneric().
            Include(PList <int> .AddT()).
            DefaultBehavior = IndirectionBehaviors.NotImplemented;

            var proxy = new PProxyList <int>();

            proxy.
            ExcludeGeneric().
            IncludeAddT().
            DefaultBehavior = IndirectionBehaviors.Fallthrough;
            LooseCrossDomainAccessor.GetOrRegister <GenericHolder <IndirectionAction <List <int>, int> > >().Source = proxy.AddT().Body;
            LooseCrossDomainAccessor.GetOrRegister <GenericHolder <List <int> > >().Source = (List <int>)proxy;


            // Act
            var addTarget = LooseCrossDomainAccessor.GetOrRegister <GenericHolder <IndirectionAction <List <int>, int> > >().Source;
            var target    = LooseCrossDomainAccessor.GetOrRegister <GenericHolder <List <int> > >().Source;
            var addOther  = PList <int> .AddT().Body;


            // Assert
            Assert.Throws <FallthroughException>(() => addTarget(target, 0));
            Assert.Throws <NotImplementedException>(() => addTarget(new List <int>(), 0));
            Assert.Throws <NotImplementedException>(() => addOther(null, 0));
        }
Exemple #6
0
 public void SetupLoadFromString(string expected_assemblyFile)
 {
     LoadFromString_expected_assemblyFile = expected_assemblyFile;
     LoadFromString().Body =
         assemblyFile =>
     {
         LoadFromString_actual_assemblyFile = assemblyFile;
         return(IndirectionsContext.ExecuteOriginal(() => Assembly.LoadFrom(assemblyFile)));
     };
 }
Exemple #7
0
 public void SetupLoadString(string expected_assemblyString)
 {
     LoadString_expected_assemblyString = expected_assemblyString;
     LoadString().Body =
         assemblyString =>
     {
         LoadString_actual_assemblyString = assemblyString;
         return(IndirectionsContext.ExecuteOriginal(() => Assembly.Load(assemblyString)));
     };
 }
Exemple #8
0
        public void IsNowLunchBreak_should_return_indeterminate_result_if_original_behavior_is_performed_internally()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                IndirectionsContext.
                ExcludeGeneric().
                DefaultBehavior = IndirectionBehaviors.Fallthrough;

                // Act, Assert
                Assert.DoesNotThrow(() => LifeInfo.IsNowLunchBreak());
            }
        }
Exemple #9
0
        public void IsNowLunchBreak_should_rethrow_any_exception_if_an_exception_is_thrown_internally()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                IndirectionsContext.
                ExcludeGeneric().
                DefaultBehavior = IndirectionBehaviors.NotImplemented;

                // Act, Assert
                Assert.Throws <NotImplementedException>(() => LifeInfo.IsNowLunchBreak());
            }
        }
Exemple #10
0
        public void IsNowLunchBreak_should_return_false_if_default_value_is_returned_internally()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                IndirectionsContext.
                ExcludeGeneric().
                DefaultBehavior = IndirectionBehaviors.DefaultValue;

                // Act
                var result = LifeInfo.IsNowLunchBreak();

                // Assert
                Assert.IsFalse(result);
            }
        }
        public void Constructor_should_be_initialized_by_non_null_if_number_that_is_not_divisible_by_10_is_passed()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var actualValue     = 0;
                PRandom.Next().Body = @this => 9;
                PNullable <int> .ConstructorT().Body = (ref Nullable <int> @this, int value) =>
                {
                    actualValue = value;
                    @this       = IndirectionsContext.ExecuteOriginal(() => new Nullable <int>(value));
                };


                // Act
                var paddy = new RicePaddy(1, new Random());


                // Assert
                Assert.AreEqual(9000, actualValue);
            }
        }
Exemple #12
0
        public void PArrayDefaultBehavior_can_apply_default_behavior_to_throw_FallthroughException_against_one_type()
        {
            // Arrange
            IndirectionsContext.
            ExcludeGeneric().
            DefaultBehavior = IndirectionBehaviors.NotImplemented;

            PArray.
            ExcludeGeneric().
            IncludeExistsOfTTArrayPredicateOfT <int>().
            DefaultBehavior = IndirectionBehaviors.Fallthrough;


            // Act
            var get_now = PDateTime.NowGet().Body;
            var exists  = PArray.ExistsOfTTArrayPredicateOfT <int>().Body;


            // Assert
            Assert.Throws <NotImplementedException>(() => get_now());
            Assert.Throws <FallthroughException>(() => exists(null, null));
        }
Exemple #13
0
        public void IndirectionsContextDefaultBehavior_can_apply_default_behavior_to_throw_FallthroughException_globally()
        {
            // Arrange
            IndirectionsContext.
            ExcludeGeneric().
            Include(PList <int> .AddT()).
            Include(PArray.ExistsOfTTArrayPredicateOfT <int>()).
            DefaultBehavior = IndirectionBehaviors.Fallthrough;


            // Act
            var get_now = PDateTime.NowGet().Body;
            var add     = PList <int> .AddT().Body;

            var exists = PArray.ExistsOfTTArrayPredicateOfT <int>().Body;


            // Assert
            Assert.Throws <FallthroughException>(() => get_now());
            Assert.Throws <FallthroughException>(() => add(null, 0));
            Assert.Throws <FallthroughException>(() => exists(null, null));
        }
Exemple #14
0
        public void IndirectionsContextDefaultBehavior_can_apply_default_behavior_to_return_default_value_globally()
        {
            // Arrange
            IndirectionsContext.
            ExcludeGeneric().
            Include(PList <int> .AddT()).
            Include(PArray.ExistsOfTTArrayPredicateOfT <int>()).
            DefaultBehavior = IndirectionBehaviors.DefaultValue;


            // Act
            var get_now = PDateTime.NowGet().Body;
            var add     = PList <int> .AddT().Body;

            var exists = PArray.ExistsOfTTArrayPredicateOfT <int>().Body;


            // Assert
            Assert.AreEqual(default(DateTime), get_now());
            Assert.DoesNotThrow(() => add(null, 10));
            Assert.IsFalse(exists(null, null));
        }
Exemple #15
0
        public void GetterOfNow_should_be_callable_originally_any_time()
        {
            using (new IndirectionsContext())
            {
                // Arrange
                var count = 0;
                PDateTime.NowGet().Body = () =>
                {
                    if (5 <= ++count)
                    {
                        return(new DateTime(2013, 12, 23, 11, 22, 33, 44));
                    }
                    else
                    {
                        return(IndirectionsContext.ExecuteOriginal(() => DateTime.Now));
                    }
                };

                // Act
                var actuals = new List <DateTime>();
                actuals.Add(DateTime.Now);
                actuals.Add(DateTime.Now);
                actuals.Add(DateTime.Now);
                actuals.Add(DateTime.Now);
                actuals.Add(DateTime.Now);
                actuals.Add(DateTime.Now);

                // Assert
                var indirectValue = new DateTime(2013, 12, 23, 11, 22, 33, 44);
                Assert.AreNotEqual(indirectValue, actuals[0]);
                Assert.AreNotEqual(indirectValue, actuals[1]);
                Assert.AreNotEqual(indirectValue, actuals[2]);
                Assert.AreNotEqual(indirectValue, actuals[3]);
                Assert.AreEqual(indirectValue, actuals[4]);
                Assert.AreEqual(indirectValue, actuals[5]);
            }
        }