Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FolkerKinzel committed Jan 24, 2025
1 parent 9b6a84d commit 2ed6e27
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public void DateTest4()
[TestMethod]
public void RoundtripsTest()
{
Roundtrip("1984", true, VCdVersion.V3_0);
Roundtrip("1984", true, VCdVersion.V4_0);

Roundtrip("1984-02", true, VCdVersion.V3_0);
Roundtrip("1984-02", true, VCdVersion.V4_0);

Roundtrip("---17", true, VCdVersion.V3_0);
Roundtrip("---17", true, VCdVersion.V4_0);

Roundtrip("--12", true, VCdVersion.V3_0);
Roundtrip("--12", true, VCdVersion.V4_0);

Roundtrip("1972-01-31", true, VCdVersion.V3_0);
Roundtrip("19720131T15-07", false, VCdVersion.V3_0);
Roundtrip("19720131T15+04", false, VCdVersion.V3_0);
Expand Down Expand Up @@ -104,8 +116,8 @@ static string ToDateTimeString(DateAndOrTime dt, VCdVersion version)
{
var builder = new StringBuilder();
dt.Switch(
dateOnly => DateAndOrTimeConverter.AppendDateTo(builder, dateOnly, version),
dto => DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, dto, version)
dateOnly => DateAndOrTimeConverter.AppendDateTo(builder, dateOnly, version, dt.HasMonth, dt.HasDay),
dto => DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, dto, version, dt.HasMonth, dt.HasDay)
);
return builder.ToString();
}
Expand Down Expand Up @@ -141,39 +153,39 @@ static string ToTimeStamp(DateTimeOffset dt, VCdVersion version)
public void AppendDateTimeStringToTest1()
{
var builder = new StringBuilder();
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, default, VCdVersion.V3_0);
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, default, VCdVersion.V3_0, true, true);
Assert.AreEqual(0, builder.Length);
}

[TestMethod]
public void AppendDateTimeStringToTest2a()
{
var builder = new StringBuilder();
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(2, 1, 1, 0, 0, 0, DateTimeKind.Utc), VCdVersion.V4_0);
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(2, 1, 1, 0, 0, 0, DateTimeKind.Utc), VCdVersion.V4_0, true, true);
Assert.AreEqual(0, builder.Length);
}

[TestMethod]
public void AppendDateTimeStringToTest2b()
{
var builder = new StringBuilder();
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(2, 1, 2, 0, 0, 0, DateTimeKind.Utc), VCdVersion.V4_0);
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(2, 1, 2, 0, 0, 0, DateTimeKind.Utc), VCdVersion.V4_0, true, true);
Assert.AreEqual(0, builder.Length);
}

[TestMethod]
public void AppendDateTimeStringToTest2c()
{
var builder = new StringBuilder();
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(2, 1, 1, 0, 0, 0, DateTimeKind.Utc), VCdVersion.V3_0);
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(2, 1, 1, 0, 0, 0, DateTimeKind.Utc), VCdVersion.V3_0, true, true);
Assert.AreEqual(0, builder.Length);
}

[TestMethod]
public void AppendDateTimeStringToTest3()
{
var builder = new StringBuilder();
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(4, 1, 1), VCdVersion.V4_0);
DateAndOrTimeConverter.AppendDateTimeOffsetTo(builder, new DateTime(4, 1, 1), VCdVersion.V4_0, true, true);
string s = builder.ToString();
Assert.IsTrue(s.StartsWith("--"));
}
Expand All @@ -182,7 +194,7 @@ public void AppendDateTimeStringToTest3()
public void AppendDateToTest1()
{
var builder = new StringBuilder();
DateAndOrTimeConverter.AppendDateTo(builder, new DateOnly(4, 5, 1), VCdVersion.V3_0);
DateAndOrTimeConverter.AppendDateTo(builder, new DateOnly(4, 5, 1), VCdVersion.V3_0, true, true);
string s = builder.ToString();
Assert.IsTrue(s.StartsWith("--"));
}
Expand Down
23 changes: 21 additions & 2 deletions src/FolkerKinzel.VCards.Tests/Models/DateAndOrTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,27 @@ public void EqualsTest1()
[TestMethod]
public void AsStringTest1()
{
var daot = DateAndOrTime.Create(2, 4);
var daot = DateAndOrTime.Create(new DateOnly(1984, 2, 4), ignoreYear: true);
string str = daot.AsString(CultureInfo.InvariantCulture);
Assert.AreEqual("02/04", str);
Assert.AreEqual("--02-04", str);
}


[TestMethod]
public void CreateTest1()
{
var daot = DateAndOrTime.Create(new DateOnly(1984, 3, 17), ignoreMonth: true, ignoreDay: true);

Assert.AreEqual("1984", daot.AsString(CultureInfo.InvariantCulture));
Assert.IsTrue(daot.Equals(DateAndOrTime.Create(new DateOnly(1984, 8, 20), ignoreMonth: true, ignoreDay: true)));
}

