-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to specify a constant value for a mapped property. When…
… reading, the same value gets set for every record for the property. When writing, the same value gets written for every record for the property.
- Loading branch information
Showing
9 changed files
with
182 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using CsvHelper.Configuration; | ||
using CsvHelper.Tests.Mocks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace CsvHelper.Tests.Reading | ||
{ | ||
[TestClass] | ||
public class ConstantTests | ||
{ | ||
[TestMethod] | ||
public void ConstantAlwaysReturnsSameValueTest() | ||
{ | ||
var rows = new Queue<string[]>(); | ||
rows.Enqueue( new[] { "Id", "Name" } ); | ||
rows.Enqueue( new[] { "1", "one" } ); | ||
rows.Enqueue( new[] { "2", "two" } ); | ||
rows.Enqueue( null ); | ||
var parser = new ParserMock( rows ); | ||
|
||
var csv = new CsvReader( parser ); | ||
csv.Configuration.RegisterClassMap<TestMap>(); | ||
var records = csv.GetRecords<Test>().ToList(); | ||
|
||
Assert.AreEqual( 1, records[0].Id ); | ||
Assert.AreEqual( "constant", records[0].Name ); | ||
Assert.AreEqual( 2, records[1].Id ); | ||
Assert.AreEqual( "constant", records[0].Name ); | ||
} | ||
|
||
private class Test | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
private sealed class TestMap : CsvClassMap<Test> | ||
{ | ||
public TestMap() | ||
{ | ||
Map( m => m.Id ); | ||
Map( m => m.Name ).Constant( "constant" ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using CsvHelper.Configuration; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace CsvHelper.Tests.Writing | ||
{ | ||
[TestClass] | ||
public class ConstantTests | ||
{ | ||
[TestMethod] | ||
public void SameValueIsWrittenForEveryRecordTest() | ||
{ | ||
using( var stream = new MemoryStream() ) | ||
using( var reader = new StreamReader( stream ) ) | ||
using( var writer = new StreamWriter( stream ) ) | ||
using( var csv = new CsvWriter( writer ) ) | ||
{ | ||
var records = new List<Test> | ||
{ | ||
new Test { Id = 1, Name = "one" }, | ||
new Test { Id = 2, Name = "two" } | ||
}; | ||
|
||
csv.Configuration.RegisterClassMap<TestMap>(); | ||
csv.WriteRecords( records ); | ||
writer.Flush(); | ||
stream.Position = 0; | ||
|
||
var expected = new StringBuilder(); | ||
expected.AppendLine( "Id,Name" ); | ||
expected.AppendLine( "1,constant" ); | ||
expected.AppendLine( "2,constant" ); | ||
|
||
var result = reader.ReadToEnd(); | ||
|
||
Assert.AreEqual( expected.ToString(), result ); | ||
} | ||
} | ||
|
||
private class Test | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
private sealed class TestMap : CsvClassMap<Test> | ||
{ | ||
public TestMap() | ||
{ | ||
Map( m => m.Id ); | ||
Map( m => m.Name ).Constant( "constant" ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters