C # / VisualStudio: sorting attributes for consistency - any hints?

I have a slightly dumb problem:

I have a large number of unit tests that have method attributes like this:

[TestMethod] [Owner("me")] [Description("It tests something.")] [TestProperty(TC.Name, "Some Test")] [TestProperty(TC.Requirement, "req203")] [TestProperty(TC.Reviewer, "someguy")] [TestProperty(TC.Environment, "MSTest")] [TestProperty(TC.CreationDate, "24.01.2012")] [TestProperty(TC.InternalTcId, "{9221A494-2B31-479D-ADE6-D4773C2A9B08}")] public void TestSomething() { ... } 

(If you are interested: these attributes are used to automatically test and cover requirements).

Now, unfortunately, these attributes are in a different order according to most testing methods, which makes it a little dirty to consider and the like. Therefore, I am looking for a way to order them.

Do you know any other way than reordering them manually?

(I was thinking about writing some kind of VS-plugin or so) - I'm just wondering if I'm really the first person with such a desire.

+4
source share
2 answers

Open Macro Explorer - and paste this code into the module (it's right from my small collection of macros):

 Sub Sort() Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection If selection Is Nothing Or String.IsNullOrWhiteSpace(selection.Text) Then Exit Sub End If Dim lines As String() = selection.Text.Split(vbCrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) If lines.Length <= 1 Then Exit Sub lines = lines.OrderBy(Function(s As String) s, StringComparer.CurrentCulture).ToArray() DTE.UndoContext.Open("Sort Lines") selection.Insert(String.Join(vbCrLf, lines)) selection.SmartFormat() DTE.UndoContext.Close() DTE.StatusBar.Text = "Sort Lines complete" selection.SmartFormat() End Sub 

(just edited it since the try / end attempt was not quite right - so I took it)

Now you can bind a shortcut to this macro in VS - it uses Linq OrderBy , using the current culture line matcher to sort the lines of the currently selected block of text. Therefore, it must group attributes together.

If you need something context-sensitive (i.e. the same attribute that is called with a different number of parameters), then you will need to do a lot more work.

+4
source

You are the first person who wants :)

I would order them manually, but also, if you are looking for a more durable solution, then I would execute the property in the class TestPropertyAttribute int Index { get; set; } int Index { get; set; } int Index { get; set; } and set the order in which I want them to be processed. In this case, you can control which attributes are read in the reflection code that reads them. Here's how NHibernate does it.

 [TestProperty(TC.Name, "Some Test", 0)] [TestProperty(TC.Requirement, "req203", 1)] 
+1
source

Source: https://habr.com/ru/post/1392705/


All Articles