Advertisement
sglienke

Spring4D Flatmap

Aug 20th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.83 KB | None | 0 0
  1. program FlatMapTests;
  2.  
  3. uses
  4.   TestFramework,
  5.   TestInsight.DUnit,
  6.   Spring.Collections,
  7.   Spring.Collections.Extensions;
  8.  
  9. type
  10.   TDeveloper = class
  11.   private
  12.     fName: string;
  13.     fLanguages: IList<string>;
  14.     function GetLanguages: IEnumerable<string>;
  15.   public
  16.     constructor Create(const name: string);
  17.     procedure Add(const language: string);
  18.     property Languages: IEnumerable<string> read GetLanguages;
  19.   end;
  20.  
  21.   TFlatMapTest = class(TTestCase)
  22.   published
  23.     procedure FlatMap;
  24.   end;
  25.  
  26. procedure TFlatMapTest.FlatMap;
  27. var
  28.   team: IList<TDeveloper>;
  29.   polyglot, busy: TDeveloper;
  30.   teamLanguages: IEnumerable<string>;
  31. begin
  32.   team := TCollections.CreateList<TDeveloper>(True);
  33.   polyglot := TDeveloper.Create('esoteric');
  34.   polyglot.Add('clojure');
  35.   polyglot.Add('scala');
  36.   polyglot.Add('groovy');
  37.   polyglot.Add('go');
  38.  
  39.   busy := TDeveloper.Create('pragmatic');
  40.   busy.Add('java');
  41.   busy.Add('javascript');
  42.  
  43.   team.Add(polyglot);
  44.   team.Add(busy);
  45.  
  46.   teamLanguages :=
  47.     TSelectManyIterator<TDeveloper,string>.Create(team,
  48.       function(d: TDeveloper): IEnumerable<string>
  49.       begin
  50.         Result := d.Languages
  51.       end);
  52.  
  53.   Check(polyglot.Languages.All(function(const s: string): Boolean begin Result := teamLanguages.Contains(s) end));
  54.   Check(busy.Languages.All(function(const s: string): Boolean begin Result := teamLanguages.Contains(s) end));
  55.   teamLanguages := nil;
  56. end;
  57.  
  58. constructor TDeveloper.Create(const name: string);
  59. begin
  60.   inherited Create;
  61.   fName := name;
  62.   fLanguages := TCollections.CreateList<string>;
  63. end;
  64.  
  65. procedure TDeveloper.Add(const language: string);
  66. begin
  67.   fLanguages.Add(language);
  68. end;
  69.  
  70. function TDeveloper.GetLanguages: IEnumerable<string>;
  71. begin
  72.   Result := fLanguages;
  73. end;
  74.  
  75. begin
  76.   RegisterTest(TFlatMapTest.Suite);
  77.   RunRegisteredTests;
  78. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement