CustomConverter2.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpersTests » CommonTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpersTests » CommonTests » CustomConverter2.cs
using FileHelpers;
using NUnit.Framework;

namespace FileHelpersTests.CommonTests{
  // SPECIAL FIELD
  public class AddressField
  {
    public string Street;
    public string Number;
    public string City;

    public override string ToString()
    {
      return Street + " - " + Number + ", " + City;
    }

  }

  // CUSTOM CONVERTER
  public class AddressConverter : ConverterBase
  {
    public override object StringToField(string from)
    {
      string[] splited1 = from.Split(',');
      string[] splited2 = splited1[0].Split('-');

      AddressField res = new AddressField();
      res.Street = splited2[0].Trim();
      res.Number = splited2[1].Trim();
      res.City = splited1[1].Trim();

      return res;
    }
  }

  // CUSTOM CONVERTER
  public class AddressConverter2 : ConverterBase
  {
    char mSep1;
    char mSep2;
    public AddressConverter2(string sep1, string sep2)
    {
      mSep1 = sep1[0];
      mSep2 = sep2[0];
    }
    public override object StringToField(string from)
    {
      string[] splited1 = from.Split(mSep1);
      string[] splited2 = splited1[0].Split(mSep2);

      AddressField res = new AddressField();
      res.Street = splited2[0].Trim();
      res.Number = splited2[1].Trim();
      res.City = splited1[1].Trim();

      return res;
    }
  }


  // TEST CLASS
  [DelimitedRecord("|")]
  public class AddressConvClass
  {
    [FieldConverter(typeof (AddressConverter))] public AddressField Address;
    public int Age;
  }

  // TEST CLASS
  [DelimitedRecord("|")]
  public class AddressConvClass2
  {
    [FieldConverter(typeof (AddressConverter2), ",", "-")]
    public AddressField Address;
    public int Age;
  }

  // NUNIT TESTS
  [TestFixture]
  public class CustomConvertAddress
  {
    FileHelperEngine engine;

    [Test]
    public void NameConverterTest()
    {
      engine = new FileHelperEngine(typeof (AddressConvClass));

      AddressConvClass[] res = (AddressConvClass[]) Common.ReadTest(engine, @"Good\CustomConverter2.txt");

      Assert.AreEqual(4, res.Length);

      Assert.AreEqual("Bahia Blanca", res[0].Address.City);
      Assert.AreEqual("Sin Nombre", res[0].Address.Street);
      Assert.AreEqual("13", res[0].Address.Number);

      Assert.AreEqual("Saavedra", res[1].Address.City);
      Assert.AreEqual("Florencio Sanches", res[1].Address.Street);
      Assert.AreEqual("s/n", res[1].Address.Number);

      Assert.AreEqual("Bs.As", res[2].Address.City);
      Assert.AreEqual("12 de Octubre", res[2].Address.Street);
      Assert.AreEqual("4", res[2].Address.Number);

      Assert.AreEqual("Chilesito", res[3].Address.City);
      Assert.AreEqual("Pololo", res[3].Address.Street);
      Assert.AreEqual("5421", res[3].Address.Number);

    }


    [Test]
    public void NameConverterTest2()
    {
      engine = new FileHelperEngine(typeof (AddressConvClass2));

      AddressConvClass2[] res = (AddressConvClass2[]) Common.ReadTest(engine, @"Good\CustomConverter2.txt");

      Assert.AreEqual(4, res.Length);

      Assert.AreEqual("Bahia Blanca", res[0].Address.City);
      Assert.AreEqual("Sin Nombre", res[0].Address.Street);
      Assert.AreEqual("13", res[0].Address.Number);

      Assert.AreEqual("Saavedra", res[1].Address.City);
      Assert.AreEqual("Florencio Sanches", res[1].Address.Street);
      Assert.AreEqual("s/n", res[1].Address.Number);

      Assert.AreEqual("Bs.As", res[2].Address.City);
      Assert.AreEqual("12 de Octubre", res[2].Address.Street);
      Assert.AreEqual("4", res[2].Address.Number);

      Assert.AreEqual("Chilesito", res[3].Address.City);
      Assert.AreEqual("Pololo", res[3].Address.Street);
      Assert.AreEqual("5421", res[3].Address.Number);

    }

    // TEST CLASS
    [DelimitedRecord("|")]
    public class AddressBadClass1
    {
      [FieldConverter(typeof (AddressConverter2), ",")]
      public AddressField Address;
      public int Age;
    }

    [DelimitedRecord("|")]
    public class AddressBadClass2
    {
      [FieldConverter(typeof (AddressConverter2))]
      public AddressField Address;
      public int Age;
    }

    [DelimitedRecord("|")]
    public class AddressBadClass3
    {
      [FieldConverter(typeof (AddressConverter2), 3, 58.25)]
      public AddressField Address;
      public int Age;
    }

    [Test]
    [ExpectedException(typeof(BadUsageException), ExpectedMessage = "Constructor for converter: AddressConverter2 with these arguments: (String) was not found. You must add a constructor with this signature (can be public or private)")]
    public void NameConverterBad1()
    {
      engine = new FileHelperEngine(typeof (AddressBadClass1));
    }

    [Test]
    [ExpectedException(typeof(BadUsageException), ExpectedMessage = "Empty constructor for converter: AddressConverter2 was not found. You must add a constructor without args (can be public or private)")]
    public void NameConverterBad2()
    {
      engine = new FileHelperEngine(typeof (AddressBadClass2));
    }

    [Test]
    [ExpectedException(typeof(BadUsageException), ExpectedMessage = "Constructor for converter: AddressConverter2 with these arguments: (Int32, Double) was not found. You must add a constructor with this signature (can be public or private)")]
    public void NameConverterBad3()
    {
      engine = new FileHelperEngine(typeof (AddressBadClass3));
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.