|
| 1 | +/*---------------------------------------------------------- |
| 2 | +This Source Code Form is subject to the terms of the |
| 3 | +Mozilla Public License, v.2.0. If a copy of the MPL |
| 4 | +was not distributed with this file, You can obtain one |
| 5 | +at http://mozilla.org/MPL/2.0/. |
| 6 | +----------------------------------------------------------*/ |
| 7 | + |
| 8 | +using FluentAssertions; |
| 9 | +using OneScript.Compilation; |
| 10 | +using OneScript.Language; |
| 11 | +using OneScript.Language.LexicalAnalysis; |
| 12 | +using OneScript.Language.SyntaxAnalysis; |
| 13 | +using OneScript.Native.Compiler; |
| 14 | +using OneScript.Sources; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace OneScript.Dynamic.Tests |
| 18 | +{ |
| 19 | + public class NativeRuntimeAnnotationHandlerTests |
| 20 | + { |
| 21 | + [Theory] |
| 22 | + [InlineData("#stack\n#native")] |
| 23 | + [InlineData("#native\n#stack")] |
| 24 | + [InlineData("#stack\n#stack")] |
| 25 | + [InlineData("#native\n#native")] |
| 26 | + public void Duplicate_Runtime_Directive_Should_Report_Error(string code) |
| 27 | + { |
| 28 | + var errors = ParseModule(code); |
| 29 | + |
| 30 | + errors.Should().ContainSingle(e => e.ErrorId == "RuntimeDirectiveAlreadyDefined"); |
| 31 | + } |
| 32 | + |
| 33 | + [Theory] |
| 34 | + [InlineData("#native")] |
| 35 | + [InlineData("#stack")] |
| 36 | + public void Single_Runtime_Directive_Should_Parse(string code) |
| 37 | + { |
| 38 | + var errors = ParseModule(code); |
| 39 | + |
| 40 | + errors.Should().BeEmpty(); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void Duplicate_Runtime_Directive_Should_Throw_CompilerException() |
| 45 | + { |
| 46 | + var code = "#stack\n#native"; |
| 47 | + |
| 48 | + var act = () => ParseModuleWithThrowingSink(code); |
| 49 | + |
| 50 | + act.Should().Throw<CompilerException>() |
| 51 | + .Which.LineNumber.Should().Be(2); |
| 52 | + } |
| 53 | + |
| 54 | + private static System.Collections.Generic.IEnumerable<CodeError> ParseModule(string code) |
| 55 | + { |
| 56 | + var errors = new ListErrorSink(); |
| 57 | + Parse(code, errors); |
| 58 | + return errors.Errors; |
| 59 | + } |
| 60 | + |
| 61 | + private static void ParseModuleWithThrowingSink(string code) |
| 62 | + { |
| 63 | + var errors = new ThrowingErrorSink(CompilerException.FromCodeError); |
| 64 | + Parse(code, errors); |
| 65 | + } |
| 66 | + |
| 67 | + private static void Parse(string code, IErrorSink errors) |
| 68 | + { |
| 69 | + var lexer = new DefaultLexer |
| 70 | + { |
| 71 | + Iterator = SourceCodeBuilder.Create() |
| 72 | + .FromString(code) |
| 73 | + .WithName("<text>") |
| 74 | + .Build() |
| 75 | + .CreateIterator() |
| 76 | + }; |
| 77 | + |
| 78 | + var handlers = new PreprocessorHandlers(new[] { new NativeRuntimeAnnotationHandler(errors) }); |
| 79 | + var preprocessingLexer = new PreprocessingLexer(lexer) |
| 80 | + { |
| 81 | + Handlers = handlers, |
| 82 | + ErrorSink = errors |
| 83 | + }; |
| 84 | + |
| 85 | + var parser = new DefaultBslParser(preprocessingLexer, errors, handlers); |
| 86 | + _ = parser.ParseStatefulModule(); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments