Advertisement
happy-barney

p6 - test example for mj

Oct 6th, 2019
1,021
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. use Test;
  3.  
  4. multi sub is ($title, :$got, :$expected) {
  5.     is($got, $expected, $title);
  6. }
  7.  
  8. subtest <Array constructor preserves {} Hash if s followed by a comma>, {
  9.     my $array = [ { a => 1, b => 4 }, ];
  10.     #                               ^ --- notice comma here
  11.  
  12.     is <created array contains one element>,
  13.         got      => $array.elems,
  14.         expected => 1,
  15.     ;
  16.  
  17.     is <it creates instance of Array class>,
  18.         got      => WHAT($array),
  19.         expected => Array,
  20.     ;
  21.  
  22.     # ... and so on
  23.     #is WHAT($array[0]), Hash;
  24.     #is $array[0]<a>, 1;
  25.     #is $array[0]<b>, 4;
  26. }
  27.  
  28. subtest <Array constructor flattens {} Hash not followed by a comma into unsorted list of Pairs>, {
  29.     my $array = [ { a => 1, b => 4 } ];
  30.     #                               ^ --- notice missing comma here
  31.  
  32.     is <created array contains two elements - Pairs of original Hash>,
  33.         got      => $array.elems,
  34.         expected => 2,
  35.     ;
  36.  
  37.     is <it creates instance of Array class>,
  38.         got      => WHAT($array),
  39.         expected => Array,
  40.     ;
  41.  
  42.     # ... and so on
  43.     #is $array.elems, 2;
  44.     #is WHAT($array), Array;
  45.     #is WHAT($array[0]), Pair;
  46.     #is WHAT($array[1]), Pair;
  47.  
  48.     # Pairs order is not predictable
  49.     #is $array[0]<a> // $array[1]<a>, 1;
  50.     #is $array[0]<b> // $array[1]<b>, 4;
  51. }
  52.  
  53. done-testing;
Advertisement
RAW Paste Data Copied
Advertisement