github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/luatesting/testtags.go (about)

     1  package luatesting
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"regexp"
     7  	"runtime"
     8  )
     9  
    10  var tagsPtn = regexp.MustCompile(`^-- tags: *(!?[a-z]+)(?:,(!?[a-z]+))* *[\n\r]`)
    11  
    12  type testTag struct {
    13  	name  string
    14  	value bool
    15  }
    16  
    17  func checkTags(source []byte) (bool, error) {
    18  	tags, err := getTags(source)
    19  	if err != nil {
    20  		return false, err
    21  	}
    22  	for _, tag := range tags {
    23  		if !checkTag(tag) {
    24  			return false, nil
    25  		}
    26  	}
    27  	return true, nil
    28  }
    29  
    30  func getTags(source []byte) ([]testTag, error) {
    31  	if !bytes.HasPrefix(source, []byte("-- tags:")) {
    32  		return nil, nil
    33  	}
    34  	match := tagsPtn.FindSubmatch(source)
    35  	if len(match) == 0 {
    36  		return nil, fmt.Errorf("bad tags line")
    37  	}
    38  	var tags []testTag
    39  	for _, b := range match[1:] {
    40  		if len(b) == 0 {
    41  			continue
    42  		}
    43  		if b[0] == '!' {
    44  			tags = append(tags, testTag{name: string(b[1:]), value: false})
    45  		} else {
    46  			tags = append(tags, testTag{name: string(b), value: true})
    47  		}
    48  	}
    49  	return tags, nil
    50  }
    51  
    52  func checkTag(tag testTag) bool {
    53  	if tag.name == "unix" {
    54  		return unixOS[runtime.GOOS] == tag.value
    55  	}
    56  	if knownOS[tag.name] {
    57  		return (tag.name == runtime.GOOS) == tag.value
    58  	}
    59  	return true
    60  }
    61  
    62  // Maps below copied from package build
    63  var unixOS = map[string]bool{
    64  	"aix":       true,
    65  	"android":   true,
    66  	"darwin":    true,
    67  	"dragonfly": true,
    68  	"freebsd":   true,
    69  	"hurd":      true,
    70  	"illumos":   true,
    71  	"ios":       true,
    72  	"linux":     true,
    73  	"netbsd":    true,
    74  	"openbsd":   true,
    75  	"solaris":   true,
    76  }
    77  
    78  var knownOS = map[string]bool{
    79  	"aix":       true,
    80  	"android":   true,
    81  	"darwin":    true,
    82  	"dragonfly": true,
    83  	"freebsd":   true,
    84  	"hurd":      true,
    85  	"illumos":   true,
    86  	"ios":       true,
    87  	"js":        true,
    88  	"linux":     true,
    89  	"nacl":      true,
    90  	"netbsd":    true,
    91  	"openbsd":   true,
    92  	"plan9":     true,
    93  	"solaris":   true,
    94  	"windows":   true,
    95  	"zos":       true,
    96  }