Skip to content

Commit 3aa701a

Browse files
Calvin Lobodaveshanley
authored andcommitted
Added Pascal-Kebab case option to casing function
1 parent a4d8e87 commit 3aa701a

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

functions/core/casing.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ import (
1414
)
1515

1616
const (
17-
flat string = "flat"
18-
camel string = "camel"
19-
pascal string = "pascal"
20-
kebab string = "kebab"
21-
cobol string = "cobol"
22-
snake string = "snake"
23-
macro string = "macro"
17+
flat string = "flat"
18+
camel string = "camel"
19+
pascal string = "pascal"
20+
pascalKebab string = "pascal-kebab"
21+
kebab string = "kebab"
22+
cobol string = "cobol"
23+
snake string = "snake"
24+
macro string = "macro"
2425
)
2526

2627
// Casing is a rule that will check the value of a node to ensure it meets the required casing type.
2728
type Casing struct {
2829
flat string
2930
camel string
3031
pascal string
32+
pascalKebab string
3133
kebab string
3234
cobol string
3335
snake string
@@ -142,6 +144,8 @@ func (c Casing) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) [
142144
pattern = c.camel
143145
case pascal:
144146
pattern = c.pascal
147+
case pascalKebab:
148+
pattern = c.pascalKebab
145149
case kebab:
146150
pattern = c.kebab
147151
case cobol:
@@ -229,6 +233,7 @@ func (c *Casing) compileExpressions() {
229233
c.flat = fmt.Sprintf("[a-z][a-z%[1]s]*", digits)
230234
c.camel = fmt.Sprintf("[a-z][a-z%[1]s]*(?:[A-Z%[1]s](?:[a-z%[1]s]+|$))*", digits)
231235
c.pascal = fmt.Sprintf("[A-Z][a-z%[1]s]*(?:[A-Z%[1]s](?:[a-z%[1]s]+|$))*", digits)
236+
c.pascalKebab = fmt.Sprintf("[A-Z][a-z%[1]s]*(-[A-Z][a-z%[1]s]*)*", digits)
232237
c.kebab = fmt.Sprintf("[a-z%[1]s-]+", digits)
233238
c.cobol = fmt.Sprintf("[A-Z%[1]s-]+", digits)
234239
c.snake = fmt.Sprintf("[a-z%[1]s_]+", digits)

functions/core/casing_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,67 @@ func TestCasing_RunRule_KebabFail(t *testing.T) {
144144
assert.Len(t, res, 1)
145145
}
146146

147+
func TestCasing_RunRule_PascalKebabSuccess(t *testing.T) {
148+
149+
testCases := []string{
150+
`melody: "Is-What-Makes-Life-Worth-Living"`,
151+
`melody: "Is-Living"`,
152+
`melody: "Living"`,
153+
}
154+
155+
for _, sampleYaml := range testCases {
156+
157+
path := "$.melody"
158+
159+
nodes, _ := gen_utils.FindNodes([]byte(sampleYaml), path)
160+
assert.Len(t, nodes, 1)
161+
162+
opts := make(map[string]string)
163+
opts["type"] = "pascal-kebab"
164+
165+
rule := buildCoreTestRule(path, model.SeverityError, "casing", "", nil)
166+
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), opts)
167+
ctx.Given = path
168+
ctx.Rule = &rule
169+
170+
def := &Casing{}
171+
res := def.RunRule(nodes, ctx)
172+
173+
assert.Len(t, res, 0)
174+
}
175+
}
176+
177+
func TestCasing_RunRule_PascalKebabFail(t *testing.T) {
178+
179+
testCases := []string{
180+
`melody: "Is-What-Makes-Life-Worth-living"`,
181+
`melody: "Is-"`,
182+
`melody: "Is-what"`,
183+
`melody: "IS-WHAT"`,
184+
`melody: "Is_What-Makes-life_worth-living"`,
185+
}
186+
187+
for _, sampleYaml := range testCases {
188+
path := "$.melody"
189+
190+
nodes, _ := gen_utils.FindNodes([]byte(sampleYaml), path)
191+
assert.Len(t, nodes, 1)
192+
193+
opts := make(map[string]string)
194+
opts["type"] = "pascal-kebab"
195+
196+
rule := buildCoreTestRule(path, model.SeverityError, "casing", "", nil)
197+
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), opts)
198+
ctx.Given = path
199+
ctx.Rule = &rule
200+
201+
def := &Casing{}
202+
res := def.RunRule(nodes, ctx)
203+
204+
assert.Len(t, res, 1, "test case: '%s'", sampleYaml)
205+
}
206+
}
207+
147208
func TestCasing_RunRule_CobolSuccess(t *testing.T) {
148209

149210
sampleYaml := `maddy: "THE-LITTLE-CHAMPION"`

0 commit comments

Comments
 (0)