<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Xunit;

namespace CSharpViaTest.IOs._10_HandleText
{
    /* 
     * Description
     * ===========
     * 
     * This test will practice how to padding string and format object using IFormatProvider.
     * 
     * Difficulty: Medium
     * 
     * Knowledge Point
     * ===============
     * 
     * - string.PadLeft(), string.PadRight()
     * - A `IFormatProvider` can be passed to `ToString()` method if the object implements
     *   `IFormattable` interface. The `CultureInfo` class implements the `IFormatProvider`
     *   interface.
     */
    public class TableFormatter
    {
        struct CellFormatRule
        {
            public CellFormatRule(bool rightAligned, short cellCharacterLength)
            {
                RightAligned = rightAligned;
                CellCharacterLength = cellCharacterLength;
            }

            public bool RightAligned { get; }
            public short CellCharacterLength { get; }
        }

        #region Please modifies the code to pass the test

        static IEnumerable<string> FormatTable(
            IEnumerable<IEnumerable<object>> data,
            CellFormatRule[] rules,
            IFormatProvider formatProvider)
        {
            throw new NotImplementedException();
        }

        #endregion

        [Fact]
        public void should_format_table_correctly()
        {
            var rules = new[]
            {
                new CellFormatRule(false, 20),
                new CellFormatRule(false, 20),
                new CellFormatRule(true, 5)
            };

            IEnumerable<IEnumerable<object>> data = new[]
            {
                new[] {"Name", "Company", "Id"},
                new[] {"Edogawa Conan", "Tokyo", "C01"},
                new[] {"Furugawa Nagisa", "Kyoto Animation", "C02"},
                new[] {"Kirigaya Katsuto", "A1 Pictures", "C03"}
            };

            IEnumerable<string> table = FormatTable(data, rules, CultureInfo.InvariantCulture);

            Assert.Equal(
                new[]
                {
                    "Name                Company                Id",
                    "Edogawa Conan       Tokyo                 C01",
                    "Furugawa Nagisa     Kyoto Animation       C02",
                    "Kirig<NAME>    A1 Pictures           C03"
                },
                table);
        }

        [Fact]
        public void should_throw_if_cell_count_is_not_equal_with_actual_data()
        {
            var rules = new[]
            {
                new CellFormatRule(false, 20),
                new CellFormatRule(false, 20),
                new CellFormatRule(true, 5)
            };

            Assert.Throws<ArgumentException>(
                () => FormatTable(
                        new[] {new[] {"c1", "c2", "c3", "c4"}},
                        rules,
                        CultureInfo.InvariantCulture)
                    .ToArray());
        }

        [Fact]<commit_msg>[liuxia] Modify table formatter test for culture formatting.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Xunit;

namespace CSharpViaTest.IOs._10_HandleText
{
    /* 
     * Description
     * ===========
     * 
     * This test will practice how to padding string and format object using IFormatProvider.
     * 
     * Difficulty: Medium
     * 
     * Knowledge Point
     * ===============
     * 
     * - string.PadLeft(), string.PadRight()
     * - A `IFormatProvider` can be passed to `ToString()` method if the object implements
     *   `IFormattable` interface. The `CultureInfo` class implements the `IFormatProvider`
     *   interface.
     */
    public class TableFormatter
    {
        struct CellFormatRule
        {
            public CellFormatRule(bool rightAligned, short cellCharacterLength)
            {
                RightAligned = rightAligned;
                CellCharacterLength = cellCharacterLength;
            }

            public bool RightAligned { get; }
            public short CellCharacterLength { get; }
        }

        #region Please modifies the code to pass the test

        static IEnumerable<string> FormatTable(
            IEnumerable<IEnumerable<object>> data,
            CellFormatRule[] rules,
            IFormatProvider formatProvider)
        {
            throw new NotImplementedException();
        }

        #endregion

        [Fact]
        public void should_format_table_correctly()
        {
            var rules = new[]
            {
                new CellFormatRule(false, 20),
                new CellFormatRule(false, 20),
                new CellFormatRule(true, 5)
            };

            IEnumerable<IEnumerable<object>> data = new[]
            {
                new object[] {"Name", "Company", "Id"},
                new object[] {"<NAME>", "Tokyo", 1.1},
                new object[] {"<NAME>", "Kyoto Animation", 1.1},
                new object[] {"<NAME>", "A1 Pictures", 1.1}
            };

            IEnumerable<string> table = FormatTable(data, rules, new CultureInfo("af-ZA"));

            Assert.Equal(
                new[]
                {
                    "Name                Company                Id",
                    "<NAME>       Tokyo                 1,1",
                    "<NAME>     Kyoto Animation       1,1",
                    "<NAME>    A1 Pictures           1,1"
                },
                table);
        }

        [Fact]
        public void should_throw_if_cell_count_is_not_equal_with_actual_data()
        {
            var rules = new[]
            {
                new CellFormatRule(false, 20),
                new CellFormatRule(false, 20),
                new CellFormatRule(true, 5)
            };

            Assert.Throws<ArgumentException>(
                () => FormatTable(
                        new[] {new[] {"c1", "c2", "c3", "c4"}},
                        rules,
                        CultureInfo.InvariantCulture)
                    .ToArray());
        }

        [Fact]
{'fraction_non_alphanumeric': 0.09916156078684295, 'fraction_numerical': 0.009674298613350531, 'mean_word_length': 1.4211553473848555, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '251230', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1523, 'n_words': 384}