Skip to content

Instantly share code, notes, and snippets.

@andrewchilds
Last active August 21, 2018 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewchilds/8658e5be71dd4e337da4 to your computer and use it in GitHub Desktop.
Save andrewchilds/8658e5be71dd4e337da4 to your computer and use it in GitHub Desktop.
Jasmine Unit Test Generator
#!/bin/bash
# Jasmine Unit Test Generator
# Assumes the codebase uses this module pattern:
# (https://gist.github.com/andrewchilds/ffcae28b2b01c5fbbb12)
# App.module('ModuleName', function (exports) {
# exports.myFunctionName = function () {};
# });
# Assumes Jasmines testing uses this structure:
# describe 'ModuleName', ->
# describe '#myFunctionName', ->
# set -x
set -e
################################################################################
# Configuration
sourceFileRoot="app/client/js"
testFileRoot="test/spec/client"
specFileExtension=".spec.coffee"
################################################################################
function escapeForSed {
echo "$1" | sed -e 's/[]\/$*.^|[]/\\&/g'
}
escapedSourceFileRoot="$(escapeForSed $sourceFileRoot)"
fileCount=0
testedCount=0
untestedCount=0
sourceFiles=$(ls -R $sourceFileRoot/*.js $sourceFileRoot/*/*.js)
for sourceFile in $sourceFiles; do
fileCount=$(($fileCount + 1))
testFile=$(echo "$sourceFile" | sed "s/$escapedSourceFileRoot//g" | sed "s/.js$/$specFileExtension/g")
testFile="$testFileRoot$testFile"
testFunctions=$(grep "exports\..* = function (" $sourceFile | sed 's/ *exports.//g' | sed 's/ = function .*//g')
fileNamespace=$(cat "$sourceFile" | grep 'App.module(' | grep 'App\.module(' | sed 's/App.module(//g' | sed 's/, .*//g')
if [ ! -e "$testFile" ]; then
mkdir -p "$(dirname "$testFile")"
touch "$testFile"
cat >> "$testFile" << EOF
describe $fileNamespace, ->
EOF
fi
for testFunction in $testFunctions; do
if grep -q "describe \'\#$testFunction\'" "$testFile"; then
testedCount=$(($testedCount + 1))
else
untestedCount=$(($untestedCount + 1))
cat >> "$testFile" << EOF
describe '#$testFunction', ->
it 'should work', ->
expect(true).toBe(false)
EOF
fi
done
done
echo "$fileCount source files scanned, found $testedCount functions tested and $untestedCount functions untested."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment