SRFI - 64コード・カタ



私は毎朝約30分のコードカタをする習慣を身につけた.テスト駆動開発(TDD)アプローチに続いてこれらのカタを練習します.
GuileディストリビューションはSRFI - 64モジュールを含みます.この記事では、どのようにそれを使用する方法とコードの練習を練習するためにそれを設定する方法を説明します.

基本的な使い方


srfi - 64モジュールにはデフォルトの設定があります.ここでどのように見えますか.
以下の1つのテストだけでテストスイートを作成します.
(use-modules (srfi srfi-64))
(test-begin "demo")
(test "Hookup" #f #t)
(test-end "demo")
プログラムの説明:
  • 行1 :名前付きモジュールをロードします.
  • 行2 :テストグループのデモを開始します.
  • 行3 :「hokup」と名付けられたテストを定義し、プロシージャテストで1番目のパラメータがテストによって期待される結果(ここでは、1)、2番目のパラメータがテストされた式(ここでは、△t)です.
  • 行4:demoテストグループの終わりを示します.
  • これは比較的簡単ですが、いくつかのフリルがあります.
    これは/tmp/testで保存した場合、このスイートの実行結果です.scmファイルを実行し、実行します.
    $ guile --no-auto-compile test.scm 
    %%%% Starting test demo (Writing full log to "demo.log")
    /tmp/test.scm:3: FAIL Hookup
    # of unexpected failures 1
    
    コンソールは、結果の要約だけを示します:3行目は「hoooup」テストから予想外の失敗を示します.それは非常に簡単ですが、それはプログラムの状態の簡単なアイデアを与える.
    完全な詳細は/tmp/demoにあります.ログファイル:
    cat demo.log
    %%%% Starting test demo
    Group begin: demo
    Test begin:
      test-name: "Hookup"
      source-file: "/tmp/test.scm".
      source-line: 3
      source-form: (test-equal "Hookup" #f #t)
    Test end:
      result-kind: fail
      actual-value: #t
      expected-value: #f
    Group end: demo
    # of unexpected failures 1
    
    このレポートには、私がたくさん使っている情報が2つあります.
    このデフォルトの設定では、使用する準備ができているが、2つの場所に情報を提供するという欠点があります.コンソール(リストとその状態の一覧)とファイル(テスト結果)で.実用的ではない.
    幸いにも、テストフレームワークは簡単に設定可能です.次の記事ではコードカタに専用の設定をします.
    コードの設定
    ここでのアイデアは、SRFI - 64モジュールをコンソールで直接必要とするすべての情報を表示するように設定することです(そして、ファイルで報告する必要はありません).一目で見たい
  • 実行されるテストのリスト
  • これらのテストの各々の状態;
  • 失敗したテストの
  • は、期待値と取得した値を表示します.
  • SRFI - 64参照によると、テストスイートの統計を維持するオブジェクト(テストの総数、成功した数、失敗した、渡されます…)テストランナーです.
    新しいテストランナーといくつかの例は、Webからgleanedを記述するセクションのおかげで、あなたはあなたに合うテストランナーを作成することができるはずです!
    これは私が自分で作ったものです(Mathieu LirzinとアレックスSassmannshausenの仕事に基づいています).
    ;;;; kata-driver.scm - Guile test driver for code kata practice
    
    (define script-version "2020-10-04") ;UTC
    
    ;;; Copyright © 2015, 2016 Mathieu Lirzin <[email protected]>
    ;;; Copyright © 2019 Alex Sassmannshausen <[email protected]>
    ;;; Copyright © 2019 Jérémy Korwin-Zmijowski <[email protected]>
    ;;;
    ;;; This program is free software; you can redistribute it and/or modify it
    ;;; under the terms of the GNU General Public License as published by
    ;;; the Free Software Foundation; either version 3 of the License, or (at
    ;;; your option) any later version.
    ;;;
    ;;; This program is distributed in the hope that it will be useful, but
    ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
    ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ;;; GNU General Public License for more details.
    ;;;
    ;;; You should have received a copy of the GNU General Public License
    ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    ;;;; Commentary:
    ;;;
    ;;; This script provides a Guile test driver using the SRFI-64 Scheme API for
    ;;; test suites.  SRFI-64 is distributed with Guile since version 2.0.9.
    ;;;
    ;;; This script is a very cleaned up version of the Alex Sassmannshausen 's 
    ;;; version. The changes make it suitable for use in a code kata practice.
    ;;;
    ;;;; Code:
    
    (define-module (runner-kata)
      #:export (test-runner-kata))
    
    (use-modules (srfi srfi-64)
                 (ice-9 pretty-print)
                 (srfi srfi-26))
    
    (define* (test-display field value  #:optional (port (current-output-port))
                           #:key pretty?)
      "Display 'FIELD: VALUE\n' on PORT."
      (if pretty?
          (begin
            (format port "~A:~%" field)
            (pretty-print value port #:per-line-prefix "+ "))
          (format port "~A: ~S~%" field value)))
    
    (define* (result->string symbol)
      "Return SYMBOL as an upper case string.  Use colors when COLORIZE is #t."
      (let ((result (string-upcase (symbol->string symbol))))
        (string-append (case symbol
                         ((pass)       "")  ;green
                         ((xfail)      "")  ;light green
                         ((skip)       "")  ;blue
                         ((fail xpass) "")  ;red
                         ((error)      "")) ;magenta
                       result
                       "")))
    
    (define* (test-runner-kata)
    
      (define (test-on-test-end-kata runner)
        (let* ((results (test-result-alist runner))
               (result? (cut assq <> results))
               (result  (cut assq-ref results <>)))
          (if (equal? 'fail (result 'result-kind))
          (begin
            (newline)
            (format #t "~a ~A~%"
                (result->string (result 'result-kind))
                (result 'test-name))
            (when (result? 'expected-value)
                  (test-display "expected-value" (result 'expected-value)))
            (when (result? 'expected-error)
                  (test-display "expected-error" (result 'expected-error) #:pretty? #t))
            (when (result? 'actual-value)
                  (test-display "actual-value" (result 'actual-value)))
            (newline))
          (begin
            (format #t "~a ~A~%"
                (result->string (result 'result-kind))
                (result 'test-name))))))
    
      (let ((runner (test-runner-null)))
        (test-runner-on-test-end! runner test-on-test-end-kata)
        runner))
    
    コードのカタのプロジェクトテンプレートを含むGitリポジトリを作成しました.ちょうどREADME命令とコードに従ってください!
    あなたの端末からのフィードバックの例です.shスクリプトの実行
    FAIL Hookup
    expected-value: #f
    actual-value: #t
    
    これ以上の問題を見つけるためにレポート(これはもはや生成されません)の内容を見てする必要はありません.それで、私は保存して、テスト結果に従って行動するたびに、私は専用のEmacsバッファでこのスクリプトを実行することができます.
    今ではあなた次第です!
    コードのカタの運動を選択し、約30分、それを共有し、コードへのリンクを共有し、以下のコメントでテスト!
    次に、次のセッションのために私の2セントを与える!

    Thank you so much for reading this article!

    Don't hesitate to give me your opinion or ask a question !
    To do so, please leave a comment below or contact me.

    And most importantly, share the blog and tell your friends it's the best blog in the history of Free Software! No shit!