Skip to content

Instantly share code, notes, and snippets.

@otac0n
Created July 12, 2011 00:38
Show Gist options
  • Save otac0n/1077130 to your computer and use it in GitHub Desktop.
Save otac0n/1077130 to your computer and use it in GitHub Desktop.
Generated IE9 Test Fixture
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace NegativeTests
{
class Program
{
static void Main(string[] args)
{
var tests = GetAllTests().ToList();
var testFixtures = tests.Select(t => t.Fixture).Distinct().OrderBy(f => f).ToList();
var outPath = @"C:\Users\otac0n\Projects\IronJS\Src\Tests\UnitTests\IE9";
foreach (var fixture in testFixtures)
{
var fixtureTests = tests.Where(t => t.Fixture == fixture).OrderBy(t => t.TestName).ToList();
var subFixtures = (from f in testFixtures
where f.StartsWith(fixture + @"\")
select f).Any();
var fixtureParts = fixture.Split('\\').Select(SafeName).ToArray();
var className = fixtureParts[fixtureParts.Length - 1] + "_Tests";
var namespaceName = string.Join(".", fixtureParts.Take(fixtureParts.Length - (subFixtures || (fixtureParts.Length == 1) ? 0 : 1)));
var output = new StringBuilder();
output.AppendLine("// <auto-generated />");
output.AppendLine("namespace IronJS.Tests.UnitTests.IE9." + namespaceName);
output.AppendLine("{");
output.AppendLine(" using System;");
output.AppendLine(" using NUnit.Framework;");
output.AppendLine("");
output.AppendLine(" [TestFixture]");
output.AppendLine(" public class " + className + " : IE9TestFixture");
output.AppendLine(" {");
output.AppendLine(" public " + className + "() : base(@\"" + fixture + "\") { }");
output.AppendLine();
foreach (var test in fixtureTests)
{
output.AppendLine(" [Test(Description = " + QuoteString(test.Description) + ")] public void " + SafeName(test.TestName) + "() { RunFile(@\"" + Path.GetFileName(test.FullPath) + "\"); }");
}
output.AppendLine(" }");
output.Append("}");
var filePath = Path.Combine(Path.Combine(outPath, namespaceName.Replace(".", Path.DirectorySeparatorChar.ToString())), className + ".cs");
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
File.WriteAllText(filePath, output.ToString());
}
}
private static string SafeName(string name)
{
var cleaned = name.Replace(".", "_").Replace("-", "__");
var validInitial = Regex.IsMatch(cleaned, "^[A-Za-z]");
return (validInitial ? "" : "_") + cleaned;
}
private static string QuoteString(string str)
{
var sb = new StringBuilder(str.Length + 10);
sb.Append("\"");
foreach (var c in str)
{
switch (c)
{
case '\'':
sb.Append(@"\'");
break;
case '\"':
sb.Append(@"\""");
break;
case '\\':
sb.Append(@"\\");
break;
case '\0':
sb.Append(@"\0");
break;
case '\b':
sb.Append(@"\b");
break;
case '\f':
sb.Append(@"\f");
break;
case '\n':
sb.Append(@"\n");
break;
case '\r':
sb.Append(@"\r");
break;
case '\t':
sb.Append(@"\t");
break;
case '\v':
sb.Append(@"\v");
break;
default:
if (c >= 32 && c <= 126)
{
sb.Append(c);
}
else
{
sb.Append(@"\u" + ((int)c).ToString("X").PadLeft(4, '0'));
}
break;
}
}
sb.Append("\"");
return sb.ToString();
}
private static IEnumerable<TestCase> GetAllTests()
{
var baseDir = @"C:\Users\otac0n\Projects\IronJS\Src\Tests\ietestcenter\";
var files = Directory.GetFiles(baseDir, "*.js", SearchOption.AllDirectories);
foreach (var file in files)
{
yield return new TestCase(baseDir, file);
}
}
private class TestCase
{
public readonly string FullPath;
public readonly string RelativePath;
public readonly string Fixture;
public readonly string TestName;
public readonly string Description;
public readonly string Category;
public TestCase(string basePath, string path)
{
this.FullPath = path;
this.RelativePath = path.Substring(basePath.Length);
this.Fixture = Path.GetDirectoryName(this.RelativePath);
this.TestName = Path.GetFileNameWithoutExtension(this.RelativePath);
this.Category = this.RelativePath.Split(@"\".ToCharArray())[0];
var text = File.ReadAllText(path);
this.Description = Regex.Match(text, @"description:\s+""((?:[^""\\]|\\.)+)""", RegexOptions.Multiline).Groups[1].Value;
}
public override string ToString()
{
return this.Description;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment