summaryrefslogtreecommitdiff
path: root/src/cmd/cgo/internal/swig/testdata/callback/main.go
blob: 73034a0c7c25e920504a6b7162429a0cfe40a66c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"os"
)

func main() {
	if len(os.Args) != 2 {
		fatal("usage: callback testname")
	}
	switch os.Args[1] {
	default:
		fatal("unknown test %q", os.Args[1])
	case "Call":
		testCall()
	case "Callback":
		testCallback()
	}
	println("OK")
}

func fatal(f string, args ...any) {
	fmt.Fprintln(os.Stderr, fmt.Sprintf(f, args...))
	os.Exit(1)
}

type GoCallback struct{}

func (p *GoCallback) Run() string {
	return "GoCallback.Run"
}

func testCall() {
	c := NewCaller()
	cb := NewCallback()

	c.SetCallback(cb)
	s := c.Call()
	if s != "Callback::run" {
		fatal("unexpected string from Call: %q", s)
	}
	c.DelCallback()
}

func testCallback() {
	c := NewCaller()
	cb := NewDirectorCallback(&GoCallback{})
	c.SetCallback(cb)
	s := c.Call()
	if s != "GoCallback.Run" {
		fatal("unexpected string from Call with callback: %q", s)
	}
	c.DelCallback()
	DeleteDirectorCallback(cb)
}