summaryrefslogtreecommitdiff
path: root/libgo/go/testing/script/script_test.go
blob: e9ab142c2b1b5f8237187385d3c16a10b2e8848a (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2009 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 script

import (
	"testing"
)

func TestNoop(t *testing.T) {
	err := Perform(0, nil)
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestSimple(t *testing.T) {
	c := make(chan int)
	defer close(c)

	a := NewEvent("send", nil, Send{c, 1})
	b := NewEvent("recv", []*Event{a}, Recv{c, 1})

	err := Perform(0, []*Event{a, b})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func TestFail(t *testing.T) {
	c := make(chan int)
	defer close(c)

	a := NewEvent("send", nil, Send{c, 2})
	b := NewEvent("recv", []*Event{a}, Recv{c, 1})

	err := Perform(0, []*Event{a, b})
	if err == nil {
		t.Errorf("Failed to get expected error")
	} else if _, ok := err.(ReceivedUnexpected); !ok {
		t.Errorf("Error returned was of the wrong type: %s", err)
	}
}

func TestClose(t *testing.T) {
	c := make(chan int)

	a := NewEvent("close", nil, Close{c})
	b := NewEvent("closed", []*Event{a}, Closed{c})

	err := Perform(0, []*Event{a, b})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}

func matchOne(v interface{}) bool {
	if i, ok := v.(int); ok && i == 1 {
		return true
	}
	return false
}

func TestRecvMatch(t *testing.T) {
	c := make(chan int)

	a := NewEvent("send", nil, Send{c, 1})
	b := NewEvent("recv", []*Event{a}, RecvMatch{c, matchOne})

	err := Perform(0, []*Event{a, b})
	if err != nil {
		t.Errorf("Got error: %s", err)
	}
}