[TestMethod]
public void CreateTest2()
{
var daot = DateAndOrTime.Create(new DateTimeOffset(1984, 3, 17, 0, 0, 0, TimeSpan.Zero), ignoreMonth: true, ignoreDay: true);

Assert.AreEqual("1984", daot.AsString(CultureInfo.InvariantCulture));
Assert.IsTrue(daot.Equals(DateAndOrTime.Create(new DateTimeOffset(1984, 8, 20, 0, 0, 0, TimeSpan.Zero), ignoreMonth: true, ignoreDay: true)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void CloneTest1()
[TestMethod]
public void CloneTest2()
{
var prop1 = new DateAndOrTimeProperty(DateAndOrTime.Create(3, 15));
var prop1 = new DateAndOrTimeProperty(DateAndOrTime.Create(new DateOnly(4, 3, 15)));

var prop2 = (DateAndOrTimeProperty)prop1.Clone();

Expand Down
2 changes: 1 addition & 1 deletion src/FolkerKinzel.VCards.Tests/V4Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void ImppTest1()
[TestMethod]
public void CalendarTest1()
{
var prop = new DateAndOrTimeProperty(DateAndOrTime.Create(5, 5));
var prop = new DateAndOrTimeProperty(DateAndOrTime.Create(new DateOnly(4, 5, 5), ignoreYear: true));
Assert.AreEqual("gregorian", prop.Parameters.Calendar);
prop.Parameters.Calendar = "GREGORIAN";
Assert.AreEqual("GREGORIAN", prop.Parameters.Calendar);
Expand Down
2 changes: 1 addition & 1 deletion src/FolkerKinzel.VCards.Tests/VCardBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void EditBirthDayViewsTest1()
builder.BirthDayViews.Edit(p => prop = p);
Assert.IsNotNull(prop);
Assert.IsFalse(prop.Any());
builder.VCard.BirthDayViews = new DateAndOrTimeProperty(DateAndOrTime.Create(12, 24)).Append(null);
builder.VCard.BirthDayViews = new DateAndOrTimeProperty(DateAndOrTime.Create(new DateOnly(1984, 12, 24), ignoreYear: true)).Append(null);
builder.BirthDayViews.Edit(p => prop = p);
Assert.IsTrue(prop.Any());
CollectionAssert.AllItemsAreNotNull(prop.ToArray());
Expand Down
3 changes: 2 additions & 1 deletion src/FolkerKinzel.VCards/BuilderParts/DateAndOrTimeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FolkerKinzel.VCards.Enums;
using FolkerKinzel.VCards.Extensions;
using FolkerKinzel.VCards.Intls;
using FolkerKinzel.VCards.Intls.Converters;
using FolkerKinzel.VCards.Models;
using FolkerKinzel.VCards.Models.Properties;
using FolkerKinzel.VCards.Models.Properties.Parameters;
Expand Down Expand Up @@ -209,7 +210,7 @@ public VCardBuilder Add(int month,

try
{
val = DateAndOrTime.Create(month, day);
val = DateAndOrTime.Create(new DateOnly(DateAndOrTimeConverter.FIRST_LEAP_YEAR, month, day), ignoreYear: true);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion src/FolkerKinzel.VCards/FolkerKinzel.VCards.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageId>FolkerKinzel.VCards</PackageId>
<Product>FolkerKinzel.VCards</Product>
<Version>8.0.0-alpha.2</Version>
<FileVersion>8.0.0.35</FileVersion>
<FileVersion>8.0.0.37</FileVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<PackageReleaseNotes>https://github.com/FolkerKinzel/VCards/releases/tag/v8.0.0-alpha.2</PackageReleaseNotes>
<PackageTags>.net vcard vcf c#</PackageTags>
Expand Down
98 changes: 79 additions & 19 deletions src/FolkerKinzel.VCards/Intls/Converters/DateAndOrTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal bool TryParse(ReadOnlySpan<char> roSpan, [NotNullWhen(true)] out DateAn

if (roSpan.StartsWith("--", StringComparison.Ordinal))
{
// "--MM" zu "0004-MM":
// "--MM" to "0004-MM":
// Note the use of YYYY-MM in the second example above. YYYYMM is
// disallowed to prevent confusion with YYMMDD.
if (roSpan.Length == 4)
Expand All @@ -92,7 +92,24 @@ internal bool TryParse(ReadOnlySpan<char> roSpan, [NotNullWhen(true)] out DateAn
return TryParseMonthDayWithoutYear(roSpan, ref dateAndOrTime);
}

return TryParseInternal(roSpan, ref dateAndOrTime);
if(TryParseInternal(roSpan, ref dateAndOrTime))
{
// yyyy
if(roSpan.Length == 4)
{
dateAndOrTime.HasDay = false;
dateAndOrTime.HasMonth = false;
}
else if (roSpan.Length == 7)
{
// yyyy-MM
dateAndOrTime.HasDay = false;
}

return true;
}

return false;

//////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -107,7 +124,13 @@ bool TryParseDateNoReduc(ReadOnlySpan<char> roSpan, ref DateAndOrTime? dateAndOr
Span<char> slice = span.Slice(firstLeapYearJanuary.Length);
roSpan.CopyTo(slice);

return TryParseInternal(span, ref dateAndOrTime);
if(TryParseInternal(span, ref dateAndOrTime))
{
dateAndOrTime.HasMonth = false;
return true;
}

return false;
}

bool TryParseMonthWithoutYear(ReadOnlySpan<char> roSpan, ref DateAndOrTime? dateAndOrTime)
Expand All @@ -131,6 +154,7 @@ bool TryParseMonthWithoutYear(ReadOnlySpan<char> roSpan, ref DateAndOrTime? date
out DateOnly dateOnly))
{
dateAndOrTime = dateOnly;
dateAndOrTime.HasDay = false;
return true;
}

Expand Down Expand Up @@ -202,43 +226,79 @@ private bool TryParseInternal(ReadOnlySpan<char> span, [NotNullWhen(true)] ref D

internal static void AppendDateTo(StringBuilder builder,
DateOnly dt,
VCdVersion version)
VCdVersion version,
bool hasMonth,
bool hasDay)
{
switch (version)
{
case VCdVersion.V2_1:
case VCdVersion.V3_0:
{
_ = dt.HasYear()
? builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}-{1:00}-{2:00}",
dt.Year, dt.Month, dt.Day)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"--{0:00}-{1:00}",
dt.Month, dt.Day);
? hasDay
? builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}-{1:00}-{2:00}",
dt.Year, dt.Month, dt.Day)
: hasMonth
? builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}-{1:00}",
dt.Year, dt.Month)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}",
dt.Year)
: hasMonth
? hasDay
? builder.AppendFormat(CultureInfo.InvariantCulture,
"--{0:00}-{1:00}",
dt.Month, dt.Day)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"--{0:00}",
dt.Month)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"---{0:00}",
dt.Day);
break;
}
default: // vCard 4.0
{
_ = dt.HasYear()
? builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}{1:00}{2:00}",
dt.Year, dt.Month, dt.Day)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"--{0:00}{1:00}",
dt.Month, dt.Day);
? hasDay
? builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}{1:00}{2:00}",
dt.Year, dt.Month, dt.Day)
: hasMonth
? builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}-{1:00}",
dt.Year, dt.Month)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"{0:0000}",
dt.Year)
: hasMonth
? hasDay
? builder.AppendFormat(CultureInfo.InvariantCulture,
"--{0:00}{1:00}",
dt.Month, dt.Day)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"--{0:00}",
dt.Month)
: builder.AppendFormat(CultureInfo.InvariantCulture,
"---{0:00}",
dt.Day);
break;
}
}//switch
}

