• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 23999500762

05 Apr 2026 10:19AM UTC coverage: 89.046% (-0.2%) from 89.233%
23999500762

push

travis-pro

lduchosal
fix: resolve SonarCloud issues (S927, S1192, S3776, CA1859, S2701, IDE0034, IDE0060, IDE0028)

- S927: rename formatter Write parameters to match interface ("output")
- S1192: extract repeated string literals into constants in Program.cs
- S3776: extract WriteUsageHelp/ComputeMaxFlagWidth to reduce cognitive complexity
- CA1859: use specific return types for private methods in ActionComputer
- S2701/MSTEST0037: use Assert.IsTrue/IsFalse instead of Assert.AreEqual(bool)
- IDE0034: simplify default(BigInteger) to default
- IDE0060: discard unused parameter in TestOperatorAddOverflow
- IDE0028: use collection initializer in GetHashCodeUnitTest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

790 of 894 branches covered (88.37%)

Branch coverage included in aggregate %.

41 of 78 new or added lines in 3 files covered. (52.56%)

1 existing line in 1 file now uncovered.

2177 of 2438 relevant lines covered (89.29%)

491923.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.96
/src/ConsoleApplication/TextFormatter.cs
1
// <copyright file="TextFormatter.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Collections.Generic;
8
using System.IO;
9
using System.Numerics;
10

