public virtual void SettingAndGettingVariableWithUndefinedValueIsCorrect()
        {
            // Arrange
            const string variableName = "myVar1";
            object       input        = Undefined.Value;

            // Act
            _jsEngine.SetVariableValue(variableName, input);
            bool variableExists = _jsEngine.HasVariable(variableName);
            var  output         = _jsEngine.GetVariableValue(variableName);

            // Assert
            Assert.IsFalse(variableExists);
            Assert.AreEqual(input, output);
        }
Beispiel #2
0
        public void SettingVariableIsCorrect()
        {
            // Arrange
            _msieJsEngine.SetVariableValue("price", 120.55m);

            // Act
            bool priceExists = _msieJsEngine.HasVariable("price");
            var  price       = _msieJsEngine.GetVariableValue <double>("price");

            // Assert
            Assert.IsTrue(priceExists);
            Assert.AreEqual(120.55m, price);
        }
Beispiel #3
0
        /// <summary>
        /// Eval JavaScript code.
        /// </summary>
        /// <param name="code">JavaScript code.</param>
        /// <param name="variableName">Variable name.</param>
        /// <returns>JavaScript code return result.</returns>
        public static object EvaluateJavaScriptCode(string code, string variableName)
        {
            try
            {
                using var engine = new MsieJsEngine();
                engine.Execute(code);

                return(engine.GetVariableValue(variableName));
            }
            catch (Exception exception)
            {
                return(exception.Message);
            }
        }
Beispiel #4
0
        private static string MinimizeWithUglifyJS(ref MsieJsEngine jsEngine, string code)
        {
            jsEngine = jsEngine ?? SetupJsEngine();
            jsEngine.SetVariableValue("CodeToCompress", code);

            jsEngine.Evaluate(
                @"(function() { 
                    var ast = UglifyJS.parse(CodeToCompress);
                    ast.figure_out_scope();
                    var compressor = UglifyJS.Compressor();
                    ast = ast.transform(compressor);
                    ast.figure_out_scope();
                    ast.compute_char_frequency();
                    ast.mangle_names();
                    CodeToCompress = ast.print_to_string();
                })();");

            return(jsEngine.GetVariableValue <string>("CodeToCompress"));
        }
        public virtual void ArrayForEachMethodIsSupported()
        {
            // Arrange
            const string resultVariableName = "enginesString";
            string       initCode           = string.Format(@"var engines = ['Chakra', 'V8', 'SpiderMonkey', 'Jurassic'],
	{0} = ''
	;

engines.forEach(function(value, index, array) {{
	if (index > 0) {{
		{0} += ';';
	}}
	{0} += value;
}});", resultVariableName);
            const string targetOutput       = "Chakra;V8;SpiderMonkey;Jurassic";

            // Act
            _jsEngine.Execute(initCode);
            var output = _jsEngine.GetVariableValue <string>(resultVariableName);

            // Assert
            Assert.AreEqual(targetOutput, output);
        }
        private static string MinimizeWithUglifyJS(ref MsieJsEngine jsEngine, string code)
        {
            jsEngine = jsEngine ?? SetupJsEngine();
            jsEngine.SetVariableValue("CodeToCompress", code);

            jsEngine.Evaluate(
                @"(function() {
                    var ast = UglifyJS.parse(CodeToCompress);
                    ast.figure_out_scope();
                    var compressor = UglifyJS.Compressor();
                    ast = ast.transform(compressor);
                    ast.figure_out_scope();
                    ast.compute_char_frequency();
                    ast.mangle_names();
                    CodeToCompress = ast.print_to_string();
                })();");

            return jsEngine.GetVariableValue<string>("CodeToCompress");
        }