Skip to content

Commit

Permalink
Why is test failing?
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisMerinoP committed Nov 21, 2023
1 parent d8f3a80 commit 7c6f203
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
78 changes: 77 additions & 1 deletion Jint.Tests/Runtime/Debugger/TestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Esprima.Ast;
using System.Globalization;
using Esprima.Ast;
using Jint.Runtime.Debugger;

namespace Jint.Tests.Runtime.Debugger
Expand Down Expand Up @@ -68,5 +69,80 @@ public static void TestAtBreak(string script, Action<DebugInformation> breakHand
{
TestAtBreak(script, (engine, info) => breakHandler(info));
}

public static void ShouldEqualWithDiff(this string actualValue, string expectedValue)
{
ShouldEqualWithDiff(actualValue, expectedValue, DiffStyle.Full, Console.Out);
}

public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle)
{
ShouldEqualWithDiff(actualValue, expectedValue, diffStyle, Console.Out);
}

public static void ShouldEqualWithDiff(this string actualValue, string expectedValue, DiffStyle diffStyle, TextWriter output)
{
if (actualValue == null || expectedValue == null)
{
//Assert.AreEqual(expectedValue, actualValue);
Assert.Equal(expectedValue, actualValue);
return;
}

//if (actualValue.Equals(expectedValue, StringComparison.Ordinal)) return;

output.WriteLine(" Idx Expected Actual");
output.WriteLine("-------------------------");
int maxLen = Math.Max(actualValue.Length, expectedValue.Length);
int minLen = Math.Min(actualValue.Length, expectedValue.Length);
for (int i = 0; i < maxLen; i++)
{
if (diffStyle != DiffStyle.Minimal || i >= minLen || actualValue[i] != expectedValue[i])
{
output.WriteLine("{0} {1,-3} {2,-4} {3,-3} {4,-4} {5,-3}",
i < minLen && actualValue[i] == expectedValue[i] ? " " : "*", // put a mark beside a differing row
i, // the index
i < expectedValue.Length ? ((int) expectedValue[i]).ToString() : "", // character decimal value
i < expectedValue.Length ? expectedValue[i].ToSafeString() : "", // character safe string
i < actualValue.Length ? ((int) actualValue[i]).ToString() : "", // character decimal value
i < actualValue.Length ? actualValue[i].ToSafeString() : "" // character safe string
);
}
}
output.WriteLine();

//Assert.AreEqual(expectedValue, actualValue);
Assert.Equal(expectedValue, actualValue);
}

private static string ToSafeString(this char c)
{
if (Char.IsControl(c) || Char.IsWhiteSpace(c))
{
switch (c)
{
case '\r':
return @"\r";
case '\n':
return @"\n";
case '\t':
return @"\t";
case '\a':
return @"\a";
case '\v':
return @"\v";
case '\f':
return @"\f";
default:
return String.Format("\\u{0:X};", (int) c);
}
}
return c.ToString(CultureInfo.InvariantCulture);
}
}
public enum DiffStyle
{
Full,
Minimal
}
}
5 changes: 3 additions & 2 deletions Jint.Tests/Runtime/NumberTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Globalization;
using Jint.Runtime;
using Jint.Tests.Runtime.Debugger;

namespace Jint.Tests.Runtime
{
Expand Down Expand Up @@ -129,8 +131,7 @@ public void ParseFloat(string input, double result)
public void ToLocaleString(string parseNumber, string culture, string result)
{
var value = _engine.Evaluate($"({parseNumber}).toLocaleString('{culture}')").AsString();
// Normalized strings to handle different space characters.
Assert.Equal(result.Normalize(), value.Normalize());
TestHelpers.ShouldEqualWithDiff(result, value);

Check failure on line 134 in Jint.Tests/Runtime/NumberTests.cs

View workflow job for this annotation

GitHub Actions / windows

Jint.Tests.Runtime.NumberTests.ToLocaleString(parseNumber: "1000000"

Assert.Equal() Failure: Strings differ ↓ (pos 1) Expected: "1 000 000" Actual: "1 000 000" ↑ (pos 1)

Check failure on line 134 in Jint.Tests/Runtime/NumberTests.cs

View workflow job for this annotation

GitHub Actions / windows

Jint.Tests.Runtime.NumberTests.ToLocaleString(parseNumber: "1e6"

Assert.Equal() Failure: Strings differ ↓ (pos 1) Expected: "1 000 000" Actual: "1 000 000" ↑ (pos 1)
}

[Theory]
Expand Down

0 comments on commit 7c6f203

Please sign in to comment.