using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace NT8.Core.Tests.Adapters { [TestClass] public class NT8StrategyBaseShadowedPropertyTests { private static readonly string[] FormerlyShadowedProperties = { "EnableBarBasedRearm", "EnableShortTrendFilter", "EnableCMEHolidayGuard", "EnableATRTrailing", "ATRTrailingPeriod", "ATRTrailingMultiplier", "ATRTrailingMinTicks", "ATRTrailingMaxTicks" }; private static readonly Regex PublicAutoProperty = new Regex( @"(?m)^\s*public\s+[\w.<>,\[\]?]+\s+(\w+)\s*\{\s*get;\s*set;\s*\}", RegexOptions.Compiled); [TestMethod] public void FormerlyShadowedProperties_AreNotDeclaredOnSimpleOrb() { HashSet derivedProperties = ReadPublicProperties(FindSourceFile("SimpleORBNT8.cs")); foreach (string name in FormerlyShadowedProperties) Assert.IsFalse(derivedProperties.Contains(name), name + " must be inherited, not redeclared by SimpleORBNT8."); } [TestMethod] public void FormerlyShadowedProperties_AreDeclaredExactlyOnceOnBase() { string source = File.ReadAllText(FindSourceFile("NT8StrategyBase.cs")); foreach (string name in FormerlyShadowedProperties) { int count = Regex.Matches(source, @"(?m)^\s*public\s+[\w.<>,\[\]?]+\s+" + Regex.Escape(name) + @"\s*\{\s*get;\s*set;\s*\}").Count; Assert.AreEqual(1, count, name + " must have exactly one public property declaration in NT8StrategyBase."); } } [TestMethod] public void SimpleOrbAndBase_HaveNoPublicPropertyNameOverlap() { HashSet baseProperties = ReadPublicProperties(FindSourceFile("NT8StrategyBase.cs")); HashSet derivedProperties = ReadPublicProperties(FindSourceFile("SimpleORBNT8.cs")); List collisions = new List(); foreach (string name in derivedProperties) { if (baseProperties.Contains(name)) collisions.Add(name); } Assert.AreEqual(0, collisions.Count, "SimpleORBNT8 must not shadow public base properties: " + string.Join(", ", collisions.ToArray())); } private static HashSet ReadPublicProperties(string path) { HashSet names = new HashSet(StringComparer.Ordinal); MatchCollection matches = PublicAutoProperty.Matches(File.ReadAllText(path)); foreach (Match match in matches) names.Add(match.Groups[1].Value); return names; } private static string FindSourceFile(string fileName) { DirectoryInfo directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); while (directory != null) { string candidate = Path.Combine(directory.FullName, "src", "NT8.Adapters", "Strategies", fileName); if (File.Exists(candidate)) return candidate; directory = directory.Parent; } throw new FileNotFoundException("Could not locate src\\NT8.Adapters\\Strategies\\" + fileName + " from test output directory."); } } }