diff --git a/.editorconfig b/.editorconfig index d946348231..f4dbfb1d55 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1028,6 +1028,9 @@ dotnet_diagnostic.RCS1112.severity = warning # Use 'string.IsNullOrEmpty' method. dotnet_diagnostic.RCS1113.severity = warning +# Mark local variable as const. +dotnet_diagnostic.RCS1118.severity = warning + # Bitwise operation on enum without Flags attribute. dotnet_diagnostic.RCS1130.severity = warning diff --git a/OpenRA.Game/Graphics/Sprite.cs b/OpenRA.Game/Graphics/Sprite.cs index a5c84675a7..2ddc0e29b8 100644 --- a/OpenRA.Game/Graphics/Sprite.cs +++ b/OpenRA.Game/Graphics/Sprite.cs @@ -42,11 +42,11 @@ namespace OpenRA.Graphics // in rendering a line of texels that sample outside the sprite rectangle. // Insetting the texture coordinates by a small fraction of a pixel avoids this // with negligible impact on the 1:1 rendering case. - var inset = 1 / 128f; - Left = (Math.Min(bounds.Left, bounds.Right) + inset) / sheet.Size.Width; - Top = (Math.Min(bounds.Top, bounds.Bottom) + inset) / sheet.Size.Height; - Right = (Math.Max(bounds.Left, bounds.Right) - inset) / sheet.Size.Width; - Bottom = (Math.Max(bounds.Top, bounds.Bottom) - inset) / sheet.Size.Height; + const float Inset = 1 / 128f; + Left = (Math.Min(bounds.Left, bounds.Right) + Inset) / sheet.Size.Width; + Top = (Math.Min(bounds.Top, bounds.Bottom) + Inset) / sheet.Size.Height; + Right = (Math.Max(bounds.Left, bounds.Right) - Inset) / sheet.Size.Width; + Bottom = (Math.Max(bounds.Top, bounds.Bottom) - Inset) / sheet.Size.Height; } } diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 8839b5ff7e..b35a4a6972 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -685,16 +685,16 @@ namespace OpenRA writer.Write((ushort)MapSize.Y); // Data offsets - var tilesOffset = 17; + const int TilesOffset = 17; var heightsOffset = Grid.MaximumTerrainHeight > 0 ? 3 * MapSize.X * MapSize.Y + 17 : 0; var resourcesOffset = (Grid.MaximumTerrainHeight > 0 ? 4 : 3) * MapSize.X * MapSize.Y + 17; - writer.Write((uint)tilesOffset); + writer.Write((uint)TilesOffset); writer.Write((uint)heightsOffset); writer.Write((uint)resourcesOffset); // Tile data - if (tilesOffset != 0) + if (TilesOffset != 0) { for (var i = 0; i < MapSize.X; i++) { @@ -805,7 +805,7 @@ namespace OpenRA bitmapWidth = 2 * bitmapWidth - 1; var stride = bitmapWidth * 4; - var pxStride = 4; + const int PxStride = 4; var minimapData = new byte[stride * height]; (Color Left, Color Right) terrainColor = default; @@ -827,10 +827,10 @@ namespace OpenRA { // Odd rows are shifted right by 1px var dx = uv.V & 1; - var xOffset = pxStride * (2 * x + dx); + var xOffset = PxStride * (2 * x + dx); if (x + dx > 0) { - var z = y * stride + xOffset - pxStride; + var z = y * stride + xOffset - PxStride; var c = actorColor.A == 0 ? terrainColor.Left : actorColor; minimapData[z++] = c.R; minimapData[z++] = c.G; @@ -850,7 +850,7 @@ namespace OpenRA } else { - var z = y * stride + pxStride * x; + var z = y * stride + PxStride * x; var c = actorColor.A == 0 ? terrainColor.Left : actorColor; minimapData[z++] = c.R; minimapData[z++] = c.G; diff --git a/OpenRA.Game/Map/MapCache.cs b/OpenRA.Game/Map/MapCache.cs index 3b83ce5e58..ca4990abf0 100644 --- a/OpenRA.Game/Map/MapCache.cs +++ b/OpenRA.Game/Map/MapCache.cs @@ -282,11 +282,11 @@ namespace OpenRA Log.Write("debug", "MapCache.LoadAsyncInternal started"); // Milliseconds to wait on one loop when nothing to do - var emptyDelay = 50; + const int EmptyDelay = 50; // Keep the thread alive for at least 5 seconds after the last minimap generation - var maxKeepAlive = 5000 / emptyDelay; - var keepAlive = maxKeepAlive; + const int MaxKeepAlive = 5000 / EmptyDelay; + var keepAlive = MaxKeepAlive; while (true) { @@ -306,11 +306,11 @@ namespace OpenRA if (todo.Count == 0) { - Thread.Sleep(emptyDelay); + Thread.Sleep(EmptyDelay); continue; } else - keepAlive = maxKeepAlive; + keepAlive = MaxKeepAlive; // Render the minimap into the shared sheet foreach (var p in todo) diff --git a/OpenRA.Game/ObjectCreator.cs b/OpenRA.Game/ObjectCreator.cs index 15db6a0c4a..6c4a62bb1b 100644 --- a/OpenRA.Game/ObjectCreator.cs +++ b/OpenRA.Game/ObjectCreator.cs @@ -133,8 +133,8 @@ namespace OpenRA public ConstructorInfo GetCtor(Type type) { - var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; - var ctors = type.GetConstructors(flags).Where(x => x.HasAttribute()).ToList(); + const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; + var ctors = type.GetConstructors(Flags).Where(x => x.HasAttribute()).ToList(); if (ctors.Count > 1) throw new InvalidOperationException("ObjectCreator: UseCtor on multiple constructors; invalid."); return ctors.FirstOrDefault(); diff --git a/OpenRA.Game/Scripting/ScriptMemberWrapper.cs b/OpenRA.Game/Scripting/ScriptMemberWrapper.cs index 41b74ad34c..bd8e1e32f8 100644 --- a/OpenRA.Game/Scripting/ScriptMemberWrapper.cs +++ b/OpenRA.Game/Scripting/ScriptMemberWrapper.cs @@ -125,8 +125,8 @@ namespace OpenRA.Scripting public static IEnumerable WrappableMembers(Type t) { // Only expose defined public non-static methods that were explicitly declared by the author - var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; - foreach (var mi in t.GetMembers(flags)) + const BindingFlags Flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; + foreach (var mi in t.GetMembers(Flags)) { // Properties are always wrappable if (mi is PropertyInfo) diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index c8543cb667..c1c3b90aa9 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -876,13 +876,13 @@ namespace OpenRA.Server public void DispatchServerOrdersToClients(byte[] data, int frame = 0) { - var from = 0; - var frameData = CreateFrame(from, frame, data); + const int From = 0; + var frameData = CreateFrame(From, frame, data); foreach (var c in Conns.ToList()) if (c.Validated) - DispatchFrameToClient(c, from, frameData); + DispatchFrameToClient(c, From, frameData); - RecordOrder(frame, data, from); + RecordOrder(frame, data, From); } public void ReceiveOrders(Connection conn, int frame, byte[] data) diff --git a/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs b/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs index 8d398d144c..09d1ece657 100644 --- a/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs +++ b/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs @@ -70,40 +70,40 @@ namespace OpenRA.Mods.Common.Lint return; // TODO: Check all available languages. - var language = "en"; - var modTranslation = new Translation(language, modData.Manifest.Translations, modData.DefaultFileSystem, _ => { }); - var mapTranslation = new Translation(language, FieldLoader.GetValue("value", map.TranslationDefinitions.Value), map, error => emitError(error.ToString())); + const string Language = "en"; + var modTranslation = new Translation(Language, modData.Manifest.Translations, modData.DefaultFileSystem, _ => { }); + var mapTranslation = new Translation(Language, FieldLoader.GetValue("value", map.TranslationDefinitions.Value), map, error => emitError(error.ToString())); TestTraits(map.Rules, emitError, key => { if (modTranslation.HasMessage(key)) { if (mapTranslation.HasMessage(key)) - emitWarning($"Map translation key `{key}` already exists in `{language}` mod translations and will not be used."); + emitWarning($"Map translation key `{key}` already exists in `{Language}` mod translations and will not be used."); } else if (!mapTranslation.HasMessage(key)) - emitWarning($"`{key}` is not present in `{language}` translation."); + emitWarning($"`{key}` is not present in `{Language}` translation."); }); } void ILintPass.Run(Action emitError, Action emitWarning, ModData modData) { // TODO: Check all available languages. - var language = "en"; - Console.WriteLine($"Testing translation: {language}"); - var translation = new Translation(language, modData.Manifest.Translations, modData.DefaultFileSystem, error => emitError(error.ToString())); + const string Language = "en"; + Console.WriteLine($"Testing translation: {Language}"); + var translation = new Translation(Language, modData.Manifest.Translations, modData.DefaultFileSystem, error => emitError(error.ToString())); TestTraits(modData.DefaultRules, emitError, key => { if (!translation.HasMessage(key)) - emitWarning($"`{key}` is not present in `{language}` translation."); + emitWarning($"`{key}` is not present in `{Language}` translation."); }); var gameSpeeds = modData.Manifest.Get(); foreach (var speed in gameSpeeds.Speeds.Values) { if (!translation.HasMessage(speed.Name)) - emitWarning($"`{speed.Name}` is not present in `{language}` translation."); + emitWarning($"`{speed.Name}` is not present in `{Language}` translation."); referencedKeys.Add(speed.Name); } @@ -126,7 +126,7 @@ namespace OpenRA.Mods.Common.Lint continue; if (!translation.HasMessage(key)) - emitWarning($"`{key}` is not present in `{language}` translation."); + emitWarning($"`{key}` is not present in `{Language}` translation."); var translationReference = Utility.GetCustomAttributes(fieldInfo, true)[0]; if (translationReference.RequiredVariableNames != null && translationReference.RequiredVariableNames.Length > 0) @@ -150,7 +150,7 @@ namespace OpenRA.Mods.Common.Lint { var nodes = MiniYaml.FromStream(modData.DefaultFileSystem.Open(filename)); foreach (var node in nodes) - CheckChrome(node, translation, language, emitError, emitWarning, translatableFields); + CheckChrome(node, translation, Language, emitError, emitWarning, translatableFields); } foreach (var file in modData.Manifest.Translations) diff --git a/OpenRA.Mods.Common/Projectiles/Missile.cs b/OpenRA.Mods.Common/Projectiles/Missile.cs index d83d122c90..4d39ba130b 100644 --- a/OpenRA.Mods.Common/Projectiles/Missile.cs +++ b/OpenRA.Mods.Common/Projectiles/Missile.cs @@ -496,8 +496,8 @@ namespace OpenRA.Mods.Common.Projectiles lastHt = 0; // Height just before the last height change // NOTE: Might be desired to unhardcode the lookahead step size - var stepSize = 32; - var step = new WVec(0, -stepSize, 0) + const int StepSize = 32; + var step = new WVec(0, -StepSize, 0) .Rotate(new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(hFacing))); // Step vector of length 128 // Probe terrain ahead of the missile @@ -505,7 +505,7 @@ namespace OpenRA.Mods.Common.Projectiles var maxLookaheadDistance = loopRadius * 4; var posProbe = pos; var curDist = 0; - var tickLimit = System.Math.Min(maxLookaheadDistance, distCheck) / stepSize; + var tickLimit = System.Math.Min(maxLookaheadDistance, distCheck) / StepSize; var prevHt = 0; // TODO: Make sure cell on map!!! @@ -517,7 +517,7 @@ namespace OpenRA.Mods.Common.Projectiles var ht = world.Map.Height[world.Map.CellContaining(posProbe)] * 512; - curDist += stepSize; + curDist += StepSize; if (ht > predClfHgt) { predClfHgt = ht; diff --git a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs index 606df843f2..c755743206 100644 --- a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs +++ b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs @@ -224,14 +224,14 @@ namespace OpenRA.Platforms.Default Console.WriteLine($"Using resolution: {windowSize.Width}x{windowSize.Height}"); - var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI; + const SDL.SDL_WindowFlags WindowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI; // HiDPI doesn't work properly on OSX with (legacy) fullscreen mode if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen) SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1"); window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(videoDisplay), SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(videoDisplay), - windowSize.Width, windowSize.Height, windowFlags); + windowSize.Width, windowSize.Height, WindowFlags); if (Platform.CurrentPlatform == PlatformType.Linux) { @@ -551,8 +551,8 @@ namespace OpenRA.Platforms.Default SetSDLAttributes(profile); - var flags = SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN | SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL; - var window = SDL.SDL_CreateWindow("", 0, 0, 1, 1, flags); + const SDL.SDL_WindowFlags Flags = SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN | SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL; + var window = SDL.SDL_CreateWindow("", 0, 0, 1, 1, Flags); if (window == IntPtr.Zero || !string.IsNullOrEmpty(SDL.SDL_GetError())) { errorLog.Add($"{profile}: SDL window creation failed: {SDL.SDL_GetError()}"); diff --git a/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs b/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs index 26dba5694b..90a4d6721a 100644 --- a/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs +++ b/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs @@ -21,7 +21,7 @@ namespace OpenRA.Test [TestCase(TestName = "Parse tree roundtrips")] public void TestParseRoundtrip() { - var yaml = + const string Yaml = @"1: 2: Test 3: # Test @@ -44,15 +44,15 @@ namespace OpenRA.Test 9.1.2: Test 9.1.3: # Test "; - var serialized = MiniYaml.FromString(yaml, discardCommentsAndWhitespace: false).WriteToString(); + var serialized = MiniYaml.FromString(Yaml, discardCommentsAndWhitespace: false).WriteToString(); Console.WriteLine(); - Assert.That(serialized, Is.EqualTo(yaml)); + Assert.That(serialized, Is.EqualTo(Yaml)); } [TestCase(TestName = "Parse tree can handle empty lines")] public void TestParseEmptyLines() { - var yaml = + const string Yaml = @"1: 2: Test @@ -97,7 +97,7 @@ namespace OpenRA.Test "; - var expectedYaml = + const string ExpectedYaml = @"1: 2: Test 3: @@ -120,14 +120,14 @@ namespace OpenRA.Test 9.1.2: Test 9.1.3: "; - var serialized = MiniYaml.FromString(yaml).WriteToString(); - Assert.That(serialized, Is.EqualTo(expectedYaml)); + var serialized = MiniYaml.FromString(Yaml).WriteToString(); + Assert.That(serialized, Is.EqualTo(ExpectedYaml)); } [TestCase(TestName = "Mixed tabs & spaces indents")] public void TestIndents() { - var yamlTabStyle = @" + const string YamlTabStyle = @" Root1: Child1: Attribute1: Test @@ -140,7 +140,7 @@ Root2: Attribute1: Test "; - var yamlMixedStyle = @" + const string YamlMixedStyle = @" Root1: Child1: Attribute1: Test @@ -152,9 +152,9 @@ Root2: Child1: Attribute1: Test "; - var tabs = MiniYaml.FromString(yamlTabStyle, "yamlTabStyle").WriteToString(); + var tabs = MiniYaml.FromString(YamlTabStyle, "yamlTabStyle").WriteToString(); Console.WriteLine(tabs); - var mixed = MiniYaml.FromString(yamlMixedStyle, "yamlMixedStyle").WriteToString(); + var mixed = MiniYaml.FromString(YamlMixedStyle, "yamlMixedStyle").WriteToString(); Console.WriteLine(mixed); Assert.That(tabs, Is.EqualTo(mixed)); } @@ -162,25 +162,25 @@ Root2: [TestCase(TestName = "Inheritance and removal can be composed")] public void InheritanceAndRemovalCanBeComposed() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockA2: ^BaseB: Inherits@a: ^BaseA MockB2: "; - var extendedYaml = @" + const string ExtendedYaml = @" Test: Inherits@b: ^BaseB -MockA2: "; - var mapYaml = @" + const string MapYaml = @" ^BaseC: MockC2: Test: Inherits@c: ^BaseC "; - var result = MiniYaml.Merge(new[] { baseYaml, extendedYaml, mapYaml }.Select(s => MiniYaml.FromString(s, ""))) + var result = MiniYaml.Merge(new[] { BaseYaml, ExtendedYaml, MapYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test").Value.Nodes; Assert.IsFalse(result.Any(n => n.Key == "MockA2"), "Node should not have the MockA2 child, but does."); @@ -191,19 +191,19 @@ Test: [TestCase(TestName = "Child can be removed after multiple inheritance")] public void ChildCanBeRemovedAfterMultipleInheritance() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockA2: Test: Inherits: ^BaseA MockA2: "; - var overrideYaml = @" + const string OverrideYaml = @" Test: -MockA2 "; - var result = MiniYaml.Merge(new[] { baseYaml, overrideYaml }.Select(s => MiniYaml.FromString(s, ""))) + var result = MiniYaml.Merge(new[] { BaseYaml, OverrideYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test").Value.Nodes; Assert.IsFalse(result.Any(n => n.Key == "MockA2"), "Node should not have the MockA2 child, but does."); @@ -212,7 +212,7 @@ Test: [TestCase(TestName = "Child can be immediately removed")] public void ChildCanBeImmediatelyRemoved() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockString: AString: Base @@ -223,7 +223,7 @@ Test: -MockString: "; - var result = MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, ""))) + var result = MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test").Value.Nodes; Assert.IsFalse(result.Any(n => n.Key == "MockString"), "Node should not have the MockString child, but does."); @@ -232,7 +232,7 @@ Test: [TestCase(TestName = "Child can be removed and immediately overridden")] public void ChildCanBeRemovedAndImmediatelyOverridden() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockString: AString: Base @@ -243,7 +243,7 @@ Test: AString: Override "; - var result = MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, ""))) + var result = MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test").Value.Nodes; Assert.IsTrue(result.Any(n => n.Key == "MockString"), "Node should have the MockString child, but does not."); @@ -254,7 +254,7 @@ Test: [TestCase(TestName = "Child can be removed and later overridden")] public void ChildCanBeRemovedAndLaterOverridden() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockString: AString: Base @@ -262,13 +262,13 @@ Test: Inherits: ^BaseA -MockString: "; - var overrideYaml = @" + const string OverrideYaml = @" Test: MockString: AString: Override "; - var result = MiniYaml.Merge(new[] { baseYaml, overrideYaml }.Select(s => MiniYaml.FromString(s, ""))) + var result = MiniYaml.Merge(new[] { BaseYaml, OverrideYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test").Value.Nodes; Assert.IsTrue(result.Any(n => n.Key == "MockString"), "Node should have the MockString child, but does not."); @@ -279,7 +279,7 @@ Test: [TestCase(TestName = "Child can be removed from intermediate parent")] public void ChildCanBeOverriddenThenRemoved() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockString: AString: Base @@ -288,14 +288,14 @@ Test: MockString: AString: Override "; - var overrideYaml = @" + const string OverrideYaml = @" Test: Inherits: ^BaseB MockString: -AString: "; - var result = MiniYaml.Merge(new[] { baseYaml, overrideYaml }.Select(s => MiniYaml.FromString(s, ""))) + var result = MiniYaml.Merge(new[] { BaseYaml, OverrideYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test").Value.Nodes; Assert.IsTrue(result.Any(n => n.Key == "MockString"), "Node should have the MockString child, but does not."); Assert.IsFalse(result.First(n => n.Key == "MockString").Value.Nodes.Any(n => n.Key == "AString"), @@ -305,7 +305,7 @@ Test: [TestCase(TestName = "Child subnode can be removed and immediately overridden")] public void ChildSubNodeCanBeRemovedAndImmediatelyOverridden() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockString: CollectionOfStrings: @@ -319,7 +319,7 @@ Test: StringC: C "; - var merged = MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, ""))) + var merged = MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test"); var traitNode = merged.Value.Nodes.Single(); @@ -334,7 +334,7 @@ Test: [TestCase(TestName = "Child subnode can be removed and later overridden")] public void ChildSubNodeCanBeRemovedAndLaterOverridden() { - var baseYaml = @" + const string BaseYaml = @" ^BaseA: MockString: CollectionOfStrings: @@ -346,14 +346,14 @@ Test: -CollectionOfStrings: "; - var overrideYaml = @" + const string OverrideYaml = @" Test: MockString: CollectionOfStrings: StringC: C "; - var merged = MiniYaml.Merge(new[] { baseYaml, overrideYaml }.Select(s => MiniYaml.FromString(s, ""))) + var merged = MiniYaml.Merge(new[] { BaseYaml, OverrideYaml }.Select(s => MiniYaml.FromString(s, ""))) .First(n => n.Key == "Test"); var traitNode = merged.Value.Nodes.Single(); @@ -368,7 +368,7 @@ Test: [TestCase(TestName = "Empty lines should count toward line numbers")] public void EmptyLinesShouldCountTowardLineNumbers() { - var yaml = @" + const string Yaml = @" TestA: Nothing: @@ -376,14 +376,14 @@ TestB: Nothing: "; - var result = MiniYaml.FromString(yaml).First(n => n.Key == "TestB"); + var result = MiniYaml.FromString(Yaml).First(n => n.Key == "TestB"); Assert.AreEqual(5, result.Location.Line); } [TestCase(TestName = "Duplicated nodes are correctly merged")] public void TestSelfMerging() { - var baseYaml = @" + const string BaseYaml = @" Test: Merge: original Child: original @@ -394,7 +394,7 @@ Test: Override: "; - var result = MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, ""))); + var result = MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, ""))); Assert.That(result.Count(n => n.Key == "Test"), Is.EqualTo(1), "Result should have exactly one Test node."); var testNodes = result.First(n => n.Key == "Test").Value.Nodes; @@ -408,13 +408,13 @@ Test: [TestCase(TestName = "Duplicated nodes across multiple sources are correctly merged")] public void TestSelfMergingMultiSource() { - var firstYaml = @" + const string FirstYaml = @" Test: Merge: original Child: original Original: "; - var secondYaml = @" + const string SecondYaml = @" Test: Merge: original Child: original @@ -425,7 +425,7 @@ Test: Override: "; - var result = MiniYaml.Merge(new[] { firstYaml, secondYaml }.Select(s => MiniYaml.FromString(s, ""))); + var result = MiniYaml.Merge(new[] { FirstYaml, SecondYaml }.Select(s => MiniYaml.FromString(s, ""))); Assert.That(result.Count(n => n.Key == "Test"), Is.EqualTo(1), "Result should have exactly one Test node."); var testNodes = result.First(n => n.Key == "Test").Value.Nodes; @@ -439,14 +439,14 @@ Test: [TestCase(TestName = "Duplicated child nodes do not throw if parent does not require merging")] public void TestMergeConflictsNoMerge() { - var baseYaml = @" + const string BaseYaml = @" Test: Merge: Child: Child: "; - var result = MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, ""))); + var result = MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, ""))); var testNodes = result.First(n => n.Key == "Test").Value.Nodes; var mergeNode = testNodes.First(n => n.Key == "Merge").Value; Assert.That(mergeNode.Nodes.Count, Is.EqualTo(2)); @@ -455,7 +455,7 @@ Test: [TestCase(TestName = "Duplicated child nodes throw merge error if first parent requires merging")] public void TestMergeConflictsFirstParent() { - var baseYaml = @" + const string BaseYaml = @" Test: Merge: Child1: @@ -463,7 +463,8 @@ Test: Merge: "; - void Merge() => MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + static void Merge() => MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + Assert.That(Merge, Throws.Exception.TypeOf().And.Message.EqualTo( "MiniYaml.Merge, duplicate values found for the following keys: Child1: [Child1 (at test-filename:4),Child1 (at test-filename:5)]")); } @@ -471,14 +472,16 @@ Test: [TestCase(TestName = "Duplicated child nodes throw merge error if second parent requires merging")] public void TestMergeConflictsSecondParent() { - var baseYaml = @" + const string BaseYaml = @" Test: Merge: Merge: Child2: Child2: "; - void Merge() => MiniYaml.Merge(new[] { baseYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + + static void Merge() => MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + Assert.That(Merge, Throws.Exception.TypeOf().And.Message.EqualTo( "MiniYaml.Merge, duplicate values found for the following keys: Child2: [Child2 (at test-filename:5),Child2 (at test-filename:6)]")); } @@ -486,18 +489,18 @@ Test: [TestCase(TestName = "Duplicated child nodes across multiple sources do not throw")] public void TestMergeConflictsMultiSourceMerge() { - var firstYaml = @" + const string FirstYaml = @" Test: Merge: Child: "; - var secondYaml = @" + const string SecondYaml = @" Test: Merge: Child: "; - var result = MiniYaml.Merge(new[] { firstYaml, secondYaml }.Select(s => MiniYaml.FromString(s, ""))); + var result = MiniYaml.Merge(new[] { FirstYaml, SecondYaml }.Select(s => MiniYaml.FromString(s, ""))); var testNodes = result.First(n => n.Key == "Test").Value.Nodes; var mergeNode = testNodes.First(n => n.Key == "Merge").Value; Assert.That(mergeNode.Nodes.Count, Is.EqualTo(1)); @@ -506,18 +509,19 @@ Test: [TestCase(TestName = "Duplicated child nodes across multiple sources throw merge error if first parent requires merging")] public void TestMergeConflictsMultiSourceFirstParent() { - var firstYaml = @" + const string FirstYaml = @" Test: Merge: Child1: Child1: "; - var secondYaml = @" + const string SecondYaml = @" Test: Merge: "; - void Merge() => MiniYaml.Merge(new[] { firstYaml, secondYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + static void Merge() => MiniYaml.Merge(new[] { FirstYaml, SecondYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + Assert.That(Merge, Throws.Exception.TypeOf().And.Message.EqualTo( "MiniYaml.Merge, duplicate values found for the following keys: Child1: [Child1 (at test-filename:4),Child1 (at test-filename:5)]")); } @@ -525,18 +529,19 @@ Test: [TestCase(TestName = "Duplicated child nodes across multiple sources throw merge error if second parent requires merging")] public void TestMergeConflictsMultiSourceSecondParent() { - var firstYaml = @" + const string FirstYaml = @" Test: Merge: "; - var secondYaml = @" + const string SecondYaml = @" Test: Merge: Child2: Child2: "; - void Merge() => MiniYaml.Merge(new[] { firstYaml, secondYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + static void Merge() => MiniYaml.Merge(new[] { FirstYaml, SecondYaml }.Select(s => MiniYaml.FromString(s, "test-filename"))); + Assert.That(Merge, Throws.Exception.TypeOf().And.Message.EqualTo( "MiniYaml.Merge, duplicate values found for the following keys: Child2: [Child2 (at test-filename:4),Child2 (at test-filename:5)]")); } @@ -573,15 +578,15 @@ Test: [TestCase(TestName = "Leading and trailing whitespace can be guarded using a backslash")] public void TestGuardedWhitespace() { - var testYaml = @"key: \ test value \ "; - var nodes = MiniYaml.FromString(testYaml, "testYaml"); + const string TestYaml = @"key: \ test value \ "; + var nodes = MiniYaml.FromString(TestYaml, "testYaml"); Assert.AreEqual(" test value ", nodes[0].Value.Value); } [TestCase(TestName = "Comments should count toward line numbers")] public void CommentsShouldCountTowardLineNumbers() { - var yaml = @" + const string Yaml = @" TestA: Nothing: @@ -589,12 +594,12 @@ TestA: TestB: Nothing: "; - var resultDiscard = MiniYaml.FromString(yaml); + var resultDiscard = MiniYaml.FromString(Yaml); var resultDiscardLine = resultDiscard.First(n => n.Key == "TestB").Location.Line; Assert.That(resultDiscardLine, Is.EqualTo(6), "Node TestB should report its location as line 6, but is not (discarding comments)"); Assert.That(resultDiscard[1].Key, Is.EqualTo("TestB"), "Node TestB should be the second child of the root node, but is not (discarding comments)"); - var resultKeep = MiniYaml.FromString(yaml, discardCommentsAndWhitespace: false); + var resultKeep = MiniYaml.FromString(Yaml, discardCommentsAndWhitespace: false); var resultKeepLine = resultKeep.First(n => n.Key == "TestB").Location.Line; Assert.That(resultKeepLine, Is.EqualTo(6), "Node TestB should report its location as line 6, but is not (parsing comments)"); Assert.That(resultKeep[4].Key, Is.EqualTo("TestB"), "Node TestB should be the fifth child of the root node, but is not (parsing comments)"); @@ -621,7 +626,7 @@ Parent: # comment without value [TestCase(TestName = "Comments should be removed when discardCommentsAndWhitespace is false")] public void CommentsShouldntSurviveRoundTrip() { - var yaml = @" + const string Yaml = @" # Top level comment node Parent: # comment without value # Indented comment node @@ -634,7 +639,7 @@ Parent: # comment without value Second: value ".Replace("\r\n", "\n"); - var result = MiniYaml.FromString(yaml).WriteToString(); + var result = MiniYaml.FromString(Yaml).WriteToString(); Assert.AreEqual(strippedYaml, result); } }