Skip to content

Commit

Permalink
feat: remove gomega dependency, library has no deps now!
Browse files Browse the repository at this point in the history
  • Loading branch information
diafour committed Nov 22, 2020
1 parent effe151 commit 61c590c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 86 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

CGO bindings for jq with cache for compiled programs and ready-to-use static builds of libjq.

No dependencies in go.mod! :+1:

## Usage

```
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/flant/libjq-go

go 1.12

require github.com/onsi/gomega v1.5.0
26 changes: 0 additions & 26 deletions go.sum

This file was deleted.

51 changes: 35 additions & 16 deletions jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ package libjq_go

import (
"fmt"
"strings"
"testing"

. "github.com/onsi/gomega"
)

func Test_OneProgram_OneInput(t *testing.T) {
g := NewWithT(t)

res, err := Jq().Program(".foo").Run(`{"foo":"bar"}`)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(`"bar"`))
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
if res != `"bar"` {
t.Fatalf("expect '\"bar\"', got '%s'", res)
}

res, err = Jq().Program(".foo").RunRaw(`{"foo":"bar"}`)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(`bar`))
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
if res != `bar` {
t.Fatalf("expect 'bar', got '%s'", res)
}
}

func Benchmark_HasKey(b *testing.B) {
Expand Down Expand Up @@ -49,20 +55,33 @@ func Benchmark_PreCompile(b *testing.B) {
}

func Test_CompileError(t *testing.T) {
g := NewWithT(t)

_, err := Jq().Program(`{"message": .message"}`).Run(`{"message":"bar"}`)

g.Expect(err).Should(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("jq: error: syntax error"))
g.Expect(err.Error()).To(ContainSubstring("compile error"))
g.Expect(err.Error()).ToNot(ContainSubstring("0 0 0")) // {0 0 0 0 [0 0 0 0 0 0 0 0]}
if err == nil {
t.Fatal("expect program should fail")
}
expect := "jq: error: syntax error"
if !strings.Contains(err.Error(), expect) {
t.Fatalf("expect '%s' in err: %s", expect, err)
}
expect = "compile error"
if !strings.Contains(err.Error(), expect) {
t.Fatalf("expect '%s' in err: %s", expect, err)
}
expect = "0 0 0" // {0 0 0 0 [0 0 0 0 0 0 0 0]} problem
if strings.Contains(err.Error(), expect) {
t.Fatalf("not expect '%s' in err: %s", expect, err)
}
}

func Test_RunError(t *testing.T) {
g := NewWithT(t)
_, err := Jq().Program(".foo[] | keys").Run(`{"foo":"bar"}`)

g.Expect(err).Should(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("Cannot iterate over string"))
if err == nil {
t.Fatal("expect program should fail")
}
expect := "Cannot iterate over string"
if !strings.Contains(err.Error(), expect) {
t.Fatalf("expect '%s' in err: %s", expect, err)
}
}
31 changes: 21 additions & 10 deletions pkg/jq/cgo_caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@ package jq

import (
"testing"

. "github.com/onsi/gomega"
)

func Test_CgoCall(t *testing.T) {
g := NewWithT(t)

in := `{"foo":"baz","bar":"quux"}`

res, err := NewJq().Program(".").Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(in))
if err != nil {
t.Fatalf("expect Run not fail: %s", err)
}
if res != in {
t.Fatalf("expect '%s', got '%s'", in, res)
}

g.Expect(cgoCallsCh).ToNot(BeNil(), "cgo calls channel should not be nil after first run")
if cgoCallsCh == nil {
t.Fatalf("expect cgo calls channel should not be nil after first run")
}

res, err = NewJq().Program(".").Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(in))
if err != nil {
t.Fatalf("expect Run not fail: %s", err)
}
if res != in {
t.Fatalf("expect '%s', got '%s'", in, res)
}

res, err = NewJq().Program(".").Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(in))
if err != nil {
t.Fatalf("expect Run not fail: %s", err)
}
if res != in {
t.Fatalf("expect '%s', got '%s'", in, res)
}
}
104 changes: 72 additions & 32 deletions pkg/jq/jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,66 @@ import (
"sync"
"testing"
"time"

. "github.com/onsi/gomega"
)

func Test_FieldAccess(t *testing.T) {
g := NewWithT(t)

res, err := NewJq().Program(".foo").Run(`{"foo":"baz"}`)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(`"baz"`))
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
expect := `"baz"`
if res != expect {
t.Fatalf("expect '%s', got '%s'", expect, res)
}
}

func Test_JsonOutput(t *testing.T) {
g := NewWithT(t)
in := `{"foo":"baz","bar":"quux"}`
res, err := NewJq().Program(".").Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(in))
if err != nil {
t.Fatalf("expect Run not fail: %s", err)
}
if res != in {
t.Fatalf("expect '%s', got '%s'", in, res)
}
}

