< Summary

Information
Class: CalculatorLibrary.CalculatorCli
Assembly: CalculatorLibrary
File(s): D:\a\vs-net-core-template\vs-net-core-template\CalculatorLibrary\CalculatorCli.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 84
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 9/30/2026 - 3:07:32 AM Line coverage: 100% (25/25) Branch coverage: 100% (12/12) Total lines: 84 9/30/2026 - 3:07:32 AM Line coverage: 100% (25/25) Branch coverage: 100% (12/12) Total lines: 84

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Run(...)100%1212100%
RunDemo()100%11100%

File(s)

D:\a\vs-net-core-template\vs-net-core-template\CalculatorLibrary\CalculatorCli.cs

#LineLine coverage
 1namespace CalculatorLibrary {
 2
 3/// <summary>
 4/// Parses and runs the calculator's command-line arguments.
 5/// </summary>
 6/// <remarks>
 7/// <para>
 8/// The parsing logic lives here, in the library, instead of in <c>CalculatorApp/Program.cs</c>, so
 9/// it can be unit tested directly (see <c>CalculatorLibrary.Tests/CalculatorCliTests.cs</c>) without
 10/// spawning the console application as a process. <c>Program.cs</c> only calls
 11/// <see cref="Run(string[])"/> or <see cref="RunDemo"/> and prints the result — it never reads from
 12/// the console, so it never blocks when the app is launched from a script, a build pipeline or CI.
 13/// </para>
 14/// <para>
 15/// When you rename this sample into your own project, replace this class with your own
 16/// argument-parsing entry point, keeping the same "parsing is a library function, not something the
 17/// console shell does inline" shape.
 18/// </para>
 19/// </remarks>
 20public static class CalculatorCli {
 21  /// <summary>The usage line shown when the arguments cannot be parsed.</summary>
 22  public const string UsageText = "Usage: CalculatorApp <add|subtract|multiply|divide> <a> <b>";
 23
 24  /// <summary>
 25  /// Parses <paramref name="args"/> as <c>&lt;operation&gt; &lt;a&gt; &lt;b&gt;</c> and returns a
 26  /// line of text describing the result.
 27  /// </summary>
 28  /// <param name="args">
 29  /// Exactly three arguments: the operation name (<c>add</c>, <c>subtract</c>, <c>multiply</c> or
 30  /// <c>divide</c>, case-insensitive) followed by the two integer operands.
 31  /// </param>
 32  /// <returns>A human-readable line such as <c>"2 add 2 = 4"</c>.</returns>
 33  /// <exception cref="ArgumentException">
 34  /// Thrown when the argument count is wrong, an operand is not a valid 32-bit integer, or the
 35  /// operation name is not recognized. The message always ends with <see cref="UsageText"/>.
 36  /// </exception>
 37  /// <exception cref="DivideByZeroException">Thrown when dividing by zero.</exception>
 38  public static string Run(string[] args) {
 3239    if (args.Length != 3) {
 840      throw new ArgumentException($"Expected 3 arguments (operation, a, b) but got {args.Length}. {UsageText}");
 41    }
 42
 2443    string operation = args[0].Trim().ToLowerInvariant();
 44
 2445    if (!int.TryParse(args[1], out int a)) {
 646      throw new ArgumentException($"'{args[1]}' is not a valid 32-bit integer for <a>. {UsageText}");
 47    }
 48
 1849    if (!int.TryParse(args[2], out int b)) {
 250      throw new ArgumentException($"'{args[2]}' is not a valid 32-bit integer for <b>. {UsageText}");
 51    }
 52
 1653    var calculator = new Calculator();
 54
 1655    int result = operation switch {
 456      "add" => calculator.Add(a, b),
 257        "subtract" => calculator.Subtract(a, b),
 258        "multiply" => calculator.Multiply(a, b),
 459        "divide" => calculator.Divide(a, b),
 460        _ => throw new ArgumentException($"Unknown operation '{args[0]}'. {UsageText}"),
 1661    };
 62
 1063    return $"{a} {operation} {b} = {result}";
 64  }
 65
 66  /// <summary>
 67  /// Runs a small built-in demonstration that exercises every operation once, used when the
 68  /// application is started with no arguments at all (so a first run always shows something,
 69  /// without waiting on keyboard input).
 70  /// </summary>
 71  /// <returns>The combined demo output, ending with <see cref="UsageText"/>.</returns>
 72  public static string RunDemo() {
 273    var calculator = new Calculator();
 274    var lines = new System.Text.StringBuilder();
 275    lines.AppendLine("Calculator Application Running (no arguments given, showing the built-in demo)...");
 276    lines.AppendLine($"2 add 2 = {calculator.Add(2, 2)}");
 277    lines.AppendLine($"2 multiply 2 = {calculator.Multiply(2, 2)}");
 278    lines.AppendLine($"2 subtract 2 = {calculator.Subtract(2, 2)}");
 279    lines.AppendLine($"2 divide 2 = {calculator.Divide(2, 2)}");
 280    lines.Append(UsageText);
 281    return lines.ToString();
 82  }
 83}
 84}

Methods/Properties

Run(System.String[])
RunDemo()