Exemplo n.º 1
0
        public void CsvFormatter_with_non_standard_charset_should_get_the_encoding_right()
        {
            var formatter = new CsvFormatter(';', '"', '\\', "\r\n", CsvQuotingStyle.Required, Encoding.Unicode);
            var csv       = formatter.ToCsv(new[] { "ett", "två", "อักษรไทย" });

            csv.ShouldBeEquivalentTo(ByteString.FromString("ett;två;อักษรไทย\r\n", Encoding.Unicode));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a Flow for converting <see cref="IImmutableList{string}"/> to ByteString.
        /// </summary>
        /// <param name="delimiter">Value delimiter, defaults to comma</param>
        /// <param name="quoteChar">Quote character, defaults to double quote</param>
        /// <param name="escapeChar">Escape character, defaults to backslash</param>
        /// <param name="endOfLine">Line ending (default CR, LF)</param>
        /// <param name="quotingStyle">Quote all fields, or only fields requiring quotes (default)</param>
        /// <param name="encoding">Character encoding, defaults to UTF-8</param>
        /// <param name="byteOrderMark">Certain CSV readers (namely Microsoft Excel) require a Byte Order mark, defaults to None</param>
        public static Flow <ImmutableList <string>, ByteString, NotUsed> Format(
            char delimiter               = Comma,
            char quoteChar               = DoubleQuote,
            char escapeChar              = Backslash,
            string endOfLine             = "\r\n",
            CsvQuotingStyle quotingStyle = CsvQuotingStyle.Required,
            Encoding encoding            = null,
            ByteString byteOrderMark     = null)
        {
            var formatter = new CsvFormatter(delimiter, quoteChar, escapeChar, endOfLine, quotingStyle, encoding);

            if (byteOrderMark == null)
            {
                return(Flow.FromFunction <ImmutableList <string>, ByteString>(list => formatter.ToCsv(list))
                       .Named("CsvFormatting"));
            }

            return(Flow.FromFunction <ImmutableList <string>, ByteString>(list => formatter.ToCsv(list))
                   .Named("CsvFormatting")
                   .Prepend(Source.Single(byteOrderMark)));
        }
Exemplo n.º 3
0
 private void ExpectInOut(CsvFormatter formatter, string[] strIn, string strOut)
 {
     formatter.ToCsv(strIn).DecodeString().Should().Be(strOut);
 }