Ejemplo n.º 1
0
 public void IsAny()
 {
     Assert.False(It.IsAny(null));
     Assert.True(It.IsAny((object)false));
     Assert.True(It.IsAny(It.Any <object>()));
     Assert.True(It.IsAny((object)It.Any <ExampleClass>()));
 }
Ejemplo n.º 2
0
        public void Can_Intercept_Many_Methods_With_One_Advice()
        {
            var ack   = 0;
            var foo   = new Foo();
            var proxy = new Proxy <IFoo>()
                        .Target(foo)
                        .InterceptMethods(
                f => f.Return(),
                f => f.GenericGo <int>(It.Any <int>()),
                f => f.Go())
                        .OnBefore(mi => ack++)
                        .Save();

            // intercepted
            proxy.Go();
            proxy.Return();
            proxy.GenericGo <int>(-1);

            // not intercepted
            proxy.Name = string.Empty;
            proxy.OverloadedGo(-1);
            proxy.OverloadedGo(string.Empty);

            Assert.Equal(3, ack);
        }
Ejemplo n.º 3
0
        public void ShimPropertyGetterTest()
        {
            var exampleClass = new ExampleClass(2);

            Assert.Equal(2, exampleClass.Factor);
            Shim.Isolate(() =>
            {
                Assert.Equal(1, exampleClass.Factor);
            }, Shim.Replace(() => It.Any <ExampleClass>().Factor)
                         .With((Func <ExampleClass, int>)(@this => 1)));
        }
Ejemplo n.º 4
0
        public void FluentAOP()
        {
            var proxy = new CastleProxy <ItExtendsAnAbstractClass>()
                        //.Target(ObjectMother.Create<ItExtendsAnAbstractClass>())
                        .Intercept(c => c.Return(It.Any <string>()))
                        //.OnBefore(()=> Console.Write("Intercepted"))
                        .OnInvoke(mi => - 3)
                        .Save();

            Assert.Equal(-3, proxy.Return(string.Empty));
        }
Ejemplo n.º 5
0
        public void Any()
        {
            Assert.True(It.IsAny(It.Any <bool>()));
            Assert.True(It.IsAny(It.Any <string>()));
            Assert.True(It.IsAny(It.Any <ExampleClass>()));

            Assert.False(It.IsAny <string>(null));
            Assert.False(It.IsAny(""));
            Assert.False(It.IsAny(" "));
            Assert.False(It.IsAny(new ExampleClass(2)));
        }
Ejemplo n.º 6
0
        public void ShimIndexerGetterTest()
        {
            var exampleClass = new ExampleClass(2);

            Assert.Equal(6, exampleClass[3]);
            Shim.Isolate(() =>
            {
                Assert.Equal(3, exampleClass[3]);
            }, Shim.Replace(() => It.Any <ExampleClass>()[It.Any <int>()])
                         .With((Func <ExampleClass, int, int>)((@this, a) => a)));
        }
Ejemplo n.º 7
0
        public void ShimSubFunctionCallTest()
        {
            var called = false;

            Shim.Isolate(() =>
            {
                Assert.False(called);
                Console.Out.WriteLine("Test");
                Assert.True(called);
            }, Shim.Replace(() => It.Any <TextWriter>().WriteLine(It.Any <string>()))
                         .With((Action <TextWriter, string>)((@this, str) => { called = true; })));
        }
Ejemplo n.º 8
0
        public void ShimIndexerSetterTest2()
        {
            var exampleClass = new ExampleClass(2);
            var called       = false;

            Shim.Isolate(() =>
            {
                Assert.False(called);
                exampleClass[3, ""] = 1;
                Assert.True(called);
            }, Shim.ReplaceSetter(() => It.Any <ExampleClass>()[It.Any <int>(), It.Any <string>()])
                         .With((Action <ExampleClass, int, string, int>)((@this, index, str, value) => { called = true; })));
        }
Ejemplo n.º 9
0
        public void Can_Intercept_Overloaded_Methods()
        {
            var ack   = false;
            var foo   = new Foo();
            var proxy = new Proxy <IFoo>()
                        .Target(foo)
                        .InterceptMethod(f => f.OverloadedGo(It.Any <string>()))
                        .OnBefore(() => ack = true)
                        .Save();

            proxy.OverloadedGo(-1);           // not intercepted
            Assert.False(ack);
            proxy.OverloadedGo(string.Empty); // intercepted
            Assert.True(ack);
        }
Ejemplo n.º 10
0
        public void ShimOutParamMethod()
        {
            var shim = new Shim((OutTestFunc)It.Any <ExampleClass>().OutTestMethod,
                                (OutTestReplacementFunc)((ExampleClass @this, int a, out int b) => b = a));

            var exampleClass = new ExampleClass(2);

            exampleClass.OutTestMethod(3, out var result);
            Assert.Equal(6, result);
            Shim.Isolate(() =>
            {
                exampleClass.OutTestMethod(3, out result);
                Assert.Equal(3, result);
            }, shim);
        }
Ejemplo n.º 11
0
        public void ShimPropertySetterTest()
        {
            var exampleClass = new ExampleClass(3)
            {
                Factor = 2
            };

            Assert.Equal(2, exampleClass.Factor);
            Shim.Isolate(() =>
            {
                exampleClass.Factor = 5;
                Assert.Equal(2, exampleClass.Factor);
            }, Shim.ReplaceSetter(() => It.Any <ExampleClass>().Factor)
                         .With((Action <ExampleClass, int>)((@this, value) => { })));
        }
Ejemplo n.º 12
0
        public void ShimConstructorTest()
        {
            var shim = new Shim(typeof(ExampleClass).GetConstructor(new [] { typeof(int) }),
                                (Func <int, ExampleClass>)ShimConstructor);

            Assert.Equal(2, new ExampleClass(2).Factor);
            Shim.Isolate(() =>
            {
                Assert.Equal(4, new ExampleClass(2).Factor);
            }, shim);

            Shim.Isolate(() =>
            {
                Assert.Equal(4, new ExampleClass(2).Factor);
            }, Shim.Replace(() => new ExampleClass(It.Any <int>()))
                         .With((Func <int, ExampleClass>)(a => new ExampleClass(a * 2))));
        }
Ejemplo n.º 13
0
        public void ShimStaticMethod()
        {
            var shim = new Shim((Func <int, int>)ExampleClass.StaticTestMethod,
                                (Func <int, int>)ShimStaticTestMethod);

            Assert.Equal(6, ExampleClass.StaticTestMethod(3));
            Shim.Isolate(() =>
            {
                Assert.Equal(3, ExampleClass.StaticTestMethod(3));
            }, shim);

            Shim.Isolate(() =>
            {
                Assert.Equal(3, ExampleClass.StaticTestMethod(3));
            }, Shim.Replace(() => ExampleClass.StaticTestMethod(It.Any <int>()))
                         .With((Func <int, int>)(a => a)));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// The main program executes the tests. Output may be routed to
        /// various locations, depending on the arguments passed.
        /// </summary>
        /// <remarks>Run with --help for a full list of arguments supported</remarks>
        /// <param name="args"></param>
        public static int Main(string[] args)
        {
            try
            {
                var @is = new HijackGlobalSetup();
                @is.AddCallingAssembly("UntestableLibrary", @"C:\Users\Derek\Documents\Visual Studio 2015\Projects\Interceptor\DemoLibrary\bin\Debug\UntestableLibrary.dll");
                @is.Setup(() => File.Open(It.Any <string>(), It.Any <FileMode>()));
                @is.Setup <Random>(r => r.Next());

                @is.Start();

                return(new AutoRun(typeof(Program).Assembly).Execute(args));
            }
            catch (Exception e)
            {
                return(-1);
            }
        }
Ejemplo n.º 15
0
        public void ShimInstanceMethod()
        {
            var shim = new Shim((Func <int, int>)It.Any <ExampleClass>().InstanceTestMethod,
                                (Func <ExampleClass, int, int>)ShimInstanceTestMethod);

            var exampleClass = new ExampleClass(2);

            Assert.Equal(6, exampleClass.InstanceTestMethod(3));
            Shim.Isolate(() =>
            {
                Assert.Equal(3, exampleClass.InstanceTestMethod(3));
            }, shim);

            Shim.Isolate(() =>
            {
                Assert.Equal(3, exampleClass.InstanceTestMethod(3));
            }, Shim.Replace(() => It.Any <ExampleClass>().InstanceTestMethod(It.Any <int>()))
                         .With((Func <ExampleClass, int, int>)((@this, a) => a)));
        }
Ejemplo n.º 16
0
        public void StaticCallOnly()
        {
            var ms = new MemoryStream();
            var sw = new StreamWriter(ms);

            {
                sw.Write("hello world");
                sw.Flush();
            }
            ms.Position = 0;

            Hijack.Setup(() => File.Open(It.Any <string>(), It.Any <FileMode>())).Returns(ms);

            var untestable = new UntestableLibrary.Untestable();

            string s = untestable.StaticCallOnly();

            Assert.AreEqual("hello world", s);
        }
Ejemplo n.º 17
0
        public void Example_of_how_to_intercept_overloaded_methods()
        {
            var foo = new Proxy <IFoo>()
                      .Target(new Foo())
                      .InterceptMethod(f => f.Overloaded(It.Any <string>()))
                      .OnBefore(mi => mi.Arguments[0] = "--Intercepted--") // Replaces arg0
                      .Save();

            foo.Overloaded(" Hi! ");

            // Notes: method was intercepted
            // Console Output:
            // --Intercepted--

            foo.Overloaded(123);

            // Notes: method was not intercepted
            // Console Output:
            // 123
        }
Ejemplo n.º 18
0
        public void Can_Intercept_Generic_Methods()
        {
            var ack   = 0;
            var foo   = new Foo();
            var proxy = new Proxy <IFoo>()
                        .Target(foo)
                        .InterceptMethod(f => f.GenericGo <int>(It.Any <int>()))
                        .OnBefore(() => { Assert.False(foo.WasExecuted); ack++; })
                        .InterceptMethod(f => f.GenericGo <string>(It.Any <string>()))
                        .OnAfter(() => { ack++; })
                        .Save();

            proxy.GenericGo <int>(1);                    // intercepted
            proxy.GenericGo <string>(string.Empty);      // intercepted
            proxy.GenericGo <double>(2d);                // not intercepted
            proxy.GenericGo <object>(It.Any <object>()); // not intercepted

            Assert.True(foo.WasExecuted);
            Assert.Equal(2, ack);
        }
Ejemplo n.º 19
0
        public void ReplaceTest()
        {
            var shim = Shim.For <DateTime>()
                       .WithParameters <long>()
                       .Replace(It.Any <DateTime>().AddTicks)
                       .With((ref DateTime time, long ticks) => time);

            Assert.Null(shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);

            Shim.For <DateTime>()
            .WithParameters <int, int>()
            .Replace(DateTime.DaysInMonth)
            .With((ref DateTime time, int year, int month) => 0);

            Shim.For <string>()
            .WithParameters <int>()
            .Replace(It.Any <string>().Substring)
            .With((s, i) => s);

            Shim.For <string>()
            .WithParameters <string>()
            .Replace(string.Copy)
            .With((s, i) => s);

            Shim.Replace(() => It.Any <string>()[It.Any <int>()]);

            /*Shim.WithParameters<int>()
             *  .Replace(It.Any<string>().Substring)
             *  .On<string>() ???
             *  .With((s, i) => s);
             *
             * Shim.WithParameters<int>()
             *  .Replace(It.Any<string>().Substring)
             *  .On<string>()*/


            //    .Replace(Console.WriteLine)
        }
Ejemplo n.º 20
0
        public void SimpleShimBuilderTest()
        {
            var shim = Shim.Replace(() => DateTime.Now)
                       .With((Func <DateTime>)(() => new DateTime(2020, 1, 1)));

            Assert.Null(shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);

            shim = Shim.Replace(() => DateTime.DaysInMonth(It.Any <int>(), It.Any <int>()))
                   .With((Func <int, int, int>)((y, d) => 0));
            Assert.Null(shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);

            shim = Shim.Replace(() => It.Any <DateTime>().AddTicks(It.Any <long>()))
                   .With((FuncRef <DateTime, long, DateTime>)((ref DateTime datetime, long ticks) => new DateTime()));
            Assert.Null(shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);

            shim = Shim.Replace(() => Console.WriteLine(It.Any <string>()))
                   .With((Action <string>)(s => {}));
            Assert.Null(shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);

            shim = Shim.Replace(() => It.Any <TextWriter>().WriteLine(It.Any <string>()))
                   .With((Action <TextWriter, string>)((tw, s) => { }));
            Assert.Null(shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);

            shim = Shim.Replace(() => Console.Out.WriteLine(It.Any <string>()))
                   .With((Action <TextWriter, string>)((tw, s) => { }));
            Assert.Equal(Console.Out, shim.Instance);
            Assert.NotNull(shim.Original);
            Assert.NotNull(shim.Replacement);
        }
Ejemplo n.º 21
0
 public void Throws_If_Target_Type_Does_Not_Have_Default_Ctor()
 {
     // Problem only happens when proxy is built with Castle
     Assert.Throws <FluentAop.ProxyInitializationException>(() =>
                                                            new CastleProxy <ItHasNonDefaultConstructor>()
                                                            .InterceptMethod(c => c.Go(It.Any <string>()))
                                                            .OnReturn(() => - 1)
                                                            .Save()
                                                            );
 }
Ejemplo n.º 22
0
        public void MethodGroupAny()
        {
            Func <int, string> group = It.Any <string>().Substring;

            Assert.NotNull(group);
        }