Skip to content

Commit 5012eea

Browse files
allow options to be overriden
1 parent 373a597 commit 5012eea

2 files changed

Lines changed: 12 additions & 20 deletions

File tree

src/Testing.SourceGenerators/GeneratorTestContextBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ public GeneratorTestContextBuilder AddMarkup(string name, CodeMarkup source)
643643
{
644644
return this with
645645
{
646-
_markedLocations = _markedLocations.Add(name, new(source.Location, source.Trigger)),
646+
_markedLocations = _markedLocations.SetItem(name, new(source.Location, source.Trigger)),
647647
_sources = _sources.Add(new(SourceText.From(source.Code, Encoding.UTF8)) { Name = name, }),
648648
};
649649
}
@@ -726,14 +726,14 @@ public GeneratorTestContextBuilder AddAdditionalTexts(params IEnumerable<Additio
726726
public GeneratorTestContextBuilder AddOption(string path, string key, string value)
727727
{
728728
var rootOptions = _options;
729-
if (rootOptions.TryGetValue(path, out var fileOptions))
729+
if (_options.TryGetValue(path, out var fileOptions))
730730
{
731-
fileOptions = fileOptions.Add(key, value);
731+
fileOptions = fileOptions.SetItem(key, value);
732732
rootOptions = rootOptions.SetItem(path, fileOptions);
733733
}
734734
else
735735
{
736-
rootOptions = rootOptions.SetItem(path, ImmutableDictionary<string, string>.Empty.Add(key, value));
736+
rootOptions = rootOptions.SetItem(path, ImmutableDictionary<string, string>.Empty.SetItem(key, value));
737737
}
738738

739739
return this with { _options = rootOptions, };
@@ -745,7 +745,7 @@ public GeneratorTestContextBuilder AddOption(string path, string key, string val
745745
/// <param name="key"></param>
746746
/// <param name="value"></param>
747747
/// <returns></returns>
748-
public GeneratorTestContextBuilder AddGlobalOption(string key, string value) => this with { _globalOptions = _globalOptions.Add(key, value), };
748+
public GeneratorTestContextBuilder AddGlobalOption(string key, string value) => this with { _globalOptions = _globalOptions.SetItem(key, value), };
749749

750750
/// <summary>
751751
/// Create the <see cref="GeneratorTestContext" /> from the builder
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Immutable;
1+
using System.Collections.Immutable;
22
using Microsoft.CodeAnalysis;
33
using Microsoft.CodeAnalysis.Diagnostics;
44

@@ -17,7 +17,7 @@ public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
1717
if (!options.TryGetValue(tree.FilePath, out var value)) return GlobalOptions;
1818
foreach (var v in globalOptions.Where(v => !value.ContainsKey(v.Key)))
1919
{
20-
value = value.Add(v.Key, v.Value);
20+
value = value.SetItem(v.Key, v.Value);
2121
}
2222

2323
return new OptionsObject(value);
@@ -28,24 +28,16 @@ public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
2828
if (!options.TryGetValue(textFile.Path, out var value)) return GlobalOptions;
2929
foreach (var v in globalOptions.Where(v => !value.ContainsKey(v.Key)))
3030
{
31-
value = value.Add(v.Key, v.Value);
31+
value = value.SetItem(v.Key, v.Value);
3232
}
3333

3434
return new OptionsObject(value);
3535
}
3636

37-
private sealed class OptionsObject : AnalyzerConfigOptions
37+
private sealed class OptionsObject(ImmutableDictionary<string, string> properties) : AnalyzerConfigOptions
3838
{
39-
private readonly ImmutableDictionary<string, string> _properties;
39+
private readonly ImmutableDictionary<string, string> _properties = properties;
4040

41-
public OptionsObject(ImmutableDictionary<string, string> properties)
42-
{
43-
_properties = properties;
44-
}
45-
46-
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value)
47-
{
48-
return _properties.TryGetValue(key, out value);
49-
}
41+
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) => _properties.TryGetValue(key, out value);
5042
}
51-
}
43+
}

0 commit comments

Comments
 (0)