<commit_before>#!/bin/bash

setUp(){
    [ -z "$TRAVIS_OS_NAME" ] \
        && printf "\033[s" #Save cursor position
}

tearDown(){
    [ -z "$TRAVIS_OS_NAME" ] \
        && printf "\033[u" `#Restore cursor position` && echo
}

testThatDrawWritesHeading() {
    local contains=' W E L C O M E'
    assertEquals "$contains" "$(draw | egrep -o "$contains")"
}

test_key_k() {
    read -r -d '' lines <<- EOF
	  * db334c1 2021-01-21  Commit 1
	  * db334c0 2021-01-20  Commit 0
	EOF
    index=1
    read_input <<< k
    assertEquals "k (up) should decrement pointer" 0 $index
}

test_key_j() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=1
    height=3
    read_input <<< j
    assertEquals "j (down) should increment pointer" 2 $index
}

test_key_j_k_graph() {
    read -r -d '' lines <<- EOF
	  *   db334c3 2021-01-23  Commit 3
	  |\ 
	  | * db334c2 2021-01-22  Commit 2
	  |/
	  *   db334c1 2021-01-21  Commit 1
	EOF
    height=5
    index=0
    read_input <<< j
    assertEquals "increment index graph" 2 $index
    read_input <<< j
    assertEquals "increment index graph" 4 $index
    read_input <<< k
    assertEquals "decrement index graph" 2 $index
    read_input <<< k
    assertEquals "decrement index graph" 0 $index
}

test_key_g() {
    from=some_commit
    index=2
    read_input <<< g
    assertEquals "g (beginning) should reset pointer" 0 $index
    assertEquals "g (beginning) should reset 'from'" HEAD $from
}

test_key_H() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=2
    read_input <<< H
    assertEquals "H (Home line) should reset pointer" 0 $index
}

test_key_L() {
    lines=""
    for i in {0..6}; do
        lines="$(printf '%s\n%s' "  * db334c$i 2021-01-2$i  Commit $i" "$lines")"
    done
    read_input <<< L
    assertEquals "L (end) should set pointer to end" 6 $index
}

test_key_L_end() {
    lines=""
    for i in {0..9}; do
        lines="$(printf '%s\n%s' "  * db334c$i 2021-01-2$i  Commit $i" "$lines")"
    done
    height=20
    read_input <<< L
    assertEquals "L (end) should set pointer to end" 9 $index
}

test_key_M() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3 index0
	  * db334c2 2021-01-22  Commit 2 index1
	  * db334c1 2021-01-21  Commit 1 index2
	EOF
    read_input <<< M
    assertEquals "M should set pointer to middle" 1 $index
}

test_index_mid_even() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3 index0
	  * db334c2 2021-01-22  Commit 2 index1
	  * db334c1 2021-01-21  Commit 1 index2
	  * db334c0 2021-01-20  Commit 0 index3
	EOF
    index_mid
    assertEquals 2 $index
}

test_index_mid_graph() {
    read -r -d '' lines <<- EOF
	  *   db334c4 2021-01-24  Commit 4 index0
	  *   db334c3 2021-01-23  Commit 3 index1
	  |\ 
	  | * db334c2 2021-01-22  Commit 2 index3
	  |/
	  *   db334c1 2021-01-21  Commit 1 index5
	EOF
    index_mid
    assertEquals "should set index" 3 $index
}

test_index_mid_graph_above() {
    read -r -d '' lines <<- EOF
	  *   db334c3 2021-01-23  Commit 3 index0
	  |\ 
	  | * db334c2 2021-01-22  Commit 2 index2
	  |/
	  *   db334c1 2021-01-21  Commit 1 index4
	  *   db334c0 2021-01-20  Commit 0 index5
	EOF
    index_mid
    assertEquals "should set index" 2 $index
}

test_key_k_at_top() {
    index=0
    read_input <<< k
    assertEquals "k (up) should not decrement pointer at start" 0 $index
}

test_key_j_bottom() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=1
    height=2
    read_input <<< j
    assertEquals "j (down) should not increment pointer at bottom" 1 $index
}

test_key_j_end() {
    read -r -d '' lines <<- EOF
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=1
    height=9
    read_input <<< j
    assertEquals "j (down) should not increment pointer at bottom" 1 $index
}

test_key_f_forward() {
    pager=('HEAD')
    from='HEAD'
    lines=""
    for i in {1..10}; do
        lines="$(printf '  * 00000%02d 2021-01-01  Commit %d\n%s' $i $i "$lines")"
    done
    index=2
    height=5
    read_input <<< f
    assertEquals "f should set index 0" 0 $index
    assertEquals "f should set HEAD" 0000001 $from
    assertEquals "f should set pager" 'HEAD 0000001' "${pager[*]}"
}

test_check_screen_size() {
    height=""
    width=""
    check_screen_size
    assertTrue "Width" "is_number $width"
}

test_state() {
    state=""
    set_state "index=2"
    assertEquals "index=2" "$state"

    set_state "from=HEAD"
    assertEquals "from=HEAD index=2 " "$(echo "$state" | sort | grep . | tr '\n' ' ')"

    set_state "index=1"
    assertEquals "from=HEAD index=1 " "$(echo "$state" | sort | grep . | tr '\n' ' ')"

    assertEquals "1" "$(get_state "$state" 'index')"
    assertEquals "HEAD" "$(get_state "$state" 'from')"

    set_state one=1 index=3 two=2
    assertEquals "3" "$(get_state "$state" 'index')"
    assertEquals "1" "$(get_state "$state" 'one')"
    assertEquals "2" "$(get_state "$state" 'two')"
    assertEquals $'1\n2' "$(get_state "$state" one two)"<commit_msg>Test less read and more functions directly<commit_after>#!/bin/bash

setUp(){
    [ -z "$TRAVIS_OS_NAME" ] \
        && printf "\033[s" #Save cursor position
}

tearDown(){
    [ -z "$TRAVIS_OS_NAME" ] \
        && printf "\033[u" `#Restore cursor position` && echo
}

testThatDrawWritesHeading() {
    local contains=' W E L C O M E'
    assertEquals "$contains" "$(draw | egrep -o "$contains")"
}

test_key_k() {
    read -r -d '' lines <<- EOF
	  * db334c1 2021-01-21  Commit 1
	  * db334c0 2021-01-20  Commit 0
	EOF
    index=1
    index_dec
    assertEquals "k (up) should decrement pointer" 0 $index
}

test_key_j() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=1
    index_inc
    assertEquals "j (down) should increment pointer" 2 $index
}