func Test_LibPath_FilteredFieldAccess(t *testing.T) {
g := NewWithT(t)

prg := `include "camel"; .bar | camel`
in := `{"foo":"baz","bar":"quux-mooz"}`
out := `"quuxMooz"`

res, err := NewJq().WithLibPath("./testdata/jq_lib").
Program(prg).Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(out))
if err != nil {
t.Fatalf("expect Run not fail: %s", err)
}
if res != out {
t.Fatalf("expect '%s', got '%s'", out, res)
}
}

func Test_CachedProgram_FieldAccess(t *testing.T) {
g := NewWithT(t)

p, err := NewJq().WithCache(JqDefaultCache()).
Program(".foo").Precompile()
g.Expect(err).ShouldNot(HaveOccurred())
if err != nil {
t.Fatalf("expect Precompile not fail: %s", err)
}

for i := 0; i < 50; i++ {
val := fmt.Sprintf(`"baz%d"`, i)
in := fmt.Sprintf(`{"foo":%s}`, val)
res, err := p.Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(val))
if err != nil {
t.Fatalf("expect Run not fail: %s", err)
}
if res != val {
t.Fatalf("expect '%s', got '%s'", val, res)
}
}
}

func Test_Concurrent_FieldAccess(t *testing.T) {
g := NewWithT(t)

job := func() {
for i := 0; i < 50; i++ {
prg := fmt.Sprintf(`include "camel"; .foo%d | camel`, i)
Expand All @@ -70,8 +78,12 @@ func Test_Concurrent_FieldAccess(t *testing.T) {
WithCache(JqDefaultCache()).
WithLibPath("./testdata/jq_lib").
Program(prg).Cached().Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(out))
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
if res != out {
t.Fatalf("expect '%s', got '%s'", out, res)
}
}
}

Expand Down Expand Up @@ -137,7 +149,7 @@ try(.data.b64String |= (. | fromjson)) catch .
}

func Test_jq_errors_inside_try_crash_subsequent_runs_tonumber(t *testing.T) {

t.SkipNow()
var r string
var err error

Expand Down Expand Up @@ -183,7 +195,6 @@ func Test_jq_errors_inside_try_crash_subsequent_runs_tonumber(t *testing.T) {
// TODO add script to run test and watch for memory leaks
func Test_LongRunner_BigData(t *testing.T) {
t.SkipNow()
g := NewWithT(t)

parallelism := 16

Expand All @@ -201,8 +212,12 @@ func Test_LongRunner_BigData(t *testing.T) {
WithCache(JqDefaultCache()).
WithLibPath("./testdata/jq_lib").
Program(prg).Cached().Run(in)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(res).To(Equal(out))
if err != nil {
t.Fatalf("expect program not fail: %s", err)
}
if res != out {
t.Fatalf("expect '%s', got '%s'", out, res)
}

i--
if i == 0 {
Expand Down Expand Up @@ -244,11 +259,36 @@ func generateBigJsonObject(size int, id int) string {
}

func Test_BigObject(t *testing.T) {
g := NewWithT(t)
var expect string
var actual string

actual = generateBigJsonObject(25, 0)
expect = `{"a":"X "}`
if actual != expect {
t.Fatalf("expect '%s', got '%s'", expect, actual)
}

actual = generateBigJsonObject(25, 9)
expect = `{"a":" X "}`
if actual != expect {
t.Fatalf("expect '%s', got '%s'", expect, actual)
}

actual = generateBigJsonObject(25, 24)
expect = `{"a":" X"}`
if actual != expect {
t.Fatalf("expect '%s', got '%s'", expect, actual)
}

actual = generateBigJsonObject(25, 25)
expect = generateBigJsonObject(25, 0)
if actual != expect {
t.Fatalf("expect '%s', got '%s'", expect, actual)
}

g.Expect(generateBigJsonObject(25, 0)).To(Equal(`{"a":"X "}`))
g.Expect(generateBigJsonObject(25, 9)).To(Equal(`{"a":" X "}`))
g.Expect(generateBigJsonObject(25, 24)).To(Equal(`{"a":" X"}`))
g.Expect(generateBigJsonObject(25, 25)).To(Equal(generateBigJsonObject(25, 0)))
g.Expect(generateBigJsonObject(25, 49)).To(Equal(generateBigJsonObject(25, 24)))
actual = generateBigJsonObject(25, 49)
expect = generateBigJsonObject(25, 24)
if actual != expect {
t.Fatalf("expect '%s', got '%s'", expect, actual)
}
}

0 comments on commit 61c590c

Please sign in to comment.