Skip to content

Instantly share code, notes, and snippets.

@mcintyre321
Created April 7, 2016 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcintyre321/fcf43118aa737d326fbd086a0812cd20 to your computer and use it in GitHub Desktop.
Save mcintyre321/fcf43118aa737d326fbd086a0812cd20 to your computer and use it in GitHub Desktop.
SpecificationsExtractor
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CC
{
public static class SpecificationsExtractor
{
public class Specification
{
private readonly string _filePath;
private readonly string _testName;
private readonly string _comments;
public string FilePath
{
get { return _filePath; }
}
public string TestName
{
get { return _testName; }
}
public string Comments
{
get { return _comments; }
}
public Specification(string filePath, string testName, IEnumerable<string> comments)
{
_filePath = filePath;
_testName = testName;
_comments = string.Join("\r\n", comments);
}
protected bool Equals(Specification other)
{
return string.Equals(_filePath, other._filePath) && string.Equals(_testName, other._testName) && string.Equals(_comments, other._comments);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Specification) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (_filePath != null ? _filePath.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (_testName != null ? _testName.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (_comments != null ? _comments.GetHashCode() : 0);
return hashCode;
}
}
public static bool operator ==(Specification left, Specification right)
{
return Equals(left, right);
}
public static bool operator !=(Specification left, Specification right)
{
return !Equals(left, right);
}
}
public static Specification[] ExtractSpecs(string gitDir)
{
var csFiles = Directory.GetFiles(gitDir, "*.cs", SearchOption.AllDirectories);
var syntaxTrees = csFiles
.Select(
f =>
CSharpSyntaxTree.ParseText(System.IO.File.ReadAllText(f),
new CSharpParseOptions(documentationMode: DocumentationMode.Diagnose), f))
.Select(f =>
{
var methods = f.GetRoot().DescendantNodes()
.Where(d => d.Kind() == SyntaxKind.MethodDeclaration)
.Select(d => new
{
name = ((MethodDeclarationSyntax) d).Identifier.ToFullString(),
comments = d.DescendantTrivia()
.Where(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia)
.Select(t => t.ToFullString())
});
// var visitor = new CustomWalker();
// visitor.Visit(f.GetRoot());
return
new
{
FilePath = f.FilePath.Substring(gitDir.Length),
methods =
methods.Where(m => m.comments.Any(c => c.Replace(" ", "").Contains("//Given")))
};
});
var methodData = syntaxTrees
.Where(t => t.methods.Any())
.SelectMany(x => x.methods.Select(y =>
new Specification(x.FilePath, y.name, y.comments)
));
return methodData.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment