public void Test(TestCaseConfig config, TextReader reader)
        {
            var dependencies = config.Runtime ? new[] { runtimeDependency } : Enumerable.Empty <PackageReferenceSyntax>();
            var package      = new PackageSyntax($"Adamant.Exploratory.Compiler.Tests.{config.TestName}", true, dependencies);
            var unit         = compiler.Parse(package, new SourceReader(config.FileName, reader));

            package = package.With(new[] { unit });
            if (package.Diagnostics.Count > 0)
            {
                Assert.Fail(ToString(package.Diagnostics));
            }
            var compiledPackage = compiler.Compile(package, Enumerable.Empty <Package>());

            if (compiledPackage.Diagnostics.Count > 0)
            {
                Assert.Fail(ToString(compiledPackage.Diagnostics));
            }

            var cppSource     = compiler.EmitCpp(compiledPackage);
            var cppSourceName = compiledPackage.Name + ".cpp";

            CreateFile(cppSourceName, cppSource);
            CreateFile(CppRuntime.FileName, CppRuntime.Source);
            var targetPath = Path.Combine(WorkPath, compiledPackage.Name + ".exe");
            var result     = CppCompiler.Invoke(Path.Combine(WorkPath, cppSourceName), targetPath);

            if (result.ExitCode != 0)
            {
                result.WriteOutputToConsole();
                Assert.Fail("C++ compiler error");
            }

            // Execute the app
            using (var process = new Process())
            {
                process.StartInfo.FileName               = targetPath;
                process.StartInfo.WorkingDirectory       = Path.GetDirectoryName(WorkPath);
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                var outputBuffer = new StringBuilder();
                var errorBuffer  = new StringBuilder();
                process.OutputDataReceived += (s, e) => outputBuffer.AppendLine(e.Data);
                process.ErrorDataReceived  += (s, e) => errorBuffer.AppendLine(e.Data);
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();

                if (config.Result != null)
                {
                    Assert.AreEqual(config.Result, process.ExitCode, "Exit Code");
                }
                if (config.VerifyConsoleOutput)
                {
                    Assert.AreEqual(config.ExpectedConsoleOutput, outputBuffer.ToString());
                }
            }
        }
Example #2
0
        public void TestTestCaseConfigJsonBinding()
        {
            var json = "{\n" +
                       "  \"EndPoint1\": \"http://test.url.com/endpoint1\",\n" +
                       "  \"EndPoint2\": \"http://test.url.com/endpoint2\",\n" +
                       "  \"Headers\": [\n" +
                       "    { \"Authorization\": \"Basic abc123\" }\n" +
                       "  ],\n" +
                       "  \"CallType\": \"SOAP\",\n" +
                       "  \"SoapAction\": \"SOAP_ACTION_ENDPOINT\"\n" +
                       "}";

            var tccBuilder = new ConfigurationBuilder().AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json)));

            var tccConfig = tccBuilder.Build();

            var tempFile       = Path.GetTempPath();
            var testCastConfig = new TestCaseConfig(tccConfig, tempFile);

            Assert.True((testCastConfig.EndPoint1 == "http://test.url.com/endpoint1"));
            Assert.True((testCastConfig.CallType == CallType.SOAP));
            Assert.True((testCastConfig.Headers == new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("Authorization", "Basic abc123")
            }));
        }
Example #3
0
        public async Task TestAutoTestCaseAsync()
        {
            // this is a poor example of a testcase since it relies on an existing service
            var endpoint1 = "http://test.url.com/endpoint1";
            var endpoint2 = "http://test.url.com/endpoint2";
            var callType  = CallType.SOAP;
            var headers   = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("Authorization", "Basic c3ZjX211bGVfdXNlcjohZG9uS2V5MTIz")
            };
            var soapAction = "SOAP_ACTION_ENDPOINT";

            var payload = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sal=\"http://test.url.com/endpoint\">\n" +
                          "   <soapenv:Header/>\n" +
                          "   <soapenv:Body>\n" +
                          "   </soapenv:Body>\n" +
                          "</soapenv:Envelope>";


            ILoggerManager logger = Mock.Of <LoggerManager>();

            string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Directory.CreateDirectory(tempDirectory);
            TestCaseConfig config   = new TestCaseConfig(endpoint1, endpoint2, headers, callType, null, soapAction);
            AutoTestCase   testCase = new AutoTestCase(tempDirectory, config, payload, logger, false);

            string report = string.Empty;
            string diff   = string.Empty;

            var htmlReportPath = await testCase.Run();

            Assert.True(Path.GetDirectoryName(htmlReportPath) == tempDirectory);
        }
