-
-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix default date constructor to clip decimal part (#1340)
- Loading branch information
Showing
5 changed files
with
142 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,73 @@ | ||
namespace Jint.Tests.Runtime | ||
namespace Jint.Tests.Runtime; | ||
|
||
public class DateTests | ||
{ | ||
public class DateTests | ||
private readonly Engine _engine; | ||
|
||
public DateTests() | ||
{ | ||
private readonly Engine _engine; | ||
_engine = new Engine() | ||
.SetValue("log", new Action<object>(Console.WriteLine)) | ||
.SetValue("assert", new Action<bool>(Assert.True)) | ||
.SetValue("equal", new Action<object, object>(Assert.Equal)); | ||
} | ||
|
||
public DateTests() | ||
{ | ||
_engine = new Engine() | ||
.SetValue("log", new Action<object>(Console.WriteLine)) | ||
.SetValue("assert", new Action<bool>(Assert.True)) | ||
.SetValue("equal", new Action<object, object>(Assert.Equal)); | ||
} | ||
[Fact] | ||
public void NaNToString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
|
||
[Fact] | ||
public void NaNToString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
[Fact] | ||
public void NaNToDateString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toDateString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
|
||
[Fact] | ||
public void NaNToDateString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toDateString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
[Fact] | ||
public void NaNToTimeString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toTimeString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
|
||
[Fact] | ||
public void NaNToTimeString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toTimeString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
[Fact] | ||
public void NaNToLocaleString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toLocaleString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
|
||
[Fact] | ||
public void NaNToLocaleString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toLocaleString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
[Fact] | ||
public void NaNToLocaleDateString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toLocaleDateString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
|
||
[Fact] | ||
public void NaNToLocaleDateString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toLocaleDateString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
[Fact] | ||
public void NaNToLocaleTimeString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toLocaleTimeString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
|
||
[Fact] | ||
public void NaNToLocaleTimeString() | ||
{ | ||
var value = _engine.Evaluate("new Date(NaN).toLocaleTimeString();").AsString(); | ||
Assert.Equal("Invalid Date", value); | ||
} | ||
[Fact] | ||
public void ToJsonFromNaNObject() | ||
{ | ||
var result = _engine.Evaluate("JSON.stringify({ date: new Date(NaN) });"); | ||
Assert.Equal("{\"date\":null}", result.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void ValuePrecisionIsIntegral() | ||
{ | ||
var number = _engine.Evaluate("new Date() / 1").AsNumber(); | ||
Assert.Equal((long) number, number); | ||
|
||
[Fact] | ||
public void ToJsonFromNaNObject() | ||
{ | ||
var result = _engine.Evaluate("JSON.stringify({ date: new Date(NaN) });"); | ||
Assert.Equal("{\"date\":null}", result.ToString()); | ||
} | ||
var dateInstance = _engine.Realm.Intrinsics.Date.Construct(123.456d); | ||
Assert.Equal((long) dateInstance.DateValue, dateInstance.DateValue); | ||
} | ||
} |
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,24 @@ | ||
namespace Jint.Native.Date; | ||
|
||
internal static class DateExtensions | ||
{ | ||
public static DateTime ToDateTime(this double t) | ||
{ | ||
return DateConstructor.Epoch.AddMilliseconds(t); | ||
} | ||
|
||
internal static double TimeClip(this double time) | ||
{ | ||
if (double.IsInfinity(time) || double.IsNaN(time)) | ||
{ | ||
return double.NaN; | ||
} | ||
|
||
if (System.Math.Abs(time) > 8640000000000000) | ||
{ | ||
return double.NaN; | ||
} | ||
|
||
return (long) time; | ||
} | ||
} |
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
Oops, something went wrong.