Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: assert messages #6096

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/03-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ log("UTC: {t1.utc.toIso())}"); // output: 2023-02-09T06:21:03.000Z
> ```TS
> log("Hello {name}");
> assert(x > 0);
> assert(x > 0, "x should be positive");
> ```

[`▲ top`][top]
Expand Down
7 changes: 6 additions & 1 deletion examples/tests/error/inflight_stacktraces.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ test "throw from closure" {
throw "ouch";
};
closure();
}
}

test "assert with message" {
let x = false;
assert(x, "x is false");
}
4 changes: 2 additions & 2 deletions libs/wingc/src/lsp/snapshots/completions/empty.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ source: libs/wingc/src/lsp/completions.rs
sortText: bb|this
- label: assert
kind: 3
detail: "(condition: bool): void"
detail: "(condition: bool, message: str?): void"
documentation:
kind: markdown
value: "```wing\nassert: (condition: bool): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert"
value: "```wing\nassert: (condition: bool, message: str?): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert\n- `message` — `str?` — The message to log if the condition is false"
sortText: cc|assert
insertText: assert($1)
insertTextFormat: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ source: libs/wingc/src/lsp/completions.rs
sortText: bb|x
- label: assert
kind: 3
detail: "(condition: bool): void"
detail: "(condition: bool, message: str?): void"
documentation:
kind: markdown
value: "```wing\nassert: (condition: bool): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert"
value: "```wing\nassert: (condition: bool, message: str?): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert\n- `message` — `str?` — The message to log if the condition is false"
sortText: cc|assert
insertText: assert($1)
insertTextFormat: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: libs/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nassert: (condition: bool): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert"
value: "```wing\nassert: (condition: bool, message: str?): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert\n- `message` — `str?` — The message to log if the condition is false"
range:
start:
line: 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: libs/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nassert: (condition: bool): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert"
value: "```wing\nassert: (condition: bool, message: str?): void\n```\n---\nAsserts that a condition is true\n### Parameters\n- `condition` — `bool` — The condition to assert\n- `message` — `str?` — The message to log if the condition is false"
range:
start:
line: 1
Expand Down
21 changes: 15 additions & 6 deletions libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,7 @@ impl<'a> TypeChecker<'a> {
}

pub fn add_builtins(&mut self, scope: &mut Scope) {
let optional_string = self.types.make_option(self.types.string());
self.add_builtin(
UtilityFunctions::Log.to_string().as_str(),
Type::Function(FunctionSignature {
Expand All @@ -1974,12 +1975,20 @@ impl<'a> TypeChecker<'a> {
UtilityFunctions::Assert.to_string().as_str(),
Type::Function(FunctionSignature {
this_type: None,
parameters: vec![FunctionParameter {
name: "condition".into(),
typeref: self.types.bool(),
docs: Docs::with_summary("The condition to assert"),
variadic: false,
}],
parameters: vec![
FunctionParameter {
name: "condition".into(),
typeref: self.types.bool(),
docs: Docs::with_summary("The condition to assert"),
variadic: false,
},
FunctionParameter {
name: "message".into(),
typeref: optional_string,
docs: Docs::with_summary("The message to log if the condition is false"),
variadic: false,
},
],
return_type: self.types.void(),
phase: Phase::Independent,
js_override: Some("$helpers.assert($args$, \"$args_text$\")".to_string()),
Expand Down
19 changes: 14 additions & 5 deletions tools/hangar/__snapshots__/error.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Duration <DURATION>"
`;

exports[`inflight_stacktraces.test.w 1`] = `
"fail ┌ inflight_stacktraces.test.wsim » root/env0/test:assert
"fail ┌ inflight_stacktraces.test.wsim » root/env0/test:assert
│ Error: assertion failed: false
│ --> ../../../examples/tests/error/inflight_stacktraces.test.w:7:3
│ | let bucket = new cloud.Bucket();
Expand All @@ -27,7 +27,7 @@ exports[`inflight_stacktraces.test.w 1`] = `
│ 7 | assert(false);
│ | ^
└ at <ABSOLUTE>/inflight_stacktraces.test.w:7:3
fail ┌ inflight_stacktraces.test.wsim » root/env1/test:expect.equal
fail ┌ inflight_stacktraces.test.wsim » root/env1/test:expect.equal
│ Error: AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
│ 1 !== 2
Expand All @@ -39,7 +39,7 @@ fail ┌ inflight_stacktraces.test.wsim » root/env1/test:expect.equal
│ 11 | expect.equal(1,2 );
│ | ^
└ at <ABSOLUTE>/inflight_stacktraces.test.w:11:3
fail ┌ inflight_stacktraces.test.wsim » root/env2/test:bucket failed get
fail ┌ inflight_stacktraces.test.wsim » root/env2/test:bucket failed get
│ Error: Object does not exist (key=doesn't exist): Error: ENOENT: no such file or directory, open '../../../examples/tests/error/target/test/inflight_stacktraces.test.wsim/.state/<STATE_FILE>'
│ --> ../../../examples/tests/error/inflight_stacktraces.test.w:15:3
│ | }
Expand All @@ -48,7 +48,7 @@ fail ┌ inflight_stacktraces.test.wsim » root/env2/test:bucket failed get
│ 15 | bucket.get(\\"doesn't exist\\");
│ | ^
└ at <ABSOLUTE>/inflight_stacktraces.test.w:15:3
fail ┌ inflight_stacktraces.test.wsim » root/env3/test:throw from closure
fail ┌ inflight_stacktraces.test.wsim » root/env3/test:throw from closure
│ Error: ouch
│ --> ../../../examples/tests/error/inflight_stacktraces.test.w:20:5
│ |
Expand All @@ -58,9 +58,18 @@ fail ┌ inflight_stacktraces.test.wsim » root/env3/test:throw from closure
│ | ^
│ at closure <ABSOLUTE>/inflight_stacktraces.test.w:20:5
└ at <ABSOLUTE>/inflight_stacktraces.test.w:22:3
fail ┌ inflight_stacktraces.test.wsim » root/env4/test:assert with message
│ Error: assertion failed: x is false
│ --> ../../../examples/tests/error/inflight_stacktraces.test.w:27:3
│ |
│ | test \\"assert with message\\" {
│ | let x = false;
│ 27 | assert(x, \\"x is false\\");
│ | ^
└ at <ABSOLUTE>/inflight_stacktraces.test.w:27:3


Tests 4 failed (4)
Tests 5 failed (5)
Test Files 1 failed (1)
Duration <DURATION>"
`;
Expand Down
4 changes: 2 additions & 2 deletions tools/hangar/__snapshots__/invalid.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1816,11 +1816,11 @@ error: Variable is not reassignable
| ^^^^^^ Variable is not reassignable


error: Expected type to be \\"(condition: bool): void\\", but got \\"str\\" instead
error: Expected type to be \\"(condition: bool, message: str?): void\\", but got \\"str\\" instead
--> ../../../examples/tests/invalid/global_symbols.test.w:12:10
|
12 | assert = \\"there\\";
| ^^^^^^^ Expected type to be \\"(condition: bool): void\\", but got \\"str\\" instead
| ^^^^^^^ Expected type to be \\"(condition: bool, message: str?): void\\", but got \\"str\\" instead


error: Variable is not reassignable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
```log
[symbol environment at debug_env.test.w:7:5]
level 0: { this => A }
level 1: { A => A [type], assert => (condition: bool): void, cloud => cloud [namespace], lift => inflight (preflightObject: Resource, qualifications: Array<str>): void, log => (message: str): void, nodeof => preflight (construct: IConstruct): Node, std => std [namespace], this => Construct, unsafeCast => (value: any): any }
level 1: { A => A [type], assert => (condition: bool, message: str?): void, cloud => cloud [namespace], lift => inflight (preflightObject: Resource, qualifications: Array<str>): void, log => (message: str): void, nodeof => preflight (construct: IConstruct): Node, std => std [namespace], this => Construct, unsafeCast => (value: any): any }
```

## stdout.log
Expand Down
Loading