コード例 #1
0
        public void MyTestCleanup()
        {
            PrivateType privateWindowType = new PrivateType(typeof(VSWindows));
            privateWindowType.SetStaticFieldOrProperty("instance", null);

            this.serviceProvider = null;
        }
コード例 #2
0
        public void InstanceTest()
        {
            try
            {
                InvisibleForm actual;

                actual = InvisibleForm.Instance;
                Assert.IsNotNull(actual, "InvisibleForm.Instance returned null");

                // Reset and try again
                PrivateType invisibleForm = new PrivateType(typeof(InvisibleForm));
                invisibleForm.SetStaticFieldOrProperty("instanceForm", null);

                actual = InvisibleForm.Instance;
                Assert.IsNotNull(actual, "InvisibleForm.Instance returned null");

                Assert.AreSame(actual, invisibleForm.GetStaticFieldOrProperty("instanceForm"), "Second call to the property should return the same opbject instance.");

                Form f = actual as Form;
                Assert.IsFalse(f.Visible, "InvisibleForm should not be visible");
            }
            catch (Exception ex)
            {
                // Use try catch to test a workaround on CI build (AppVeyor)
                Console.WriteLine(ex.Message);
            }
        }
コード例 #3
0
 public void TestCountBulls()
 {
     bool[] isBull = new bool[4];
     PrivateType pr = new PrivateType(typeof(Game));
     pr.SetStaticFieldOrProperty("secretNumberAsString", "1234");
     int res = Game.CountBulls("1235", 0, isBull);
     Assert.AreEqual(3, res);
 }
コード例 #4
0
 public void TestCountHits()
 {
     bool[] isBull = new bool[4];
     bool[] isCow = new bool[10];
     PrivateType pr = new PrivateType(typeof(Game));
     pr.SetStaticFieldOrProperty("secretNumberAsString", "1234");
     int bulls = Game.CountBulls("1234", 0, isBull);
     int cows = Game.CountCows("1234", 0, isBull, isCow);
     Assert.AreEqual(4, bulls);
     Assert.AreEqual(0, cows);
 }
コード例 #5
0
 public void GameTestCurrentHits()
 {
     PrivateType pr = new PrivateType(typeof(Game));
     pr.SetStaticFieldOrProperty("secretNumberAsString", "1234");
     StringWriter sw = new StringWriter();
     Console.SetOut(sw);
     Game.RevealCurrentHits("1234");
     var result = sw.ToString();
     var expected = String.Format("Wrong number! Bulls: 4, Cows: 0!{0}{0}",
         Environment.NewLine);
     Assert.AreEqual(expected, result);
 }
コード例 #6
0
 public void MyTestCleanup()
 {
     try
     {
         PrivateType instanceForm = new PrivateType(typeof(InvisibleForm));
         instanceForm.SetStaticFieldOrProperty("instanceForm", null);
     }
     catch (Exception ex)
     {
         // Use try catch to test a workaround on CI build (AppVeyor)
         Console.WriteLine(ex.Message);
     }
 }
コード例 #7
0
        public void InstanceTest()
        {
            InvisibleForm actual;

            actual = InvisibleForm.Instance;
            Assert.IsNotNull(actual, "InvisibleForm.Instance returned null");

            // Reset and try again
            PrivateType invisibleForm = new PrivateType(typeof(InvisibleForm));
            invisibleForm.SetStaticFieldOrProperty("instanceForm", null);
            
            actual = InvisibleForm.Instance;
            Assert.IsNotNull(actual, "InvisibleForm.Instance returned null");
            
            Assert.AreSame(actual, invisibleForm.GetStaticFieldOrProperty("instanceForm"), "Second call to the property should return the same opbject instance.");
            
            Form f = actual as Form;
            Assert.IsFalse(f.Visible, "InvisibleForm should not be visible");
        }
コード例 #8
0
 public void MyTestInitialize()
 {
     PrivateType instanceForm = new PrivateType(typeof(InvisibleForm));
     instanceForm.SetStaticFieldOrProperty("instanceForm", null);
 }
