LoginSignup
4
3

More than 5 years have passed since last update.

組み合わせテストを簡単にするgemを作る

Last updated at Posted at 2017-08-27
1 / 21

コンセプト


pictPairwiser(2017/12/1でサービス終了) を使って組み合わせテストのパターンを作るのは楽になった

参考


が、


rspecで使うためには加工が必要。


No. first_name last_name
1 "aaa" "bbb"
2 "あああ" ""
: : :

みたいなやつを


 where(:id, :name, :comment) do
          [
            [2, "てすと", "コメント"],
            [5, "test", "comment"],
            [6, "a" * 32, "test"],
            [7, "テスト", "test"],
          ]
        end

のようにイイ感じに整形しないといけない。


(yamlのパーサーを書けばよかったのではないか。)


gemにしました!!!


アルゴリズムの紹介


高校数学をちょっと思い出す。

異なる n 個のものから r 個を選ぶ場合の、組み合わせの数

nCr = \frac{nPr}{r!} = \frac{n!}{r!(n-r)!}

便利メソッド1

combination

[1,2,3,4].combination(2) do |first, second|
  p "(#{first, #{second}})"
end

=> (1,2) (1,3) (1,4) (2,3) (2,4) (3,4)

これで、2引数のarrayを組み合わせるのが実現できる。

{
  :first=>[1, 1, 1, 2, 2, 2, 3, 3, 3], 
  :second=>["a", "b", "c", "a", "b", "c", "a", "b", "c"]
}

テーブルにすると、こんな感じ。

key v1 v2 v3 ・・・
first 1 1 1 ・・・
second a b c ・・・

目指すのはこの形

No. first_name last_name
1 "aaa" "bbb"
2 "あああ" ""
: : :

:thinking: 行列を入れ替えたい。。。


便利メソッド2

transpose

values =  { :first=>[1, 1, 1, 2, 2, 2, 3, 3, 3], 
            :second=>["a", "b", "c", "a", "b", "c", "a", "b", "c"]}
values.transpose

=> [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"], [3, "b"], [3, "c"]]

以上をベースに、重複したパターンの削除、テーブル作成時に欠損するデータの補完処理を足した。


これから作るところ


  • 網羅する水準の指定ができるようにしたい。
  • 組み合わせを作るアルゴリズムを高速に。
  • 禁則処理
  • 出力された組み合わせをファイル出力 (0.1.3)

作ってから知ったgem。pairwise

4
3
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
4
3