diff --git a/src/TermInfo.Tests/Data/xterm+88color b/src/TermInfo.Tests/Data/xterm+88color new file mode 100644 index 0000000..64f5913 Binary files /dev/null and b/src/TermInfo.Tests/Data/xterm+88color differ diff --git a/src/TermInfo.Tests/TermInfo.Tests.csproj b/src/TermInfo.Tests/TermInfo.Tests.csproj index 37ac296..914d219 100644 --- a/src/TermInfo.Tests/TermInfo.Tests.csproj +++ b/src/TermInfo.Tests/TermInfo.Tests.csproj @@ -7,10 +7,12 @@ + + diff --git a/src/TermInfo.Tests/TermInfoDataTests.cs b/src/TermInfo.Tests/TermInfoDataTests.cs new file mode 100644 index 0000000..874d63f --- /dev/null +++ b/src/TermInfo.Tests/TermInfoDataTests.cs @@ -0,0 +1,23 @@ +using Shouldly; +using Xunit; + +namespace TermInfo.Tests +{ + public sealed class TermInfoDataTests + { + [Theory] + [InlineData("xterm+256color", 256)] + [InlineData("xterm+88color", 88)] + public void Should_Read_MaxColors(string terminfo, int expected) + { + // Given + var stream = EmbeddedResourceReader.LoadResourceStream($"TermInfo.Tests/Data/{terminfo}"); + + // When + var info = TermInfoData.Read(stream); + + // Then + info.MaxColors.ShouldBe(expected); + } + } +} diff --git a/src/TermInfo.Tests/ParserTests.cs b/src/TermInfo.Tests/Utilities/EmbeddedResourceReader.cs similarity index 66% rename from src/TermInfo.Tests/ParserTests.cs rename to src/TermInfo.Tests/Utilities/EmbeddedResourceReader.cs index ab36eff..4c370d5 100644 --- a/src/TermInfo.Tests/ParserTests.cs +++ b/src/TermInfo.Tests/Utilities/EmbeddedResourceReader.cs @@ -1,29 +1,9 @@ -using System; +using System; using System.IO; using System.Reflection; -using Shouldly; -using Xunit; namespace TermInfo.Tests { - public sealed class ParserTests - { - [Fact] - public void Should_Read_Terminfo_File() - { - // Given - var stream = EmbeddedResourceReader.LoadResourceStream("TermInfo.Tests/Data/xterm+256color"); - - // When - var info = TermInfoData.Read(stream); - - // Then - info.MaxColors.ShouldBe(256); - info.AutoLeftMargin.ShouldBe(false); - info.OrigColors.ShouldBe("\u001b]104\a"); - } - } - public static class EmbeddedResourceReader { public static Stream LoadResourceStream(string resourceName) diff --git a/src/TermInfo/TermInfoData.cs b/src/TermInfo/TermInfoData.cs index 430d8b6..24af11b 100644 --- a/src/TermInfo/TermInfoData.cs +++ b/src/TermInfo/TermInfoData.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; using System.IO; namespace TermInfo @@ -49,12 +48,13 @@ namespace TermInfo /// The terminfo capability value. public bool? GetBoolean(TermInfoCaps.Boolean value) { - if (TryGetBoolean(value, out var result)) + var index = (int)value; + if (index >= _booleans.Length) { - return result; + return null; } - return null; + return _booleans[index]; } /// @@ -64,12 +64,19 @@ namespace TermInfo /// The terminfo capability value. public int? GetNum(TermInfoCaps.Num value) { - if (TryGetNum(value, out var result)) + var index = (int)value; + if (index >= _nums.Length) { - return result; + return null; } - return null; + var result = _nums[index]; + if (result == -1) + { + return null; + } + + return result; } /// @@ -78,82 +85,20 @@ namespace TermInfo /// The capability to get the value for. /// The terminfo capability value. public string? GetString(TermInfoCaps.String value) - { - if (TryGetString(value, out var result)) - { - return result; - } - - return null; - } - - /// - /// Tries to get a specific boolean terminfo capability value. - /// - /// The capability to get the value for. - /// The terminfo capability value, or null if missing. - /// true if the capability was found, otherwise false. - public bool TryGetBoolean(TermInfoCaps.Boolean value, [NotNullWhen(true)] out bool? result) - { - var index = (int)value; - if (index >= _booleans.Length) - { - result = null; - return false; - } - - result = _booleans[index]; - return true; - } - - /// - /// Tries to get a specific numeric terminfo capability value. - /// - /// The capability to get the value for. - /// The terminfo capability value, or null if missing. - /// true if the capability was found, otherwise false. - public bool TryGetNum(TermInfoCaps.Num value, [NotNullWhen(true)] out int? result) - { - var index = (int)value; - if (index >= _nums.Length) - { - result = null; - return false; - } - - result = _nums[index]; - if (result == -1) - { - result = null; - return false; - } - - return true; - } - - /// - /// Tries to get a specific string terminfo capability value. - /// - /// The capability to get the value for. - /// The terminfo capability value, or null if missing. - /// true if the capability was found, otherwise false. - public bool TryGetString(TermInfoCaps.String value, [NotNullWhen(true)] out string? result) { var index = (int)value; if (index >= _strings.Length) { - result = null; - return false; + return null; } - result = _strings[index]; + var result = _strings[index]; if (result == null) { - result = null; - return false; + return null; } - return true; + return result; } } }