-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parseFloat, parseInt, and parseHex methods
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using NUnit.Framework; | ||
|
||
namespace Mond.Tests.Libraries; | ||
|
||
[TestFixture] | ||
internal class ParseModuleTests | ||
{ | ||
[Test] | ||
public void ParseFloat_ValidNumber() | ||
{ | ||
var result = Script.Run(@" | ||
return parseFloat('123.456'); | ||
"); | ||
|
||
Assert.AreEqual((MondValue)123.456, result); | ||
} | ||
|
||
[Test] | ||
public void ParseFloat_InvalidNumber() | ||
{ | ||
var result = Script.Run(@" | ||
return parseFloat('hello'); | ||
"); | ||
|
||
Assert.AreEqual(MondValue.Undefined, result); | ||
} | ||
|
||
[Test] | ||
public void ParseInt_ValidNumber() | ||
{ | ||
var result = Script.Run(@" | ||
return parseInt('123'); | ||
"); | ||
|
||
Assert.AreEqual((MondValue)123, result); | ||
} | ||
|
||
[Test] | ||
public void ParseInt_InvalidNumber() | ||
{ | ||
var result = Script.Run(@" | ||
return parseInt('hello'); | ||
"); | ||
|
||
Assert.AreEqual(MondValue.Undefined, result); | ||
} | ||
|
||
[Test] | ||
public void ParseHex_ValidNumber() | ||
{ | ||
var result = Script.Run(@" | ||
return parseHex('DEaDb33F'); | ||
"); | ||
|
||
Assert.AreEqual((MondValue)0xDEaDb33F, result); | ||
} | ||
|
||
[Test] | ||
public void ParseHex_DigitsOnly() | ||
{ | ||
var result = Script.Run(@" | ||
return parseHex('1000'); | ||
"); | ||
|
||
Assert.AreEqual((MondValue)0x1000, result); | ||
} | ||
|
||
[Test] | ||
public void ParseHex_InvalidNumber() | ||
{ | ||
var result = Script.Run(@" | ||
return parseFloat('hello'); | ||
"); | ||
|
||
Assert.AreEqual(MondValue.Undefined, result); | ||
} | ||
} |
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,32 @@ | ||
using System.Globalization; | ||
using Mond.Binding; | ||
|
||
namespace Mond.Libraries.Core; | ||
|
||
[MondModule("Parse", bareMethods: true)] | ||
internal static partial class ParseModule | ||
{ | ||
[MondFunction] | ||
public static MondValue ParseFloat(string str) | ||
{ | ||
return double.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out var result) | ||
? result | ||
: MondValue.Undefined; | ||
} | ||
|
||
[MondFunction] | ||
public static MondValue ParseInt(string str) | ||
{ | ||
return long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result) | ||
? result | ||
: MondValue.Undefined; | ||
} | ||
|
||
[MondFunction] | ||
public static MondValue ParseHex(string str) | ||
{ | ||
return long.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result) | ||
? result | ||
: MondValue.Undefined; | ||
} | ||
} |
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