< Summary

Class:NanoCLang.StringFormatter
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\StringFormatter.cs
Covered lines:25
Uncovered lines:9
Coverable lines:34
Total lines:74
Line coverage:73.5% (25 of 34)
Covered branches:11
Total branches:12
Branch coverage:91.6% (11 of 12)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
ToString(...)100%2100%
AppendToken(...)87.5%890%
NewLine()100%1100%
PrintIndent()100%2100%
Indented(...)100%10%

File(s)

C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\StringFormatter.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace NanoCLang {
 6    /// <summary>
 7    /// Provides a class for formatting a NanoC entity to a string representation.
 8    /// </summary>
 9    public class StringFormatter {
 10        private const int DEFAULT_INDENT = 4;
 11        private const char INDENT_CHAR = ' ';
 134912        private readonly StringBuilder sb = new StringBuilder();
 134913        private int indent = 0;
 14
 15        /// <summary>
 16        /// Creates a string representation of the <paramref name="tokens"/> stream.
 17        /// </summary>
 18        /// <param name="tokens">Token stream that is converted to a string representation.</param>
 19        /// <returns>String representation of the <paramref name="tokens"/>.</returns>
 134920        public string ToString(IEnumerable<StringFormatterToken> tokens) {
 6484821            foreach (var token in tokens) {
 2026722                AppendToken(token);
 2026723            }
 134924            return sb.ToString();
 134925        }
 26
 27        /// <summary>
 28        /// Appends the <paramref name="token"/> to the string builder.
 29        /// </summary>
 30        /// <param name="token">Token to append</param>
 31        /// <exception cref="ArgumentOutOfRangeException">Token has unexpected type.</exception>
 2026732        internal StringFormatter AppendToken(StringFormatterToken token) {
 2026733            switch (token) {
 3926034            case StringToken tk: sb.Append(tk.Value); break;
 91835            case NewLineToken _: NewLine(); break;
 36            case IndentChangeToken tk:
 23437                if (tk.Amount == 0) PrintIndent();
 12238                else indent += tk.Amount;
 17839                break;
 040            default: throw new ArgumentOutOfRangeException("Unknown token type!");
 41            }
 2026742            return this;
 2026743        }
 44
 45        /// <summary>
 46        /// Inserts a new line into the formatter while keeping track of the current indentation.
 47        /// </summary>
 48        /// <returns>Formatter after the new line and indent was inserted.</returns>
 45949        public StringFormatter NewLine() {
 45950            sb.AppendLine();
 45951            PrintIndent();
 45952            return this;
 45953        }
 54
 51555        private void PrintIndent() {
 353856            for (int i = 0; i < indent; i++) sb.Append(INDENT_CHAR);
 51557        }
 58
 59        /// <summary>
 60        /// Prints a multi-line text block containing <see cref="NewLine"/> while indenting the block further than the c
 61        /// </summary>
 62        /// <param name="print">Function of the formatter that prints the contents of the block.</param>
 63        /// <param name="amount">Amount of indentation.</param>
 64        /// <returns>Formatter after the new indented block was inserted.</returns>
 065        public StringFormatter Indented(Func<StringFormatter, StringFormatter> print, int amount = DEFAULT_INDENT) {
 066            indent += amount;
 067            try {
 068                return print(this);
 069            } finally {
 070                indent -= amount;
 071            }
 072        }
 73    }
 74}