Skip to content

Instantly share code, notes, and snippets.

@taji
Created December 18, 2015 04:54
Show Gist options
  • Save taji/bc2ae8d5e996eaadc03f to your computer and use it in GitHub Desktop.
Save taji/bc2ae8d5e996eaadc03f to your computer and use it in GitHub Desktop.
Sublime snippet to convert a method call and comment into a mocha unit test.
<snippet>
<content><![CDATA[
${TM_SELECTED_TEXT/\s*var\s*(\S*)\s*=\s*([a-zA-Z0-9]*)[(](.*)[)];\s*\/\/\s*[-=]>\s*(.*)/describe\("$2\(\)", \(\) => {\n it\("should ...", \(\) => {\n var $1 = $2\($3\);\n expect\($1\).to.equal\($4\);\n }\);\n}\);/}
$0
]]></content>
<!-- Notes:
1. We are searching for this:
var actual = myMethod(myArgs); // => myResult
2. and converting it to this:
describe("myMethod()", () => {
it("should ...", () => {
var actual = myMethod(myArgs);
expect(actual).to.equal(myResult);
});
});
3. Conditions:
a. THERE HAS TO BE A SEMICOLON AFTER THE METHOD CALL.
b. YOU HAVE TO SELECT THE ENTIRE LINE TO GET THIS SNIPPET TO WORK.
c. YOU HAVE TO MAP IT TO A KEY AS SO:
[
{ "keys": ["ctrl+1"], "command": "insert_snippet", "args": {"name": "Packages/User/mysnippet1.sublime-snippet"} }
]
4. use this format string for debugging the search:
/actual=$1\nmyMethod=$2\nmyArgs=$3\nmyResult=$4/
End Notes -->
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!--<tabTrigger>t2m</tabTrigger>-->
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment