Skip to content

Commit

Permalink
Feature/issue 6 handle type string (#114)
Browse files Browse the repository at this point in the history
* feat: add ParseString() function to library

* feat: add ReplaceString and SubString

fixes #113

---------

Co-authored-by: Stephen Reindl <[email protected]>
  • Loading branch information
steven-r and Stephen Reindl authored Nov 19, 2024
1 parent d793dc6 commit 4c11d08
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 10 deletions.
87 changes: 79 additions & 8 deletions Oberon0.System.Tests/Oberon0System.StringsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Oberon0.System.Tests;

using Oberon0System;
using Xunit;

public static partial class Oberon0SystemTests
{
[Theory]
Expand All @@ -16,7 +13,7 @@ public static partial class Oberon0SystemTests
public static void CanCallConvertToStringWithInt(int value)
{
// Act
string result = Oberon0System.ConvertToString(value);
string result = Oberon0System.Oberon0System.ConvertToString(value);

// Assert
Assert.Equal(value.ToString(), result);
Expand All @@ -31,7 +28,7 @@ public static void CanCallConvertToStringWithInt(int value)
public static void CanCallConvertToStringWithReal(double value)
{
// Act
string result = Oberon0System.ConvertToString(value);
string result = Oberon0System.Oberon0System.ConvertToString(value);

// Assert
Assert.Equal(value.ToString(CultureInfo.InvariantCulture), result);
Expand All @@ -43,7 +40,7 @@ public static void CanCallConvertToStringWithReal(double value)
public static void CanCallConvertToStringWithBoolean(bool value)
{
// Act
string result = Oberon0System.ConvertToString(value);
string result = Oberon0System.Oberon0System.ConvertToString(value);

// Assert
Assert.Equal(value.ToString(), result);
Expand All @@ -60,7 +57,7 @@ public static void CanCallConvertToStringWithBoolean(bool value)
[InlineData(double.MinValue, "-1.7976931348623157E+308", "G")]
public static void TestToStringRealFormat(double value, string expected, string format)
{
string result = Oberon0System.ConvertToString(value, format);
string result = Oberon0System.Oberon0System.ConvertToString(value, format);
Assert.Equal(expected, result);
}

Expand All @@ -71,7 +68,81 @@ public static void TestToStringRealFormat(double value, string expected, string
[InlineData(true, "1", "1", "0")]
public static void TestToStringBooleanFormat(bool value, string expected, string trueVal, string falseVal)
{
string result = Oberon0System.ConvertToString(value, trueVal, falseVal);
string result = Oberon0System.Oberon0System.ConvertToString(value, trueVal, falseVal);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("0", 0, true)]
[InlineData(" 0 ", 0, true)]
[InlineData(" 1.2 ", 1.2, true)]
[InlineData(" 1 E2 ", 0, false)]
[InlineData(" 1,2 ", 0, false)]
public static void TestParseStringReal(string value, double expected, bool expectedReturn)
{
double result = 0;
bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result);
Assert.Equal(expectedReturn, returnValue);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("0", 0, true)]
[InlineData(" 0 ", 0, true)]
[InlineData(" 12 ", 12, true)]
[InlineData(" 1 E2 ", 0, false)]
[InlineData(" 1,2 ", 0, false)]
public static void TestParseStringInt(string value, int expected, bool expectedReturn)
{
int result = 0;
bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result);
Assert.Equal(expectedReturn, returnValue);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("0", false, false)]
[InlineData(" true ", true, true)]
[InlineData(" TRUE ", true, true)]
[InlineData(" E2 ", false, false)]
[InlineData(" 1,2 ", false, false)]
[InlineData("False", false, true)]
public static void TestParseStringBool(string value, bool expected, bool expectedReturn)
{
bool result = false;
bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result);
Assert.Equal(expectedReturn, returnValue);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("Hello World", "Hello", " Hi", " Hi World")]
[InlineData("Hello World", "Hello ", "", "World")]
[InlineData("Hello World Test", " World", " Unit", "Hello Unit Test")]
[InlineData("Hello World", "NotFound", "Replaced", "Hello World")]
public static void TestReplaceString(string value, string source, string target, string expected)
{
string result = Oberon0System.Oberon0System.ReplaceString(value, source, target);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("This is a Test", 1, 2, "Th")]
[InlineData("This is a Test", 6, 2, "is")]
[InlineData("This is a Test", 1, 14, "This is a Test")]
public static void TestSubString(string value, int start, int len, string expected)
{
string result = Oberon0System.Oberon0System.SubString(value, start, len);
Assert.Equal(expected, result);
}

[Theory]
[InlineData("This is a Test", 0, 2)]
[InlineData("This is a Test", 6, 14)]
[InlineData("This is a Test", 1, 20)]
public static void TestSubStringIndexOobException(string value, int start, int len)
{
Assert.Throws<ArgumentOutOfRangeException>(() => Oberon0System.Oberon0System.SubString(value, start, len));
}

}
67 changes: 65 additions & 2 deletions Oberon0.System/Oberon0System.Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static string ConvertToString(bool value)
}

/// <summary>
/// Convert REAL to STRING.
/// Convert REAL to STRING with format.
/// </summary>
/// <param name="value">the value to convert</param>
/// <param name="format">The format according to https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-8.0</param>
Expand All @@ -62,7 +62,7 @@ public static string ConvertToString(double value, string format)
}

