Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw when variable can't be resolved #382

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions commonItems.UnitTests/BufferedReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,25 @@ public void EvaluateExpressionLogsErrorWhenInterpolatedExpressionCannotBeEvaluat
Assert.Contains("[WARN] Failed to evaluate expression \"@[@a-2]\"", output.ToString());
Assert.Equal(expressionStr, value);
}

[Fact]
public void VariableIsResolvedWhenItExists() {
var reader = new BufferedReader();
reader.Variables.Add("a", 3);
var value = reader.ResolveVariable("@a");

Assert.Equal(3, value);
}

[Fact]
public void WarningIsLoggedAndNullReturnedWhenVariableCannotBeResolved() {
var output = new StringWriter();
Console.SetOut(output);

var reader = new BufferedReader();
var value = reader.ResolveVariable("@a");

Assert.Contains("[WARN] Variable not found: @a", output.ToString());
Assert.Null(value);
}
}
12 changes: 9 additions & 3 deletions commonItems/BufferedReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public List<string> GetStrings() {
);
if (Variables.Count > 0) {
parser.RegisterRegex(CommonRegexes.Variable, (reader, varStr) => {
var value = reader.ResolveVariable(varStr).ToString();
var value = reader.ResolveVariable(varStr)?.ToString();
if (value is null) {
Logger.Warn($"StringList: variable {varStr} resolved to null value!");
} else {
Expand Down Expand Up @@ -278,8 +278,14 @@ public Dictionary<string, string> GetAssignments() {

public Dictionary<string, object> Variables { get; } = new();

public object ResolveVariable(string lexeme) {
return Variables[lexeme[1..]];
public object? ResolveVariable(string lexeme) {
var key = lexeme[1..];
if (Variables.TryGetValue(key, out var value)) {
return value;
}

Logger.Warn($"Variable not found: {lexeme}");
return null;
}

public object EvaluateExpression(string lexeme) {
Expand Down
2 changes: 1 addition & 1 deletion commonItems/commonItems.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<PackageId>PGCG.$(AssemblyName)</PackageId>
<Version>9.1.1</Version>
<Version>9.2.0</Version>
<Authors>PGCG</Authors>
<PackageProjectUrl>https://github.com/ParadoxGameConverters/commonItems.NET</PackageProjectUrl>
<RepositoryUrl>https://github.com/ParadoxGameConverters/commonItems.NET</RepositoryUrl>
Expand Down
Loading