예제 #1
0
        public void SerializesHeader()
        {
            var subject = new CsjSerializer <MovieExample>();

            var result = subject.Serialize(Enumerable.Empty <MovieExample>());

            Check.That(result).IsEqualTo(@"""Slug"",""Title"",""Length"",""Released"",""Tags"",""WatchedTimes""" + Environment.NewLine);
        }
예제 #2
0
        public void NestedArrays()
        {
            var subject = new CsjSerializer();
            var input   = new object[] { 42, new[] { "foo", "bar" } };

            var output = subject.Serialize(new List <object[]> {
                input
            });

            Check.That(output).IsEqualTo("42,[\"foo\",\"bar\"]" + Environment.NewLine);
        }
예제 #3
0
        public void Null_IsALiteral()
        {
            var subject = new CsjSerializer();
            var input   = new object[] { null, null };

            var output = subject.Serialize(new List <object[]> {
                input
            });

            Check.That(output).IsEqualTo("null,null" + Environment.NewLine);
        }
예제 #4
0
        public void Numbers_AreLiterals()
        {
            var subject = new CsjSerializer();
            var input   = new object[] { 42, 15.759 };

            var output = subject.Serialize(new List <object[]> {
                input
            });

            Check.That(output).IsEqualTo("42,15.759" + Environment.NewLine);
        }
예제 #5
0
        public void Strings_AreEscaped()
        {
            var subject = new CsjSerializer();
            var strings = new[] { "foo\nbar", "quote:\"me\"" };

            var output = subject.Serialize(new List <object[]> {
                strings
            });

            Check.That(output).IsEqualTo("\"foo\\nbar\",\"quote:\\\"me\\\"\"" + Environment.NewLine);
        }
예제 #6
0
        public void Strings_WrappedInDoubleQuotes()
        {
            var subject = new CsjSerializer();
            var strings = new[] { "foo", "bar" };

            var output = subject.Serialize(new List <object[]> {
                strings
            });

            Check.That(output).IsEqualTo("\"foo\",\"bar\"" + Environment.NewLine);
        }
예제 #7
0
        public void NestedObjects()
        {
            var subject = new CsjSerializer();
            var input   = new object[] { 42, new NestedObject()
                                         {
                                             Foo = "foo",
                                             Baz = "baz"
                                         } };

            var output = subject.Serialize(new List <object[]> {
                input
            });

            Check.That(output).IsEqualTo("42,{\"Foo\":\"foo\",\"Baz\":\"baz\"}" + Environment.NewLine);
        }
예제 #8
0
        public void Dates_AreISOFormatted()
        {
            var subject = new CsjSerializer();
            var dates   = new object[]
            {
                new DateTime(2015, 2, 3, 10, 37, 15, DateTimeKind.Utc),
                new DateTime(2015, 2, 3, 10, 37, 15, DateTimeKind.Local),
            };

            var output = subject.Serialize(new List <object[]> {
                dates
            });
            var currentOffset = TimeZoneInfo.Local.BaseUtcOffset;
            var sign          = currentOffset.Hours >= 0 ? '+' : '-';

            Check.That(output).IsEqualTo($"\"2015-02-03T10:37:15Z\",\"2015-02-03T10:37:15{sign}{currentOffset:hh':'mm}\"" + Environment.NewLine);
        }
예제 #9
0
        public void SerializesRow()
        {
            var subject = new CsjSerializer <MovieExample>();

            var result = subject.Serialize(new[]
            {
                new MovieExample
                {
                    Slug         = "t1",
                    Title        = "Terminator",
                    Length       = 127,
                    Released     = new DateTime(1984, 10, 26, 0, 0, 0, DateTimeKind.Utc),
                    Tags         = new[] { "adventure", "action" },
                    WatchedTimes = null
                }
            });

            var firstRow = result.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();

            Check.That(firstRow)
            .IsEqualTo(@"""t1"",""Terminator"",127,""1984-10-26T00:00:00Z"",[""adventure"",""action""],null");
        }
예제 #10
0
        public void SerializesToStream()
        {
            var subject = new CsjSerializer <MovieExample>();
            var stream  = new StringWriter();

            subject.Serialize(stream, new[]
            {
                new MovieExample
                {
                    Slug         = "t1",
                    Title        = "Terminator",
                    Length       = 127,
                    Released     = new DateTime(1984, 10, 26, 0, 0, 0, DateTimeKind.Utc),
                    Tags         = new[] { "adventure", "action" },
                    WatchedTimes = null
                }
            });

            Check.That(stream.ToString()).IsEqualTo(
                @"""Slug"",""Title"",""Length"",""Released"",""Tags"",""WatchedTimes""" + Environment.NewLine +
                @"""t1"",""Terminator"",127,""1984-10-26T00:00:00Z"",[""adventure"",""action""],null" + Environment.NewLine
                );
        }
예제 #11
0
        public void CanRoundTripLargeCollectionToFile()
        {
            var samples      = GenerateSamples().Take(100).ToArray();
            var serializer   = new CsjSerializer <Sample>();
            var deserializer = new CsjDeserializer <Sample>();

            var filename = Path.GetTempFileName();

            using (var writer = new StreamWriter(File.OpenWrite(filename), Encoding.UTF8))
            {
                serializer.Serialize(writer, samples);
            }

            using (var reader = new StreamReader(File.OpenRead(filename), Encoding.UTF8))
            {
                var result = deserializer.Deserialize(reader).ToArray();
                for (var i = 0; i < samples.Length; i++)
                {
                    Check.That(result[i]).HasFieldsWithSameValues(samples[i]);
                }
            }

            File.Delete(filename);
        }