|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using Aquality.Selenium.Browsers; |
| 5 | +using NUnit.Framework; |
| 6 | +using Aquality.Selenium.Core.Localization; |
| 7 | + |
| 8 | +namespace Aquality.Selenium.Tests.Unit |
| 9 | +{ |
| 10 | + [TestFixture] |
| 11 | + [Parallelizable(ParallelScope.All)] |
| 12 | + internal class LocalizationManagerTests |
| 13 | + { |
| 14 | + private const string LocalizedNavigationMessage = "Navigate to url - 'test'"; |
| 15 | + private const string LogPath = "../../../Log/log.log"; |
| 16 | + private const string TestUrl = "test"; |
| 17 | + private const string NavigationKey = "loc.browser.navigate"; |
| 18 | + private static ILocalizedLogger LocalizedLogger => BrowserManager.GetRequiredService<ILocalizedLogger>(); |
| 19 | + private static ILocalizationManager LocalizationManager => BrowserManager.GetRequiredService<ILocalizationManager>(); |
| 20 | + |
| 21 | + [TestCase(LogLevel.Info)] |
| 22 | + [TestCase(LogLevel.Debug)] |
| 23 | + [TestCase(LogLevel.Error)] |
| 24 | + [TestCase(LogLevel.Fatal)] |
| 25 | + [TestCase(LogLevel.Warn)] |
| 26 | + [Parallelizable(ParallelScope.None)] |
| 27 | + public void Should_BeAble_LogLocalizedMessage(LogLevel logLevel) |
| 28 | + { |
| 29 | + switch (logLevel) |
| 30 | + { |
| 31 | + case LogLevel.Info: |
| 32 | + LocalizedLogger.Info(NavigationKey, TestUrl); |
| 33 | + break; |
| 34 | + case LogLevel.Debug: |
| 35 | + LocalizedLogger.Debug(NavigationKey, null, TestUrl); |
| 36 | + break; |
| 37 | + case LogLevel.Error: |
| 38 | + LocalizedLogger.Error(NavigationKey, TestUrl); |
| 39 | + break; |
| 40 | + case LogLevel.Fatal: |
| 41 | + LocalizedLogger.Fatal(NavigationKey, null, TestUrl); |
| 42 | + break; |
| 43 | + case LogLevel.Warn: |
| 44 | + LocalizedLogger.Warn(NavigationKey, TestUrl); |
| 45 | + break; |
| 46 | + default: |
| 47 | + throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, "Cannot process log level"); |
| 48 | + } |
| 49 | + |
| 50 | + var logMessage = File.ReadAllLines(LogPath).LastOrDefault(); |
| 51 | + Assert.IsFalse(string.IsNullOrEmpty(logMessage), "Message should appear in log file"); |
| 52 | + Assert.IsTrue(logMessage.Contains(LocalizedNavigationMessage), |
| 53 | + $"Message should be localized. Expected: {LocalizedNavigationMessage}, actual: {logMessage}"); |
| 54 | + } |
| 55 | + |
| 56 | + [Test] |
| 57 | + public void Should_BeAble_ToLocalizeLoggerMessage() |
| 58 | + { |
| 59 | + var message = LocalizationManager.GetLocalizedMessage("loc.browser.navigate", "test"); |
| 60 | + Assert.AreEqual(LocalizedNavigationMessage, message, "Message should be localized"); |
| 61 | + } |
| 62 | + |
| 63 | + public enum LogLevel |
| 64 | + { |
| 65 | + Info, |
| 66 | + Debug, |
| 67 | + Error, |
| 68 | + Fatal, |
| 69 | + Warn |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments