forked from arnodel/golua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesttags.go
96 lines (88 loc) · 1.76 KB
/
testtags.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package luatesting
import (
"bytes"
"fmt"
"regexp"
"runtime"
)
var tagsPtn = regexp.MustCompile(`^-- tags: *(!?[a-z]+)(?:,(!?[a-z]+))* *[\n\r]`)
type testTag struct {
name string
value bool
}
func checkTags(source []byte) (bool, error) {
tags, err := getTags(source)
if err != nil {
return false, err
}
for _, tag := range tags {
if !checkTag(tag) {
return false, nil
}
}
return true, nil
}
func getTags(source []byte) ([]testTag, error) {
if !bytes.HasPrefix(source, []byte("-- tags:")) {
return nil, nil
}
match := tagsPtn.FindSubmatch(source)
if len(match) == 0 {
return nil, fmt.Errorf("bad tags line")
}
var tags []testTag
for _, b := range match[1:] {
if len(b) == 0 {
continue
}
if b[0] == '!' {
tags = append(tags, testTag{name: string(b[1:]), value: false})
} else {
tags = append(tags, testTag{name: string(b), value: true})
}
}
return tags, nil
}
func checkTag(tag testTag) bool {
if tag.name == "unix" {
return unixOS[runtime.GOOS] == tag.value
}
if knownOS[tag.name] {
return (tag.name == runtime.GOOS) == tag.value
}
return true
}
// Maps below copied from package build
var unixOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"linux": true,
"netbsd": true,
"openbsd": true,
"solaris": true,
}
var knownOS = map[string]bool{
"aix": true,
"android": true,
"darwin": true,
"dragonfly": true,
"freebsd": true,
"hurd": true,
"illumos": true,
"ios": true,
"js": true,
"linux": true,
"nacl": true,
"netbsd": true,
"openbsd": true,
"plan9": true,
"solaris": true,
"windows": true,
"zos": true,
}