Skip to content

Commit

Permalink
feat: add ReplaceString and SubString
Browse files Browse the repository at this point in the history
fixes #113
  • Loading branch information
Stephen Reindl committed Nov 19, 2024
1 parent a97a38e commit 7daa0c6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
49 changes: 38 additions & 11 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,7 @@ 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);
}

Expand All @@ -84,7 +81,7 @@ public static void TestToStringBooleanFormat(bool value, string expected, string
public static void TestParseStringReal(string value, double expected, bool expectedReturn)
{
double result = 0;
bool returnValue = Oberon0System.ParseString(value, ref result);
bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result);
Assert.Equal(expectedReturn, returnValue);
Assert.Equal(expected, result);
}
Expand All @@ -98,7 +95,7 @@ public static void TestParseStringReal(string value, double expected, bool expec
public static void TestParseStringInt(string value, int expected, bool expectedReturn)
{
int result = 0;
bool returnValue = Oberon0System.ParseString(value, ref result);
bool returnValue = Oberon0System.Oberon0System.ParseString(value, ref result);
Assert.Equal(expectedReturn, returnValue);
Assert.Equal(expected, result);
}
Expand All @@ -113,9 +110,39 @@ public static void TestParseStringInt(string value, int expected, bool expectedR
public static void TestParseStringBool(string value, bool expected, bool expectedReturn)
{
bool result = false;
bool returnValue = Oberon0System.ParseString(value, ref result);
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));
}

}
26 changes: 26 additions & 0 deletions Oberon0.System/Oberon0System.Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,30 @@ 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 7daa0c6

Please sign in to comment.