internal static void AppendDateTimeOffsetTo(StringBuilder builder,
DateTimeOffset dt,
VCdVersion version)
DateTimeOffset dt,
VCdVersion version,
bool hasMonth,
bool hasDay)
{
if (HasDate(dt))
{
AppendDateTo(builder, DateOnly.FromDateTime(dt.Date), version);
AppendDateTo(builder, DateOnly.FromDateTime(dt.Date), version, hasMonth, hasDay);
}

if (HasTime(dt))
Expand Down
5 changes: 5 additions & 0 deletions src/FolkerKinzel.VCards/Intls/Converters/TimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ internal bool TryParse(ReadOnlySpan<char> s, [NotNullWhen(true)] out DateAndOrTi
{
dtOffset = new DateTimeOffset(2, 1, 1, dtOffset.Hour, dtOffset.Minute, dtOffset.Second, dtOffset.Offset);
dateAndOrTime = dtOffset;
dateAndOrTime.HasDay = false;
dateAndOrTime.HasMonth = false;
return true;
}
}
Expand All @@ -87,6 +89,8 @@ internal bool TryParse(ReadOnlySpan<char> s, [NotNullWhen(true)] out DateAndOrTi
out TimeOnly timeOnly))
{
dateAndOrTime = timeOnly;
dateAndOrTime.HasDay = false;
dateAndOrTime.HasMonth = false;
return true;
}
}
Expand Down Expand Up @@ -132,6 +136,7 @@ internal static void AppendTimeTo(StringBuilder builder, DateTimeOffset dt, VCdV
}
else
{
// The '-' sign is added by the TimeSpan.ToString() method.
string sign = utcOffset < TimeSpan.Zero ? "" : "+";

switch (version)
Expand Down
Loading

0 comments on commit 2ed6e27

Please sign in to comment.