public void ShouldReturnOnlyLastValueOfVariablesDeclaredManyTimes([NoAutoProperties] MonoScriptEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty <string>(), scriptPackSession);
                engine.Execute("int x = 2;", new string[0], new AssemblyReferences(), Enumerable.Empty <string>(), scriptPackSession);

                engine.GetLocalVariables(scriptPackSession).ShouldEqual(new Collection <string> {
                    "int x = 2"
                });
            }
            public void ShouldReturnReturnValueIfCodeExecutionReturnsValue(
                [Frozen] Mock <IScriptHostFactory> scriptHostFactory,
                [NoAutoProperties] MonoScriptEngine engine,
                ScriptPackSession scriptPackSession)
            {
                // Mono doesn't support comments after evals as well as Roslyn
                const string Code = "\"Hello\"";  //this should return \"Hello\"";

                // Arrange
                scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()))
                .Returns <IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));

                var session = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;
                var refs = new AssemblyReferences(new[] { "System" });

                // Act
                var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty <string>(), scriptPackSession);

                // Assert
                result.ReturnValue.ShouldEqual("Hello");
            }
            public void ShouldNotReturnCompileExceptionIfCodeDoesCompile(
                [Frozen] Mock <IScriptHostFactory> scriptHostFactory,
                [NoAutoProperties] MonoScriptEngine engine,
                ScriptPackSession scriptPackSession)
            {
                // Arrange
                const string Code = "var theNumber = 42; //this should compile";

                scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()))
                .Returns <IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));

                var session = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;
                var refs = new AssemblyReferences(new[] { "System" });

                // Act
                var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty <string>(), scriptPackSession);

                // Assert
                result.ShouldBeType <ScriptResult>();
                result.CompileExceptionInfo.ShouldBeNull();
                result.ExecuteExceptionInfo.ShouldBeNull();
            }
            public void ShouldReturnExecuteExceptionIfCodeExecutionThrowsException(
                [Frozen] Mock <IScriptHostFactory> scriptHostFactory,
                [NoAutoProperties] MonoScriptEngine engine,
                ScriptPackSession scriptPackSession)
            {
                // Arrange
                const string Code = "throw new System.Exception(); //this should throw an Exception";

                scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()))
                .Returns <IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q)));

                var session = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;
                var refs = new AssemblyReferences();

                refs.PathReferences.Add("System");

                // Act
                var result = engine.Execute(Code, new string[0], refs, Enumerable.Empty <string>(), scriptPackSession);

                // Assert
                result.ExecuteExceptionInfo.ShouldNotBeNull();
            }
            public void ShouldReturnAScriptResult(
                [Frozen] Mock <IScriptHostFactory> scriptHostFactory,
                [NoAutoProperties] MonoScriptEngine engine,
                ScriptPackSession scriptPackSession)
            {
                // Arrange
                var code = string.Empty;

                scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()))
                .Returns <IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q)));

                var session = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;
                var refs = new AssemblyReferences();

                refs.PathReferences.Add("System");

                // Act
                var result = engine.Execute(code, new string[0], refs, Enumerable.Empty <string>(), scriptPackSession);

                // Assert
                result.ShouldBeType <ScriptResult>();
            }
            public void ShouldReturn0VariablesAfterReset([NoAutoProperties] MonoScriptEngine engine, ScriptPackSession scriptPackSession)
            {
                var session = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                scriptPackSession.State[MonoScriptEngine.SessionKey] = session;

                engine.Execute("int x = 1;", new string[0], new AssemblyReferences(), Enumerable.Empty <string>(),
                               scriptPackSession);
                engine.Execute(@"var y = ""www"";", new string[0], new AssemblyReferences(), Enumerable.Empty <string>(),
                               scriptPackSession);

                scriptPackSession.State[MonoScriptEngine.SessionKey] = new SessionState <Evaluator> {
                    Session = new Evaluator(new CompilerContext(new CompilerSettings(), new ConsoleReportPrinter()))
                };

                engine.GetLocalVariables(scriptPackSession).ShouldBeEmpty();
            }
            public void ShouldCreateScriptHostWithContexts(
                [Frozen] Mock <IScriptHostFactory> scriptHostFactory,
                [Frozen] Mock <IScriptPack> scriptPack,
                ScriptPackSession scriptPackSession,
                [NoAutoProperties] MonoScriptEngine engine)
            {
                // Arrange
                const string Code = "var a = 0;";

                scriptHostFactory.Setup(f => f.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()))
                .Returns <IScriptPackManager, string[]>((p, q) => new ScriptHost(p, new ScriptEnvironment(q, _console, _printers)));

                scriptPack.Setup(p => p.Initialize(It.IsAny <IScriptPackSession>()));
                scriptPack.Setup(p => p.GetContext()).Returns((IScriptPackContext)null);

                // Act
                engine.Execute(Code, new string[0], new AssemblyReferences(), Enumerable.Empty <string>(), scriptPackSession);

                // Assert
                scriptHostFactory.Verify(f => f.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()));
            }
            public void ShouldInitializeScriptLibraryWrapperHost(
                [Frozen] Mock <IScriptHostFactory> scriptHostFactory,
                Mock <IScriptPackManager> manager,
                [NoAutoProperties] MonoScriptEngine engine,
                ScriptPackSession scriptPackSession
                )
            {
                // Arrange
                const string Code = "var theNumber = 42; //this should compile";

                var refs = new AssemblyReferences(new[] { "System" });

                scriptHostFactory.Setup(s => s.CreateScriptHost(It.IsAny <IScriptPackManager>(), It.IsAny <string[]>()))
                .Returns(new ScriptHost(manager.Object, null));

                // Act
                engine.Execute(Code, new string[0], refs, Enumerable.Empty <string>(), scriptPackSession);

                // Assert
                ScriptLibraryWrapper.ScriptHost.ShouldNotEqual(null);
            }