-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Okhlopkov <[email protected]>
- Loading branch information
Pavel Okhlopkov
committed
Nov 2, 2024
1 parent
bdd089e
commit 67b0557
Showing
5 changed files
with
61 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,68 @@ | ||
//go:build test | ||
// +build test | ||
|
||
// TODO: remove useless code | ||
|
||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
// import ( | ||
// "encoding/json" | ||
// "fmt" | ||
|
||
"github.com/onsi/gomega/matchers" | ||
"github.com/onsi/gomega/types" | ||
// "github.com/onsi/gomega/matchers" | ||
// "github.com/onsi/gomega/types" | ||
|
||
. "github.com/flant/libjq-go" | ||
) | ||
// . "github.com/flant/libjq-go" | ||
// ) | ||
|
||
// var _ = Ω(verBcs).To(MatchJq(`.[0] | has("objects")`, Equal(true))) | ||
func MatchJq(jqExpression string, matcher interface{}) types.GomegaMatcher { | ||
return &matchJq{ | ||
JqExpr: jqExpression, | ||
Matcher: matcher, | ||
} | ||
} | ||
// // var _ = Ω(verBcs).To(MatchJq(`.[0] | has("objects")`, Equal(true))) | ||
// func MatchJq(jqExpression string, matcher interface{}) types.GomegaMatcher { | ||
// return &matchJq{ | ||
// JqExpr: jqExpression, | ||
// Matcher: matcher, | ||
// } | ||
// } | ||
|
||
type matchJq struct { | ||
JqExpr string | ||
InputString string | ||
Matcher interface{} | ||
ResMatcher types.GomegaMatcher | ||
} | ||
// type matchJq struct { | ||
// JqExpr string | ||
// InputString string | ||
// Matcher interface{} | ||
// ResMatcher types.GomegaMatcher | ||
// } | ||
|
||
func (matcher *matchJq) Match(actual interface{}) (success bool, err error) { | ||
switch v := actual.(type) { | ||
case string: | ||
matcher.InputString = v | ||
case []byte: | ||
matcher.InputString = string(v) | ||
default: | ||
inputBytes, err := json.Marshal(v) | ||
if err != nil { | ||
return false, fmt.Errorf("MatchJq marshal object to json: %s\n", err) | ||
} | ||
matcher.InputString = string(inputBytes) | ||
} | ||
// func (matcher *matchJq) Match(actual interface{}) (success bool, err error) { | ||
// switch v := actual.(type) { | ||
// case string: | ||
// matcher.InputString = v | ||
// case []byte: | ||
// matcher.InputString = string(v) | ||
// default: | ||
// inputBytes, err := json.Marshal(v) | ||
// if err != nil { | ||
// return false, fmt.Errorf("MatchJq marshal object to json: %s\n", err) | ||
// } | ||
// matcher.InputString = string(inputBytes) | ||
// } | ||
|
||
//nolint:typecheck // Ignore false positive: undeclared name: `Jq`. | ||
res, err := Jq().Program(matcher.JqExpr).Run(matcher.InputString) | ||
if err != nil { | ||
return false, fmt.Errorf("MatchJq apply jq expression: %s\n", err) | ||
} | ||
// //nolint:typecheck // Ignore false positive: undeclared name: `Jq`. | ||
// res, err := Jq().Program(matcher.JqExpr).Run(matcher.InputString) | ||
// if err != nil { | ||
// return false, fmt.Errorf("MatchJq apply jq expression: %s\n", err) | ||
// } | ||
|
||
var isMatcher bool | ||
matcher.ResMatcher, isMatcher = matcher.Matcher.(types.GomegaMatcher) | ||
if !isMatcher { | ||
matcher.ResMatcher = &matchers.EqualMatcher{Expected: res} | ||
} | ||
// var isMatcher bool | ||
// matcher.ResMatcher, isMatcher = matcher.Matcher.(types.GomegaMatcher) | ||
// if !isMatcher { | ||
// matcher.ResMatcher = &matchers.EqualMatcher{Expected: res} | ||
// } | ||
|
||
return matcher.ResMatcher.Match(res) | ||
} | ||
// return matcher.ResMatcher.Match(res) | ||
// } | ||
|
||
func (matcher *matchJq) FailureMessage(actual interface{}) (message string) { | ||
return fmt.Sprintf("Jq expression `%s` applied to '%s' not match: %v", matcher.JqExpr, matcher.InputString, matcher.ResMatcher.FailureMessage(actual)) | ||
} | ||
// func (matcher *matchJq) FailureMessage(actual interface{}) (message string) { | ||
// return fmt.Sprintf("Jq expression `%s` applied to '%s' not match: %v", matcher.JqExpr, matcher.InputString, matcher.ResMatcher.FailureMessage(actual)) | ||
// } | ||
|
||
func (matcher *matchJq) NegatedFailureMessage(actual interface{}) (message string) { | ||
return fmt.Sprintf("Jq expression `%s` applied to '%s' should not match: %s", matcher.JqExpr, matcher.InputString, matcher.ResMatcher.NegatedFailureMessage(actual)) | ||
} | ||
// func (matcher *matchJq) NegatedFailureMessage(actual interface{}) (message string) { | ||
// return fmt.Sprintf("Jq expression `%s` applied to '%s' should not match: %s", matcher.JqExpr, matcher.InputString, matcher.ResMatcher.NegatedFailureMessage(actual)) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
//go:build test | ||
// +build test | ||
|
||
// TODO: remove useless code | ||
|
||
package utils | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
//go:build test | ||
// +build test | ||
|
||
// TODO: remove useless code | ||
|
||
package utils | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
//go:build test | ||
// +build test | ||
|
||
// TODO: remove useless code | ||
|
||
package utils | ||
|
||
import ( | ||
|