Skip to content

Commit

Permalink
Add some smaller stuff to csv handler, not perfect or finished but at…
Browse files Browse the repository at this point in the history
… least some groundwork is done.
  • Loading branch information
LoneWandererProductions committed Jun 17, 2024
1 parent b2fa02c commit ceee0fa
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 47 deletions.
64 changes: 55 additions & 9 deletions CommonLibraryTests/DataFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DataFormatterTests
/// <summary>
/// The test file path
/// </summary>
private const string TestFilePath = "testfile.txt";
private readonly string _testFilePath = Path.Combine(Directory.GetCurrentDirectory(), "testFile.txt");

/// <summary>
/// Sets up.
Expand All @@ -33,7 +33,7 @@ public class DataFormatterTests
public void SetUp()
{
// Create a test file with UTF-8 encoding and special characters
using var writer = new StreamWriter(TestFilePath, false, Encoding.UTF8);
using var writer = new StreamWriter(_testFilePath, false, Encoding.UTF8);
writer.WriteLine("Line 1 with Ü");
writer.WriteLine("Line 2 with ö");
}
Expand All @@ -58,17 +58,17 @@ public void Cvs()
lst.Add(line);
}

CsvHandler.WriteCsv(TestFilePath, lst);
CsvHandler.WriteCsv(_testFilePath, lst);

Assert.IsTrue(File.Exists(TestFilePath), "File exists");
Assert.IsTrue(File.Exists(_testFilePath), "File exists");

lst = CsvHandler.ReadCsv(TestFilePath, ',');
lst = CsvHandler.ReadCsv(_testFilePath, ',');

Assert.AreEqual("0", lst[1][0], "Right Element");

Assert.AreEqual("9", lst[2][9], "Right Element");

FileHandleDelete.DeleteFile(TestFilePath);
FileHandleDelete.DeleteFile(_testFilePath);
}

/// <summary>
Expand All @@ -78,9 +78,9 @@ public void Cvs()
public void TearDown()
{
// Clean up the test file
if (File.Exists(TestFilePath))
if (File.Exists(_testFilePath))
{
File.Delete(TestFilePath);
File.Delete(_testFilePath);
}
}

Expand All @@ -94,10 +94,56 @@ public void ReadFileShouldReadSpecialCharacters()
var expectedLines = new List<string> { "Line 1 with Ü", "Line 2 with ö" };

// Act
var actualLines = ReadText.ReadFile(TestFilePath);
var actualLines = ReadText.ReadFile(_testFilePath);

// Assert
CollectionAssert.AreEqual(expectedLines, actualLines);
}

[TestMethod]
public void TestReadCsv()
{
// Arrange
const string csvContent = "123,Hello\n456,World";
File.WriteAllText(_testFilePath, csvContent);
const char separator = ',';

// Act
List<MyCustomType> result = CsvHandler.ReadCsv<MyCustomType>(_testFilePath, separator);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count);

Assert.AreEqual(123, result[0].Property1);
Assert.AreEqual("Hello", result[0].Property2);

Assert.AreEqual(456, result[1].Property1);
Assert.AreEqual("World", result[1].Property2);
}
}

/// <summary>
/// Test Class
/// </summary>
internal class MyCustomType
{
/// <summary>
/// Gets or sets the property1.
/// </summary>
/// <value>
/// The property1.
/// </value>
[CsvColumn(0, typeof(IntAttributeConverter))]
public int Property1 { get; set; }

/// <summary>
/// Gets or sets the property2.
/// </summary>
/// <value>
/// The property2.
/// </value>
[CsvColumn(1, typeof(StringAttributeConverter))]
public string Property2 { get; set; }
}
}
61 changes: 61 additions & 0 deletions DataFormatter/AttributeConverters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: DataFormatter
* FILE: DataFormatter/AttributeConverter.cs
* PURPOSE: Helper for csv, Interface and implementation of AttributeConverter.
* PROGRAMER: Peter Geinitz (Wayfarer)
*/

namespace DataFormatter
{
/// <summary>
/// Interface for AttributeConverter Converter
/// </summary>
internal interface IAttributeConverter
{
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Generic return Type.</returns>
object Convert(string value);
}

/// <inheritdoc />
/// <summary>
/// Implementation of AttributeConverter for int
/// </summary>
/// <seealso cref="T:DataFormatter.IAttributeConverter" />
public sealed class IntAttributeConverter : IAttributeConverter
{
/// <inheritdoc />
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Converted to int</returns>
public object Convert(string value)
{
return int.Parse(value);
}
}

/// <inheritdoc />
/// <summary>
/// Implementation of AttributeConverter for string
/// </summary>
/// <seealso cref="T:DataFormatter.IAttributeConverter" />
public sealed class StringAttributeConverter : IAttributeConverter
{
/// <inheritdoc />
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Converted to string</returns>
public object Convert(string value)
{
return value;
}
}
}
49 changes: 49 additions & 0 deletions DataFormatter/CsvColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: DataFormatter
* FILE: DataFormatter/CsvColumnAttribute.cs
* PURPOSE: Helper for csv, Helps converter
* PROGRAMER: Peter Geinitz (Wayfarer)
*/

using System;

namespace DataFormatter
{
/// <inheritdoc />
/// <summary>
/// Needed for our custom csv conversion
/// </summary>
/// <seealso cref="T:System.Attribute" />
[AttributeUsage(AttributeTargets.Property)]
public sealed class CsvColumnAttribute : Attribute
{
/// <summary>
/// Gets the index of the row.
/// </summary>
/// <value>
/// The index.
/// </value>
internal int Index { get; }

/// <summary>
/// Gets the type of the converter.
/// </summary>
/// <value>
/// The type of the converter.
/// </value>
internal Type ConverterType { get; }

/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="T:DataFormatter.CsvColumnAttribute" /> class.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="converterType">Type of the converter.</param>
public CsvColumnAttribute(int index, Type converterType)
{
Index = index;
ConverterType = converterType;
}
}
}
Loading

0 comments on commit ceee0fa

Please sign in to comment.