using System; using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace NT8.Core.Tests.Adapters { [TestClass] public class NT8StrategyBaseOnBarUpdateVisibilityTests { [TestMethod] public void OnBarUpdate_CatchBlockWritesExceptionToFileLog() { string source = File.ReadAllText(FindStrategyBaseSourceFile()); string method = ExtractMethodBlock(source, "protected override void OnBarUpdate"); int catchIndex = method.IndexOf("catch (Exception ex)", StringComparison.Ordinal); int fileLogIndex = method.IndexOf("FileLog(string.Format(\"SDK ONBARUPDATE FAILED: {0}: {1}\", ex.GetType().Name, ex.Message));", StringComparison.Ordinal); int printIndex = method.IndexOf("Print(string.Format(\"[SDK ERROR] OnBarUpdate: {0}\", ex.Message));", StringComparison.Ordinal); Assert.IsTrue(catchIndex >= 0, "OnBarUpdate must keep its exception guard."); Assert.IsTrue(fileLogIndex > catchIndex, "OnBarUpdate catch must write the exception to FileLog."); Assert.IsTrue(printIndex > fileLogIndex, "FileLog visibility must be additive and preserve the existing Print path after it."); } private static string FindStrategyBaseSourceFile() { DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); while (dir != null) { string candidate = Path.Combine(dir.FullName, "src", "NT8.Adapters", "Strategies", "NT8StrategyBase.cs"); if (File.Exists(candidate)) return candidate; dir = dir.Parent; } throw new FileNotFoundException("Could not locate src\\NT8.Adapters\\Strategies\\NT8StrategyBase.cs from test output directory."); } private static string ExtractMethodBlock(string source, string signature) { int start = source.IndexOf(signature, StringComparison.Ordinal); if (start < 0) Assert.Fail("Source does not contain method: " + signature); int brace = source.IndexOf("{", start, StringComparison.Ordinal); if (brace < 0) Assert.Fail("Method has no opening brace: " + signature); int depth = 0; for (int i = brace; i < source.Length; i++) { if (source[i] == '{') depth++; else if (source[i] == '}') { depth--; if (depth == 0) return source.Substring(start, i - start + 1); } } Assert.Fail("Method has no closing brace: " + signature); return string.Empty; } } }