Last active
May 4, 2017 20:01
-
-
Save stdray/966ec31e083f02d8684788e632b802a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Nemerle; | |
| using Nemerle.Collections; | |
| using Nemerle.Peg; | |
| using Nemerle.Utility; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace XUnitMigrator | |
| { | |
| using nt = Nemerle.Peg.NToken; | |
| [PegGrammar(Option = EmitDebugSources, rewrite, grammar { | |
| ch = ['a'..'z', 'A'..'Z']; | |
| nl = "\r\n" / '\n' / '\r' / '\u2028' / '\u2029' / '\u0085'; | |
| ws = ' ' / '\t'; | |
| op : void = "("; | |
| cl : void = ")"; | |
| bop : void = "["; | |
| bcl : void = "]"; | |
| eq : void = "="; | |
| s : void = ws*; | |
| singleLineComment = "//" (!nl [Any])+; | |
| multiLineComment = "/*" (!"*/" [Any])+ "*/"; | |
| comment = singleLineComment / multiLineComment; | |
| stringContent = '"' (<#\"#> / (!'"' [Any]))* '"'; | |
| braceContent = (!")" [Any]); | |
| operatorContent = ("typeof" / "nameof") s op braceContent+ cl; | |
| attributeBraceContent = (!operatorContent !stringContent braceContent)+; | |
| attributeContent = operatorContent / stringContent / attributeBraceContent; | |
| namespaceConvert : string = "NUnit.Framework"; | |
| testFixture1 = bop s "TestFixture" s (op s cl)? s bcl; | |
| testFixture2 = s "TestFixture" s ',' s; | |
| dontConvertInComment : string = comment; | |
| removeTestFixture : string = testFixture1 / testFixture2; | |
| testToFact : string = bop s ("TestCase" / "Test") s (op s cl)? s bcl; | |
| testCategoryToTrait : string = s "Category" s op s attributeContent+ s cl s; | |
| testCaseToInlineData : string = bop s "TestCase" s op s attributeContent+ s cl s bcl; | |
| attributeSeparator = comment / ws / nl; | |
| //ignoreToSkip : string = "Ignore" s eq s ("true" / "false"); | |
| //ignoreReasonToSkip : string = "IgnoreReason" s eq stringContent; | |
| testCasesToTheory : string = (attributeSeparator* testCaseToInlineData)+; | |
| convertAssert : string = (!"Assert"ch)* "Assert" s "." s ch+ (!"(" [Any])? op attributeContent* cl; | |
| rules : string = dontConvertInComment | |
| / namespaceConvert | |
| / removeTestFixture | |
| / testToFact | |
| / testCategoryToTrait | |
| / testCasesToTheory | |
| / convertAssert; | |
| others : string = (!rules [Any])+; | |
| rewrite : string = s (rules / others)+ s ![Any]; | |
| })] | |
| public class Parser | |
| { | |
| dontConvertInComment(comment : nt) : string | |
| { | |
| GetText(comment) | |
| } | |
| //ignoreToSkip(_ : nt, val : nt) : string | |
| //{ | |
| // $"Skip = $(GetText(val))"; | |
| //} | |
| //ignoreReasonToSkip(_ : nt, reason : nt) : string | |
| //{ | |
| // $"Skip = true /*$(GetText(reason))*/"; | |
| //} | |
| removeTestFixture(_ : nt) : string | |
| { | |
| string.Empty | |
| } | |
| testCategoryToTrait(_ : nt, cat : nt) : string | |
| { | |
| def catStr = GetText(cat); | |
| $<#Trait("Category", $catStr)#> | |
| } | |
| testCaseToInlineData(_ : nt, args : nt) : string | |
| { | |
| def data = GetText(args); | |
| $<#[InlineData($data)]#> | |
| } | |
| testCasesToTheory(attrs : List[nt*string]) : string | |
| { | |
| def (findent, _) = attrs[0]; | |
| def sb = StringBuilder(); | |
| _ = sb.Append(GetText(findent)).AppendLine("[Theory]"); | |
| foreach((indent, attr) in attrs with i) | |
| { | |
| def indentText = GetText(indent); | |
| def indentText2 = | |
| if(i == 0) indentText.Replace("\r\n", "").Replace("\n", ""); | |
| else indentText; | |
| _ = sb.Append(indentText2).Append(attr); | |
| } | |
| sb.ToString(); | |
| } | |
| namespaceConvert(_ : nt) : string | |
| { | |
| "Xunit" | |
| } | |
| others(str : nt) : string | |
| { | |
| System.Diagnostics.Trace.WriteLine(GetText(str)); | |
| GetText(str); | |
| } | |
| rewrite(strs : List[string]) : string | |
| { | |
| string.Join(string.Empty, strs) | |
| } | |
| testToFact(_ : nt) : string | |
| { | |
| "[Fact]" | |
| } | |
| convertAssert(pref : nt, assert : nt, _ : nt, method : nt, generic : nt, args : nt) : string | |
| { | |
| def g = GetText(generic); | |
| def a = GetText(args); | |
| def prms = a.Split(array [','], StringSplitOptions.RemoveEmptyEntries); | |
| def m = GetText(method); | |
| def original = $"$(GetText(pref))$(GetText(assert)).$m$g($a)"; | |
| match(m) | |
| { | |
| | "AreEqual" => $"Assert.Equal($a)"; | |
| | "IsTrue" => $"Assert.True($a)"; | |
| | "IsNotNull" when prms.Length == 1 => $"Assert.NotNull($a)"; | |
| | "IsNotNull" when prms.Length == 2 => | |
| def data = prms[0]; | |
| def msg = prms[1]; | |
| $"Assert.False(ReferenceEquals(null, $data), $msg); //$original"; | |
| | "IsNull" => $"Assert.Null($a)"; | |
| | "IsFalse" => $"Assert.False($a)"; | |
| | "Fail" => $"Assert.True(false, $a);" | |
| | "DoesNotContain" => $"Assert.DoesNotContain($a)"; | |
| | "IsNotEmpty" => $"Assert.NotEmpty($a)"; | |
| | "IsNotNullOrEmpty"when prms.Length == 1 => $"Assert.NotEmpty($a)"; | |
| | "IsNotNullOrEmpty"when prms.Length == 2 => | |
| def data = prms[0]; | |
| def msg = prms[1]; | |
| $"Assert.True(!ReferenceEquals(null, $data) && $data.Any(), $msg); //$original"; | |
| | "StartsWith" => $"Assert.StartsWith($a)"; | |
| | "Contains" => $"Assert.Contains($a)"; | |
| | "That" => $"Assert.True($a)"; | |
| | "IsEmpty" => $"Assert.Empty($a)"; | |
| | "AreNotEqual" => $"Assert.NotEqual($a)"; | |
| | "AreEqualIgnoringCase" => $"Assert.Equal($a, StringComparer.OrdinalIgnoreCase)"; | |
| | "EndsWith" => $"Assert.EndsWith($a)"; | |
| | "IsNullOrEmpty" => $"Assert.Empty$g($a)"; | |
| | "Throws" => $"Assert.Throws$g($a)"; | |
| | "NotNull" => $"Assert.NotNull($a)"; | |
| | "DoesNotThrow" => $"Assert.DoesNotThrows$g($a)"; | |
| | "DoesNotStartWith" => | |
| def s1 = prms[0]; | |
| def s2 = prms[1]; | |
| if(prms.Length == 3) $"Assert.False($s1.StartsWith($s2), $(prms[2]))"; | |
| else $"Assert.False($s1.StartsWith($s2))"; | |
| | m => | |
| //Console.WriteLine($"$m not supported"); | |
| $"Assert.$m$g($a)"; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment