File: tag.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 885 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
// Copyright (c) 2019, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package util

import (
	"errors"
	"unicode"
)

// ErrTagEmpty is the error returned by [CheckTag] for an empty tag.
var ErrTagEmpty = errors.New("A tag cannot be empty")

// ErrTagInvalid is the error returned by [CheckTag] for an invalid tag.
var ErrTagInvalid = errors.New("Invalid tag, should match (Letter|_)(Letter|_|Number)*")

// CheckTag checks that tag is a valid tag (see operator [Tag]) or not.
//
// [Tag]: https://go-testdeep.zetta.rocks/operators/tag/
func CheckTag(tag string) error {
	if tag == "" {
		return ErrTagEmpty
	}

	for i, r := range tag {
		if !(unicode.IsLetter(r) || r == '_' || (i > 0 && unicode.IsNumber(r))) {
			return ErrTagInvalid
		}
	}

	return nil
}