2

I'm writing a series of tests to validate code I'm writing. I want the documentation for the tests to list how the tests are finding errors. Instead of making sure the documentation gets updated with the tests I'd like to output the line of code. Is there a way to save it in a string?

The code would look like:

string Date = String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);
string Code = ""Name = {0}, hours = {1:hh}", name, DateTime.Now"

Date would be evaluated, while Code would be the literal string. Something like:

string Date = string.Format(Code);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • What do you actually mean by this ... "*Instead of making sure the documentation gets updated with the tests I'd like to output the line of code*" What tests and what documentation are we talking about, how do they relate to each other. – TheGeneral Jan 07 '21 at 00:53
  • I'm checking that equations in ladder logic all agree with each other. Parts of one equation should show up in others. I'm using string.Format to build what the equation should look be and comparing it with what was written. In each section of the test I'm including how the expected equations are being built since there are assumptions that are made. – Ryan Daniels Jan 07 '21 at 01:06

2 Answers2

0

I think you should better use a Service to log it.But I'll still give you a solution. You can use the delegate to record the current state(It's a closure),and when sometime you need it , you can Invoke it and get its return value. Do it like this:

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var john = new Producer();
            var log = john.Produce();
            Console.WriteLine($"John's log is \"{log()}\"");
        }
    }

    class Producer
    {
        public Func<string> Produce()
        {
            var name = "John";
            Console.WriteLine("John is producing.");
            return () =>
            {
                return $"Name = {name}, hours = {DateTime.Now:hh}";
            };
        }
    }
}

Output:

John is producing.
John's log is "Name = John, hours = 08"
Liplum
  • 11
  • 1
  • 1
  • 2
  • In this example I'd want console to output "Name = {name}, hours = {DateTime.Now:hh}". I know there must be better ways to document the code, but it's important for it to be displayed next to test results to know how the test is built and what assumptions were made. I explained a little more in the other comments. – Ryan Daniels Jan 07 '21 at 01:12
  • Do you mean that you want to make a funtion call by a string and get its return value? – Liplum Jan 07 '21 at 01:25
  • The issue I have is the string would need to be functional when fed into a string.Format function. Right now I copy the code and escape the parenthese and I can see the code used fine, but there's no guarantee that I haven't changed the code without updating the 'documentation'. When I have that string I can't just use string.Format(code), because it doesn't fit the format any more. – Ryan Daniels Jan 07 '21 at 07:16
0

I believe that you might be able to use the new C# 9.0 code generators to do that. They allow you to do automated code rewriting such as injecting log calls which is similar to what your use case is.

Here is a cookbook with some "code rewriting" recipes:

https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.cookbook.md

I am new to code generators and they are still in preview(!) so I don't know if what you want is really possible but at least the general idea of inspecting the code and augmenting it on a per-line basis (in your case by making a string out of the code line as well as executing it) looks like what code rewriting generators could be used for.

Patrick Klug
  • 14,056
  • 13
  • 71
  • 118
  • Thanks for the heads up. I'll look into that in the morning. I think this is truly a work flow problem, but I am hoping to fix it with code. These tests have to be trusted, so it's a pain to do a code review every time we use them. The tests are constantly growing, but I may be able to section them off and generate checksums for known good tests. – Ryan Daniels Jan 07 '21 at 07:25