File: testutils_test.go

package info (click to toggle)
golang-github-yuin-gopher-lua 0.0~git20170915.0.eb1c729-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 852 kB
  • sloc: yacc: 479; python: 73; makefile: 18
file content (78 lines) | stat: -rw-r--r-- 2,109 bytes parent folder | download | duplicates (2)
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
76
77
78
package lua

import (
	"fmt"
	"path/filepath"
	"regexp"
	"runtime"
	"testing"
)

func positionString(level int) string {
	_, file, line, _ := runtime.Caller(level + 1)
	return fmt.Sprintf("%v:%v:", filepath.Base(file), line)
}

func errorIfNotEqual(t *testing.T, v1, v2 interface{}) {
	if v1 != v2 {
		t.Errorf("%v '%v' expected, but got '%v'", positionString(1), v1, v2)
	}
}

func errorIfFalse(t *testing.T, cond bool, msg string, args ...interface{}) {
	if !cond {
		if len(args) > 0 {
			t.Errorf("%v %v", positionString(1), fmt.Sprintf(msg, args...))
		} else {
			t.Errorf("%v %v", positionString(1), msg)
		}
	}
}

func errorIfNotNil(t *testing.T, v1 interface{}) {
	if fmt.Sprint(v1) != "<nil>" {
		t.Errorf("%v nil expected, but got '%v'", positionString(1), v1)
	}
}

func errorIfNil(t *testing.T, v1 interface{}) {
	if fmt.Sprint(v1) == "<nil>" {
		t.Errorf("%v non-nil value expected, but got nil", positionString(1))
	}
}

func errorIfScriptFail(t *testing.T, L *LState, script string) {
	if err := L.DoString(script); err != nil {
		t.Errorf("%v %v", positionString(1), err.Error())
	}
}

func errorIfGFuncFail(t *testing.T, L *LState, f LGFunction) {
	if err := L.GPCall(f, LNil); err != nil {
		t.Errorf("%v %v", positionString(1), err.Error())
	}
}

func errorIfScriptNotFail(t *testing.T, L *LState, script string, pattern string) {
	if err := L.DoString(script); err != nil {
		reg := regexp.MustCompile(pattern)
		if len(reg.FindStringIndex(err.Error())) == 0 {
			t.Errorf("%v error message '%v' does not contains given pattern string '%v'.", positionString(1), err.Error(), pattern)
			return
		}
		return
	}
	t.Errorf("%v script should fail", positionString(1))
}

func errorIfGFuncNotFail(t *testing.T, L *LState, f LGFunction, pattern string) {
	if err := L.GPCall(f, LNil); err != nil {
		reg := regexp.MustCompile(pattern)
		if len(reg.FindStringIndex(err.Error())) == 0 {
			t.Errorf("%v error message '%v' does not contains given pattern string '%v'.", positionString(1), err.Error(), pattern)
			return
		}
		return
	}
	t.Errorf("%v LGFunction should fail", positionString(1))
}