Skip to content

Commit

Permalink
wip - beautify
Browse files Browse the repository at this point in the history
  • Loading branch information
Macey Baker committed Mar 4, 2024
1 parent 0cd4708 commit 81b89c2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 444 deletions.
61 changes: 0 additions & 61 deletions expr/expr.go

This file was deleted.

154 changes: 0 additions & 154 deletions expr/functions.go

This file was deleted.

149 changes: 33 additions & 116 deletions expr/functions_test.go
Original file line number Diff line number Diff line change
@@ -1,147 +1,64 @@
package expr

import (
"reflect"
"fmt"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
"github.com/incident-io/catalog-importer/source"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Functions", func() {
var _ = Describe("Javascript evaluation", func() {
var (
src string
prg cel.Program
src string
subject string
sourceEntry source.SourceEntry
parsedEntries []source.SourceEntry
)

JustBeforeEach(func() {
env, err := cel.NewEnv(Stdlib())
Expect(err).NotTo(HaveOccurred())

ast, issues := env.Parse(src)
Expect(issues.Err()).NotTo(HaveOccurred())

prg, err = env.Program(ast)
sourceEntry = source.SourceEntry{
Origin: fmt.Sprintf("inline: entries.1"),
Filename: "",
Content: []byte{}, // some data here
}
parsedEntries, err := sourceEntry.Parse()
Expect(err).NotTo(HaveOccurred())
})

Describe("First", func() {
When("the input is a valid list", func() {
BeforeEach(func() {
src = "first(['chinchilla', 'capybara', 'corgi'])"
})

It("returns the first item in an array", func() {
out, _, err := prg.Eval(map[string]any{})
Expect(err).NotTo(HaveOccurred())
When("Grabbing a top-level attribute", func() {

Expect(out).To(Equal(types.String("chinchilla")))
})
})

When("the input is not a valid list", func() {
It("returns the correct value", func() {
BeforeEach(func() {
src = "first('corgi')"
// Set source
// Set subject
// Evaluate
})

It("returns the first item in an array", func() {
_, _, err := prg.Eval(map[string]any{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("no such overload: first(string)"))
})
})
})

Describe("Coalesce", func() {
When("the input is a list of objects", func() {
BeforeEach(func() {
src = "coalesce([{'key': 'value'}, null, null, 1])"
})

It("returns all non-nulls", func() {
out, _, err := prg.Eval(map[string]any{})
Expect(err).NotTo(HaveOccurred())

result, err := out.ConvertToNative(reflect.TypeOf([]any{}))
Expect(err).NotTo(HaveOccurred())

Expect(result).To(Equal([]any{
map[ref.Val]ref.Val{types.String("key"): types.String("value")}, int64(1),
}))
})
})
})

Describe("Pluck", func() {
BeforeEach(func() {
src = "pluck([{'severity': 'major'}, {'severity': 'minor'}], 'severity')"
})

When("the input is a valid list of objects", func() {
It("returns an array of the values for the given key", func() {
out, _, err := prg.Eval(map[string]any{})
Expect(err).NotTo(HaveOccurred())

result, err := out.ConvertToNative(reflect.TypeOf([]string{}))
Expect(err).NotTo(HaveOccurred())
When("Manipulating a string", func() {

Expect(result).To(Equal([]string{"major", "minor"}))
})
})
})

Describe("TrimPrefix", func() {
BeforeEach(func() {
src = "trimPrefix(value, 'group:')"
})

When("the input is a string", func() {
It("returns the input without prefix", func() {
out, _, err := prg.Eval(map[string]any{
"value": "group:engineering",
})
Expect(err).NotTo(HaveOccurred())

result, err := out.ConvertToNative(reflect.TypeOf(""))
Expect(err).NotTo(HaveOccurred())

Expect(result).To(Equal("engineering"))
It("returns the manipulated value", func() {
BeforeEach(func() {
src = "first('corgi')"
// Set source with string manipulation eg .replace
// Set subject
// Evaluate
})
})
})

Describe("Replace", func() {
BeforeEach(func() {
src = "replace(value, r'^https://([0-9A-Za-z_\\-]+)\\.example\\.com.*$', '$1')"
})

When("the input is a matching string", func() {
It("Returns the replacement", func() {
out, _, err := prg.Eval(map[string]any{
"value": "https://some-slug.example.com/test",
})
Expect(err).NotTo(HaveOccurred())

result, err := out.ConvertToNative(reflect.TypeOf(""))
Expect(err).NotTo(HaveOccurred())

Expect(result).To(Equal("some-slug"))
})
})

When("the input has no matches", func() {
It("returns the source string", func() {
out, _, err := prg.Eval(map[string]any{
"value": "https://some-slug.incident.io/test",
})
Expect(err).NotTo(HaveOccurred())

result, err := out.ConvertToNative(reflect.TypeOf(""))
Expect(err).NotTo(HaveOccurred())

Expect(result).To(Equal("https://some-slug.incident.io/test"))
When("Parsing an array value", func() {
It("returns the correct value", func() {
BeforeEach(func() {
src = "first('corgi')"
// Set source
// Set subject
// Set attribute with array=true
// Evaluate and expect array
})
})
})
Expand Down
Loading

0 comments on commit 81b89c2

Please sign in to comment.