Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lerna-util-sequence/src/test/resources/test-cases/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Test Cases

テストケースを洗い出すためのリソースです

## テストケースの見方

テストケースは [picts](./picts) ディレクトリ内の `*.pict` という拡張子のファイルに記述されています。

ファイル下部にある次の部分にテストケースが列挙されています。

```
##################### OUTPUT #####################
# 全組み合わせ網羅ケース:
# No maxReservedValue nextValue after_nextValue after_isStarving after_freeAmount @isOverflow @isEmpty @採番 @after_isOverflow @after_isEmpty @採番値予約 @採番値リセット
# 1 < maxSequenceValue < maxReservedValue < maxReservedValue FALSE = 0 FALSE FALSE する FALSE FALSE しない しない
...以降略...
```

テストケースの更新方法は以降のセクションを確認してください。

## テストケースの作成手順

テストケースを洗い出すのに `pict` というツールを利用します。

[microsoft/pict: Pairwise Independent Combinatorial Tool](https://github.com/microsoft/pict)

`pict` でテストケースを出力するためにはモデルファイルの作成が必要です。
既存のモデルファイルは `*.pict` という拡張子で [picts](./picts) ディレクトリに格納されています。

モデルファイルの詳細な記法については次のページを参照してください:
https://github.com/Microsoft/pict/blob/main/doc/pict.md

モデルファイルを作成した後は `pict` をラップした [scripts/pict-coverall.sh](scripts/pict-coverall.sh) を使ってテストケースを出力できます。
このスクリプトを使うには Docker がインストールされている必要があります。

パラメータ名にプレフィックス `@` を付けると、このスクリプトではテストケースの結果(期待値)として特別扱いされます。
同じ条件で複数の結果が導かれるようになってしまっている場合は「条件重複ケース」として出力されます。
条件重複ケースがなくなるように結果の条件を調整してください。

モデルファイルを引数に指定してスクリプトを実行すると、生成されたテストケースがコンソールに出力されます。

```shell
cd test-cases
scripts/pict-coverall.sh picts/sequence_factory_worker_gen.pict
```

同時にモデルファイル内に、コンソールに出力された内容が追記されます。

```
##################### OUTPUT #####################
# 全組み合わせ網羅ケース:
# No maxReservedValue nextValue after_nextValue after_isStarving after_freeAmount @isOverflow @isEmpty @採番 @after_isOverflow @after_isEmpty @採番値予約 @採番値リセット
# 1 < maxSequenceValue < maxReservedValue < maxReservedValue FALSE = 0 FALSE FALSE する FALSE FALSE しない しない
...以降略...
```

モデルファイルの内容を変更した場合は、スクリプトを再実行することモデルファイル内に出力されたテストケースを更新できます。
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,8 @@ IF [@after_isOverflow] = "TRUE" AND [@after_isEmpty] = "TRUE"
THEN [@採番値リセット] = "する"
ELSE [@採番値リセット] = "しない";


# ❯ cat sequence_factory_worker.pict | docker run --rm -i ghcr.io/iceomix/pict -o:12 | tee pict.txt
# ❯ cat <(head -n1 pict.txt) <(sort -t $'\t' <(sed '1d' pict.txt)) | awk -v FS='\t' -v OFS='\t' '{ print (NR == 1) ? "No" : NR - 1, $0 }' | column --table -s $'\t' | sed -E 's/^/# /'
#
# -o: オプションで条件数と同じ数を指定することで全網羅パターンを出力できる。(デフォルトではペアワイズ法によりケースが絞られる)
# 参考:
# ペアワイズ法によるテストケース抽出ツール「PICT」を使ってテストケースを85%削減する - Qiita
# https://qiita.com/bremen/items/6eceddc534d87fc797cc
#
##################### OUTPUT #####################
# 全組み合わせ網羅ケース:
# No maxReservedValue nextValue after_nextValue after_isStarving after_freeAmount @isOverflow @isEmpty @採番 @after_isOverflow @after_isEmpty @採番値予約 @採番値リセット
# 1 < maxSequenceValue < maxReservedValue < maxReservedValue FALSE = 0 FALSE FALSE する FALSE FALSE しない しない
# 2 < maxSequenceValue < maxReservedValue < maxReservedValue FALSE > 0 FALSE FALSE する FALSE FALSE しない しない
Expand All @@ -187,8 +180,5 @@ ELSE [@採番値リセット] = "しない";
# 10 = maxSequenceValue = maxReservedValue > maxReservedValue TRUE = 0 FALSE FALSE する TRUE TRUE しない する
# 11 = maxSequenceValue > maxReservedValue > maxReservedValue TRUE = 0 TRUE TRUE しない TRUE TRUE しない する
#
# プレフィックス "@" が付いてない条件の組み合わせに重複がある場合、「結果の条件」の定義に漏れがあることがわかる。
#
# ❯ cat pict.txt | cut -f 1,2,3,4,5 | sort | uniq --repeated | wc -l
# 0
#
# 条件重複ケース(存在する場合は「結果の条件」の定義に漏れがある):
# 重複ID No maxReservedValue nextValue after_nextValue after_isStarving after_freeAmount @isOverflow @isEmpty @採番 @after_isOverflow @after_isEmpty @採番値予約 @採番値リセット
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash

set -eu

readonly output_label='##################### OUTPUT #####################'

readonly result_file="$(mktemp)"
readonly pict_output_file="$(mktemp)"

trap on_exit EXIT
function on_exit {
rm "${result_file}" "${pict_output_file}"
}

function main {
local pict_file="$1" nr_of_column

nr_of_column="$(count_column "${pict_file}")"

# -o: オプションで条件数と同じ数を指定することで全網羅パターンを出力できる。(デフォルトではペアワイズ法によりケースが絞られる)
# 参考:
# ペアワイズ法によるテストケース抽出ツール「PICT」を使ってテストケースを85%削減する - Qiita
# https://qiita.com/bremen/items/6eceddc534d87fc797cc
cat "${pict_file}" | pict -o:${nr_of_column} > "${pict_output_file}"

{
cat "${pict_file}" | remove_output
echo "${output_label}"
} > "${result_file}"
{
echo "# 全組み合わせ網羅ケース:"
normalize_pict_output "${pict_output_file}" | decorate_table
echo "#"
echo "# 条件重複ケース(存在する場合は「結果の条件」の定義に漏れがある):"
normalize_pict_output "${pict_output_file}" | print_repeated_factors | decorate_table
} | tee -a "${result_file}"

cat "${result_file}" > "${pict_file}"
}

function count_column {
local pict_file="$1"
cat "${pict_file}" | pict | head -n 1 | awk -v FS='\t' '{ print NF }'
}

function remove_output {
awk -v output_label="${output_label}" '
$0 == output_label {
exit 0
}
{
print
}
'
}

function normalize_pict_output {
local output_file="$1"
cat <(head -n1 "${output_file}") <(sort -t $'\t' <(sed '1d' "${output_file}")) \
| awk -v FS='\t' -v OFS='\t' '{ print (NR == 1) ? "No" : NR - 1, $0 }'
}

function decorate_table {
column --table -s $'\t' | sed -E 's/^/# /'
}

function print_repeated_factors {
awk -v FS='\t' -v OFS='\t' '
BEGIN {
current_factor = ""
previous_factor = ""
previous_all = ""
previous_output = ""
group_id = 1
}
{
split($0, line, FS)
}
NR == 1 {
for (i in line) {
if (match(line[i], /^[^@]/) && i != 1) {
factor_indexes[i] = i
}
}
print "重複ID" OFS $0
}
NR > 1 {
current_factor = ""
for (i in factor_indexes) {
current_factor = current_factor line[factor_indexes[i]] OFS
}
if (current_factor == previous_factor) {
if (previous_all != previous_output) {
print group_id OFS previous_all
}
print group_id OFS $0
previous_output = $0
} else {
group_id = group_id + 1
}
previous_factor = current_factor
previous_all = $0
}
'
}

function pict {
docker run --rm -i ghcr.io/iceomix/pict "$@"
}

main "$@"