Code Monkey home page Code Monkey logo

Comments (6)

togakangaroo avatar togakangaroo commented on June 4, 2024

So given a compiled awaited method it should definitely be possible to figure out the original method name - that is after all something that ILSpy does - but looking at the StackTrace in the debugger I couldn't find any clear link between the stackframe and the original method.

So here's an idea.

The relevant stackframe that should be picked up as containing the name is something like

{ApprovalTests.Xunit.<FullAsyncTest>d__8.MoveNext()}

So in theory it is absolutely possible to parse out the original method name (FullAsyncTest). This generated method is compiled into a nested class that is inside the original class.

Therefore, you're in a stackframe that matches the above pattern, you can navigate to the original containing class, and try to get the method that matches the generic parameter name. If this method is async its probably a safe bet that this was the original method and we can use it for naming.

Roundabout and I haven't tried it, but it seems like it should work...

from approvaltests.net.

mattscully avatar mattscully commented on June 4, 2024

Anyone ever find a workaround for this?

from approvaltests.net.

togakangaroo avatar togakangaroo commented on June 4, 2024

@mattscully the workaround that I used was to just feed in a test name manually

from approvaltests.net.

togakangaroo avatar togakangaroo commented on June 4, 2024
    public class ConstantNamer : IApprovalNamer
    {
        public string SourcePath { get { return defaultNamer.SourcePath; } }
        public string Name { get; private set; }
        public ConstantNamer(string name) {
            Name = Path.GetInvalidPathChars().Aggregate(name, (path, c) => path.Replace(c, ' ')).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Join(" ");
        }

        readonly IApprovalNamer defaultNamer = Approvals.GetDefaultNamer();

        static ConstantNamer() {
            if (!StackTraceParser.GetParsers().OfType<XbehaveStackTraceParser>().Any())
                StackTraceParser.AddParser(new XbehaveStackTraceParser());
        }
        public class XbehaveStackTraceParser : AttributeStackTraceParser
        {
            public override bool Parse(StackTrace trace) {
                base.Parse(trace);
                //This should work but for some reason doesn't http://stackoverflow.com/questions/26553231/why-can-i-not-find-a-custom-attribute-on-this-methodinfo
                //this.approvalFrame = caller.NonLambdaCallers.FirstOrDefault(c => c.Method.GetCustomAttributes(typeof (SpecificationAttribute), true).Any());
                //instead this is a little fragile but should work.
                this.approvalFrame = caller.NonLambdaCallers.Last(c => c.Class.Namespace.Split('.').Contains("AppleHms"));
                return this.approvalFrame != null;
            }

            public override string ForTestingFramework {
                get { return "xUnit.net"; }
            }

            protected override string GetAttributeType() {
                return typeof(ScenarioAttribute).FullName;
            }
        }
    }

use like this:

Approvals.Verify(
                new ApprovalTextWriter(JsonConvert.SerializeObject(obj, Formatting.Indented), "json"),
                new ConstantNamer(testName), new MultiReporter(new FileLauncherReporter(), new PowerShellClipboardReporter())
                )

from approvaltests.net.

mattscully avatar mattscully commented on June 4, 2024

@togakangaroo Thanks! I'll give this a try. Is the AppleHms namespace specific to your application or is that something that is generic?

from approvaltests.net.

togakangaroo avatar togakangaroo commented on June 4, 2024

@mattscully whoopsie - that's my app. The point is that I'm walking the stacktrace to find the highest level stack frame that is still in my app (and in my app-specific namespace)

from approvaltests.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.