Пример #1
0
    public void DeserializeShouldThrowErrorSomethingWithInvalidFormat()
    {
        const string input = @"Count
""This is the description""";

        Assert.Throws <FormatException>(() => CsvConvert.Deserialize <SimpleExample>(input));
    }
 public void Deserialize_InvalidArgs_Exception()
 {
     Assert.Throws <ArgumentNullException>(() => CsvConvert.Deserialize(null, null));
     Assert.Throws <ArgumentNullException>(() => CsvConvert.Deserialize(_table, null));
     Assert.Throws <ArgumentException>(() => CsvConvert.Deserialize(_table, typeof(TestType)));
     Assert.Throws <ArgumentException>(() => CsvConvert.Deserialize(_table, typeof(CsvData)));
 }
Пример #3
0
    public void DeserializeNoEntriesOnlyHeader()
    {
        const string input  = @"Count,Flag,Description";
        var          result = CsvConvert.Deserialize <SimpleExample>(input);

        Assert.That(result, Is.SameAs(default(SimpleExample)));
    }
Пример #4
0
        public void FileWithHeaderDateAndSemi()
        {
            var text   = File.ReadAllText(Path.Combine("Files", "FileWithHeaderDateAndSemi.csv"));
            var result = CsvConvert.Deserialize <FileWithHeaderDateAndSemi>(text).ToList();

            result.First().Name.Should().Be("Gabs");
            result.First().DateOfBirth.Should().Be(new DateTime(1980, 10, 11));
        }
Пример #5
0
    public void DeserializeShouldHonorEnums()
    {
        const string input  = @"Status
Done";
        var          result = CsvConvert.Deserialize <EnumExample>(input);

        Assert.That(result.Status, Is.EqualTo(ExampleStatuses.Done));
    }
Пример #6
0
    public void DeserializeShouldHonorEnumDescriptions()
    {
        const string input  = @"Status
""In Progress""";
        var          result = CsvConvert.Deserialize <EnumExample>(input);

        Assert.That(result.Status, Is.EqualTo(ExampleStatuses.InProgress));
    }
Пример #7
0
        public void FileHeaderContainsQuotes()
        {
            var text   = File.ReadAllText(Path.Combine("Files", "FileWithQuotes.csv"));
            var result = CsvConvert.Deserialize <FileWithQuotes>(text).ToList();

            result.First().Title.Should().Be("Greetings");
            result.First(r => r.Title.Contains("New York")).Title.Should().Be("New York, New York");
        }
Пример #8
0
        public void FileAttributeIsDefined()
        {
            var text   = File.ReadAllText(Path.Combine("Files", "FileWithHeader.csv"));
            var result = CsvConvert.Deserialize <FileWithHeader>(text).ToList();

            result.Count().Should().Be(3);
            result.First().Name.Should().Be("Gabs");
        }
Пример #9
0
    public void DeserializeShouldNotCareAboutInputOrder()
    {
        const string input  = @"Description,Count,Flag
""This is the description"",5,true";
        var          result = CsvConvert.Deserialize <SimpleExample>(input);

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #10
0
    public void Deserialize_ShouldHonorPropertyAttribute()
    {
        const string input  = @"count,flag,desc
5,true,""This is the description""";
        var          result = CsvConvert.Deserialize <PropertyAttributeExample>(input);

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #11
0
    public void Deserialize_ShouldHandleEscapedPropertyNamesAttribute()
    {
        const string input  = @"""The Count"",""A Flag"",""A Description""
5,True,""This is the description""";
        var          result = CsvConvert.Deserialize <EscapedPropertyAttributeExample>(input);

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #12
0
    public void DeserializeShouldIgnoreHeadersThatDoNotCorrespondWithAccessors()
    {
        const string input  = @"Description,ID
""This is the description"",5";
        var          result = CsvConvert.Deserialize <SimpleExample>(input);

        Assert.That(result.Count, Is.EqualTo(0));
        Assert.That(result.Flag, Is.False);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #13
0
    public void Deserialize_ShouldHonorIgnoreAttribute()
    {
        const string input  = @"Count,Flag,Description
5,true,""This is the description""";
        var          result = CsvConvert.Deserialize <IgnoreAttributeExample>(input);

        Assert.That(result.Count, Is.EqualTo(0));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #14
0
    public void DeserializeShouldHandleMissingProperties()
    {
        const string input  = @"Count
5";
        var          result = CsvConvert.Deserialize <SimpleExample>(input);

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.False);
        Assert.That(result.Description, Is.EqualTo(default(string)));
    }
Пример #15
0
    public void DeserializeRecord()
    {
        const string input  = @"Count,Flag,Description
5,true,""This is the description""";
        var          result = CsvConvert.Deserialize <RecordExample>(input);

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #16
0
        public void FileWithHeaderAndModelWithoutAttributes()
        {
            var text   = File.ReadAllText(Path.Combine("Files", "FileWithHeaderWithoutAttributes.csv"));
            var result = CsvConvert.Deserialize <FileWithHeaderWithoutAttributes>(text).ToList();

            result.Count().Should().Be(3);
            result.First().Name.Should().Be("Gabs");
            result.First().Age.Should().Be(35);
            result.Last().Name.Should().Be("Anna");
            result.Last().Age.Should().Be(23);
        }
Пример #17
0
    public void Deserialize_ShouldHonorEscapeDoubleQuote()
    {
        const string input = @"Count,Flag,Description
5,True,""This is a quote""""";

        var result = CsvConvert.Deserialize <SimpleExample>(input);

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is a quote\""));
    }