コード例 #9
0
        public void OnNavigateToDocNotInProjectTest()
        {
            var mockDocumentEnumerator = new SequenceMock<IEnumerator>();
            var mockDte = new Mock<DTE>();
            var mockDocuments = new Mock<Documents>();
            var mockDocument = new SequenceMock<Document>();
            var mockActiveDocument = new Mock<Document>();
            var mockTextSelection = new SequenceMock<TextSelection>();
            var mockVirtualPoint = new SequenceMock<VirtualPoint>();

            this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, "DummyFile.txt");
            var mockSecondDocument = new SequenceMock<Document>();
            mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true);
            mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockSecondDocument.Instance);
            mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false);

            mockSecondDocument.AddExpectationExpr(doc => doc.FullName, "DummyFile.txt");

            AnalysisHelper analysisHelper = this.SetCoreNoUI();
            bool eventFired = false;

            PrivateObject actual = new PrivateObject(this.package, new PrivateType(typeof(StyleCopVSPackage)));
            actual.SetFieldOrProperty("core", this.package.Core);

            // Register output generated event to test event fired
            this.package.Core.OutputGenerated += (sender, args) => { eventFired = true; };

            mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance);

            mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true));
            mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance);

            mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0));

            this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance);

            // Use private type to set static private field
            PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities));
            privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance);

            // Execute
            PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask)));
            taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty);

            // Verify the required methods are called to show the violation
            mockTextSelection.Verify();
            mockVirtualPoint.Verify();
            mockDocument.Verify();

            Assert.IsTrue(eventFired, "Core did not fire output event");
        }
コード例 #10
0
        public void OnNavigateToDocInProjectTest()
        {
            var mockDocumentEnumerator = new SequenceMock<IEnumerator>();
            var mockDte = new Mock<DTE>();
            var mockDocuments = new Mock<Documents>();
            var mockDocument = new SequenceMock<Document>();
            var mockActiveDocument = new Mock<Document>();
            var mockTextSelection = new SequenceMock<TextSelection>();
            var mockVirtualPoint = new SequenceMock<VirtualPoint>();

            this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, this.violation.File);

            mockDocument.AddExpectationExpr(doc => doc.Activate());
            mockDocument.AddExpectationExpr(doc => doc.DTE, (Func<DTE>)delegate { return (EnvDTE.DTE)mockDte.Instance; });

            mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance);

            mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true));
            mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance);

            mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0));

            this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance);

            this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance);
            this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(SVsSolutionBuildManager)), new MockSolutionBuildManager());

            // Use private type to set static private field
            PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities));
            privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance);

            // Execute
            PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask)));
            taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty);

            // Verify the required methods are called to show the violation
            mockTextSelection.Verify();
            mockVirtualPoint.Verify();
            mockDocument.Verify();
        }
コード例 #11
0
        public void OnNavigateNoDocumentTest()
        {
            bool eventFired = false;
            Mock<DTE> mockDte = new Mock<DTE>();

            // No UI for test
            AnalysisHelper analysisHelper = this.SetCoreNoUI();

            PrivateObject actual = new PrivateObject(this.package, new PrivateType(typeof(StyleCopVSPackage)));
            actual.SetFieldOrProperty("core", this.package.Core);

            // Register output generated event to test event fired
            this.package.Core.OutputGenerated += (sender, args) => { eventFired = true; };

            // Does nothing - included for code coverage and to catch it if it starts doing something unexpectedly
            Assert.IsNotNull(this.taskUnderTestShell, "this.taskUnderTestShell is null.");
            this.taskUnderTestShell.Document = null;

            // Use private type to set static private field
            PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities));           
            privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance);

            PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask)));
            taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty);

            Assert.IsTrue(eventFired, "Core did not fire output event");
        }
コード例 #12
0
        public void TestInitialize()
        {
            this.mockServiceProvider = new Mock<IServiceProvider>();

            PrivateType projectUtilities = new PrivateType(typeof(ProjectUtilities));
            projectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance);
        }
コード例 #13
0
 public void TestCleanup()
 {
     PrivateType projectUtilities = new PrivateType(typeof(ProjectUtilities));
     projectUtilities.SetStaticFieldOrProperty("serviceProvider", null);
 }
