Skip to content

Commit

Permalink
Adapt tests to new design
Browse files Browse the repository at this point in the history
  • Loading branch information
Elscrux committed Nov 21, 2024
1 parent 5fd9d1f commit df2ce93
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
using Mutagen.Bethesda.Analyzers.Config.Analyzer;
using System.Diagnostics.CodeAnalysis;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Mutagen.Bethesda.Analyzers.Config;
using Mutagen.Bethesda.Analyzers.Config.Analyzer;
using Mutagen.Bethesda.FormKeys.SkyrimSE;
using Mutagen.Bethesda.Plugins;
using Noggog;
using NSubstitute;
using Xunit;

namespace Mutagen.Bethesda.Analyzers.Tests.Config;

[method: SuppressMessage("ReSharper", "ContextualLoggerProblem", Justification = "Passed in")]
public class AnalyzerConfigReader(
ILogger<ConfigReader<IAnalyzerConfig>> logger,
ProcessDataDirectoryPath p1,
ProcessGameRelease p2,
ProcessLoadOrderSetByDataDirectory p3,
ProcessLoadOrderSetToMods p4,
ProcessOutputFilePath p5)
{
public readonly ConfigReader<IAnalyzerConfig> Reader = new(logger, [p1, p2, p3, p4, p5]);
}

public class AnalyzerConfigReaderTests
{
[Theory]
[AnalyzerInlineData("environment.data_directory = C:/some/path")]
[AnalyzerInlineData(@"environment.data_directory = C:\some\path")]
public void TestDataDirectory(
string line,
IAnalyzerConfig config,
AnalyzerConfig config,
AnalyzerConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
config.Received(1).OverrideDataDirectory("C:/some/path");
sut.Reader.ReadInto(line.AsSpan(), config);
config.DataDirectoryPath.Should().NotBeNull();
config.DataDirectoryPath!.Value.Path.Should().Be(@"C:\some\path");
}

[Theory]
Expand All @@ -29,20 +44,19 @@ public void TestSetByDataDirectory(
IAnalyzerConfig config,
AnalyzerConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
sut.Reader.ReadInto(line.AsSpan(), config);
config.Received(1).OverrideLoadOrderSetByDataDirectory(Arg.Any<bool>());
}

[Theory]
[AnalyzerInlineData("environment.load_order.set_to_mods = Skyrim.esm, Update.esm")]
public void TestSetToMods(
string line,
IAnalyzerConfig config,
AnalyzerConfig config,
AnalyzerConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
List<ModKey> modKeys = [FormKeys.SkyrimSE.Skyrim.ModKey, Update.ModKey];
config.Received(1).OverrideLoadOrderSetToMods(Arg.Is<List<ModKey>>(list => list.SequenceEqual(modKeys)));
sut.Reader.ReadInto(line.AsSpan(), config);
config.LoadOrderSetToMods.Should().BeEquivalentTo([FormKeys.SkyrimSE.Skyrim.ModKey, Update.ModKey]);
}

[Theory]
Expand All @@ -54,7 +68,7 @@ public void TestGameRelease(
IAnalyzerConfig config,
AnalyzerConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
sut.Reader.ReadInto(line.AsSpan(), config);
config.Received(1).OverrideGameRelease(Arg.Any<GameRelease>());
}

Expand All @@ -63,10 +77,11 @@ public void TestGameRelease(
[AnalyzerInlineData(@"output_file = C:\some\path")]
public void TestOutputFilePath(
string line,
IAnalyzerConfig config,
AnalyzerConfig config,
AnalyzerConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
config.Received(1).OverrideOutputFilePath(Arg.Any<FilePath>());
sut.Reader.ReadInto(line.AsSpan(), config);
config.OutputFilePath.Should().NotBeNull();
config.OutputFilePath!.Value.Path.Should().Be(@"C:\some\path");
}
}
160 changes: 81 additions & 79 deletions Mutagen.Bethesda.Analyzers.Tests/Config/ConfigReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
using FluentAssertions;
using System.Diagnostics.CodeAnalysis;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Mutagen.Bethesda.Analyzers.Config;
using Xunit;

namespace Mutagen.Bethesda.Analyzers.Tests.Config;

public record TestConfig;

public class FuncProcessor : IConfigReaderProcessor<TestConfig>
{
public Func<TestConfig, IReadOnlyList<string>, string, bool> Func { get; set; } = (_, _, _) => true;

public bool Process(TestConfig config, IReadOnlyList<string> instructionParts, string value)
{
return Func(config, instructionParts, value);
}
}

[method: SuppressMessage("ReSharper", "ContextualLoggerProblem", Justification = "Passed in")]
public class TestConfigReader(ILogger<ConfigReader<TestConfig>> logger, FuncProcessor processor)
{
public FuncProcessor Processor { get; } = processor;
public readonly IConfigReader<TestConfig> Reader = new ConfigReader<TestConfig>(logger, [processor]);
}

public class ConfigReaderTests
{
[Theory]
[AnalyzerInlineData("other.A123.severity = Warning")]
public void TestThreePartAndValue(
string line,
ConfigReader<TestConfig> sut)
TestConfigReader sut)
{
var ran = false;

sut.Register((config, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("other");
parts[1].Should().Be("A123");
parts[2].Should().Be("severity");
value.Should().Be("Warning");
return true;
})
.ReadInto(line.AsSpan(), new TestConfig());
sut.Processor.Func = (_, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("other");
parts[1].Should().Be("A123");
parts[2].Should().Be("severity");
value.Should().Be("Warning");
return true;
};

sut.Reader.ReadInto(line.AsSpan(), new TestConfig());

ran.Should().BeTrue();
}
Expand All @@ -35,108 +55,90 @@ public void TestThreePartAndValue(
[AnalyzerInlineData("diagnostic.A123.severity = Warning And Gibberish")]
public void TestThreePartAndValueWithSpaces(
string line,
ConfigReader<TestConfig> sut)
TestConfigReader sut)
{
var ran = false;

sut.Register((config, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("diagnostic");
parts[1].Should().Be("A123");
parts[2].Should().Be("severity");
value.Should().Be("Warning And Gibberish");
return true;
})
.ReadInto(line.AsSpan(), new TestConfig());
sut.Processor.Func = (_, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("diagnostic");
parts[1].Should().Be("A123");
parts[2].Should().Be("severity");
value.Should().Be("Warning And Gibberish");
return true;
};

sut.Reader.ReadInto(line.AsSpan(), new TestConfig());

ran.Should().BeTrue();
}


[Theory]
[AnalyzerInlineData("diagnostic.A123")]
public void TestSkipNoValue(
string line,
ConfigReader<TestConfig> sut)
TestConfigReader sut)
{
var ran = false;

sut.Register((config, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("diagnostic");
parts[1].Should().Be("A123");
value.Should().Be("");
return true;
})
.ReadInto(line.AsSpan(), new TestConfig());
sut.Processor.Func = (_, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("diagnostic");
parts[1].Should().Be("A123");
value.Should().Be("");
return true;
};

ran.Should().BeFalse();
}


