Esempio n. 1
0
        private IEnumerator RunTests()
        {
            VoxelPlayer  player;
            GameObject   managerObj;
            ChunkManager manager;

            foreach (var testSuite in GetComponentsInChildren <AbstractTestSuite>())
            {
                if (selectedTestSuiteNames != null && !selectedTestSuiteNames.Contains(testSuite.gameObject.name))
                {
                    continue;//Skip any test suites that were not selected
                }

                testSuite.Initialise();
                var suiteName = testSuite.SuiteName;
                //For each repeat
                for (int repeatIndex = 0; repeatIndex <= NumRepeats; repeatIndex++)
                {
                    //Run all tests in the suite
                    for (int testIndex = 0; testIndex < testSuite.Tests.Length; testIndex++)
                    {
                        managerObj = Worlds.Find(chunkManagerName).gameObject;
                        manager    = managerObj.GetComponent <ChunkManager>();
                        testSuite.SetManagerForNextPass(manager);

                        //Run all passes in the suite
                        foreach (var passDetails in testSuite.Passes())
                        {
                            yield return(null);//Give time for component removals from pass to be applied

                            float startTime = Time.unscaledTime;

                            var groupName     = passDetails.GroupName;
                            var test          = testSuite.Tests[testIndex];
                            var testDirectory = $@"{@TestResultPath}{@suiteName}\{groupName}\";
                            EnsureDirectoryExists(testDirectory);

                            var fileName         = $"{test.TestName}";
                            var completeFilePath = @testDirectory + @fileName + @FileExtension;
                            using (StreamWriter log = new StreamWriter(@TestResultPath + @LogFileName + @FileExtension, true))
                            {
                                var testRunIdentifier = $"{suiteName}\\{groupName}\\{fileName}\\{passDetails.TechniqueName}\\R{repeatIndex}";
                                log.WriteLine($"\n\n\nTest {testRunIdentifier}:");
                                Debug.Log($"Starting test for {testRunIdentifier}");

                                //Reset virtual input
                                virtualPlayer = new VirtualPlayerInput();
                                CrossPlatformInputManager.SetActiveInputMethod(virtualPlayer);

                                managerObj.SetActive(true);
                                player         = FindObjectOfType <VoxelPlayer>();
                                player.enabled = true;

                                //Run the test
                                yield return(StartCoroutine(test.Run(manager)));

                                bool makeHeader = !filesWithHeader.Contains(completeFilePath);

                                //Write outputs
                                using (StreamWriter testResults = new StreamWriter(completeFilePath, true))
                                {
                                    WriteTestLog(log, test);
                                    WriteTestResults(testResults, test, makeHeader, passDetails.TechniqueName, repeatIndex);
                                    filesWithHeader.Add(completeFilePath);
                                }

                                ///Cleanup--------------
                                test.Clear();
                                //reload the scene
                                yield return(SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single));

                                //Do garbage collection
                                GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);

                                yield return(new WaitForSecondsRealtime(2));

                                //Locate Worlds object in scene
                                Worlds = GameObject.Find("VoxelWorlds").transform;
                                //Locate manager again for next pass
                                managerObj = Worlds.Find(chunkManagerName).gameObject;
                                manager    = managerObj.GetComponent <ChunkManager>();
                                testSuite.SetManagerForNextPass(manager);

                                float duration = Time.unscaledTime - startTime;
                                log.WriteLine($"\nFinished after total time of {duration}");
                            }
                        }
                    }
                }
            }

            Debug.Log("All tests done");

            ///Must initalise manager, as it is not guaranteed to exit correctly otherwise
            ///(Stuff needs disposing)
            managerObj = Worlds.Find(chunkManagerName).gameObject;

            ///Ensure manager is in a valid configuration (only one provider and one mesher component)
            AbstractTestSuite.RemoveComponentsOfTypeExceptSubtype <AbstractProviderComponent, DebugProvider>(managerObj);
            AbstractTestSuite.RemoveComponentsOfTypeExceptSubtype <AbstractMesherComponent, CullingMesher>(managerObj);

            managerObj.SetActive(true);
            manager = managerObj.GetComponent <ChunkManager>();
            manager.Initialise();

            manager    = null;
            managerObj = null;
            yield return(SceneManager.LoadSceneAsync("MainMenu"));

            //Do garbage collection
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Default, true);

            //Destroy this object
            Destroy(gameObject);
        }