Skip to content

Commit

Permalink
Code cleanup and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Aug 1, 2021
1 parent 4b5f221 commit 9ccc1c5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
17 changes: 13 additions & 4 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,32 @@ func FindDynamicSelectorParts(selector string) DynamicSelectorParts {
i := 0
parts := DynamicSelectorParts{}
for _, v := range selector {
if v == '(' {
switch {

// Start of a group
case v == '(':
if parts.Comparison == "" {
parts.Key += string(v)
} else {
parts.Value += string(v)
}
i++
} else if v == ')' {

// End of a group
case v == ')':
i--
if parts.Comparison == "" {
parts.Key += string(v)
} else {
parts.Value += string(v)
}
} else if i == 0 && (v == '<' || v == '>' || v == '=') {

// Matches a comparison character
case i == 0 && (v == '<' || v == '>' || v == '=' || v == '!'):
parts.Comparison += string(v)
} else {

// Add to key or value based on comparison existence
default:
if parts.Comparison == "" {
parts.Key += string(v)
} else {
Expand Down
96 changes: 96 additions & 0 deletions selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dasel_test

import (
"github.com/tomwright/dasel"
"reflect"
"testing"
)

Expand All @@ -17,6 +18,28 @@ func testExtractNextSelector(in string, exp string, expRead int) func(t *testing
}
}

func testDynamicSelectorToGroups(in string, exp []string) func(t *testing.T) {
return func(t *testing.T) {
got, err := dasel.DynamicSelectorToGroups(in)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
}
}
}

func testFindDynamicSelectorParts(in string, exp dasel.DynamicSelectorParts) func(t *testing.T) {
return func(t *testing.T) {
got := dasel.FindDynamicSelectorParts(in)
if !reflect.DeepEqual(exp, got) {
t.Errorf("expected %v, got %v", exp, got)
}
}
}

func TestExtractNextSelector(t *testing.T) {
t.Run("Simple", testExtractNextSelector(`.metadata.name`, `.metadata`, 9))
t.Run("EscapedDot", testExtractNextSelector(`.before\.after.name`, `.before.after`, 14))
Expand All @@ -27,3 +50,76 @@ func TestExtractNextSelector(t *testing.T) {
t.Run("SimpleIndex", testExtractNextSelector(`.[123]`, `.[123]`, 6))
t.Run("SimpleLength", testExtractNextSelector(`.[#]`, `.[#]`, 4))
}

func TestDynamicSelectorToGroups(t *testing.T) {
t.Run("Blank", testDynamicSelectorToGroups("", []string{}))
t.Run("Single", testDynamicSelectorToGroups("(a=1)", []string{
"a=1",
}))
t.Run("Double", testDynamicSelectorToGroups("(a=1)(b=2)", []string{
"a=1",
"b=2",
}))
t.Run("Many", testDynamicSelectorToGroups("(a=1)(b=2)(c=3)(d=4)", []string{
"a=1",
"b=2",
"c=3",
"d=4",
}))
t.Run("Nested", testDynamicSelectorToGroups("(a=1.(x=3)(y=4))(b=2)", []string{
"a=1.(x=3)(y=4)",
"b=2",
}))
t.Run("Dot", testDynamicSelectorToGroups("(a=.)(b=2)", []string{
"a=.",
"b=2",
}))
}

func TestFindDynamicSelectorParts(t *testing.T) {
t.Run("Blank", testFindDynamicSelectorParts("", dasel.DynamicSelectorParts{
Key: "",
Comparison: "",
Value: "",
}))
t.Run("Equal", testFindDynamicSelectorParts("a=b", dasel.DynamicSelectorParts{
Key: "a",
Comparison: "=",
Value: "b",
}))
t.Run("NotEqual", testFindDynamicSelectorParts("a!=b", dasel.DynamicSelectorParts{
Key: "a",
Comparison: "!=",
Value: "b",
}))
t.Run("MoreThanEqual", testFindDynamicSelectorParts("a>=b", dasel.DynamicSelectorParts{
Key: "a",
Comparison: ">=",
Value: "b",
}))
t.Run("LessThanEqual", testFindDynamicSelectorParts("a<=b", dasel.DynamicSelectorParts{
Key: "a",
Comparison: "<=",
Value: "b",
}))
t.Run("NestedGroupIgnored", testFindDynamicSelectorParts("(.(x=y)).x=1", dasel.DynamicSelectorParts{
Key: "(.(x=y)).x",
Comparison: "=",
Value: "1",
}))
t.Run("NestedGroupIgnored", testFindDynamicSelectorParts("a=(.(x=y))", dasel.DynamicSelectorParts{
Key: "a",
Comparison: "=",
Value: "(.(x=y))",
}))
t.Run("SelectorOnBothSides", testFindDynamicSelectorParts("(.a.b.c)=(.x.y.z)", dasel.DynamicSelectorParts{
Key: "(.a.b.c)",
Comparison: "=",
Value: "(.x.y.z)",
}))
t.Run("NestedWithSelectorOnBothSides", testFindDynamicSelectorParts("(.a.b.(1=2).c)=(.x.(3=4).y.z)", dasel.DynamicSelectorParts{
Key: "(.a.b.(1=2).c)",
Comparison: "=",
Value: "(.x.(3=4).y.z)",
}))
}

0 comments on commit 9ccc1c5

Please sign in to comment.