public void MappingCompilationErrorDuringEvaluationOfExpressionIsCorrect()
        {
            // Arrange
            const string input = @"var $variable1 = 611;
var _variable2 = 711;
var @variable3 = 678;

$variable1 + _variable2 - @variable3;";

            JsCompilationException exception = null;

            // Act
            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    int result = jsEngine.Evaluate <int>(input, "variables.js");
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Compilation error", exception.Category);
            Assert.Equal("Unexpected character '@'.", exception.Description);
            Assert.Equal("SyntaxError", exception.Type);
            Assert.Equal("variables.js", exception.DocumentName);
            Assert.Equal(3, exception.LineNumber);
            Assert.Equal(0, exception.ColumnNumber);
            Assert.Empty(exception.SourceFragment);
        }
        public void GenerationOfCompilationErrorMessageIsCorrect()
        {
            // Arrange
            const string input        = @"var arr = [];
var obj = {};
var foo = 'Browser's bar';";
            string       targetOutput = "SyntaxError: Expected operator but found 's'" + Environment.NewLine +
                                        "   at variables.js:3"
            ;

            JsCompilationException exception = null;

            // Act
            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    jsEngine.Execute(input, "variables.js");
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            Assert.NotNull(exception);
            Assert.Equal(targetOutput, exception.Message);
        }
Ejemplo n.º 3
0
        public void MappingCompilationErrorDuringPrecompilationOfCodeIsCorrect()
        {
            // Arrange
            const string input = @"function guid() {
	function s4() {
		return Math.floor((1 + Math.random() * 0x10000)
			.toString(16)
			.substring(1)
			;
	}

	var result = s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();

	return result;
}";

            IPrecompiledScript     precompiledScript = null;
            JsCompilationException exception         = null;

            // Act
            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    precompiledScript = jsEngine.Precompile(input, "guid.js");
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.Null(precompiledScript);
            Assert.NotNull(exception);
            Assert.Equal("Compilation error", exception.Category);
            Assert.Equal("Expected ')'", exception.Description);
            Assert.Equal("SyntaxError", exception.Type);
            Assert.Equal("guid.js", exception.DocumentName);
            Assert.Equal(6, exception.LineNumber);
            Assert.Equal(4, exception.ColumnNumber);
            Assert.Equal("			;", exception.SourceFragment);
        }
        public void MappingCompilationErrorDuringExecutionOfCodeIsCorrect()
        {
            // Arrange
            const string input = @"function factorial(value) {
	if (value <= 0) {
		throw new Error(""The value must be greater than or equal to zero."");
	}

	return value !== 1 ? value * factorial(value - 1) : 1;
}

factorial(5);
factorial(2%);
factorial(0);";

            JsCompilationException exception = null;

            // Act
            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    jsEngine.Execute(input, "factorial.js");
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Compilation error", exception.Category);
            Assert.Equal("Wrong number of operands", exception.Description);
            Assert.Equal("SyntaxError", exception.Type);
            Assert.Equal("factorial.js", exception.DocumentName);
            Assert.Equal(10, exception.LineNumber);
            Assert.Equal(0, exception.ColumnNumber);
            Assert.Empty(exception.SourceFragment);
        }
        public void MappingCompilationErrorDuringRecursiveEvaluationOfFilesIsCorrect()
        {
            // Arrange
            const string directoryPath = "Files/recursive-evaluation/compilation-error";
            const string input         = "evaluateFile('index').calculateResult();";

            // Act
            JsCompilationException exception = null;

            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    Func <string, object> evaluateFile = path => {
                        string absolutePath = Path.Combine(directoryPath, $"{path}.js");
                        string code         = File.ReadAllText(absolutePath);
                        object result       = jsEngine.Evaluate(code, absolutePath);

                        return(result);
                    };

                    jsEngine.EmbedHostObject("evaluateFile", evaluateFile);
                    double output = jsEngine.Evaluate <double>(input);
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Compilation error", exception.Category);
            Assert.Equal("Unexpected token ,", exception.Description);
            Assert.Equal("SyntaxError", exception.Type);
            Assert.Equal("math.js", exception.DocumentName);
            Assert.Equal(25, exception.LineNumber);
            Assert.Equal(11, exception.ColumnNumber);
            Assert.Empty(exception.SourceFragment);
        }
Ejemplo n.º 6
0
        public void GenerationOfCompilationErrorMessageIsCorrect()
        {
            // Arrange
            const string input        = @"function makeId(length) {
	var result = '',
		possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
		charIndex
		;

	for (charIndex = 0; charIndex < length; charIndex++)
		result += possible.charAt(Math.floor(Math.random() * possible.length));
	}

	return result;
}";
            string       targetOutput = "SyntaxError: 'return' statement outside of function" + Environment.NewLine +
                                        "   at make-id.js:11:2 -> 	return result;"
            ;

            IPrecompiledScript     precompiledScript = null;
            JsCompilationException exception         = null;

            // Act
            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    precompiledScript = jsEngine.Precompile(input, "make-id.js");
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            Assert.Null(precompiledScript);
            Assert.NotNull(exception);
            Assert.Equal(targetOutput, exception.Message);
        }
        public void MappingCompilationErrorDuringRecursiveExecutionOfFilesIsCorrect()
        {
            // Arrange
            const string directoryPath = "Files/recursive-execution/compilation-error";
            const string variableName  = "num";

            // Act
            JsCompilationException exception = null;

            using (var jsEngine = CreateJsEngine())
            {
                try
                {
                    Action <string> executeFile = path => jsEngine.ExecuteFile(path);

                    jsEngine.SetVariableValue("directoryPath", directoryPath);
                    jsEngine.EmbedHostObject("executeFile", executeFile);
                    jsEngine.ExecuteFile(Path.Combine(directoryPath, "main-file.js"));

                    int output = jsEngine.GetVariableValue <int>(variableName);
                }
                catch (JsCompilationException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Compilation error", exception.Category);
            Assert.Equal("Unexpected token ILLEGAL", exception.Description);
            Assert.Equal("SyntaxError", exception.Type);
            Assert.Equal("second-file.js", exception.DocumentName);
            Assert.Equal(1, exception.LineNumber);
            Assert.Equal(6, exception.ColumnNumber);
            Assert.Empty(exception.SourceFragment);
        }