コード例 #14
0
        public void SavePng_BuildFinished()
        {
            { // MachineInfo is in the picture, so make MachineInfo as machine indepenent string for unit test puroposes
                string      machineIndepenentInfo = "Processors: 1 | Cores: 2 | CPU Speed: 2.2GHz | Hyper Threading: Enabled | RAM: 8GB | HDD: 1 SSD";
                MachineInfo mi          = MachineInfo.Instance;
                var         machineInfo = new PrivateObject(mi); // Use PrivateObject class to change private member of MachineInfo object.
                machineInfo.SetField("info", machineIndepenentInfo);

                Assert.AreEqual(MachineInfo.Instance.ToString(), machineIndepenentInfo); // Verify if internal data were updated
            }

            {                                                // Feed DataModel with sample data
                DataModel dm        = DataModel.Instance;
                var       dataModel = new PrivateObject(dm); // Use PrivateObject class to change private member of MachineInfo object.
                dataModel.SetProperty("SolutionName", "Example.sln");
                dataModel.SetProperty("StartTime", new System.DateTime(636849135031953262L));
                dataModel.SetProperty("MaxParallelBuilds", 4);

                //<c>string</c> is ProjectUniqueName, <c>uint</c> is project build order number, <c>long</c> is project Start time, relative, counted since <c>DataModel.StartTime</c> in <c>DateTime.Ticks</c> units.
                Dictionary <string, Tuple <uint, long> > currentBuilds = new Dictionary <string, Tuple <uint, long> > {
                };
                dataModel.SetField("currentBuilds", currentBuilds);

                List <BuildInfo> finishedBuilds = new List <BuildInfo>()
                {
                    new BuildInfo("junk-finddir\\junk-finddir.vcxproj", "junk-finddir.vcxproj", 1, 399931, 54684041, true),
                    new BuildInfo("DiskPerformance\\DiskPerformance.csproj", "DiskPerformance.csproj", 4, 59892361, 97806434, false),
                    new BuildInfo("junk-vector-const\\junk-vector-const.vcxproj", "junk-vector-const.vcxproj", 5, 61022178, 121272755, true),
                    new BuildInfo("WpfToolTip\\WpfToolTip.csproj", "WpfToolTip.csproj", 2, 56140367, 144319141, true),
                    new BuildInfo("constexpr_templates\\constexpr_templates.vcxproj", "constexpr_templates.vcxproj", 3, 58330492, 151578262, true),
                    new BuildInfo("variadic-macros-v1\\variadic-macros-v1.vcxproj", "variadic-macros-v1.vcxproj", 6, 144449121, 172642436, true)
                };
                dataModel.SetField("finishedBuilds", finishedBuilds);

                Dictionary <string, List <string> > projectDependenies = new Dictionary <string, List <string> > //<c>string</c> is ProjectUniqueName, <c>List<string></c> is list of projects that <c>Key</c> project depends on
                {
                    { "constexpr_templates\\constexpr_templates.vcxproj", new List <string> {
                          "junk-finddir\\junk-finddir.vcxproj"
                      } },
                    { "DiskPerformance\\DiskPerformance.csproj", new List <string> {
                          "junk-finddir\\junk-finddir.vcxproj"
                      } },
                    { "junk-finddir\\junk-finddir.vcxproj", new List <string> {
                      } },
                    { "junk-vector-const\\junk-vector-const.vcxproj", new List <string> {
                          "junk-finddir\\junk-finddir.vcxproj"
                      } },
                    { "variadic-macros-v1\\variadic-macros-v1.vcxproj", new List <string> {
                          "junk-finddir\\junk-finddir.vcxproj", "WpfToolTip\\WpfToolTip.csproj"
                      } },
                    { "WpfToolTip\\WpfToolTip.csproj", new List <string> {
                          "junk-finddir\\junk-finddir.vcxproj"
                      } }
                };
                dataModel.SetField("projectDependenies", projectDependenies);

                List <BuildInfo> criticalPath = new List <BuildInfo>
                {
                    new BuildInfo("junk-finddir\\junk-finddir.vcxproj", "junk-finddir.vcxproj", 1, 399931, 54684041, true),
                    new BuildInfo("WpfToolTip\\WpfToolTip.csproj", "WpfToolTip.csproj", 2, 56140367, 144319141, true),
                    new BuildInfo("variadic-macros-v1\\variadic-macros-v1.vcxproj", "variadic-macros-v1.vcxproj", 6, 144449121, 172642436, true)
                };
                dataModel.SetField("criticalPath", criticalPath);

                List <Tuple <long, float> > cpuUsage = new List <Tuple <long, float> >()
                {
                    new Tuple <long, float>(636849135031963260, 0.0f),
                    new Tuple <long, float>(636849135042025422, 47.36607f),
                    new Tuple <long, float>(636849135052097336, 43.76379f),
                    new Tuple <long, float>(636849135062946043, 36.62839f),
                    new Tuple <long, float>(636849135073055216, 45.90309f),
                    new Tuple <long, float>(636849135083364284, 48.84662f),
                    new Tuple <long, float>(636849135094055281, 48.48177f),
                    new Tuple <long, float>(636849135108063077, 98.88455f),
                    new Tuple <long, float>(636849135118301475, 100f),
                    new Tuple <long, float>(636849135130849521, 100f),
                    new Tuple <long, float>(636849135142927622, 87.70002f),
                    new Tuple <long, float>(636849135152926055, 91.4049f),
                    new Tuple <long, float>(636849135163764355, 76.97614f),
                    new Tuple <long, float>(636849135174642665, 89.21751f),
                    new Tuple <long, float>(636849135188218652, 79.268f),
                    new Tuple <long, float>(636849135199266903, 58.98672f)
                };
                dataModel.SetField("cpuUsage", cpuUsage);

                List <Tuple <long, float> > hddUsage = new List <Tuple <long, float> >()
                {
                    new Tuple <long, float>(636849135031963260, 0.0f),
                    new Tuple <long, float>(636849135042025422, 64.68444f),
                    new Tuple <long, float>(636849135052097336, 10.2781f),
                    new Tuple <long, float>(636849135062946043, 2.541418f),
                    new Tuple <long, float>(636849135073055216, 5.857255f),
                    new Tuple <long, float>(636849135083364284, 8.998146f),
                    new Tuple <long, float>(636849135094055281, 3.024021f),
                    new Tuple <long, float>(636849135108063077, 18.93993f),
                    new Tuple <long, float>(636849135118301475, 25.54949f),
                    new Tuple <long, float>(636849135130849521, 1.439461f),
                    new Tuple <long, float>(636849135142927622, 2.991249f),
                    new Tuple <long, float>(636849135152926055, 1.817592f),
                    new Tuple <long, float>(636849135163764355, 4.232076f),
                    new Tuple <long, float>(636849135174642665, 6.579974f),
                    new Tuple <long, float>(636849135188218652, 8.768057f),
                    new Tuple <long, float>(636849135199266903, 0.652989f),
                };
                dataModel.SetField("hddUsage", hddUsage);

                { // private static need to be handled through PrivateType
                    PrivateType pt = new PrivateType(typeof(DataModel));
                    pt.SetStaticFieldOrProperty("projectBuildOrderNumber", 6u);
                }

                Assert.AreEqual(DataModel.Instance.SolutionName, "Example.sln"); // Verify if internal data were updated
            }

            PBMControl pBMcontrol = new PBMControl();

            var graphControl = new PrivateObject(GraphControl.Instance);                     // Use PrivateObject class to change private member of GraphControl object.

            graphControl.SetField("nowTickForTest", 636849135031953262L + 172642436 + 1000); //1000 is just assumed delay for finishing (counting dependencies etc.)

            Window window = new Window
            {
                Width   = 800,
                Height  = 600,
                Content = pBMcontrol
            };

            window.Show(); //this will draw PBMControl
            //window.ShowDialog(); //for debug

            string expected = TestUtils.GetTestFile("BuildFinished.png");
            // ResultsDirectory   <= this is where results should be saved. How to get value of this xaml tag?
            string tmpFileName = Path.GetTempFileName(); //this method return path to non-existing file in temp directory

            tmpFileName += ".png";

            bool res = pBMcontrol.SaveGraph(tmpFileName /*pathToPngFile*/);

            Assert.IsTrue(res);
            Assert.IsTrue(ImageComparer.AreImagesEqual(expected, tmpFileName));
        }