test_key_j_k_graph() {
    read -r -d '' lines <<- EOF
	  *   db334c3 2021-01-23  Commit 3
	  |\ 
	  | * db334c2 2021-01-22  Commit 2
	  |/
	  *   db334c1 2021-01-21  Commit 1
	EOF
    index=0
    index_inc
    assertEquals "increment index graph" 2 $index
    index_inc
    assertEquals "increment index graph" 4 $index
    index_dec
    assertEquals "decrement index graph" 2 $index
    index_dec
    assertEquals "decrement index graph" 0 $index
}

test_key_g() {
    from=some_commit
    index=2
    goto_beginning
    assertEquals "g (beginning) should reset pointer" 0 $index
    assertEquals "g (beginning) should reset 'from'" HEAD $from
}

test_key_H() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=2
    read_input <<< H
    assertEquals "H (Home line) should reset pointer" 0 $index
}

test_index_end() {
    lines=""
    for i in {0..6}; do
        lines="$(printf '%s\n%s' "  * db334c$i 2021-01-2$i  Commit $i" "$lines")"
    done
    index_end
    assertEquals "should set pointer to end" 6 $index
}

test_index_mid() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3 index0
	  * db334c2 2021-01-22  Commit 2 index1
	  * db334c1 2021-01-21  Commit 1 index2
	EOF
    index_mid
    assertEquals "M should set pointer to middle" 1 $index
}

test_index_mid_even() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3 index0
	  * db334c2 2021-01-22  Commit 2 index1
	  * db334c1 2021-01-21  Commit 1 index2
	  * db334c0 2021-01-20  Commit 0 index3
	EOF
    index_mid
    assertEquals 2 $index
}

test_index_mid_graph() {
    read -r -d '' lines <<- EOF
	  *   db334c4 2021-01-24  Commit 4 index0
	  *   db334c3 2021-01-23  Commit 3 index1
	  |\ 
	  | * db334c2 2021-01-22  Commit 2 index3
	  |/
	  *   db334c1 2021-01-21  Commit 1 index5
	EOF
    index_mid
    assertEquals "should set index" 3 $index
}

test_index_mid_graph_above() {
    read -r -d '' lines <<- EOF
	  *   db334c3 2021-01-23  Commit 3 index0
	  |\ 
	  | * db334c2 2021-01-22  Commit 2 index2
	  |/
	  *   db334c1 2021-01-21  Commit 1 index4
	  *   db334c0 2021-01-20  Commit 0 index5
	EOF
    index_mid
    assertEquals "should set index" 2 $index
}

test_index_dec_at_top() {
    index=0
    index_dec
    assertEquals "k (up) should not decrement pointer at start" 0 $index
}

test_index_inc_bottom() {
    read -r -d '' lines <<- EOF
	  * db334c3 2021-01-23  Commit 3
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=1
    height=2
    index_inc
    assertEquals "j (down) should not increment pointer at bottom" 1 $index
}

test_index_inc_end() {
    read -r -d '' lines <<- EOF
	  * db334c2 2021-01-22  Commit 2
	  * db334c1 2021-01-21  Commit 1
	EOF
    index=1
    height=9
    index_inc
    assertEquals "j (down) should not increment pointer at bottom" 1 $index
}

test_forward_page() {
    pager=('HEAD')
    from='HEAD'
    lines=""
    for i in {1..10}; do
        lines="$(printf '  * 00000%02d 2021-01-01  Commit %d\n%s' $i $i "$lines")"
    done
    index=2
    forward_page
    assertEquals "f should set index 0" 0 $index
    assertEquals "f should set HEAD" 0000001 $from
    assertEquals "f should set pager" 'HEAD 0000001' "${pager[*]}"
}

test_check_screen_size() {
    height=""
    width=""
    check_screen_size
    assertTrue "Width" "is_number $width"
}

test_state() {
    state=""
    set_state "index=2"
    assertEquals "index=2" "$state"

    set_state "from=HEAD"
    assertEquals "from=HEAD index=2 " "$(echo "$state" | sort | grep . | tr '\n' ' ')"

    set_state "index=1"
    assertEquals "from=HEAD index=1 " "$(echo "$state" | sort | grep . | tr '\n' ' ')"

    assertEquals "1" "$(get_state "$state" 'index')"
    assertEquals "HEAD" "$(get_state "$state" 'from')"

    set_state one=1 index=3 two=2
    assertEquals "3" "$(get_state "$state" 'index')"
    assertEquals "1" "$(get_state "$state" 'one')"
    assertEquals "2" "$(get_state "$state" 'two')"
    assertEquals $'1\n2' "$(get_state "$state" one two)"
{'fraction_non_alphanumeric': 0.12962782065058123, 'fraction_numerical': 0.10159226335840578, 'mean_word_length': 2.59102069449316, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1506512', 'n_tokens_mistral': 4607, 'n_tokens_neox': 3875, 'n_words': 1117}