ってことで、巷で話題の Exercise に挑戦してみました。もちろん Perl5 で。
うちは手元にある Macでやってみます。まだ Big Sur にあげておらず、Catalina です。
Exercism への登録は GitHub の ID と連携しました。
ecercism
コマンドはいつも通り、homebrew でインストールします。
$ brew install exercism
手元の環境では久々に brew するので色々と警告が出たりしましたが、本題と関係薄いので飛ばします。
exercism
コマンドが無事入りました。
この辺りは登録直後のページ右側にあります。モダンな感じのページですね。
早速、最初の課題に取り組みます・・・が、API を設定しなよ!って出てくるので書いてある通りに設定します。親切。
$ exercism download --exercise=hello-world --track=perl5 Error: Welcome to Exercism! To get started, you need to configure the tool with your API token. Find your token at https://exercism.io/my/settings Then run the configure command: exercism configure --token=YOUR_TOKEN
API を登録します。
$ exercism configure --token=YOUR_EXERCISM_TOKEN You have configured the Exercism command-line client: Config dir: /Users/sironekotoro/.config/exercism Token: (-t, --token) YOUR_EXERCISM_TOKEN Workspace: (-w, --workspace) /Users/sironekotoro/Exercism API Base URL: (-a, --api) https://api.exercism.io/v1
今度こそ最初の問題です。
$ exercism download --exercise=hello-world --track=perl5 Downloaded to /Users/sironekotoro/Exercism/perl5/hello-world
おなじみ Hello World
から。ダウンロードされたものを確認します。
- HelloWorld.pm
- README.md
- hello-world.t
ふむふむ。まずは README.md
から
$ cat README.md # Hello World The classical introductory exercise. Just say "Hello, World!". ["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is the traditional first program for beginning programming in a new language or environment. The objectives are simple: - Write a function that returns the string "Hello, World!". - Run the test suite and make sure that it succeeds. - Submit your solution and check it at the website. If everything goes well, you will be ready to fetch your first real exercise. ## Source This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise
次はテスト。おぉ、 Test2
使ってる。このテストに通るのを書けばいいんだな〜
$ cat hello-world.t #!/usr/bin/env perl use Test2::V0; use FindBin qw<$Bin>; use lib $Bin, "$Bin/local/lib/perl5"; # Find modules in the same dir as this file. use HelloWorld qw(hello); plan 2; # This is how many tests we expect to run. imported_ok qw<hello> or bail_out; # Run the 'is' sub from 'Test2::V0' with three arguments. is( hello(), # Run the 'hello' sub imported from the module. 'Hello, World!', # The expected result to compare with 'hello'. 'Say Hi!' # The test description. );
で、手を入れる必要があるのはこのファイル HelloWorld.pm
と。
おー、モジュールだ。
$ cat HelloWorld.pm # Declare package 'HelloWorld' package HelloWorld; use strict; use warnings; use Exporter qw<import>; our @EXPORT_OK = qw<hello>; sub hello { # Remove the comments and write some code here to pass the test suite. } 1;
って感じです。
この HelloWorld.pm
を書いたら、Perl ではおなじみの prove
コマンドでテストを実行します。
おおっとテスト失敗!
どんなコード書いてテスト失敗したかはご想像にお任せします・・・
$ prove hello-world.t hello-world.t .. 1/2 # Failed test 'Say Hi!' # at hello-world.t line 14. # +-----+----+---------------+ # | GOT | OP | CHECK | # +-----+----+---------------+ # | 1 | eq | Hello, World! | # +-----+----+---------------+ # Seeded srand with seed '20210108' from local date. hello-world.t .. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/2 subtests Test Summary Report ------------------- hello-world.t (Wstat: 256 Tests: 1 Failed: 0) Non-zero exit status: 1 Parse errors: Bad plan. You planned 2 tests but ran 1. Files=1, Tests=1, 1 wallclock secs ( 0.02 usr 0.01 sys + 0.11 cusr 0.04 csys = 0.18 CPU) Result: FAIL
改めて書き直し。今度は合格点をもらえました
$ prove hello-world.t hello-world.t .. ok All tests successful. Files=1, Tests=2, 0 wallclock secs ( 0.02 usr 0.01 sys + 0.11 cusr 0.04 csys = 0.18 CPU) Result: PASS
さて、課題提出です。
$ exercism submit HelloWorld.pm Your solution has been submitted successfully. You can complete the exercise and unlock the next core exercise at: https://exercism.io/my/solutions/fc5c6ec1b3d7471fbc037e65d970a417
ということで、最後に発行された URL に行くと・・・合格っぽい感じです。
解答を公開することもできるようなので置いておきます。いや、Hello, World! しただけですが・・・あ、多分 Exercism へのログインが必要です。
課題のページには次の問題へ!とあるので、暇な時に進めてみようと思います。
追記
この最初の課題をやるのに必要な知識は
Perlでのテスト
サブルーチンとその返り値
です。
テスト、特に Perl の Test2 については Gihyo さんに記事があります。
実は、Test2 に触れたの今回が初めてでした。
サブルーチンについては 1 月 23 日のPerl入学式でやる予定です。
やれるといいな。
時間が押さなければできるんじゃないだろうか、多分。
ということで、興味のある方の参加をお待ちしております!