LoginSignup
15
14

More than 5 years have passed since last update.

shunit の導入(zshで使う場合)

Last updated at Posted at 2014-04-05

shunit の導入(zshで使う場合)

持ってくる

  • 最新とか簡単に入れたいからgithubから
$ git clone https://github.com/zanshine/shunit2.git

テストコード書く

#!/usr/bin/env zsh

set -ue

cd $(cd $(dirname $0); pwd)

# zshの場合は明示してあげないとダメ
SHUNIT_PARENT=$0

. ../bin/string.sh

function test_trim () {
    # 1. 先頭だけにスペースがあるケース
    assertEquals "$(trim '    hoge  foo   bar')" 'hoge  foo   bar'

    # 2. 末尾だけにスペースがあるケース
    assertEquals "$(trim 'hoge  foo   bar   ')" 'hoge  foo   bar'

    # 3. 先頭/末尾ともにスペースがあるケース
    assertEquals "$(trim '    hoge  foo   bar   ')" 'hoge  foo   bar'

    # 4. 先頭/末尾ともにスペースがないケース
    assertEquals "$(trim 'hoge  foo   bar')" 'hoge  foo   bar'
}

. ../../shunit2/src/shunit2

テストが通る関数書く

#!/usr/bin/env zsh

set -ue

function trim () {
    echo "$(echo "$1" | sed 's/^  *//' | sed 's/  *$//')"
}

とりあえず、動かしてみる その1

  • これだと何も起こらない
$ zsh string.t

とりあえず、動かしてみる その2

  • shwordsplit オプションが必要
$ zsh -o shwordsplit -- string.t
../../shunit2/src/shunit2:readonly:64: not valid in this context: (標準入力)

問題箇所を修正する

  • あとでプルリク投げておこう
diff --git a/src/shunit2 b/src/shunit2
index 7077149..796fa18 100755
--- a/src/shunit2
+++ b/src/shunit2
@@ -51,7 +51,7 @@ __SHUNIT_MODE_STANDALONE='standalone'
 __SHUNIT_PARENT=${SHUNIT_PARENT:-$0}

 # set the constants readonly
-shunit_constants_=`set |grep '^__SHUNIT_' |cut -d= -f1`
+shunit_constants_=`set |grep --text --binary-files=text '^__SHUNIT_' |cut -d= -f1`
 echo "${shunit_constants_}" |grep '^Binary file' >/dev/null && \
     shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1`
 for shunit_constant_ in ${shunit_constants_}; do
  • これで動く
$ zsh -o shwordsplit -- string.t
test_trim

Ran 1 test.

OK
15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14