1package atc_test
2
3import (
4	"github.com/concourse/concourse/atc"
5
6	. "github.com/onsi/ginkgo"
7	. "github.com/onsi/gomega"
8)
9
10var _ = Describe("ValidateIdentifier", func() {
11	type testCase struct {
12		description string
13		identifier  string
14		message     string
15		warning     bool
16	}
17
18	for _, test := range []testCase{
19		{
20			description: "starts with a valid letter",
21			identifier:  "something",
22			warning:     false,
23		},
24		{
25			description: "contains multilingual characters",
26			identifier:  "ひらがな",
27			warning:     false,
28		},
29		{
30			description: "starts with a number",
31			identifier:  "1something",
32			message:     "must start with a lowercase letter",
33			warning:     true,
34		},
35		{
36			description: "starts with hyphen",
37			identifier:  "-something",
38			message:     "must start with a lowercase letter",
39			warning:     true,
40		},
41		{
42			description: "starts with period",
43			identifier:  ".something",
44			message:     "must start with a lowercase letter",
45			warning:     true,
46		},
47		{
48			description: "starts with an uppercase letter",
49			identifier:  "Something",
50			message:     "must start with a lowercase letter",
51			warning:     true,
52		},
53		{
54			description: "contains an underscore",
55			identifier:  "some_thing",
56			message:     "illegal character '_'",
57			warning:     true,
58		},
59		{
60			description: "contains an uppercase letter",
61			identifier:  "someThing",
62			message:     "illegal character 'T'",
63			warning:     true,
64		},
65	} {
66		test := test
67
68		Context("when an identifier "+test.description, func() {
69			var it string
70			if test.warning {
71				it = "returns a warning"
72			} else {
73				it = "runs without warning"
74			}
75			It(it, func() {
76				warning := atc.ValidateIdentifier(test.identifier)
77				if test.warning {
78					Expect(warning).NotTo(BeNil())
79					Expect(warning.Message).To(ContainSubstring(test.message))
80				} else {
81					Expect(warning).To(BeNil())
82				}
83			})
84		})
85	}
86
87	Describe("ValidateIdentifier with context", func() {
88		Context("when an identifier is invalid", func() {
89			It("returns an error with context", func() {
90				warning := atc.ValidateIdentifier("_something", "pipeline")
91				Expect(warning).NotTo(BeNil())
92				Expect(warning.Message).To(ContainSubstring("'_something' is not a valid identifier"))
93			})
94		})
95	})
96})
97