Skip to content

Commit

Permalink
Added Pascal-Kebab case option to casing function
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Lobo authored and daveshanley committed Jun 22, 2024
1 parent a4d8e87 commit 3aa701a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
19 changes: 12 additions & 7 deletions functions/core/casing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import (
)

const (
flat string = "flat"
camel string = "camel"
pascal string = "pascal"
kebab string = "kebab"
cobol string = "cobol"
snake string = "snake"
macro string = "macro"
flat string = "flat"
camel string = "camel"
pascal string = "pascal"
pascalKebab string = "pascal-kebab"
kebab string = "kebab"
cobol string = "cobol"
snake string = "snake"
macro string = "macro"
)

// Casing is a rule that will check the value of a node to ensure it meets the required casing type.
type Casing struct {
flat string
camel string
pascal string
pascalKebab string
kebab string
cobol string
snake string
Expand Down Expand Up @@ -142,6 +144,8 @@ func (c Casing) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) [
pattern = c.camel
case pascal:
pattern = c.pascal
case pascalKebab:
pattern = c.pascalKebab
case kebab:
pattern = c.kebab
case cobol:
Expand Down Expand Up @@ -229,6 +233,7 @@ func (c *Casing) compileExpressions() {
c.flat = fmt.Sprintf("[a-z][a-z%[1]s]*", digits)
c.camel = fmt.Sprintf("[a-z][a-z%[1]s]*(?:[A-Z%[1]s](?:[a-z%[1]s]+|$))*", digits)
c.pascal = fmt.Sprintf("[A-Z][a-z%[1]s]*(?:[A-Z%[1]s](?:[a-z%[1]s]+|$))*", digits)
c.pascalKebab = fmt.Sprintf("[A-Z][a-z%[1]s]*(-[A-Z][a-z%[1]s]*)*", digits)
c.kebab = fmt.Sprintf("[a-z%[1]s-]+", digits)
c.cobol = fmt.Sprintf("[A-Z%[1]s-]+", digits)
c.snake = fmt.Sprintf("[a-z%[1]s_]+", digits)
Expand Down
61 changes: 61 additions & 0 deletions functions/core/casing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,67 @@ func TestCasing_RunRule_KebabFail(t *testing.T) {
assert.Len(t, res, 1)
}

func TestCasing_RunRule_PascalKebabSuccess(t *testing.T) {

testCases := []string{
`melody: "Is-What-Makes-Life-Worth-Living"`,
`melody: "Is-Living"`,
`melody: "Living"`,
}

for _, sampleYaml := range testCases {

path := "$.melody"

nodes, _ := gen_utils.FindNodes([]byte(sampleYaml), path)
assert.Len(t, nodes, 1)

opts := make(map[string]string)
opts["type"] = "pascal-kebab"

rule := buildCoreTestRule(path, model.SeverityError, "casing", "", nil)
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), opts)
ctx.Given = path
ctx.Rule = &rule

def := &Casing{}
res := def.RunRule(nodes, ctx)

assert.Len(t, res, 0)
}
}

func TestCasing_RunRule_PascalKebabFail(t *testing.T) {

testCases := []string{
`melody: "Is-What-Makes-Life-Worth-living"`,
`melody: "Is-"`,
`melody: "Is-what"`,
`melody: "IS-WHAT"`,
`melody: "Is_What-Makes-life_worth-living"`,
}

for _, sampleYaml := range testCases {
path := "$.melody"

nodes, _ := gen_utils.FindNodes([]byte(sampleYaml), path)
assert.Len(t, nodes, 1)

opts := make(map[string]string)
opts["type"] = "pascal-kebab"

rule := buildCoreTestRule(path, model.SeverityError, "casing", "", nil)
ctx := buildCoreTestContext(model.CastToRuleAction(rule.Then), opts)
ctx.Given = path
ctx.Rule = &rule

def := &Casing{}
res := def.RunRule(nodes, ctx)

assert.Len(t, res, 1, "test case: '%s'", sampleYaml)
}
}

func TestCasing_RunRule_CobolSuccess(t *testing.T) {

sampleYaml := `maddy: "THE-LITTLE-CHAMPION"`
Expand Down

0 comments on commit 3aa701a

Please sign in to comment.