Pattern-engineering implementation and expansion (design pattern C # edition) iterator pattern iterator -- self-test reference answer

Source: Internet
Author: User
Transferred from: Model-engineering implementation and expansion (Design Mode C)

Http://www.cnblogs.com/callwangxiang/

 

 

1,Traverse the current folder to list all recent3Full path of files created or modified by Tianji

 

Reference answer

Using System;
Using System. diagnostics;
Using System. LINQ;
Using System. IO;
Using Microsoft. visualstudio. testtools. unittesting;
Namespace Marvellousworks. practicalpattern. iterator. Tests. Exercise
{
[Testclass]
Public   Class Iteratefilefixture
{
Const   String Path =   @" . " ;

[Testmethod]
Public   Void Testlistmodifiedandnewcreatedfiles ()
{
Func < Datetime, Bool > Lessthanthreedayshandler = X => (Datetime. Now - X). Days <=   3 ;
VaR files =
New Directoryinfo (PATH). getfiles (). Where (x =>
Lessthanthreedayshandler (X. lastwritetime) |
lessthanthreedayshandler (X. creationtime)
);

//There will be some newly compiled files in the current unit test directory.
Assert. istrue (files. Count ()> 0);

// Output matching information
Foreach (Var file In Files)
{
Trace. Write (file. fullname );
If (Lessthanthreedayshandler (file. lastwritetime ))
Trace. Write ( " \ TM " );
If (Lessthanthreedayshandler (file. creationtime ))
Trace. Write ( " \ TC " );
Trace. writeline ( "" );
}
}
}
}

 

------ Test started: Assembly: iterator . Tests . DLL ------

E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . DLL C
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . PDB C
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . Tests . DLL M C
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . Tests . PDB M C

1Passed, 0Failed, 0Skipped,Took0.48Seconds(Mstest10.0).

 

 

2,UseAttributeTo ensure that the iterator can traverse related content based on different themes.

 

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using Microsoft. visualstudio. testtools. unittesting;
Namespace Marvellousworks. practicalpattern. iterator. Tests. Exercise
{
[Testclass]
Public   Class Attributsubjectediteratorfixture
{
[Serializable]
[Attributeusage (attributetargets. Class, allowmultiple =   True )]
Class Iterableattribute: attribute {}

ClassSubjectaattribute: iterableattribute {}
ClassSubjectbattribute: iterableattribute {}

Static   Class Subjectediterator
{
///   <Summary>
/// Traverse by specified topic-that is, attribute
///   </Summary>
///   <Typeparam name = "telement"> Set element type </Typeparam>
///   <Typeparam name = "tattribute"> Traverse the attribute type </Typeparam>
///   <Param name = "data"> </param>
///   <Returns> </returns>
Public   Static Ienumerable < Telement > Getenumerator < Telement, tattribute > (Ienumerable < Telement > Data)
Where Tattribute: iterableattribute
{
If (Data =   Null ) Return   Null ;
If (Data. Count () =   0 ) Return Data;

Return Data. Where (x =>
X. GetType (). getcustomattributes ( Typeof (Tattribute ), False ). Length >   0 );
}
}

[Subjecta]
ClassA {}

[Subjectb]
ClassB {}

[Subjecta]
[Subjectb]
ClassAB {}

ClassO {}

[Testmethod]
Public   Void Testiteratebysubject ()
{
VaR data =   New List < Object > { New A (), New B (), New AB (), New A (), New O {}, New O {}, New AB ()};

VaR subjecta = Subjectediterator. getenumerator < Object , Subjectaattribute > (Data );
Assert. areequal < Int > ( 4 , Subjecta. Count ()); // Two A and two AB

VaR subjectb = Subjectediterator. getenumerator < Object , Subjectbattribute > (Data );
Assert. areequal < Int > ( 3 , Subjectb. Count ()); // One B and two AB
}
}
}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.