Пример #18
0
    public void Deserialize_DifferentSeparator()
    {
        const string input  = @"Count!Flag!Description
5!true!""This is the description""";
        var          result = CsvConvert.Deserialize <SimpleExample>(input, new CsvConvertSettings
        {
            Separator = '!'
        });

        Assert.That(result.Count, Is.EqualTo(5));
        Assert.That(result.Flag, Is.True);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #19
0
    public void DeserializeShouldIgnoreProtectedAccessors()
    {
        const string input  = @"Flag,Description
true,""This is the description""";
        var          result = CsvConvert.Deserialize <AccessModifierExample>(input);

        var privateAccessors = typeof(AccessModifierExample)
                               .GetAccessors(BindingFlags.Instance | BindingFlags.NonPublic)
                               .ToDictionary(gs => gs.Name, gs => gs);

        Assert.That(privateAccessors["Flag"].Value[result], Is.False);
        Assert.That(result.Description, Is.EqualTo("This is the description"));
    }
Пример #20
0
        public void FileWithHeaderAndDateAsStream()
        {
            var text   = File.ReadAllText(Path.Combine("Files", "FileWithHeaderAndDate.csv"));
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);

            writer.Write(text);
            writer.Flush();
            stream.Position = 0;
            var result = CsvConvert.Deserialize <FileWithHeaderAndDate>(stream).ToList();

            result.First().Name.Should().Be("Gabs");
            result.First().DateOfBirth.Should().Be(new DateTime(1980, 10, 11));
        }
Пример #21
0
    public void DeserializeShouldThrowUseErrorCallbackIfProvided()
    {
        const string input = @"Count
""This is the description""";

        var onErrorWasCalled = false;

        CsvConvert.Deserialize <SimpleExample>(input, new CsvConvertSettings
        {
            OnError = _ => onErrorWasCalled = true
        });

        Assert.That(onErrorWasCalled, Is.True);
    }
Пример #22
0
        // Loads the specified CsvDataTable<TCsvData> type at the specified path, where type must be a generic.
        public override void LoadAsset(Type type, string path)
        {
            // Make sure its a generic type.
            Debug.Assert(type.IsGenericType, "type should be generic.");

            // Use the generic argument in type, as the arguments
            // in CsvConvert.Deserialize(CsvTable, Type).
            // The genericArgs should be of type CsvData.
            var genericArgs = type.GetGenericArguments()[0];

            Debug.Assert(genericArgs.BaseType == typeof(CsvData) && !genericArgs.IsAbstract);

            // Load .csv file into a CsvTable object.
            var table = new CsvTable(path);

            // Convert that CsvTable object into a CsvDataTable<TCsvData> object.
            var dataTable = CsvConvert.Deserialize(table, genericArgs);

            Table.Add(dataTable);

            _type2id.Add(type, dataTable.KindID);
        }
 public override void Execute()
 {
     CsvConvert.Deserialize(_table, typeof(BuildingData));
 }
Пример #24
0
    public void DeserializeEmptyString()
    {
        var result = CsvConvert.Deserialize <SimpleExample>("");

        Assert.That(result, Is.SameAs(default(SimpleExample)));
    }
        public void Deserialize_ReturnType()
        {
            var table = CsvConvert.Deserialize(_table, typeof(BuildingData));

            Assert.IsInstanceOf <CsvDataTable <BuildingData> >(table);
        }
Пример #26
0
        public void DeserializeIgnore()
        {
            IEnumerable <GeniusIgnore> genius = CsvConvert.Deserialize <GeniusIgnore>(Properties.Resources.sample, CultureInfo.InvariantCulture);

            Assert.AreEqual(3, geniusIgnore.Count());
        }