Example #4
0
        public void Test(TestCaseConfig config, TextReader reader)
        {
            var dependencies = config.Runtime ? new[] { runtimeDependency } : Enumerable.Empty <PackageReferenceSyntax>();
            var package      = new PackageSyntax($"Adamant.Exploratory.Compiler.Tests.{config.TestName}", true, dependencies);
            var unit         = compiler.Parse(package, new SourceReader(config.FileName, reader));

            package = package.With(new[] { unit });
            if (package.Diagnostics.Count > 0)
            {
                Assert.Fail(ToString(package.Diagnostics));
            }
            var compiledPackage = compiler.Compile(package, Enumerable.Empty <Package>());

            if (compiledPackage.Diagnostics.Count > 0)
            {
                Assert.Fail(ToString(compiledPackage.Diagnostics));
            }

            var interpreter  = new AdamantInterpreter(compiledPackage);
            var entryPoint   = compiledPackage.EntryPoints.Single();
            var outputBuffer = new StringBuilder();
            var errorBuffer  = new StringBuilder();
            var exitCode     = interpreter.Invoke(entryPoint, new string[0], new StringWriter(outputBuffer), new StringWriter(errorBuffer));

            Assert.AreEqual(config.Result, exitCode, "Exit Code");
            if (config.VerifyConsoleOutput)
            {
                Assert.AreEqual(config.ExpectedConsoleOutput, outputBuffer.ToString());
            }
        }
        public IEnumerable <TestCaseData> TestCases()
        {
            var namespaceLength = typeof(Harness).Namespace.Length + 1;
            var assembly        = Assembly.GetExecutingAssembly();
            var resourceNames   = assembly.GetManifestResourceNames().Where(name => name.EndsWith(Extension));

            foreach (var resourceName in resourceNames)
            {
                var stream = assembly.GetManifestResourceStream(resourceName);
                var reader = new StreamReader(stream);
                var config = TestCaseConfig.Read(reader);
                config.FileName = resourceName;
                config.TestName = resourceName.Substring(namespaceLength, resourceName.Length - namespaceLength - Extension.Length);
                var testCaseData = new TestCaseData(config, reader).SetName(config.TestName);
                if (!string.IsNullOrWhiteSpace(config.Description))
                {
                    testCaseData.SetDescription(config.Description);
                }
                yield return(testCaseData);
            }
        }
Example #6
0
        public static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
//#if DEBUG
//            var builder = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory).AddJsonFile($"appsettings.dev.json", false, true);
//#else
            var builder = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory).AddJsonFile($"appsettings.json", false, true);

