Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions strings-csharp/ToStringMethod/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
16 changes: 13 additions & 3 deletions strings-csharp/ToStringMethod/Tests/ToStringMethodUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void WhenPersonDetailsArePassed_TheReturnString()
}

[Theory]
[InlineData("en-GB", "Range Rover Vogue costs £200,000.00 and was sold on 12/02/2022 20:50:10")]
[InlineData("ja-JP", "Range Rover Vogue costs ¥200,000 and was sold on 12/02/2022 20:50:10")]
[InlineData("en-GB", "Range Rover Vogue costs £200,000.00 and was sold on 02/12/2022 20:50:10")]
[InlineData("ja-JP", "Range Rover Vogue costs ¥200,000 and was sold on 2022/12/02 20:50:10")]
public void WhenCultureIsPassed_ThenReturnFormattedCurrency(string culture, string expected)
{
var result = _car.ToString(culture);
Expand All @@ -39,7 +39,7 @@ public void WhenCultureIsPassed_ThenReturnFormattedCurrency(string culture, stri
[Theory]
[InlineData("en-GB", "dd/MM/yyyy", "Range Rover Vogue costs £200,000.00 and was sold on 02/12/2022")]
[InlineData("en-US", "MM/dd/yyyy", "Range Rover Vogue costs $200,000.00 and was sold on 12/02/2022")]
[InlineData("ja-JP", "yyyy-MMM-dd", "Range Rover Vogue costs ¥200,000 and was sold on 2022-Dec-02")]
[InlineData("ja-JP", "yyyy-MM-dd", "Range Rover Vogue costs ¥200,000 and was sold on 2022-12-02")]
public void WhenCultureAndDateFormatIsSpecified_ThenReturnFormattedString(string culture, string dateFormat,
string expected)
{
Expand All @@ -49,4 +49,14 @@ public void WhenCultureAndDateFormatIsSpecified_ThenReturnFormattedString(string
Assert.NotNull(result);
Assert.Equal(expected, result);
}

[Fact]
public void WhenRecordIsPrinted_ThenReturnPropertyNamesAndValues()
{
var personRecord = new PersonRecord("Jane Doe", 26, "Secretary");

var result = personRecord.ToString();

Assert.Equal("PersonRecord { Name = Jane Doe, Age = 26, Profession = Secretary }", result);
}
}
11 changes: 7 additions & 4 deletions strings-csharp/ToStringMethod/ToStringMethod/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ public Car(string make, string model, decimal price, DateTime soldAt)

public string ToString(string culture)
{
var price = Price.ToString("C", new CultureInfo(culture));
var cultureInfo = new CultureInfo(culture);
var price = Price.ToString("C", cultureInfo);
var saleDate = SoldAt.ToString(cultureInfo);

return $"{Make} {Model} costs {price} and was sold on {SoldAt}";
return $"{Make} {Model} costs {price} and was sold on {saleDate}";
}

public string ToString(string culture, string dateFormat)
{
var price = Price.ToString("C", new CultureInfo(culture));
var saleDate = SoldAt.ToString(dateFormat);
var cultureInfo = new CultureInfo(culture);
var price = Price.ToString("C", cultureInfo);
var saleDate = SoldAt.ToString(dateFormat, cultureInfo);

return $"{Make} {Model} costs {price} and was sold on {saleDate}";
}
Expand Down
3 changes: 3 additions & 0 deletions strings-csharp/ToStringMethod/ToStringMethod/PersonRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ToStringMethod;

public record PersonRecord(string Name, int Age, string Profession);
11 changes: 7 additions & 4 deletions strings-csharp/ToStringMethod/ToStringMethod/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using ToStringMethod;

var person = new Person("John Doe", 30, "Software Developer");
person.ToString();
Console.WriteLine(person.ToString());

var car = new Car("Tesla", "Model S", 100000M, DateTime.Now);
car.ToString("en-US");
car.ToString("en-US", "dd-MMM-yyyy");
var car = new Car("Tesla", "Model S", 100000M, new DateTime(2022, 12, 2, 17, 10, 56));
Console.WriteLine(car.ToString("en-US"));
Console.WriteLine(car.ToString("en-US", "dd-MMM-yyyy"));

var personRecord = new PersonRecord("John Doe", 30, "Software Developer");
Console.WriteLine(personRecord);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
Expand Down
Loading