GithubHelp home page GithubHelp logo

Comments (3)

VladD2 avatar VladD2 commented on August 16, 2024

type.GetModifiers().AddCustomAttribute (<[ TestFixture ]>);

You should use full name of attribut or open appropriate namespace in file which contains macro.

def methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);

The GetMethods() method work only at MacroPhase.WithTypedMembers phase (as, since it return typed members).
You should use MacroPhase.WithTypedMembers, or work with antyped AST (through TypeBuilder.Ast).

This version work well:

// Macro.n

using Nemerle;
using Nemerle.Collections;
using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestMacro
{
  [MacroUsage(MacroPhase.WithTypedMembers, MacroTargets.Class)]
  macro UnitTestFixture(typeBuilder : TypeBuilder)
  {
    UnitTestFixtureImpl.DoTransform(Macros.ImplicitCTX(), typeBuilder)
  }

  module UnitTestFixtureImpl
  {
    public DoTransform(typer : Typer, typeBuilder : TypeBuilder) : void
    {
      Macros.DefineCTX(typer);
      typeBuilder.GetModifiers().AddCustomAttribute (<[ NUnit.Framework.TestFixtureAttribute ]>);

      def methods = typeBuilder.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

      foreach(method in methods)
        method.GetModifiers().AddCustomAttribute(<[ NUnit.Framework.TestAttribute ]>);      
    }
  }
}
// Test.n
using TestMacro;

[UnitTestFixture()]
public class OptionalHeaderTests
{
  public SomeTest() : void
  {            
  }
}

module Program
{
  Main() : void
  {
  }
}

from nemerle.

philiplaureano avatar philiplaureano commented on August 16, 2024

Thanks. What about Assembly-level meta attributes? Is there any way to use one attribute declaration such as:

[assembly:UnitTestSuite("MyNamespace.Tests.*")]

...to apply the [UnitTestFixture()] attribute to all types within a given namespace?

from nemerle.

VladD2 avatar VladD2 commented on August 16, 2024

Thanks. What about Assembly-level meta attributes?

Simply replace the MacroTargets.Class by MacroTargets.Assembly and remove the typeBuilder parameter:

using Nemerle;
using Nemerle.Collections;
using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestMacro
{
  [MacroUsage(MacroPhase.WithTypedMembers, MacroTargets.Assembly)]
  macro UnitTestFixture()
  {
    UnitTestFixtureImpl.DoTransform(Macros.ImplicitCTX())
  }

  module UnitTestFixtureImpl
  {
    public DoTransform(typer : Typer) : void
    {
      Macros.DefineCTX(typer);

      def typeBuilders = typer.Manager.CoreEnv.NameTree.NamespaceTree.GetTypeBuilders(onlyTopDeclarations = true);

      foreach (typeBuilder in typeBuilders)
      {
        typeBuilder.GetModifiers().AddCustomAttribute (<[ NUnit.Framework.TestFixtureAttribute ]>);

        def methods = typeBuilder.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

        foreach(method in methods)
          method.GetModifiers().AddCustomAttribute(<[ NUnit.Framework.TestAttribute ]>);
      }
    }
  }
}

Note: If oyu specify "onlyTopDeclarations = false", you get all typebuilders including nested types.

from nemerle.

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.