photon
10/22/2018 - 8:36 AM

demo unit test for bash, just personal usage

demo unit test for bash, just personal usage

#!/bin/bash

source ./testf.bashlib

test1(){
    echo "hello world"
}

test_init_dir_unexist(){
    [ -e ./test9 ] && rm -r ./test9
    init_dir
    rslt="$(ls -d ./test9)"
    assert_equal_str "./test9" "$rslt"
}

test_init_dir_exist_no_op(){
    opt=""
    [ ! -e ./test9 ] && mkdir ./test9
    rslt=$(init_dir)
    n_words=$(get_first_n_words_by_space "$rslt" 3)
    assert_equal_str "Cannot create dir::" "$n_words"
    opt="-o"
}

test_init_dir_exist_op(){
    [ ! -e ./test9 ] && mkdir ./test9
    opt="-o"
    rslt=$(init_dir)
    assert_equal_str "" "$rslt"
}

test_assert_equal_str(){
    assert_equal_str "a" "a"
    assert_equal_str 1 1
    assert_equal_str "" ""
    assert_equal_str '' ""
    assert_equal_str ''
    
    rslt=$(assert_equal_str "a" "b")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"
}

test_assert_equal_num(){   
    assert_equal_num 1 1
    assert_equal_num "1" "1"

    rslt=$(assert_equal_num "" "")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"

    rslt=$(assert_equal_num 1 "")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"
    
    rslt=$(assert_equal_num "" 1)
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"

    rslt=$(assert_equal_num 1 2)
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"
    
    rslt=$(assert_equal_num "a" "a")
    n_words=$(get_first_n_words_by_space "$rslt" 1)
    assert_equal_str "Fail:" "$n_words"
}

set -- -o
opt="$1"

#run_tests test_assert_equal_num

tests1="test1 test_init_dir_unexist test_init_dir_exist_no_op test_init_dir_exist_op"
tests2="test_assert_equal_str test_assert_equal_num"
tests="$tests1 $tests2"
run_tests $(echo $tests)
#!/bin/bash

# ####test_demo_testf.bash
# #!/bin/bash
# 
# # Usage:
# # ./test_demo_testf.bash
# 
# # Use the -o to override the check on the test dir
# # ./test_demo_testf.bash -o
#
# source ./testf.bash
# 
# test1(){
#     echo "hello"
# }
# 
# test2(){
#     echo "world"
# }
# 
# opt="$1"
# run_tests "test1" "test2"


echo_start(){
    echo "$1 ============================="
}

get_bwd(){
    # bwd: base working dir
    # cwd: current working dir
    cwd=$(realpath .)
    : ${bwd=$cwd}
}

init_time_log(){
    fpath_log="$bwd"/time.log
    if [ -f $fpath_log ]
        then
        if [ "-o" != "$opt" ]
            then
            echo "Cannot create file:: File $fpath_log exists. Exit."
            exit 1
        fi
        
        rm $fpath_log
    fi

    touch $fpath_log
}

log_datetime(){
    datef=`date '+%Y%m%d.%H%M%S'`
    echo "$1$datef" >> $fpath_log
}

init_dir(){
    dpath_test="$bwd"/test9
    if [ -e $dpath_test ]
        then
        if [ "-o" != "$opt" ]
            then
            echo "Cannot create dir:: Dir (or file) $dpath_test exists. Exit."
            exit 1
        fi
        
        rm -r $dpath_test
    fi
    
    mkdir $dpath_test
    cd $dpath_test
}

init_script_dir(){
    # to be overwritten by the calling script
    init_dir
}

init_script_extra(){
    # to be overwritten by the calling script
    :
}

finish_script_extra(){
    # to be overwritten by the calling script
    :
}

init_script(){
    echo_start "Start script $0"
    get_bwd
    init_time_log
    log_datetime "Start script $0 : "  
    init_script_dir  
    init_script_extra
}

finish_script(){
    finish_script_extra
    echo_start "Finish script $0"
    log_datetime "Finish script $0 : "
}

init_test_extra(){
    # to be overwritten by the calling script
    :
}

finish_test_extra(){
    # to be overwritten by the calling script
    :
}

init_test(){
    echo_start "Start $1 : "
    log_datetime "Start $1 : "
    init_test_extra
}

finish_test(){
    finish_test_extra
    log_datetime "Finish $1 : "
}

run_tests(){
    init_script

    for test in "$@"
        do
        init_test "$test"
        $test
        finish_test "$test"
    done

    finish_script
}

get_first_n_words_by_space(){
    line="$1"
    n="$2"
    echo $(printf "$line" | cut -d " " -f 1-"$n")
}

assert_equal_str(){
    expected="$1"
    actual="$2"
    if [ "$expected" == "$actual" ]
        then
        echo Pass
        return 0
    else  
        echo "Fail: $expected != $actual"
        return 1
    fi
}

assert_equal_num(){
    expected="$1"
    actual="$2"

    if [[ "y" == "y$expected" && "y"=="y$actual" ]]
        then
            echo "Fail: arg1:expected is '', arg2:actual is ''"
        return 1
    fi
    
    if [ "y" == "y$expected" ]
        then
        echo "Fail: arg1:expected is ''"
        return 1
    fi
    
    if [ "y" == "y$actual" ]
        then
        echo "Fail: arg2:actual is ''"
        return 1
    fi
    
    if [ $expected -eq $actual ]
        then
        echo Pass
    else  
        echo "Fail: $expected != $actual"
        return 0
    fi
}

test1(){
    # call the tested script
    :
}