Skip to content

Instantly share code, notes, and snippets.

@photonxp
Last active October 28, 2018 08:35

Revisions

  1. photonxp revised this gist Oct 28, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test_testf.bash
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash

    source ./testf.bash
    source ./testf.bashlib

    test1(){
    echo "hello world"
  2. photonxp revised this gist Oct 28, 2018. 1 changed file with 192 additions and 0 deletions.
    192 changes: 192 additions & 0 deletions testf.bashlib
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,192 @@
    #!/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
    :
    }
  3. photonxp revised this gist Oct 28, 2018. 1 changed file with 0 additions and 192 deletions.
    192 changes: 0 additions & 192 deletions testf.bashlib
    Original file line number Diff line number Diff line change
    @@ -1,192 +0,0 @@
    #!/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
    :
    }
  4. photonxp renamed this gist Oct 28, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. photonxp revised this gist Oct 26, 2018. 1 changed file with 42 additions and 4 deletions.
    46 changes: 42 additions & 4 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -27,8 +27,15 @@ echo_start(){
    echo "$1 ============================="
    }

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

    init_time_log(){
    fpath_log=./time.log
    fpath_log="$bwd"/time.log
    if [ -f $fpath_log ]
    then
    if [ "-o" != "$opt" ]
    @@ -49,7 +56,7 @@ log_datetime(){
    }

    init_dir(){
    dpath_test=./test9
    dpath_test="$bwd"/test9
    if [ -e $dpath_test ]
    then
    if [ "-o" != "$opt" ]
    @@ -62,26 +69,57 @@ init_dir(){
    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_dir
    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 : "
    }

  6. photonxp revised this gist Oct 24, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -110,8 +110,10 @@ assert_equal_str(){
    if [ "$expected" == "$actual" ]
    then
    echo Pass
    return 0
    else
    echo "Fail: $expected != $actual"
    return 1
    fi
    }

  7. photonxp revised this gist Oct 24, 2018. 2 changed files with 32 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion test_testf.bash
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,18 @@ test_assert_equal_str(){
    test_assert_equal_num(){
    assert_equal_num 1 1
    assert_equal_num "1" "1"
    assert_equal_num "" ""

    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)
    20 changes: 20 additions & 0 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -118,11 +118,31 @@ assert_equal_str(){
    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
    }

  8. photonxp revised this gist Oct 24, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions test_testf.bash
    Original file line number Diff line number Diff line change
    @@ -14,18 +14,19 @@ test_init_dir_unexist(){
    }

    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"
    opt=""
    }

    test_assert_equal_str(){
    @@ -59,7 +60,7 @@ opt="$1"

    #run_tests test_assert_equal_num

    tests1="test1 test_init_dir_unexist test_init_dir_exist_op test_init_dir_exist_no_op"
    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)
  9. photonxp revised this gist Oct 24, 2018. 2 changed files with 42 additions and 2 deletions.
    33 changes: 31 additions & 2 deletions test_testf.bash
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,38 @@ test_init_dir_exist_op(){
    opt=""
    }

    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"
    assert_equal_num "" ""

    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_init_dir_exist
    tests="test1 test_init_dir_unexist test_init_dir_exist_op test_init_dir_exist_no_op"
    #run_tests test_assert_equal_num

    tests1="test1 test_init_dir_unexist test_init_dir_exist_op test_init_dir_exist_no_op"
    tests2="test_assert_equal_str test_assert_equal_num"
    tests="$tests1 $tests2"
    run_tests $(echo $tests)
    11 changes: 11 additions & 0 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -115,6 +115,17 @@ assert_equal_str(){
    fi
    }

    assert_equal_num(){
    expected="$1"
    actual="$2"
    if [ $expected -eq $actual ]
    then
    echo Pass
    else
    echo "Fail: $expected != $actual"
    fi
    }

    test1(){
    # call the tested script
    :
  10. photonxp revised this gist Oct 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion testf.bash
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    # # Usage:
    # # ./test_demo_testf.bash
    #
    # # Use the -o to override the check on test dir
    # # Use the -o to override the check on the test dir
    # # ./test_demo_testf.bash -o
    #
    # source ./testf.bash
  11. photonxp revised this gist Oct 24, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,12 @@
    # ####test_demo_testf.bash
    # #!/bin/bash
    #
    # # Usage:
    # # ./test_demo_testf.bash
    #
    # # Use the -o to override the check on test dir
    # # ./test_demo_testf.bash -o
    #
    # source ./testf.bash
    #
    # test1(){
  12. photonxp revised this gist Oct 24, 2018. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ init_time_log(){
    then
    if [ "-o" != "$opt" ]
    then
    echo "Cannot create file:: File time.log exists. Exit."
    echo "Cannot create file:: File $fpath_log exists. Exit."
    exit 1
    fi

    @@ -43,18 +43,19 @@ log_datetime(){
    }

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

    rm -r ./test9
    rm -r $dpath_test
    fi

    mkdir ./test9
    mkdir $dpath_test
    }

    init_script(){
  13. photonxp revised this gist Oct 24, 2018. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion testf.bash
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,12 @@ init_time_log(){
    fpath_log=./time.log
    if [ -f $fpath_log ]
    then
    # if ! -o, exit
    if [ "-o" != "$opt" ]
    then
    echo "Cannot create file:: File time.log exists. Exit."
    exit 1
    fi

    rm $fpath_log
    fi

  14. photonxp revised this gist Oct 24, 2018. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -47,10 +47,9 @@ init_dir(){
    fi

    rm -r ./test9
    mkdir ./test9
    else
    mkdir ./test9
    fi

    mkdir ./test9
    }

    init_script(){
  15. photonxp revised this gist Oct 24, 2018. 2 changed files with 79 additions and 8 deletions.
    36 changes: 36 additions & 0 deletions test_testf.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/bin/bash

    source ./testf.bash

    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(){
    [ ! -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"
    }

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

    set -- -o
    opt="$1"

    #run_tests test_init_dir_exist
    tests="test1 test_init_dir_unexist test_init_dir_exist_op test_init_dir_exist_no_op"
    run_tests $(echo $tests)
    51 changes: 43 additions & 8 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash

    # ####test_testf.bash
    # ####test_demo_testf.bash
    # #!/bin/bash
    #
    # source ./testf.bash
    @@ -13,12 +13,19 @@
    # echo "world"
    # }
    #
    # opt="$1"
    # run_tests "test1" "test2"


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

    init_time_log(){
    fpath_log=./time.log
    if [ -f $fpath_log ]
    then
    # if ! -o, exit
    rm $fpath_log
    fi

    @@ -30,21 +37,34 @@ log_datetime(){
    echo "$1$datef" >> $fpath_log
    }

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

    rm -r ./test9
    mkdir ./test9
    else
    mkdir ./test9
    fi
    }

    init_script(){
    echo_start "Start script $0"
    init_time_log
    log_datetime "Start script $0 : "
    init_dir
    }

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

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

    init_test(){
    echo_start "Start $1 : "
    log_datetime "Start $1 : "
    @@ -67,9 +87,24 @@ run_tests(){
    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
    else
    echo "Fail: $expected != $actual"
    fi
    }

    test1(){
    # call the tested script
    :
    }

    #run_tests "test1"
    }
  16. photonxp revised this gist Oct 23, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -59,9 +59,9 @@ run_tests(){

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

    finish_script
  17. photonxp revised this gist Oct 23, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    #!/bin/bash

    # ####test_testf.bash
    # #!/bin/bash
    #
    # source ./testf.bash
  18. photonxp revised this gist Oct 23, 2018. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,18 @@
    #!/bin/bash

    # ####test_testf.bash
    # #!/bin/bash
    #
    # source ./testf.bash
    #
    # test1(){
    # echo "hello, world"
    # echo "hello"
    # }
    #
    # run_tests "test1"
    # test2(){
    # echo "world"
    # }
    #
    # run_tests "test1" "test2"

    init_time_log(){
    fpath_log=./time.log
  19. photonxp created this gist Oct 22, 2018.
    71 changes: 71 additions & 0 deletions testf.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    #!/bin/bash

    # ####test_testf.bash
    # #!/bin/bash
    #
    # source ./testf.bash
    #
    # test1(){
    # echo "hello, world"
    # }
    #
    # run_tests "test1"

    init_time_log(){
    fpath_log=./time.log
    if [ -f $fpath_log ]
    then
    rm $fpath_log
    fi

    touch $fpath_log
    }

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

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

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

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

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

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

    run_tests(){
    init_script

    for test in "$@"
    do
    init "$test"
    $test
    finish "$test"
    done

    finish_script
    }

    test1(){
    # call the tested script
    :
    }

    #run_tests "test1"