Skip to content
Dave Marquardt edited this page May 6, 2020 · 2 revisions

Coding Standards

Variable and function names

All shUnit2 specific constants, variables, and functions will be prefixed appropriately with 'shunit'. This is to distinguish usage in the shUnit2 code from users own scripts so that the shell name space remains predictable to users. The exceptions here are the standard assertEquals, etc. functions.

All non-builtin constants and variables will be surrounded with squiggle brackets, e.g. ${shunit_someVariable} to improve code readability.

Due to some shells not supporting local variables in functions, care in the naming and use of variables, both public and private, is very important. Accidental overriding of the variables can occur easily if care is not taken as all variables are technically global variables in some shells.

Type Sample
global public constant SHUNIT_TRUE
global private constant __SHUNIT_SHELL_FLAGS
global public variable not used
global private variable __shunit_someVariable
global macro _SHUNIT_SOME_MACRO_
public function assertEquals
public function, local variable shunit_someVariable_
private function _shunit_someFunction
private function, local variable _shunit_someVariable_

Where it makes sense, variables can have the first letter of the second and later words capitalized. For example, the local variable name for the total number of test cases seen might be shunit_totalTestsSeen_.

Local variable cleanup

As many shells do not support local variables, no support for cleanup of variables is present either. As such, all variables local to a function must be cleared up with the unset command at the end of each function.

Conditionals

if/then conditionals can be shortened using [ ].

[ -z 'sone string' ] && someFunction

Indentation

Code block indentation is two (2) spaces, and tabs may not be used.

if [ -z 'some string' ]; then
  someFunction
fi

Wrapping

Lines of code should be no longer than 80 characters unless absolutely necessary. When lines are wrapped using the backslash character '\', subsequent lines should be indented with four (4) spaces so as to differentiate from the standard spacing of two characters. Tabs may not be used.

for x in some set of very long set of arguments that make for a very long \
    that extends much too long for one line
do
  echo ${x}
done

Shortened if/then conditionals should place the && or || on the next line, indented by four (4) spaces.

[ -z 'some string' ] \
    && someFunction