コード例 #15
0
        public void SavePng_BuildInProgress()
        {
            { // MachineInfo is in the picture, so make MachineInfo as machine indepenent string for unit test puroposes
                string      machineIndepenentInfo = "Processors: 1 | Cores: 2 | CPU Speed: 2.2GHz | Hyper Threading: Enabled | RAM: 8GB | HDD: 1 SSD";
                MachineInfo mi          = MachineInfo.Instance;
                var         machineInfo = new PrivateObject(mi); // Use PrivateObject class to change private member of MachineInfo object.
                machineInfo.SetField("info", machineIndepenentInfo);

                Assert.AreEqual(MachineInfo.Instance.ToString(), machineIndepenentInfo); // Verify if internal data were updated
            }

            {                                                // Feed DataModel with sample data
                DataModel dm        = DataModel.Instance;
                var       dataModel = new PrivateObject(dm); // Use PrivateObject class to change private member of MachineInfo object.
                dataModel.SetProperty("SolutionName", "Example.sln");
                dataModel.SetProperty("StartTime", new System.DateTime(636848298660196710L));
                dataModel.SetProperty("MaxParallelBuilds", 4);

                //<c>string</c> is ProjectUniqueName, <c>uint</c> is project build order number, <c>long</c> is project Start time, relative, counted since <c>DataModel.StartTime</c> in <c>DateTime.Ticks</c> units.
                Dictionary <string, Tuple <uint, long> > currentBuilds = new Dictionary <string, Tuple <uint, long> >
                {
                    { "WpfToolTip\\WpfToolTip.csproj", new Tuple <uint, long>(2, 77409902) },
                    { "constexpr_templates\\constexpr_templates.vcxproj", new Tuple <uint, long>(3, 79745101) },
                    { "DiskPerformance\\DiskPerformance.csproj", new Tuple <uint, long>(4, 81644812) },
                    { "junk-vector-const\\junk-vector-const.vcxproj", new Tuple <uint, long>(5, 83574518) },
                };
                dataModel.SetField("currentBuilds", currentBuilds);

                List <BuildInfo> finishedBuilds = new List <BuildInfo>()
                {
                    new BuildInfo("junk-finddir\\junk-finddir.vcxproj", "junk-finddir.vcxproj", 1, 359937, 77299946, true)
                };
                dataModel.SetField("finishedBuilds", finishedBuilds);

                Dictionary <string, List <string> > projectDependenies = new Dictionary <string, List <string> >(); //<c>string</c> is ProjectUniqueName, <c>List<string></c> is list of projects that <c>Key</c> project depends on
                dataModel.SetField("projectDependenies", projectDependenies);

                List <BuildInfo> criticalPath = new List <BuildInfo>();
                dataModel.SetField("criticalPath", criticalPath);

                List <Tuple <long, float> > cpuUsage = new List <Tuple <long, float> >()
                {
                    new Tuple <long, float>(636848298660196710, 0.0f),
                    new Tuple <long, float>(636848298670348211, 77.42183f),
                    new Tuple <long, float>(636848298681079693, 58.4654f),
                    new Tuple <long, float>(636848298691089400, 78.53645f),
                    new Tuple <long, float>(636848298701193247, 43.16831f),
                    new Tuple <long, float>(636848298711194104, 40.23949f),
                    new Tuple <long, float>(636848298721322554, 33.27891f),
                    new Tuple <long, float>(636848298731336449, 37.58672f),
                    new Tuple <long, float>(636848298741491580, 43.4553f),
                    new Tuple <long, float>(636848298752339901, 94.59882f)
                };
                dataModel.SetField("cpuUsage", cpuUsage);

                List <Tuple <long, float> > hddUsage = new List <Tuple <long, float> >()
                {
                    new Tuple <long, float>(636848298660196710, 0.0f),
                    new Tuple <long, float>(636848298670348211, 3.894294f),
                    new Tuple <long, float>(636848298681079693, 10.60813f),
                    new Tuple <long, float>(636848298691089400, 37.33977f),
                    new Tuple <long, float>(636848298701193247, 3.864434f),
                    new Tuple <long, float>(636848298711194104, 4.997182f),
                    new Tuple <long, float>(636848298721322554, 3.919567f),
                    new Tuple <long, float>(636848298731336449, 9.422446f),
                    new Tuple <long, float>(636848298741491580, 5.652133f),
                    new Tuple <long, float>(636848298752339901, 0.8484202f)
                };
                dataModel.SetField("hddUsage", hddUsage);

                { // private static need to be handled through PrivateType
                    PrivateType pt = new PrivateType(typeof(DataModel));
                    pt.SetStaticFieldOrProperty("projectBuildOrderNumber", 5u);
                }

                Assert.AreEqual(DataModel.Instance.SolutionName, "Example.sln"); // Verify if internal data were updated
            }

            PBMControl pBMcontrol = new PBMControl();

            var graphControl = new PrivateObject(GraphControl.Instance); // Use PrivateObject class to change private member of GraphControl object.

            graphControl.SetField("nowTickForTest", 636848298760000000L);

            Window window = new Window
            {
                Width   = 800,
                Height  = 600,
                Content = pBMcontrol
            };

            window.Show(); //this will draw PBMControl
            //window.ShowDialog(); //for debug

            string expected = TestUtils.GetTestFile("BuildInProgress.png");
            // ResultsDirectory   <= this is where results should be saved. How to get value of this xaml tag?
            string tmpFileName = Path.GetTempFileName(); //this method return path to non-existing file in temp directory

            tmpFileName += ".png";

            bool res = pBMcontrol.SaveGraph(tmpFileName /*pathToPngFile*/);

            Assert.IsTrue(res);
            Assert.IsTrue(ImageComparer.AreImagesEqual(expected, tmpFileName));
        }