<commit_before>﻿// Copyright 2015, 2014 <NAME>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.TaskRunnerFramework;
using JetBrains.ReSharper.UnitTestFramework;
using JetBrains.ReSharper.UnitTestFramework.Strategy;
using TestFx.ReSharper.Model.Tree;
using TestFx.ReSharper.Runner.Tasks;
using TestFx.ReSharper.UnitTesting.Utilities;
using TestFx.ReSharper.Utilities.Psi.Tree;
using TestFx.Utilities;
using TestFx.Utilities.Collections;
using RecursiveRemoteTaskRunner = TestFx.ReSharper.Runner.RecursiveRemoteTaskRunner;

namespace TestFx.ReSharper.UnitTesting.Elements
{
  [DebuggerDisplay (Identifiable.DebuggerDisplay)]
  public abstract partial class TestElementBase : ITestElement
  {
    private static readonly IUnitTestRunStrategy RunStrategy =
        new OutOfProcessUnitTestRunStrategy(new RemoteTaskRunnerInfo(RecursiveRemoteTaskRunner.ID, typeof (RecursiveRemoteTaskRunner)));

    private readonly ITestIdentity _identity;
    private readonly IList<Task> _tasks;
    private readonly IUnitTestProvider _unitTestProvider;
    private readonly HashSet<IUnitTestElement> _children;

    private UnitTestElementState _state;
    private TestElementBase _parent;
    private string _text;
    private string _explicitReason;
    private IEnumerable<UnitTestElementCategory> _categories;

    protected TestElementBase (ITestIdentity identity, IList<Task> tasks)
    {
      _identity = identity;
      _tasks = tasks;
      _unitTestProvider = identity.Provider;
      _children = new HashSet<IUnitTestElement>();
    }

    [CanBeNull]
    public abstract IEnumerable<IProjectFile> GetProjectFiles ();

    public abstract UnitTestNamespace GetNamespace ();

    [CanBeNull]
    public abstract IDeclaredElement GetDeclaredElement ();

    internal abstract IEnumerable<ITestFile> GetTestFiles ();

    public IIdentity Identity
    {
      get { return _identity; }
    }

    [CanBeNull]
    public IProject GetProject ()
    {
      return _identity.GetProject();
    }

    public string Kind
    {
      get { return "Test"; }
    }

    public UnitTestElementKind ElementKind
    {
      get { return _children.Any() ? UnitTestElementKind.TestContainer : UnitTestElementKind.Test; }
    }

    public virtual string ShortName
    {
      get { return Identity.Relative; }
    }

    public void Update (string text, [CanBeNull] string explicitReason, IEnumerable<UnitTestElementCategory> categories)
    {
      _text = text;
      _explicitReason = explicitReason;
      _categories = categories;
      _state = UnitTestElementState.Valid;
    }

    public IEnumerable<UnitTestElementCategory> Categories
    {
      get { return _categories; }
    }

    public bool Explicit
    {
      get { return ExplicitReason != null; }
    }

    [CanBeNull]
    public string ExplicitReason
    {
      get { return _explicitReason; }
    }

    public UnitTestElementState State
    {
      get { return _state; }
      set { _state = value; }
    }

    [CanBeNull]
    public IUnitTestElement Parent
    {
      get { return _parent; }
      set
      {
        if (ReferenceEquals(value, _parent))
          return;

        if (_parent != null)
          _parent._children.Remove(this);

        _parent = value as TestElementBase;

        if (_parent != null)
          _parent._children.Add(this);
      }
    }

    public ICollection<IUnitTestElement> Children
    {
      get { return _children.ToList().AsReadOnly(); }
    }

    public IUnitTestProvider Provider
    {
      get { return _unitTestProvider; }
    }

    public IUnitTestRunStrategy GetRunStrategy (IHostProvider hostProvider)
    {
      return RunStrategy;
    }

    public IList<UnitTestTask> GetTaskSequence (ICollection<IUnitTestElement> explicitElements)
    {
      var parentTasks = _parent != null ? _parent.GetTaskSequence(explicitElements) : new List<UnitTestTask>();
      return parentTasks.Concat(_tasks.Select(x => new UnitTestTask(this, x))).ToList();
    }

    public UnitTestElementDisposition GetDisposition ()
    {
      return GetDispositionFromFiles(GetTestFiles().ToArray());
    }

    public UnitTestElementDisposition GetDispositionFromFiles (params ITestFile[] testFiles)
    {
      var declarations = testFiles
          .Select(x => x.TestDeclarations.Search(Identity, y => y.TestDeclarations))
          .WhereNotNull().ToList();

      var locations = declarations.Select(x => x.GetTestElementLocation()).ToList();
      if (locations.Count != 0)
        return new UnitTestElementDisposition(locations, this);

      if (_state == UnitTestElementState.Dynamic)
        return UnitTestElementDisposition.NotYetClear(this);

      return UnitTestElementDisposition.InvalidDisposition;
    }

    public bool Equals (IUnitTestElement other)
    {
      return ReferenceEquals(this, other) || Id.Equals(other.Id);
    }
  }
}<commit_msg>Fixed TestElementBase to return InvalidDisposition for dynamic elements if their parent element is invalid.<commit_after>﻿// Copyright 2015, 2014 <NAME>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.TaskRunnerFramework;
using JetBrains.ReSharper.UnitTestFramework;
using JetBrains.ReSharper.UnitTestFramework.Strategy;
using TestFx.ReSharper.Model.Tree;
using TestFx.ReSharper.Runner.Tasks;
using TestFx.ReSharper.UnitTesting.Utilities;
using TestFx.ReSharper.Utilities.Psi.Tree;
using TestFx.Utilities;
using TestFx.Utilities.Collections;
using RecursiveRemoteTaskRunner = TestFx.ReSharper.Runner.RecursiveRemoteTaskRunner;

namespace TestFx.ReSharper.UnitTesting.Elements
{
  [DebuggerDisplay (Identifiable.DebuggerDisplay)]
  public abstract partial class TestElementBase : ITestElement
  {
    private static readonly IUnitTestRunStrategy RunStrategy =
        new OutOfProcessUnitTestRunStrategy(new RemoteTaskRunnerInfo(RecursiveRemoteTaskRunner.ID, typeof (RecursiveRemoteTaskRunner)));

    private readonly ITestIdentity _identity;
    private readonly IList<Task> _tasks;
    private readonly IUnitTestProvider _unitTestProvider;
    private readonly HashSet<IUnitTestElement> _children;

    private UnitTestElementState _state;
    private TestElementBase _parent;
    private string _text;
    private string _explicitReason;
    private IEnumerable<UnitTestElementCategory> _categories;

    protected TestElementBase (ITestIdentity identity, IList<Task> tasks)
    {
      _identity = identity;
      _tasks = tasks;
      _unitTestProvider = identity.Provider;
      _children = new HashSet<IUnitTestElement>();
    }

    [CanBeNull]
    public abstract IEnumerable<IProjectFile> GetProjectFiles ();

    public abstract UnitTestNamespace GetNamespace ();

    [CanBeNull]
    public abstract IDeclaredElement GetDeclaredElement ();

    internal abstract IEnumerable<ITestFile> GetTestFiles ();

    public IIdentity Identity
    {
      get { return _identity; }
    }

    [CanBeNull]
    public IProject GetProject ()
    {
      return _identity.GetProject();
    }

    public string Kind
    {
      get { return "Test"; }
    }

    public UnitTestElementKind ElementKind
    {
      get { return _children.Any() ? UnitTestElementKind.TestContainer : UnitTestElementKind.Test; }
    }

    public virtual string ShortName
    {
      get { return Identity.Relative; }
    }

    public void Update (string text, [CanBeNull] string explicitReason, IEnumerable<UnitTestElementCategory> categories)
    {
      _text = text;
      _explicitReason = explicitReason;
      _categories = categories;
      _state = UnitTestElementState.Valid;
    }

    public IEnumerable<UnitTestElementCategory> Categories
    {
      get { return _categories; }
    }

    public bool Explicit
    {
      get { return ExplicitReason != null; }
    }

    [CanBeNull]
    public string ExplicitReason
    {
      get { return _explicitReason; }
    }

    public UnitTestElementState State
    {
      get { return _state; }
      set { _state = value; }
    }

    [CanBeNull]
    public IUnitTestElement Parent
    {
      get { return _parent; }
      set
      {
        if (ReferenceEquals(value, _parent))
          return;

        if (_parent != null)
          _parent._children.Remove(this);

        _parent = value as TestElementBase;

        if (_parent != null)
          _parent._children.Add(this);
      }
    }

    public ICollection<IUnitTestElement> Children
    {
      get { return _children.ToList().AsReadOnly(); }
    }

    public IUnitTestProvider Provider
    {
      get { return _unitTestProvider; }
    }

    public IUnitTestRunStrategy GetRunStrategy (IHostProvider hostProvider)
    {
      return RunStrategy;
    }

    public IList<UnitTestTask> GetTaskSequence (ICollection<IUnitTestElement> explicitElements)
    {
      var parentTasks = _parent != null ? _parent.GetTaskSequence(explicitElements) : new List<UnitTestTask>();
      return parentTasks.Concat(_tasks.Select(x => new UnitTestTask(this, x))).ToList();
    }

    public UnitTestElementDisposition GetDisposition ()
    {
      return GetDispositionFromFiles(GetTestFiles().ToArray());
    }

    public UnitTestElementDisposition GetDispositionFromFiles (params ITestFile[] testFiles)
    {
      var declarations = testFiles
          .Select(x => x.TestDeclarations.Search(Identity, y => y.TestDeclarations))
          .WhereNotNull().ToList();

      var locations = declarations.Select(x => x.GetTestElementLocation()).ToList();
      if (locations.Count != 0)
        return new UnitTestElementDisposition(locations, this);

      if (_state == UnitTestElementState.Dynamic && _parent.State != UnitTestElementState.Invalid)
        return UnitTestElementDisposition.NotYetClear(this);

      _state = UnitTestElementState.Invalid;
      return UnitTestElementDisposition.InvalidDisposition;
    }

    public bool Equals (IUnitTestElement other)
    {
      return ReferenceEquals(this, other) || Id.Equals(other.Id);
    }
  }
}
{'fraction_non_alphanumeric': 0.08127644569816643, 'fraction_numerical': 0.0022919605077574047, 'mean_word_length': 3.4058252427184468, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3313668', 'n_tokens_mistral': 3180, 'n_tokens_neox': 3102, 'n_words': 793}