Skip to content

Commit f14037b

Browse files
committed
feat: Add new select statement compliance tests
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 76c6d5f commit f14037b

7 files changed

Lines changed: 98 additions & 2 deletions

File tree

builtin/builtin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export interface SelectCase<T> {
408408
channel: Channel<any>;
409409
value?: any; // Value to send for send cases
410410
// Optional handlers for when this case is selected
411-
onSelected?: (result: SelectResult<T>) => void | Promise<void>;
411+
onSelected?: (result: SelectResult<T>) => Promise<void>;
412412
}
413413

414414

@@ -423,7 +423,7 @@ export interface SelectCase<T> {
423423
export async function selectStatement<T>(
424424
cases: SelectCase<T>[],
425425
hasDefault: boolean = false
426-
): Promise<void> { // Changed return type to void as onSelected handles the case body
426+
): Promise<void> {
427427
if (cases.length === 0 && !hasDefault) {
428428
// Go spec: If there are no cases, the select statement blocks forever.
429429
// Emulate blocking forever with a promise that never resolves.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Received zero value with ok==false: 0
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
func main() {
4+
ch := make(chan int) // Unbuffered
5+
close(ch)
6+
7+
select {
8+
case val, ok := <-ch:
9+
if ok {
10+
println("Received value with ok==true:", val) // Should not be reached
11+
} else {
12+
println("Received zero value with ok==false:", val) // Should be reached
13+
}
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Generated file based on select_receive_on_closed_channel_no_default.go
2+
// Updated when compliance tests are re-run, DO NOT EDIT!
3+
4+
import * as goscript from "@go/builtin";
5+
6+
export async function main(): Promise<void> {
7+
let ch = goscript.makeChannel<number>(0, 0) // Unbuffered
8+
ch.close()
9+
10+
// Should not be reached
11+
12+
// Should be reached
13+
const _tempVar1: goscript.SelectCase<any>[] = []
14+
_tempVar1.push({
15+
id: 0,
16+
isSend: false,
17+
channel: ch,
18+
onSelected: async (result) => {
19+
const val = result.value
20+
const ok = result.ok
21+
if (ok) {
22+
console.log("Received value with ok==true:", val) // Should not be reached
23+
} else {
24+
console.log("Received zero value with ok==false:", val) // Should be reached
25+
}
26+
}
27+
}),
28+
29+
await goscript.selectStatement(_tempVar1, false)
30+
}
31+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Default case hit
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
func main() {
4+
ch := make(chan int, 1)
5+
ch <- 1 // Fill the buffer
6+
7+
select {
8+
case ch <- 2:
9+
println("Sent value") // Should not be reached
10+
default:
11+
println("Default case hit") // Should be reached
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated file based on select_send_on_full_buffered_channel_with_default.go
2+
// Updated when compliance tests are re-run, DO NOT EDIT!
3+
4+
import * as goscript from "@go/builtin";
5+
6+
export async function main(): Promise<void> {
7+
let ch = goscript.makeChannel<number>(1, 0)
8+
await ch.send(1)
9+
10+
// Should not be reached
11+
12+
// Should be reached
13+
const _tempVar1: goscript.SelectCase<any>[] = []
14+
_tempVar1.push({
15+
id: 0,
16+
isSend: true,
17+
channel: ch,
18+
value: 2,
19+
onSelected: async (result) => {
20+
console.log("Sent value")
21+
}
22+
}),
23+
24+
_tempVar1.push({
25+
id: -1,
26+
isSend: false,
27+
channel: null,
28+
onSelected: async (result) => {
29+
console.log("Default case hit")
30+
}
31+
}),
32+
33+
await goscript.selectStatement(_tempVar1, true)
34+
}
35+

0 commit comments

Comments
 (0)