Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling System.String.Substring() #254

Open
TorstenM1979 opened this issue Apr 17, 2024 · 1 comment
Open

Avoid calling System.String.Substring() #254

TorstenM1979 opened this issue Apr 17, 2024 · 1 comment
Labels
kind/support Categorizes issue or PR as a support question.

Comments

@TorstenM1979
Copy link

I'm trying to understand how to avoid calling a method like "System.String.Substring()" with ArchUnitNET.

First I have a simple library Lib with static method:

namespace Lib
{
public static class Utility
{
public static string SubString(string input, int position)
{
return input.Substring(position);
}
}
}

Second I have an example console project to show what I want exactly:

namespace Prg
{
internal class Program
{
static void Main(string[] args)
{
// this usage is fine
Console.WriteLine(Lib.Utility.SubString("Hello World", 6));

		// this usage should not be possible and should be "reported" by ArchUnit
		Console.WriteLine("Hello World".Substring(6));

		Console.ReadKey();
	}
}

}

And, I have a simple MS unit test as follows:

using ArchUnitNET.Domain;
using ArchUnitNET.Loader;
using static ArchUnitNET.Fluent.ArchRuleDefinition;

namespace LibTest
{
[TestClass]
public class PrgTest
{
private static readonly Architecture Architecture = new ArchLoader().LoadAssemblies(System.Reflection.Assembly.Load("Prg")).Build();

	[TestMethod]
	public void TestIndexOf()
	{
		var classesThatCallStringDotSubString = Classes().That().CallAny("System.String.Substring").Should().NotExist();
		var result = classesThatCallStringDotSubString.Evaluate(Architecture).ToList();

		Assert.IsTrue(result[0].Passed);
	}
}

}

I do something wrong because the test does not fail. Can anyone help? Thanks a lot.

BR Torsten

@TorstenM1979
Copy link
Author

Sorry for bad formatting, I don't know how this can happen :-(

@mak638 mak638 added the kind/support Categorizes issue or PR as a support question. label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question.
Projects
None yet
Development

No branches or pull requests

2 participants