ArchUnitNET icon indicating copy to clipboard operation
ArchUnitNET copied to clipboard

Avoid calling System.String.Substring()

Open TorstenM1979 opened this issue 1 year ago • 2 comments

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 avatar Apr 17 '24 13:04 TorstenM1979

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

TorstenM1979 avatar Apr 17 '24 13:04 TorstenM1979

I have little experience with this library but here to try and help

Documentation shows you should call Check(Architecture) instead of Evaluate(Architecture) perhaps that helps?

ArcadeMode avatar Jun 17 '24 14:06 ArcadeMode