winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
5.03k stars 196 forks source link

`expect.stringContains(haystack, needle)` #5710

Open Chriscbr opened 8 months ago

Chriscbr commented 8 months ago

Use Case

An API that display appropriate error messages in CI when the check fails.

Today if you use:

let haystack = "hello, world!";
let needle = "wing";
assert(haystack.contains(needle));

The program will error, but the error message won't show you either of the strings, making it harder to debug why it failed:

Error: assertion failed: haystack.contains(needle)
  --> wing/main.w:3:1
  | let haystack = "hello, world!";
  | let needle = "wing";
3 | assert(haystack.contains(needle));
  | ^
at /app/wing/main.w:3:1

This can be addressed by adding log statements, but a cleaner solution would be to provide a dedicated expect API that shows the concrete values in the error message:

bring expect;
let haystack = "hello, world!";
let needle = "wing";
assert(expect.stringContains(haystack, needle));
Error: Expected "needle" string to be contained within "haystack":

needle: "wing"
haystack: "hello, world!"

Proposed Solution

No response

Implementation Notes

No response

Component

SDK

Community Notes

Chriscbr commented 8 months ago

I wonder if there's a simpler way to support more matchers since this design would imply some need for expect.arrayContains(), expect.setContains(), expect.mapContains(), etc. so it's worth considering if there are alternative designs.