Motoko Devs,
I’m running into a bunch of places where our meticulously engineered responses are making writing tests harder because we run into an array and pattern matching becomes complicated.
Before: #ok(#Ok(Nat))
switch(approvalResponse, approvalResponse1, approvalResponse2, approvalResponse3, approvalResponse4){
case(#ok(#Ok(_)),#ok(#Ok(_)),#ok(#Ok(_)),#ok(#Ok(_)),#ok(#Ok(_))){};
case(_){
return assert(false);
};
};
After: we moved to batch by default and returns look like #ok([?#Ok(Nat)])
switch(approvalResponse, approvalResponse1, approvalResponse2, approvalResponse3, approvalResponse4){
case(#ok(val1),#ok(val2),#ok(val3),#ok(val4),#ok(val5)){
switch(val1[0], val2[0], val3[0], val4[0], val5[0]){
case(?#Ok(_), ?#Ok(_), ?#Ok(_), ?#Ok(_), ?#Ok(_)){
};
case(_){
return assert(false);
};
};
};
case(_){
return assert(false);
};
};
Is there a clever way to pattern match inside an array? I’m fine with a trap here since it is a test.
If not, can we get one? Something like:
switch(approvalResponse, approvalResponse1, approvalResponse2, approvalResponse3, approvalResponse4){
case(#ok([[0]?val; [1]?val2])){};
case(_){
return assert(false);
};
};