最初のテストを書くときに、怠zyなプログラマーの伝統的な疑問が生じました:エネルギーを節約する方法と、誰が神経を読み、私が得たいものを素早く明確に想像するか?
したがって、私はそのような方法を思いつきました:
例
dynamic rootTag = new XmlBuilder("Root", _nmsps); dynamic modelTag = new XmlBuilder("Model", _nmsps); dynamic elementTag = new XmlBuilder("ElementRef", _nmsps); dynamic attrTag = new XmlBuilder("ElementAttribute", _nmsps); XDocument doc = rootTag(id: Guid.Empty, model: modelTag(id: model.ObjectId, name: model.Name, e1: elementTag(id: er.ObjectId, name: elem1.Name, ea1: attrTag(id: attr1.ObjectId, name: attr1.Name), ea2: attrTag(id: attr2.ObjectId, name: attr2.Name)), e2: elementTag(id: er2.ObjectId, name: elem2.Name)));
XmlBuilderは、DynamicObjectを継承する自己記述型のクラスです。
ドキュメントを作成するには、メソッドとして呼び出され、引数をパラメーター名に基づいて属性に変換します-XmlBuilder以外の型の引数である場合-子要素(ここではパラメーター名は重要ではありません)-XmlBuilderである場合
さらに、各XmlBuilderは、結果のドキュメント内のタグを表します。
テストでは、単純なコンパレータを使用して、必要なものと発生したものを比較します。その結果、このテストは次のようになります。
// //... // , dynamic rootTag = new XmlBuilder("Root", _nmsps); dynamic modelTag = new XmlBuilder("Model", _nmsps); dynamic elementTag = new XmlBuilder("ElementRef", _nmsps); dynamic attrTag = new XmlBuilder("ElementAttribute", _nmsps); XDocument targetDocument = rootTag(id: Guid.Empty, model: modelTag(id: model.ObjectId, name: model.Name, e1: elementTag(id: er.ObjectId, name: elem1.Name, ea1: attrTag(id: attr1.ObjectId, name: attr1.Name), ea2: attrTag(id: attr2.ObjectId, name: attr2.Name)), e2: elementTag(id: er2.ObjectId, name: elem2.Name))); // var resultDocument = TestableMethod(); // var _comparer = new XDocComparer(); Assert.IsTrue(_comparer.Equals(targetDocument, resultDocument));
簡単にするために、不要な機能(宣言など)を犠牲にしなければなりませんでした。
さて、コンパレータは、IEqualityComparerであるという事実により、正確に一致しないものは表示されず、最終結果のみが表示されます。
それでも、XmlBuilderの実装はここにあり、XDocComparerの実装はここにあります。誰かが便利になれるなら、それを使用するようにお願いします。