|
| 1 | +// Copyright (c) Toni Solarin-Sodara |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using ConsoleTables; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace coverlet.msbuild.tasks.tests |
| 8 | +{ |
| 9 | + public class ConsoleTableTests |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void Write_DefaultFormat_WritesToConsole() |
| 13 | + { |
| 14 | + // Arrange |
| 15 | + var columns = new[] { "", "Line", "Branch", "Method" }; |
| 16 | + var table = new ConsoleTable(columns); |
| 17 | + table.AddRow("Value1", "Value2", "Value3", "Value4"); |
| 18 | + |
| 19 | + var consoleOutput = new StringWriter(); |
| 20 | + Console.SetOut(consoleOutput); |
| 21 | + |
| 22 | + // Act |
| 23 | + table.Write(Format.Default); |
| 24 | + |
| 25 | + // Assert |
| 26 | + var output = consoleOutput.ToString(); |
| 27 | + Assert.Contains("-------------------------------------", output); |
| 28 | + Assert.Contains("| | Line | Branch | Method |", output); |
| 29 | + Assert.Contains("| Value1 | Value2 | Value3 | Value4 |", output); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Write_MarkDownFormat_WritesToConsole() |
| 34 | + { |
| 35 | + // Arrange |
| 36 | + var columns = new[] { "", "Line", "Branch", "Method" }; |
| 37 | + var table = new ConsoleTable(columns); |
| 38 | + table.AddRow("Value1", "Value2", "Value3", "Value4"); |
| 39 | + |
| 40 | + var consoleOutput = new StringWriter(); |
| 41 | + Console.SetOut(consoleOutput); |
| 42 | + |
| 43 | + // Act |
| 44 | + table.Write(Format.MarkDown); |
| 45 | + |
| 46 | + // Assert |
| 47 | + var output = consoleOutput.ToString(); |
| 48 | + Assert.Contains("| | Line | Branch | Method |", output); |
| 49 | + Assert.Contains("|--------|--------|--------|--------|", output); |
| 50 | + Assert.Contains("| Value1 | Value2 | Value3 | Value4 |", output); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void Write_AlternativeFormat_WritesToConsole() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var columns = new[] { "", "Line", "Branch", "Method" }; |
| 58 | + var table = new ConsoleTable(columns); |
| 59 | + table.AddRow("Value1", "Value2", "Value3", "Value4"); |
| 60 | + |
| 61 | + var consoleOutput = new StringWriter(); |
| 62 | + Console.SetOut(consoleOutput); |
| 63 | + |
| 64 | + // Act |
| 65 | + table.Write(Format.Alternative); |
| 66 | + |
| 67 | + // Assert |
| 68 | + var output = consoleOutput.ToString(); |
| 69 | + Assert.Contains("+--------+--------+--------+--------+", output); |
| 70 | + Assert.Contains("| | Line | Branch | Method |", output); |
| 71 | + Assert.Contains("| Value1 | Value2 | Value3 | Value4 |", output); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Write_MinimalFormat_WritesToConsole() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + var columns = new[] { "", "Line", "Branch", "Method" }; |
| 79 | + var table = new ConsoleTable(columns); |
| 80 | + table.AddRow("Value1", "Value2", "Value3", "Value4"); |
| 81 | + |
| 82 | + var consoleOutput = new StringWriter(); |
| 83 | + Console.SetOut(consoleOutput); |
| 84 | + |
| 85 | + // Act |
| 86 | + table.Write(Format.Minimal); |
| 87 | + |
| 88 | + // Assert |
| 89 | + var output = consoleOutput.ToString(); |
| 90 | + Assert.Contains(" Line Branch Method", output); |
| 91 | + Assert.Contains("------------------------------", output); |
| 92 | + Assert.Contains("Value1 Value2 Value3 Value4", output); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void Write_InvalidFormat_ThrowsArgumentOutOfRangeException() |
| 97 | + { |
| 98 | + // Arrange |
| 99 | + var columns = new[] { "", "Line", "Branch", "Method" }; |
| 100 | + var table = new ConsoleTable(columns); |
| 101 | + |
| 102 | + // Act & Assert |
| 103 | + Assert.Throws<ArgumentOutOfRangeException>(() => table.Write((Format)999)); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments