chai-fs
Chai アサーションの プラグイン。Node.js のファイルシステム API 用です。path
と同期的な fs
を使用して、ファイルとディレクトリをアサートします。
すべてのアサーションは、expect
、should
、assert
の各スタイルで利用でき、オプションのメッセージパラメータをサポートしています。
フォーク
peerDependencies の問題のためフォークされました。
使用法
サーバーサイド
npm からインストール
$ npm install chai-fs
chai に chai-fs モジュールを使用させる
var chai = require('chai');
chai.use(require('chai-fs'));
ブラウザサイド
ファイルシステムはありません。
アサーション
basename()
path.basename(path)
の戻り値をアサートします
expect(path).to.have.basename(name, ?msg);
expect(path).to.not.have.basename(name, ?msg);
path.should.have.basename(name, ?msg);
path.should.not.have.basename(name, ?msg);
assert.basename(path, name, ?msg);
assert.notBasename(path, name, ?msg);
dirname()
path.dirname(path)
の戻り値をアサートします
expect(path).to.have.dirname(name, ?msg);
expect(path).to.not.have.dirname(name, ?msg);
path.should.have.dirname(name, ?msg);
path.should.not.have.dirname(name, ?msg);
assert.dirname(path, name, ?msg);
assert.notDirname(path, name, ?msg);
extname()
path.extname(path)
の戻り値をアサートします
expect(path).to.have.extname(name, ?msg);
expect(path).to.not.have.extname(name, ?msg);
path.should.have.extname(name, ?msg);
path.should.not.have.extname(name, ?msg);
assert.extname(path, name, ?msg);
assert.notExtname(path, name, ?msg);
path()
パスが存在することをアサートします。
fs.existsSync()
を使用します。
expect(path).to.be.a.path(?msg);
expect(path).to.not.be.a.path(?msg);
path.should.be.a.path(?msg);
path.should.not.be.a.path(?msg);
assert.pathExists(path, ?msg);
assert.notPathExists(path, ?msg);
Chai の exist
チェーンを使用した方が良かったのですが、否定とメッセージパラメータに問題があります。ですので、使用しないでください。
directory()
パスが存在し、ディレクトリであることをアサートします。
fs.statSync().isDirectory()
を使用します。
expect(path).to.be.a.directory(?msg);
expect(path).to.not.be.a.directory(?msg);
path.should.be.a.directory(?msg);
path.should.not.be.a.directory(?msg);
assert.isDirectory(path, ?msg);
assert.notIsDirectory(path, ?msg);
directory().and.empty
パスが存在し、ディレクトリであり、項目がゼロであることをアサートします。
expect(path).to.be.a.directory(?msg).and.empty;
expect(path).to.be.a.directory(?msg).and.not.empty;
path.should.be.a.directory(?msg).and.empty;
path.should.be.a.directory(?msg).and.not.empty;
assert.isEmptyDirectory(path, ?msg);
assert.notIsEmptyDirectory(path, ?msg);
directory()
の後にチェーンしますfs.readdirSync().length === 0
を使用します。expect/should
を使用してこれを否定するには、通常のdirectory()
の後に.not
の否定をチェーンします。
file()
パスが存在し、ファイルであることをアサートします。
fs.statSync().isFile()
を使用します。
expect(path).to.be.a.file(?msg);
expect(path).to.not.be.a.file(?msg);
path.should.be.a.file(?msg);
path.should.not.be.a.file(?msg);
assert.isFile(path, ?msg);
assert.notIsFile(path, ?msg);
file().and.empty
パスが存在し、ファイルであり、サイズがゼロであることをアサートします。
expect(path).to.be.a.file(?msg).and.empty;
expect(path).to.be.a.file(?msg).and.not.empty;
path.should.be.a.file(?msg).and.empty;
path.should.be.a.file(?msg).and.not.empty;
assert.isEmptyFile(path, ?msg);
assert.notIsEmptyFile(path, ?msg);
file()
の後にチェーンしますfs.statSync().size === 0
を使用します。expect/should
を使用してこれを否定するには、通常のfile()
の後に.not
の否定をチェーンします。
file().with.json
パスが存在し、ファイルであり、JSON パース可能なテキストを含んでいることをアサートします。
expect(path).to.be.a.file(?msg).with.json;
expect(path).to.be.a.file(?msg).with.not.json;
path.should.be.a.file(?msg).with.json;
path.should.be.a.file(?msg).with.not.json;
assert.jsonFile(path, ?msg);
assert.notJsonFile(path, ?msg);
file()
の後にチェーンしますexpect/should
を使用してこれを否定するには、通常のfile()
の後に.not
の否定をチェーンします。with
チェーンは単なる構文糖衣です。
file().using.json.schema(obj)
パスが存在し、ファイルであり、指定された JSON スキーマに準拠した JSON パース可能なテキストを含んでいることをアサートします。
expect(path).to.be.a.file(?msg).with.json.using.schema(obj);
expect(path).to.be.a.file(?msg).with.json.not.using.schema(obj);
path.should.be.a.file(?msg).with.json.using.schema(obj);
path.should.be.a.file(?msg).with.json.not.using.schema(obj);
assert.jsonSchemaFile(path, schema,?msg);
assert.notJsonSchemaFile(path, schema, ?msg);
file().with.json
の後にチェーンします- schema パラメータは、有効な JSON-Schema v4 である必要があります。
chai.use()
で別途アクティベートされる chai-json-schema プラグインに依存します。expect/should
を使用してこれを否定するには、通常のjson
の後に.not
の否定をチェーンします。with
チェーンとusing
チェーンは単なる構文糖衣です。
content()
パスが存在し、ファイルであり、特定のコンテンツを持っていることをアサートします。
expect(path).to.have.content(data, ?msg);
expect(path).to.not.have.content(data, ?msg);
path.should.have.content(data, ?msg);
path.should.not.have.content(data, ?msg);
assert.fileContent(path, data, ?msg);
assert.notFileContent(path, data, ?msg);
- ファイルを utf8 テキストとして読み取ります(base64、バイナリ Buffer などをサポートするように更新できます)。
注:今後のバージョンでは、file() および directory() の背後にあるチェーンとしてサポートされる可能性があります
content.that.match(/xyz/)
パスが存在し、ファイルであり、正規表現に一致するコンテンツを持っていることをアサートします。
expect(path).to.have.content.that.match(/xyz/, ?msg);
expect(path).to.not.have.content.that.match(/xyz/, ?msg);
path.should.have.content.that.match(/xyz/, ?msg);
path.should.not.have.content.that.match(/xyz/, ?msg);
assert.fileContentMatch(path, /xyz/, ?msg);
assert.notFileContentMatch(path, /xyz/, ?msg);
- ファイルを utf8 テキストとして読み取ります。
計画中のアサーション
今後のアサーションのいくつかのアイデアが このドキュメントに保存されています。
履歴
- 0.1.0 - content.match 機能を追加しました(@legendary-mich に感謝します)
- 0.0.2 - プラグインリリース
- 0.0.1 - アルファリリース
貢献
貢献を歓迎します。コード、テスト、スタイルのパターンに従い、JSHint を正常に保ってください。すべてのプラットフォーム、または少なくとも Widows/Mac/Linux で動作することを確認してください。
ビルド & テスト
git チェックアウトで開発依存関係をインストールします
$ npm install
グローバルの grunt コマンドが必要です
$ npm install grunt-cli -g
ビルドしてテストを実行します
$ grunt
追加のコマンドについては、Gruntfile
を参照してください。
:wrench: テストジェネレーター
このプラグインは、仕様を DRY に保ちながら、アサーションのすべての側面に対してテストを生成する「アサーションプラグインテストジェネレーター」のプロトタイプを使用します。
このパターンでは、テストをスタイル宣言ツリーと 3 種類のテストシナリオのバリエーションのセットに分割します。次に、ジェネレーターは、すべてのシナリオのバリエーションをスタイルツリーデータと組み合わせて(「乗算」)、すべてのケースを適切に網羅します。
スタイルツリーは、アサーションを使用する方法を定義します。最初のレベルは、スタイル(expect/should と assert)です。次に、通常の使用と否定の両方を定義し、それらをスタイルごとに異なる呼び出しパターンに分割します。したがって、メッセージの有無、チェーンされたメソッドまたはプロパティなどとしてテストできます。
テストは、アサーションとテストの期待値を指定する方法です。
valid
- テストがパスすることが期待されます(ただし、否定は失敗します)invalid
- テストが失敗することが期待されます(ただし、否定はパスします)。error
- データが無効であるため(例:不正なデータ型、パラメータの欠落など)、否定された場合でも、常にテストが失敗することが期待されます。
report フィールドは、テストが失敗した場合にエラーメッセージを検証するために使用されます。アサーションデータオブジェクトを使用した簡単なテンプレート形式をサポートしています。
なぜ?
これは少し複雑で面倒に見えますが、すべてのアサーションに対して、詳細なテストを大量にすばやく追加できます。今のところ、機能強化に役立つようですので、後でこれを別の npm モジュールに抽出するかもしれません。
大量のケースバリエーションが生成されるため、コードまたはテスト設定で小さなエラーが発生すると、多くの失敗したアサーションを含むスイートが爆発する可能性があることに注意してください。どのテストが失敗しているかをよく見て、何が原因かを把握してください。
ライセンス
Copyright (c) 2013 Bart van der Schoor
MIT ライセンスに基づいてライセンスされています。