Skip to content

Instantly share code, notes, and snippets.

@tillig
Created March 15, 2019 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tillig/afba0671b5bafe45eaadef4754ce0519 to your computer and use it in GitHub Desktop.
Save tillig/afba0671b5bafe45eaadef4754ce0519 to your computer and use it in GitHub Desktop.
User-level script to convert NUnit test fixtures to Xunit with simple regex replacement.
import sublime, sublime_plugin, re
class NunitToXunitCommand(sublime_plugin.TextCommand):
def run(self, edit):
regions = self.view.sel()
if len(regions) == 1 and regions[0].empty():
regions = [sublime.Region(0, self.view.size())]
for region in regions:
self.replace_test_syntax(edit, region)
def replace_test_syntax(self, edit, region):
namespace_re = re.compile("using\s+NUnit.Framework;")
testfixture_re = re.compile("\[TestFixture\]")
test_re = re.compile("\[Test(\(.+\))?\]")
testcase_re = re.compile("\[TestCase\((.+)\)\]")
testcasesource_re = re.compile("\[TestCaseSource\((.+)\)\]")
memberdatanameof_re = re.compile("\[MemberData\(\"(.+)\"\)\]")
fluent_equal_re = re.compile("Assert\.That\((.+)\s*,\s*Is\.EqualTo\((.+)\)\s*\);")
standard_equal_re = re.compile("Assert\.AreEqual\(\s*(.+)\s*,\s*(.+)\s*\);")
standard_notequal_re = re.compile("Assert\.AreNotEqual\(\s*(.+)\s*,\s*(.+)\s*\);")
fluent_same_re = re.compile("Assert\.That\((.+)\s*,\s*Is\.SameAs\((.+)\)\s*\);")
standard_same_re = re.compile("Assert\.AreSame\(\s*(.+)\s*,\s*(.+)\s*\);")
fluent_notsame_re = re.compile("Assert\.That\((.+)\s*,\s*Is\.Not\.SameAs\((.+)\)\s*\);")
standard_notsame_re = re.compile("Assert\.AreNotSame\(\s*(.+)\s*,\s*(.+)\s*\);")
fluent_null_re = re.compile("Assert\.That\((.+)\s*,\s*Is\.Null\s*\);")
standard_null_re = re.compile("Assert\.IsNull\(\s*(.+)\s*\);")
standard_notnull_re = re.compile("Assert\.IsNotNull\(\s*(.+)\s*\);")
fluent_instanceof_re = re.compile("Assert\.That\((.+)\s*,\s*Is\.InstanceOf\<([^>]+)\>\(\s*\)\s*\);")
standard_instanceof_re = re.compile("Assert\.IsInstanceOf\<([^(]+)\>\(\s*(.+)\s*\);")
standard_isfalse_re = re.compile("Assert\.IsFalse\(\s*(.+)\s*\);")
standard_istrue_re = re.compile("Assert\.IsTrue\(\s*(.+)\s*\);")
standard_empty_re = re.compile("Assert\.IsEmpty\(\s*(.+)\s*\);")
original = self.view.substr(region)
updated = namespace_re.sub("using Xunit;", original)
updated = testfixture_re.sub("", updated)
updated = test_re.sub("[Fact]", updated)
updated = testcase_re.sub("[InlineData(\g<1>)]", updated)
updated = testcasesource_re.sub("[MemberData(\g<1>)]", updated)
updated = memberdatanameof_re.sub("[MemberData(nameof(\g<1>))]", updated)
updated = fluent_equal_re.sub("Assert.Equal(\g<2>, \g<1>);", updated)
updated = standard_equal_re.sub("Assert.Equal(\g<1>, \g<2>);", updated)
updated = standard_notequal_re.sub("Assert.NotEqual(\g<1>, \g<2>);", updated)
updated = fluent_same_re.sub("Assert.Same(\g<2>, \g<1>);", updated)
updated = standard_same_re.sub("Assert.Same(\g<1>, \g<2>);", updated)
updated = fluent_notsame_re.sub("Assert.NotSame(\g<2>, \g<1>);", updated)
updated = standard_notsame_re.sub("Assert.NotSame(\g<1>, \g<2>);", updated)
updated = fluent_null_re.sub("Assert.Null(\g<1>);", updated)
updated = standard_null_re.sub("Assert.Null(\g<1>);", updated)
updated = standard_notnull_re.sub("Assert.NotNull(\g<1>);", updated)
updated = fluent_instanceof_re.sub("Assert.IsType<\g<2>>(\g<1>);", updated)
updated = standard_instanceof_re.sub("Assert.IsType<\g<1>>(\g<2>);", updated)
updated = standard_isfalse_re.sub("Assert.False(\g<1>);", updated)
updated = standard_istrue_re.sub("Assert.True(\g<1>);", updated)
updated = standard_empty_re.sub("Assert.Empty(\g<1>);", updated)
self.view.replace(edit, region, updated)
class NunitToXunitBatchCommand(NunitToXunitCommand):
def run(self, edit):
self.run_all_views()
def run_all_views(self):
for workingview in sublime.active_window().views():
workingview.run_command("nunit_to_xunit")
[
{
"caption": "Unit Test: NUnit to XUnit",
"command": "nunit_to_xunit"
},
{
"caption": "Unit Test: NUnit to XUnit (Batch)",
"command": "nunit_to_xunit_batch"
}
]
@tillig
Copy link
Author

tillig commented Mar 15, 2019

It's not 100% but it'll get you pretty far. Drop these in your User folder for Sublime Text and you'll get some commands for conversion in the palette.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment