Skip to content

Commit 80a57c7

Browse files
committed
cm: add tests for #284
1 parent fb0b3ac commit 80a57c7

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

cm/result_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cm
22

33
import (
4+
"fmt"
45
"runtime"
56
"testing"
67
"unsafe"
@@ -238,3 +239,98 @@ func TestIssue95BoolInt64(t *testing.T) {
238239
t.Errorf("*res.OK(): %v, expected %v", got, want)
239240
}
240241
}
242+
243+
func TestIssue284(t *testing.T) {
244+
// Using int8 instead of bool for shape works on Go and TinyGo.
245+
// See https://github.com/bytecodealliance/go-modules/issues/284
246+
type BoolS8Result Result[int8, bool, int8]
247+
248+
tests := []struct {
249+
isErr bool
250+
ok bool
251+
err int8
252+
}{
253+
{false, false, 0},
254+
{false, true, 0},
255+
{true, false, 0},
256+
{true, false, 1},
257+
{true, false, 5},
258+
{true, false, 126},
259+
{true, false, 127},
260+
}
261+
for _, tt := range tests {
262+
if !tt.isErr {
263+
t.Run(fmt.Sprintf("OK[BoolS8Result](%t)", tt.ok), func(t *testing.T) {
264+
r := OK[BoolS8Result](tt.ok)
265+
ok, _, isErr := r.Result()
266+
if isErr != tt.isErr {
267+
t.Errorf("isErr == %t, expected %t", isErr, tt.isErr)
268+
}
269+
if ok != tt.ok {
270+
t.Errorf("ok == %t, expected %t", ok, tt.ok)
271+
}
272+
})
273+
} else {
274+
t.Run(fmt.Sprintf("Err[BoolS8Result](%d)", tt.err), func(t *testing.T) {
275+
r := Err[BoolS8Result](tt.err)
276+
_, err, isErr := r.Result()
277+
if isErr != tt.isErr {
278+
t.Errorf("isErr == %t, expected %t", isErr, tt.isErr)
279+
}
280+
if err != tt.err {
281+
t.Errorf("err == %d, expected %d", err, tt.err)
282+
}
283+
})
284+
}
285+
}
286+
}
287+
288+
func TestIssue284NotTinyGo(t *testing.T) {
289+
if runtime.Compiler == "tinygo" {
290+
return
291+
}
292+
293+
// Using bool as result shape changes how [OK] returns the result by value.
294+
// This works on Go, but breaks on TinyGo / LLVM.
295+
// See https://github.com/bytecodealliance/go-modules/issues/284
296+
type BoolS8Result Result[bool, bool, int8]
297+
298+
tests := []struct {
299+
isErr bool
300+
ok bool
301+
err int8
302+
}{
303+
{false, false, 0},
304+
{false, true, 0},
305+
{true, false, 0},
306+
{true, false, 1},
307+
{true, false, 5},
308+
{true, false, 126},
309+
{true, false, 127},
310+
}
311+
for _, tt := range tests {
312+
if !tt.isErr {
313+
t.Run(fmt.Sprintf("OK[BoolS8Result](%t)", tt.ok), func(t *testing.T) {
314+
r := OK[BoolS8Result](tt.ok)
315+
ok, _, isErr := r.Result()
316+
if isErr != tt.isErr {
317+
t.Errorf("isErr == %t, expected %t", isErr, tt.isErr)
318+
}
319+
if ok != tt.ok {
320+
t.Errorf("ok == %t, expected %t", ok, tt.ok)
321+
}
322+
})
323+
} else {
324+
t.Run(fmt.Sprintf("Err[BoolS8Result](%d)", tt.err), func(t *testing.T) {
325+
r := Err[BoolS8Result](tt.err)
326+
_, err, isErr := r.Result()
327+
if isErr != tt.isErr {
328+
t.Errorf("isErr == %t, expected %t", isErr, tt.isErr)
329+
}
330+
if err != tt.err {
331+
t.Errorf("err == %d, expected %d", err, tt.err)
332+
}
333+
})
334+
}
335+
}
336+
}

0 commit comments

Comments
 (0)