//#endif

            configuration = builder.Build();
            LogManager.LoadConfiguration(String.Concat(Directory.GetCurrentDirectory(), "/nlog.config"));

            var serviceCollection = new ServiceCollection();

            ConfigureServices(serviceCollection, configuration);


            var serviceProvider = serviceCollection.BuildServiceProvider();

            logger = serviceProvider.GetService <ILoggerManager>();
            var serviceName = Assembly.GetEntryAssembly().GetName().Name;

            logger.LogInfo($"Beginning {serviceName}");

            var testCaseBaseFolder     = configuration.GetValue <string>("TestCaseBaseFolder");
            var testCaseDirectories    = Directory.EnumerateDirectories(testCaseBaseFolder);
            var pollingIntervalSeconds = configuration.GetValue <int>("PollingIntervalSeconds");
            var pollingInterval        = new TimeSpan(0, 0, pollingIntervalSeconds);
            var degreesOfParallelism   = configuration.GetValue <int>("DegreesOfParallelism");


            Func <string, Task> processTestCaseCallback = async(testCaseDirectory) =>
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                var logger = serviceProvider.GetService <ILoggerManager>();

                var testCaseName        = Path.GetFileName(testCaseDirectory);
                var configFileName      = configuration.GetValue <string>("ConfigFileName");
                var payloadFileName     = configuration.GetValue <string>("PayloadFileName");
                var reportFileName      = configuration.GetValue <string>("ReportFileName");
                var archiveTestCaseRuns = configuration.GetValue <bool>("ArchiveTestCaseRuns");

                // I hate this part but lets build a custom logger for each test case
                var logTarget = new FileTarget();
                logTarget.Name     = testCaseName;
                logTarget.FileName = testCaseDirectory + "/${shortdate}_logfile.txt";
                logTarget.Layout   = "${longdate} ${level:uppercase=true} ${message}";

                LogManager.Configuration.AddTarget(testCaseName, logTarget);
                LogManager.Configuration.AddRule(LogLevel.Info, LogLevel.Fatal, logTarget, testCaseName);
                LogManager.ReconfigExistingLoggers();

                var testCaseLogger = new LoggerManager(testCaseName);


                testCaseLogger.LogInfo($"{testCaseName} Logger is initialized, retrieving config file");
                TestCaseConfig testCaseConfig = null;

                try
                {
                    var configPath = Path.Combine(testCaseDirectory, configFileName);
                    var tccBuilder = new ConfigurationBuilder().AddJsonFile(configPath);

                    var iConfig = tccBuilder.Build();

                    testCaseConfig = new TestCaseConfig(iConfig, configFileName);
                }
                catch (Exception e)
                {
                    testCaseLogger.LogError($"Configuration Failure -> {e.ToDetailedString()}");
                    logger.LogError($"{testCaseName} Failed -> Configuration Failure -> {e.ToDetailedString()}");
                }

                var resultFilePath = string.Empty;
                try
                {
                    var autoTestCase = new AutoTestCase(testCaseDirectory, testCaseConfig, payloadFileName, reportFileName, testCaseLogger, archiveTestCaseRuns);

                    resultFilePath = await autoTestCase.Run();
                }
                catch (Exception e)
                {
                    testCaseLogger.LogError($"Test Case Compare Failure -> {e.ToDetailedString()}");
                    logger.LogError($"{testCaseName} Failed -> Test Case Compare Failure -> {e.ToDetailedString()}");
                }

                stopwatch.Stop();

                logger.LogInfo($"Finished processing test case: {testCaseName} with report at {resultFilePath} elapsed time: { stopwatch.Elapsed.TotalMilliseconds } ms.");
            };

            var autoTestCaseProcessor = new AutoTestCaseProcessor(testCaseBaseFolder, processTestCaseCallback, pollingInterval, degreesOfParallelism, logger);

            autoTestCaseProcessor.Start();

            // Because its a console app and not a service
            while (autoTestCaseProcessor.ProcessingCompleted == false)
            {
                continue;
            }
            if (autoTestCaseProcessor.ProcessingCompleted == true)
            {
                //autoTestCaseProcessor.Stop();
                return(StatusCode);
            }


            return(StatusCode);
        }
        /// <summary>
        /// Callback to be executed after SaveCircuitMessageBox.
        /// Unfreezes the canvas and closes the message box.
        /// Positive trigger saves the circuit as JSON.
        /// </summary>
        /// <param name="triggerData">Data to pass from the trigger source to the trigger target.</param>
        public void Trigger(MessageBoxTriggerData triggerData)
        {
            if (triggerData.ButtonPressed == UIMessageBox.MessageBoxButtonType.Positive)
            {
                // Bind a GUID for each component
                Dictionary <SPLogicComponent, Guid> guidMap = Canvas.Components
                                                              .ToDictionary(c => c, c => Guid.NewGuid());

                // Generate configs for components
                List <LogicComponentConfig> componentConfigs = guidMap
                                                               .Select(kvp => kvp.Key.GenerateConfig(kvp.Value))
                                                               .ToList();

                // Generate configs for edges, mapping connected components using GUIDs
                List <EdgeConfig> edgeConfigs = Canvas.Edges
                                                .Select(e => e.GenerateConfig(guidMap))
                                                .ToList();

                // Generate additional configs to save state of input togglers
                List <InputToggleConfig> toggleConfigs = guidMap
                                                         .Where(kvp => kvp.Key.GetType() == typeof(SPInputToggler))
                                                         .Select(kvp => new InputToggleConfig
                {
                    guid_string = kvp.Value.ToString(),
                    value       = ((InputComponent)kvp.Key.LogicComponent).value
                })
                                                         .ToList();

                List <InputToggleConfig> numberedToggleConfigs = guidMap
                                                                 .Where(kvp => kvp.Key.GetType() == typeof(SPNumberedInputToggler))
                                                                 .Select(kvp => new InputToggleConfig
                {
                    guid_string = kvp.Value.ToString(),
                    value       = ((InputComponent)kvp.Key.LogicComponent).value
                })
                                                                 .ToList();

                // Generate additional configs to save state of clock components
                List <ClockComponentConfig> clockConfigs = guidMap
                                                           .Where(kvp => kvp.Key.GetType() == typeof(SPClock))
                                                           .Select(kvp => new ClockComponentConfig
                {
                    guid_string = kvp.Value.ToString(),
                    period      = ((Clock)kvp.Key.LogicComponent).Period
                })
                                                           .ToList();

                // Generate an empty test cases config:
                List <TestCaseConfig> testCaseConfigs = new List <TestCaseConfig>();

                // Build savefile
                CircuitConfig spConfig = new CircuitConfig(
                    componentConfigs,
                    edgeConfigs,
                    toggleConfigs,
                    numberedToggleConfigs,
                    clockConfigs,
                    testCaseConfigs,
                    mode: GameMode.Sandbox);

#if DEVELOPMENT_BUILD
                string saveData = JsonUtility.ToJson(spConfig, prettyPrint: true);
#else
                string saveData = JsonUtility.ToJson(spConfig, prettyPrint: false);
#endif

                // Build full file path
                string filename = triggerData.TextInput + ".json";
                string fullpath = Directories.SAVEFILE_FOLDER_FULL_PATH + "/" + filename;

                // Write to file
                Debug.Log("Saving to " + fullpath);
                try
                {
                    Directory.CreateDirectory(Directories.SAVEFILE_FOLDER_FULL_PATH);
                    File.WriteAllText(fullpath, saveData);
                    Canvas.SetAsSaved();
                }
                catch (Exception e)
                {
                    // uh oh
                    Debug.LogException(e);
                    MessageBoxFactory.MakeFromConfig(SaveErrorMessageBoxConfig);
                }
            }
            else if (triggerData.ButtonPressed == UIMessageBox.MessageBoxButtonType.Neutral)
            {
                // Bind a GUID for each component
                Dictionary <SPLogicComponent, Guid> guidMap = Canvas.Components
                                                              .ToDictionary(c => c, c => Guid.NewGuid());

                // Generate configs for components
                List <LogicComponentConfig> componentConfigs = guidMap
                                                               .Select(kvp => kvp.Key.GenerateConfig(kvp.Value))
                                                               .ToList();

                // Generate configs for edges, mapping connected components using GUIDs
                List <EdgeConfig> edgeConfigs = Canvas.Edges
                                                .Select(e => e.GenerateConfig(guidMap))
                                                .ToList();

                // Generate additional configs to save state of input togglers
                List <InputToggleConfig> toggleConfigs = guidMap
                                                         .Where(kvp => kvp.Key.GetType() == typeof(SPInputToggler))
                                                         .Select(kvp => new InputToggleConfig
                {
                    guid_string = kvp.Value.ToString(),
                    value       = ((InputComponent)kvp.Key.LogicComponent).value
                })
                                                         .ToList();

                List <InputToggleConfig> numberedToggleConfigs = guidMap
                                                                 .Where(kvp => kvp.Key.GetType() == typeof(SPNumberedInputToggler))
                                                                 .Select(kvp => new InputToggleConfig
                {
                    guid_string = kvp.Value.ToString(),
                    value       = ((InputComponent)kvp.Key.LogicComponent).value
                })
                                                                 .ToList();

                // Generate test cases config:
                List <TestCaseConfig> testCaseConfigs = new List <TestCaseConfig>();
                // Generate one based on Circuit:
                // Just a dummy one so you have a template on how to modify it.
                TestCaseConfig testCase = new TestCaseConfig();
                foreach (var kvp in Canvas.NumberedInputs)
                {
                    InputComponent inputComponent = kvp.Value.LogicComponent as InputComponent;
                    testCase.SetInput(kvp.Key, inputComponent.value);
                }
                foreach (var kvp in Canvas.NumberedOutputs)
                {
                    Output output = kvp.Value.LogicComponent as Output;
                    testCase.SetOutput(kvp.Key, output.Value);
                }
                testCaseConfigs.Add(testCase);

                // Generate additional configs to save state of clock components
                List <ClockComponentConfig> clockConfigs = guidMap
                                                           .Where(kvp => kvp.Key.GetType() == typeof(SPClock))
                                                           .Select(kvp => new ClockComponentConfig
                {
                    guid_string = kvp.Value.ToString(),
                    period      = ((Clock)kvp.Key.LogicComponent).Period
                })
                                                           .ToList();

                // Generate default title strings.
                String titleText = "Objective";
                String bodyText  = "Your aim is to turn all the lights on.\nYou can not delete pre-placed components.\nAll components placed must use all endpoints and contribute to at least one output light.";

                // Build savefile
                CircuitConfig spConfig = new CircuitConfig(
                    componentConfigs,
                    edgeConfigs,
                    toggleConfigs,
                    numberedToggleConfigs,
                    clockConfigs,
                    testCaseConfigs,
                    GameMode.ActivateAllOutputsChallenge,
                    titleText: titleText,
                    bodyText: bodyText);

#if DEVELOPMENT_BUILD
                string saveData = JsonUtility.ToJson(spConfig, prettyPrint: true);
#else
                string saveData = JsonUtility.ToJson(spConfig, prettyPrint: false);
#endif

                // Build full file path
                string filename = triggerData.TextInput + ".json";
                string fullpath = Directories.CHALLENGE_FOLDER_FULL_PATH + "/" + filename;

                // Write to file
                Debug.Log("Saving to " + fullpath);
                try
                {
                    Directory.CreateDirectory(Directories.CHALLENGE_FOLDER_FULL_PATH);
                    File.WriteAllText(fullpath, saveData);
                    Canvas.SetAsSaved();
                }
                catch (Exception e)
                {
                    // uh oh
                    Debug.LogException(e);
                    MessageBoxFactory.MakeFromConfig(SaveErrorMessageBoxConfig);
                }
            }

            Canvas.Frozen = false;
            Destroy(triggerData.Sender.gameObject);
        }