11
/// <summary>
12
/// Renders action results as human-readable text.
13
/// </summary>
14
public class TextFormatter : IFormatter
15
{
16
    private readonly TextWriter _writer;
17

18
    /// <summary>
19
    /// Initializes a new instance of the <see cref="TextFormatter"/> class.
20
    /// </summary>
21
    public TextFormatter(TextWriter writer)
11✔
22
    {
11✔
23
        _writer = writer;
11✔
24
    }
11✔
25

26
    /// <inheritdoc/>
27
    public void Write(ActionOutput output, ProgramContext ac)
28
    {
11✔
29
        output.WriteTo(this, ac);
11✔
30
    }
11✔
31

32
    /// <inheritdoc/>
33
    public void Write(ActionOutput.Networks output, ProgramContext ac)
34
    {
6✔
35
        WriteNetworkList(output.Items, ac);
6✔
36
    }
6✔
37

38
    /// <inheritdoc/>
39
    public void Write(ActionOutput.NetworkGroups output, ProgramContext ac)
40
    {
1✔
41
        for (int i = 0; i < output.Groups.Count; i++)
4✔
42
        {
1✔
43
            var group = output.Groups[i];
1✔
44
            if (group.Count == 0)
1!
45
            {
×
NEW
46
                _writer.WriteLine("Unable to subnet ipnetwork {0} into cidr {1}", output.InputNetworks[i], output.SubnetCidr);
×
47
            }
×
48
            else
49
            {
1✔
50
                WriteNetworkList(group, ac);
1✔
51
            }
1✔
52

53
            WriteSeparator(output.Groups.Count, i + 1);
1✔
54
        }
1✔
55
    }
1✔
56

57
    /// <inheritdoc/>
58
    public void Write(ActionOutput.SubtractResults output, ProgramContext ac)
59
    {
1✔
60
        foreach (var ipn in output.Items)
13✔
61
        {
5✔
62
            _writer.WriteLine("{0}", ipn);
5✔
63
        }
5✔
64
    }
1✔
65

66
    /// <inheritdoc/>
67
    public void Write(ActionOutput.ContainResults output, ProgramContext ac)
68
    {
1✔
69
        foreach (var r in output.Items)
5✔
70
        {
1✔
71
            _writer.WriteLine("{0} contains {1} : {2}", r.Network, r.Test, r.Contains);
1✔
72
        }
1✔
73
    }
1✔
74

75
    /// <inheritdoc/>
76
    public void Write(ActionOutput.OverlapResults output, ProgramContext ac)
77
    {
1✔
78
        foreach (var r in output.Items)
5✔
79
        {
1✔
80
            _writer.WriteLine("{0} overlaps {1} : {2}", r.Network, r.Test, r.Overlaps);
1✔
81
        }
1✔
82
    }
1✔
83

84
    /// <inheritdoc/>
85
    public void Write(ActionOutput.IpAddresses output, ProgramContext ac)
86
    {
×
NEW
87
        foreach (var ipnetwork in output.InputNetworks)
×
88
        {
×
89
            if (ipnetwork.Cidr < 16)
×
90
            {
×
91
                Console.Error.WriteLine(
×
92
                    "WARNING: listing all IP addresses in {0} ({1} addresses). This may take a very long time.",
×
93
                    ipnetwork,
×
94
                    ipnetwork.Total);
×
95
            }
×
96
        }
×
97

NEW
98
        foreach (string addr in output.Items)
×
99
        {
×
100
            _writer.WriteLine("{0}", addr);
×
101
        }
×
102
    }
×
103

104
    /// <inheritdoc/>
105
    public void Write(ActionOutput.Error output, ProgramContext ac)
106
    {
×
NEW
107
        _writer.WriteLine(output.Message);
×
108
    }
×
109

110
    /// <inheritdoc/>
111
    public void Write(ActionOutput.UsageInfo output, ProgramContext ac)
112
    {
1✔
113
        if (output.Errors.Count > 0)
1!
114
        {
1✔
115
            foreach (string error in output.Errors)
7✔
116
            {
2✔
117
                _writer.WriteLine(error);
2✔
118
            }
2✔
119

120
            return;
1✔
121
        }
122

NEW
123
        WriteUsageHelp(output);
×
124
    }
1✔
125

126
    private void WriteUsageHelp(ActionOutput.UsageInfo output)
NEW
127
    {
×
NEW
128
        _writer.WriteLine("Usage: {0}", output.Synopsis);
×
NEW
129
        _writer.WriteLine("Version: {0}", output.Version);
×
130

NEW
131
        foreach (var group in output.OptionGroups)
×
132
        {
×
133
            _writer.WriteLine();
×
134
            _writer.WriteLine(group.Name);
×
135

NEW
136
            int maxWidth = ComputeMaxFlagWidth(group.Options);
×
137

138
            foreach (var opt in group.Options)
×
139
            {
×
140
                string flagPart = opt.ArgName is not null
×
141
                    ? $"-{opt.Flag} {opt.ArgName}"
×
142
                    : $"-{opt.Flag}";
×
143
                _writer.WriteLine("\t{0} : {1}", flagPart.PadRight(maxWidth), opt.Description);
×
144
                if (opt.Example is not null)
×
145
                {
×
146
                    _writer.WriteLine("\t{0}   {1}", new string(' ', maxWidth), opt.Example);
×
147
                }
×
148
            }
×
149
        }
×
150

151
        _writer.WriteLine();
×
NEW
152
        _writer.WriteLine("networks  : {0} ", output.NetworksDescription);
×
NEW
153
        _writer.WriteLine("            ({0} )", string.Join(" ", output.NetworksExamples));
×
NEW
154
    }
×
155

156
    private static int ComputeMaxFlagWidth(List<UsageOption> options)
NEW
157
    {
×
NEW
158
        int maxWidth = 0;
×
NEW
159
        foreach (var opt in options)
×
NEW
160
        {
×
NEW
161
            int width = 1 + opt.Flag.Length;
×
NEW
162
            if (opt.ArgName is not null)
×
NEW
163
            {
×
NEW
164
                width += 1 + opt.ArgName.Length;
×
NEW
165
            }
×
166

NEW
167
            if (width > maxWidth)
×
NEW
168
            {
×
NEW
169
                maxWidth = width;
×
NEW
170
            }
×
NEW
171
        }
×
172

NEW
173
        return maxWidth;
×
UNCOV
174
    }
×
175

176
    private void WriteNetworkList(List<IPNetwork2> networks, ProgramContext ac)
177
    {
7✔
178
        for (int i = 0; i < networks.Count; i++)
552✔
179
        {
269✔
180
            WriteNetwork(networks[i], ac);
269✔
181
            WriteSeparator(networks.Count, i + 1);
269✔
182
        }
269✔
183
    }
7✔
184

185
    private void WriteNetwork(IPNetwork2 ipn, ProgramContext ac)
186
    {
269✔
187
        if (ac.IPNetwork) { _writer.WriteLine("IPNetwork   : {0}", ipn); }
1,070✔
188
        if (ac.Network) { _writer.WriteLine("Network     : {0}", ipn.Network); }
1,058✔
189
        if (ac.Netmask) { _writer.WriteLine("Netmask     : {0}", ipn.Netmask); }
1,058✔
190
        if (ac.Cidr) { _writer.WriteLine("Cidr        : {0}", ipn.Cidr); }
1,058✔
191
        if (ac.Broadcast) { _writer.WriteLine("Broadcast   : {0}", ipn.Broadcast); }
1,058✔
192
        if (ac.FirstUsable) { _writer.WriteLine("FirstUsable : {0}", ipn.FirstUsable); }
1,058✔
193
        if (ac.LastUsable) { _writer.WriteLine("LastUsable  : {0}", ipn.LastUsable); }
1,058✔
194
        if (ac.Usable) { _writer.WriteLine("Usable      : {0}", ipn.Usable); }
1,058✔
195
        if (ac.Total) { _writer.WriteLine("Total       : {0}", ipn.Total); }
1,052✔
196
    }
269✔
197

198
    private void WriteSeparator(BigInteger max, BigInteger index)
199
    {
270✔
200
        if (max > 1 && index != max)
270✔
201
        {
262✔
202
            _writer.WriteLine("--");
262✔
203
        }
262✔
204
    }
270✔
205
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc