Skip to content

Commit

Permalink
add wit-parser tests for streams, futures, and error-contexts
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Nov 18, 2024
1 parent 285fb91 commit a9decd6
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 0 deletions.
39 changes: 39 additions & 0 deletions crates/wit-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,4 +1143,43 @@ mod test {
assert_eq!(discriminant_type(num_cases), Int::U32);
}
}

#[test]
fn test_find_futures_and_streams() {
let mut resolve = Resolve::default();
let t0 = resolve.types.alloc(TypeDef {
name: None,
kind: TypeDefKind::Future(Some(Type::U32)),
owner: TypeOwner::None,
docs: Docs::default(),
stability: Stability::Unknown,
});
let t1 = resolve.types.alloc(TypeDef {
name: None,
kind: TypeDefKind::Future(Some(Type::Id(t0))),
owner: TypeOwner::None,
docs: Docs::default(),
stability: Stability::Unknown,
});
let t2 = resolve.types.alloc(TypeDef {
name: None,
kind: TypeDefKind::Stream(Type::U32),
owner: TypeOwner::None,
docs: Docs::default(),
stability: Stability::Unknown,
});
let found = Function {
name: "foo".into(),
kind: FunctionKind::Freestanding,
params: vec![("p1".into(), Type::Id(t1)), ("p2".into(), Type::U32)],
results: Results::Anon(Type::Id(t2)),
docs: Docs::default(),
stability: Stability::Unknown,
}
.find_futures_and_streams(&resolve);
assert_eq!(3, found.len());
assert_eq!(t0, found[0]);
assert_eq!(t1, found[1]);
assert_eq!(t2, found[2]);
}
}
7 changes: 7 additions & 0 deletions crates/wit-parser/tests/ui/error-context.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo:error-contexts;

interface error-contexts {
type t1 = error-context;

foo: func(x: error-context, y: t1) -> result<_, error-context>;
}
66 changes: 66 additions & 0 deletions crates/wit-parser/tests/ui/error-context.wit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"worlds": [],
"interfaces": [
{
"name": "error-contexts",
"types": {
"t1": 0
},
"functions": {
"foo": {
"name": "foo",
"kind": "freestanding",
"params": [
{
"name": "x",
"type": 1
},
{
"name": "y",
"type": 0
}
],
"results": [
{
"type": 2
}
]
}
},
"package": 0
}
],
"types": [
{
"name": "t1",
"kind": "errorcontext",
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": "errorcontext",
"owner": null
},
{
"name": null,
"kind": {
"result": {
"ok": null,
"err": 1
}
},
"owner": null
}
],
"packages": [
{
"name": "foo:error-contexts",
"interfaces": {
"error-contexts": 0
},
"worlds": {}
}
]
}
14 changes: 14 additions & 0 deletions crates/wit-parser/tests/ui/streams-and-futures.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package foo:streams-and-futures;

interface streams-and-futures {
type t1 = stream<u8>;
type t2 = stream<stream<string>>;
type t3 = future;
type t4 = future<list<u8>>;
type t5 = option<stream<future>>;
resource r1;
type t6 = stream<r1>;
type t7 = future<result<r1>>;

foo: func(x: stream<u32>, y: t6) -> future<result<list<string>, string>>;
}
199 changes: 199 additions & 0 deletions crates/wit-parser/tests/ui/streams-and-futures.wit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
{
"worlds": [],
"interfaces": [
{
"name": "streams-and-futures",
"types": {
"t1": 0,
"t2": 2,
"t3": 3,
"t4": 5,
"t5": 8,
"r1": 9,
"t6": 11,
"t7": 13
},
"functions": {
"foo": {
"name": "foo",
"kind": "freestanding",
"params": [
{
"name": "x",
"type": 14
},
{
"name": "y",
"type": 11
}
],
"results": [
{
"type": 17
}
]
}
},
"package": 0
}
],
"types": [
{
"name": "t1",
"kind": {
"stream": "u8"
},
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": {
"stream": "string"
},
"owner": null
},
{
"name": "t2",
"kind": {
"stream": 1
},
"owner": {
"interface": 0
}
},
{
"name": "t3",
"kind": {
"future": null
},
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": {
"list": "u8"
},
"owner": null
},
{
"name": "t4",
"kind": {
"future": 4
},
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": {
"future": null
},
"owner": null
},
{
"name": null,
"kind": {
"stream": 6
},
"owner": null
},
{
"name": "t5",
"kind": {
"option": 7
},
"owner": {
"interface": 0
}
},
{
"name": "r1",
"kind": "resource",
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": {
"handle": {
"own": 9
}
},
"owner": null
},
{
"name": "t6",
"kind": {
"stream": 10
},
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": {
"result": {
"ok": 10,
"err": null
}
},
"owner": null
},
{
"name": "t7",
"kind": {
"future": 12
},
"owner": {
"interface": 0
}
},
{
"name": null,
"kind": {
"stream": "u32"
},
"owner": null
},
{
"name": null,
"kind": {
"list": "string"
},
"owner": null
},
{
"name": null,
"kind": {
"result": {
"ok": 15,
"err": "string"
}
},
"owner": null
},
{
"name": null,
"kind": {
"future": 16
},
"owner": null
}
],
"packages": [
{
"name": "foo:streams-and-futures",
"interfaces": {
"streams-and-futures": 0
},
"worlds": {}
}
]
}

0 comments on commit a9decd6

Please sign in to comment.