コード例 #1
0
        /// <summary>
        /// Provides a generic overload for engine.Evaluate.
        /// This method will invoke Convert.ChangeType on the returned value to try convert it to type T.
        /// This provides a more easier way to get the value of a type you want.
        /// </summary>
        /// <typeparam name="T">The Type to convert the returned value of Evaluate to.</typeparam>
        /// <param name="engine">The ActiveScriptEngine to invoke Evaluate on.</param>
        /// <param name="code">The code to evaluate inside engine.</param>
        /// <returns>The value returned after the evaluation as type T.</returns>
        public static T Evaluate <T>(this ActiveScriptEngine engine, string code)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }

            return((T)Convert.ChangeType(engine.Evaluate(code), typeof(T), CultureInfo.InvariantCulture));
        }
コード例 #2
0
        public void When_GenericEvaluate_is_called_with_a_null_engine()
        {
            // Arrange.
            ActiveScriptEngine engine = null;

            // Act.
            Action action = () => engine.Evaluate <object>("GetValue");

            // Assert.
            action.ShouldThrow <ArgumentNullException>();
        }
コード例 #3
0
        public void When_NewInstanceOf_is_called_with_a_null_engine()
        {
            // Arrange.
            ActiveScriptEngine engine = null;

            // Act.
            Action action = () => engine.NewInstanceOf("Person");

            // Assert.
            action.ShouldThrow <ArgumentNullException>();
        }
コード例 #4
0
        public static void OverrideGlobalFunction <T1, T2, T3, T4, T5, T6>(this ActiveScriptEngine engine, string functionName, Func <T1, T2, T3, T4, T5, T6, object> func)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }

            var overrider = new FunctionOverrider();

            overrider.WhenCalled(func);
            engine.AddObject(functionName, overrider);
        }
コード例 #5
0
        public static void OverrideGlobalFunction(this ActiveScriptEngine engine, string functionName, Action <object[]> action)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }

            var overrider = new FunctionOverrider();

            overrider.WhenCalled(action);
            engine.AddObject(functionName, overrider);
        }
コード例 #6
0
        public void When_ActiveScriptEngine_is_created_from_a_ActiveScript_instance_ProgId_should_be_null()
        {
            Type type = Type.GetTypeFromProgID("VBScript", true);

            object activeScriptInstance = Activator.CreateInstance(type);

            // Act.
            ActiveScriptEngine engine = new ActiveScriptEngine(activeScriptInstance);

            // Assert.
            engine.ProgId.Should().Be(null);
        }
コード例 #7
0
        public void GlobalTestSetup()
        {
            Trace.WriteLine("Environment is " + (Environment.Is64BitProcess ? "64bit" : "32bit"));

            this.WScript      = new SimpleHostObject();
            this.scriptEngine = new ActiveScriptEngine(progID);
            this.scriptEngine.ScriptErrorOccurred += scriptEngine_ScriptErrorOccurred;
            this.scriptEngine.AddObject("WScript", this.WScript);
            this.script = this.scriptEngine.GetScriptHandle();

            this.expectedError = null;
        }
コード例 #8
0
        public void When_ActiveScriptEngine_is_passed_a_valid_ActiveScript_instance()
        {
            // Arrange.
            Type type = Type.GetTypeFromProgID("VBScript", true);

            object activeScriptInstance = Activator.CreateInstance(type);

            // Act.
            ActiveScriptEngine engine = new ActiveScriptEngine(activeScriptInstance);

            // Assert.
            engine.Should().NotBeNull();
        }
コード例 #9
0
        protected void scriptEngine_ScriptErrorOccurred(ActiveScriptEngine sender, ScriptErrorInfo error)
        {
            Trace.WriteLine(error.DebugDump());

            if (expectedError == null)
            {
                Assert.Fail("Script threw an unexpected error");
            }
            else
            {
                CheckErrors(expectedError, error);
            }
        }
コード例 #10
0
        /// <summary>
        /// Creates a new instance of the specified class from within the script engine and returns it.
        /// </summary>
        /// <param name="engine">The ActiveScriptEngine to create the instance from.</param>
        /// <param name="className">The name of the class to create an instance of.</param>
        /// <returns>The created instance.</returns>
        public static dynamic NewInstanceOf(this ActiveScriptEngine engine, string className)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }

            if (engine.ProgId.Equals(VBScript.ProgId, StringComparison.OrdinalIgnoreCase))
            {
                return(engine.Evaluate("New " + className));
            }

            return(engine.Evaluate("new " + className + "()"));
        }
コード例 #11
0
        public void When_ActiveScriptEngine_is_connected_it_IsRunning()
        {
            // Arrange.
            ActiveScriptEngine engine = new ActiveScriptEngine(VBScript.ProgId);

            bool isRunningBeforeStart = engine.IsRunning;

            // Act.
            engine.Start();

            // Assert.
            isRunningBeforeStart.Should().BeFalse();
            engine.IsRunning.Should().BeTrue();
        }
コード例 #12
0
        public static void OverrideGlobalFunction <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(this ActiveScriptEngine engine, string functionName, Action <T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> action)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }

            var overrider = new FunctionOverrider();

            overrider.WhenCalled(action);
            engine.AddObject(functionName, overrider);
        }