/// <summary>
/// Convert REAL to STRING.
/// Convert BOOLEAN to STRING with format.
/// </summary>
/// <param name="value">the value to convert</param>
/// <param name="trueValue">value to be returned if <c>value</c> is true</param>
Expand All @@ -86,4 +86,67 @@ public static int Length(string value)
{
return value.Length;
}

/// <summary>
/// Parse a string containing a number and return the REAL number itself
/// </summary>
/// <param name="value"></param>
/// <param name="result"></param>
[Oberon0Export("ParseString", "BOOLEAN", "STRING", "&REAL")]
// ReSharper disable once UnusedMember.Global
public static bool ParseString(string value, ref double result)
{
return double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
}

/// <summary>
/// Parse a REAL number from a string and return the number itself
/// </summary>
/// <param name="value">The string to be parsed</param>
/// <param name="result">The resulting number</param>
/// <returns>true, if the parse was successful</returns>
[Oberon0Export("ParseString", "BOOLEAN", "STRING", "&INTEGER")]
// ReSharper disable once UnusedMember.Global
public static bool ParseString(string value, ref int result)
{
return int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
}

/// <summary>
/// Parse a BOOLEAN from a string and return the boolean itself
/// </summary>
/// <param name="value"></param>
/// <param name="result"></param>
[Oberon0Export("ParseString", "BOOLEAN", "STRING", "&BOOLEAN")]
// ReSharper disable once UnusedMember.Global
public static bool ParseString(string value, ref bool result)
{
return bool.TryParse(value, out result);
}

/// <summary>
/// Parse a BOOLEAN from a string and return the boolean itself
/// </summary>
/// <param name="value">The original string</param>
/// <param name="source">The string to be replaced</param>
/// <param name="target">The replacement string</param>
[Oberon0Export("ReplaceString", "STRING", "STRING", "STRING", "STRING")]
// ReSharper disable once UnusedMember.Global
public static string ReplaceString(string value, string source, string target)
{
return value.Replace(source, target);
}

/// <summary>
/// Parse a BOOLEAN from a string and return the boolean itself
/// </summary>
/// <param name="value">The string where the substring has to be built</param>
/// <param name="start">Start of substring (counting from 1)</param>
/// <param name="len">The length of the substring. Passing 0 will return the rest of the string starting from *start*.</param>
[Oberon0Export("SubString", "STRING", "STRING", "INTEGER", "INTEGER")]
// ReSharper disable once UnusedMember.Global
public static string SubString(string value, int start, int len)
{
return len > 0 ? value.Substring(start - 1, len) : value[(start - 1)..];
}
}

0 comments on commit 4c11d08

Please sign in to comment.