-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Проверка инициализации переменной +semver:feature (#98)
* fix * Контракт exit codes * изменение области видимости * #84 - подготовил интеграционники для скриптов с семантическими ошибками * #84 - реализация фичи * #84 - подправил тесты
- Loading branch information
Showing
16 changed files
with
137 additions
and
23 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
...plication/HydraScript.Application.StaticAnalysis/Exceptions/AccessBeforeInitialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using HydraScript.Domain.FrontEnd.Parser.Impl.Ast.Nodes.Expressions.PrimaryExpressions; | ||
|
||
namespace HydraScript.Application.StaticAnalysis.Exceptions; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class AccessBeforeInitialization(IdentifierReference variable) : SemanticException( | ||
variable.Segment, | ||
$"Cannot access '{variable.Name}' before initialization"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ public interface ISymbol | |
{ | ||
public string Id { get; } | ||
public Type Type { get; } | ||
public bool Initialized { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace HydraScript; | ||
|
||
internal static class ExitCodes | ||
{ | ||
public const int Success = 0; | ||
|
||
public const int HydraScriptError = 1; | ||
|
||
public const int DotnetRuntimeError = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/HydraScript.IntegrationTests/ErrorPrograms/VariableInitializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.CommandLine.Parsing; | ||
using FluentAssertions; | ||
using Xunit.Abstractions; | ||
|
||
namespace HydraScript.IntegrationTests.ErrorPrograms; | ||
|
||
public class VariableInitializationTests( | ||
TestHostFixture fixture, | ||
ITestOutputHelper testOutputHelper) : IClassFixture<TestHostFixture>, IDisposable | ||
{ | ||
private readonly StringWriter _writer = new(); | ||
|
||
[Fact] | ||
public void VariableWithoutTypeDeclared_AccessedBeforeInitialization_ExitCodeHydraScriptError() | ||
{ | ||
const string script = | ||
""" | ||
let x = f() | ||
function f() { | ||
print(x as string) | ||
return 5 | ||
} | ||
"""; | ||
var runner = fixture.GetRunner( | ||
testOutputHelper, | ||
_writer, | ||
configureTestServices: services => services.SetupInMemoryScript(script)); | ||
var code = runner.Invoke(fixture.InMemoryScript); | ||
code.Should().Be(ExitCodes.HydraScriptError); | ||
var output = _writer.ToString().Trim(); | ||
output.Should().Be("(3, 11)-(3, 12) Cannot access 'x' before initialization"); | ||
} | ||
|
||
[Fact] | ||
public void TypedVariableDeclared_AccessedBeforeInitialization_ExitCodeHydraScriptError() | ||
{ | ||
const string script = | ||
""" | ||
let x: number = f() | ||
function f() { | ||
print(x as string) | ||
return 5 | ||
} | ||
"""; | ||
var runner = fixture.GetRunner( | ||
testOutputHelper, | ||
_writer, | ||
configureTestServices: services => services.SetupInMemoryScript(script)); | ||
var code = runner.Invoke(fixture.InMemoryScript); | ||
code.Should().Be(ExitCodes.HydraScriptError); | ||
var output = _writer.ToString().Trim(); | ||
output.Should().Be("(3, 11)-(3, 12) Cannot access 'x' before initialization"); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_writer.Dispose(); | ||
fixture.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/HydraScript.IntegrationTests/ServiceCollectionTestExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.IO.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NSubstitute; | ||
|
||
namespace HydraScript.IntegrationTests; | ||
|
||
internal static class ServiceCollectionTestExtensions | ||
{ | ||
internal static void SetupInMemoryScript(this IServiceCollection services, string script) | ||
{ | ||
var fileSystem = Substitute.For<IFileSystem>(); | ||
var file = Substitute.For<IFile>(); | ||
file.ReadAllText(default!).ReturnsForAnyArgs(script); | ||
fileSystem.File.ReturnsForAnyArgs(file); | ||
services.AddSingleton(fileSystem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters