In C it is possible to write a macro function that replaces an input with the input as a string.
#define x(k) {#k, k}
'(4)' would generate '{"4", 4}'
I have a usecase in C# where i want to pass an input like this to a unit test.
private void AssertInt64Expression(Int64 i, string str)
{
Assert.AreEqual(i, MathFactory.make(str));
}
[Test]
public void ParseBasic()
{
AssertInt64Expression(4, "4");
AssertInt64Expression(2+3, "2+3");
AssertInt64Expression(7-11, "7-11");
AssertInt64Expression(7-11 *2, "7-11 *2");
AssertInt64Expression(7 - 11 * 2, "7 - 11 * 2");
}
I am essentially repeating the information (including whitespace) here, how can i use something like the c style macro to solve this in c#?
edit:
essentially i would love to write:
private void AssertInt64Expression(GeneratorMagic magic)
{
Assert.AreEqual(magic.ToCode(), MathFactory.make(magic.ToString()));
}
[Test]
public void ParseBasic()
{
AssertInt64Expression(<#7 - 11 * 2#>);
}
I am aware that this would not compile.
edit:
I added a code snippet as an answer to illustrate what i am looking for.
However this snippet runs very slow, since i need it to refactor my unit tests into cleaner code with less repetition i need the snippet to run faster.
The snippet essentially provides the magic
from the previous edit as a KeyValuePair
.