[Theory]
[AnalyzerInlineData("")]
public void TestSkipEmptyLine(
string line,
ConfigReader<TestConfig> sut)
{
var ran = false;

sut.Register((config, parts, value) =>
{
ran = true;
parts.Should().HaveCount(0);
return true;
})
.ReadInto(line.AsSpan(), new TestConfig());
sut.Reader.ReadInto(line.AsSpan(), new TestConfig());

ran.Should().BeFalse();
}


[Theory]
[AnalyzerInlineData("diagnostic.A123.severity = Other #test")]
public void TestComment(
string line,
ConfigReader<TestConfig> sut)
TestConfigReader sut)
{
var ran = false;

sut.Register((config, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("diagnostic");
parts[1].Should().Be("A123");
parts[2].Should().Be("severity");
value.Should().Be("Other");
return true;
})
.ReadInto(line.AsSpan(), new TestConfig());
sut.Processor.Func = (_, parts, value) =>
{
ran = true;
parts.Should().HaveCount(3);
parts[0].Should().Be("diagnostic");
parts[1].Should().Be("A123");
parts[2].Should().Be("severity");
value.Should().Be("Other");
return true;
};

sut.Reader.ReadInto(line.AsSpan(), new TestConfig());

ran.Should().BeTrue();
}


[Theory]
[AnalyzerInlineData("")]
[AnalyzerInlineData("#diagnostic.A123.severity = Warning")]
public void TestSkipJustComment(
public void TestSkipNoContent(
string line,
ConfigReader<TestConfig> sut)
TestConfigReader sut)
{
var ran = false;

sut.Register((config, parts, value) =>
{
ran = true;
parts.Should().HaveCount(0);
return true;
})
.ReadInto(line.AsSpan(), new TestConfig());
sut.Processor.Func = (_, parts, _) =>
{
ran = true;
parts.Should().HaveCount(0);
return true;
};

sut.Reader.ReadInto(line.AsSpan(), new TestConfig());

ran.Should().BeFalse();
}
Expand Down
29 changes: 21 additions & 8 deletions Mutagen.Bethesda.Analyzers.Tests/Config/TopicConfigReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
using Mutagen.Bethesda.Analyzers.Config.Topic;
using System.Diagnostics.CodeAnalysis;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Mutagen.Bethesda.Analyzers.Config;
using Mutagen.Bethesda.Analyzers.Config.Topic;
using Mutagen.Bethesda.Analyzers.SDK.Topics;
using NSubstitute;
using Xunit;

namespace Mutagen.Bethesda.Analyzers.Tests.Config;

[method: SuppressMessage("ReSharper", "ContextualLoggerProblem", Justification = "Passed in")]
public class TopicConfigReader(
ILogger<ConfigReader<ITopicConfig>> logger,
ProcessSeverity p1)
{
public readonly ConfigReader<ITopicConfig> Reader = new(logger, [p1]);
}

public class TopicConfigReaderTests
{
private readonly TopicDefinition _topicDefinition = new(new TopicId("A", 123), "", Severity.None);

[Theory]
[AnalyzerInlineData("diagnostic.A123.severity = Warning")]
[AnalyzerInlineData("diagnostic.A123.severity = Warning # A Comment")]
public void TestSeverity(
string line,
ITopicConfig config,
TopicConfig config,
TopicConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
config.Received(1).Override(new TopicId("A", 123), Severity.Warning);
sut.Reader.ReadInto(line.AsSpan(), config);
config.LookupSeverity(_topicDefinition).Should().Be(Severity.Warning);
}

[Theory]
Expand All @@ -31,10 +44,10 @@ public void TestSeverity(
[AnalyzerInlineData("#diagnostic.A123.severity = Warning")]
public void AbnormalLinesDontOverride(
string line,
ITopicConfig config,
TopicConfig config,
TopicConfigReader sut)
{
sut.ReadInto(line.AsSpan(), config);
config.DidNotReceiveWithAnyArgs().Override(default!, default);
sut.Reader.ReadInto(line.AsSpan(), config);
config.LookupSeverity(_topicDefinition).Should().Be(Severity.None);
}
}

0 comments on commit df2ce93

Please sign in to comment.