Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Cleanup OTTL RemoveXML tests #35422

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 58 additions & 101 deletions pkg/ottl/ottlfuncs/func_remove_xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,135 +14,92 @@ import (

func Test_RemoveXML(t *testing.T) {
tests := []struct {
name string
target ottl.StringGetter[any]
xPath string
want string
name string
document string
xPath string
want string
}{
{
name: "remove single element",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b/></a>`, nil
},
},
xPath: "/a/b",
want: `<a></a>`,
name: "remove single element",
document: `<a><b/></a>`,
xPath: "/a/b",
want: `<a></a>`,
},
{
name: "remove multiple element",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b/><b/></a>`, nil
},
},
xPath: "/a/b",
want: `<a></a>`,
name: "remove multiple element",
document: `<a><b/><b/></a>`,
xPath: "/a/b",
want: `<a></a>`,
},
{
name: "remove multiple element with children",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b/><b><c/></b></a>`, nil
},
},
xPath: "/a/b",
want: `<a></a>`,
name: "remove multiple element with children",
document: `<a><b/><b><c/></b></a>`,
xPath: "/a/b",
want: `<a></a>`,
},
{
name: "remove multiple element various depths",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b/><b/><c><b><d/></b></c></a>`, nil
},
},
xPath: "/a//b",
want: `<a><c></c></a>`,
name: "remove multiple element various depths",
document: `<a><b/><b/><c><b><d/></b></c></a>`,
xPath: "/a//b",
want: `<a><c></c></a>`,
},
{
name: "remove attribute",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a foo="bar"/>`, nil
},
},
xPath: "/a/@foo",
want: `<a></a>`,
name: "remove attribute",
document: `<a foo="bar"/>`,
xPath: "/a/@foo",
want: `<a></a>`,
},
{
name: "remove element with attribute",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b foo="bar"/><b foo="notbar"/></a>`, nil
},
},
xPath: "/a/b[@foo='bar']",
want: `<a><b foo="notbar"></b></a>`,
name: "remove element with attribute",
document: `<a><b foo="bar"/><b foo="notbar"/></a>`,
xPath: "/a/b[@foo='bar']",
want: `<a><b foo="notbar"></b></a>`,
},
{
name: "remove attributes from multiple nodes",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b foo="bar"/><c foo="bar"/></a>`, nil
},
},
xPath: "//@foo",
want: `<a><b></b><c></c></a>`,
name: "remove attributes from multiple nodes",
document: `<a><b foo="bar"/><c foo="bar"/></a>`,
xPath: "//@foo",
want: `<a><b></b><c></c></a>`,
},
{
name: "remove multiple attributes from single node",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b foo="bar" hello="world" keep="this"/></a>`, nil
},
},
xPath: "//@*[local-name() != 'keep']",
want: `<a><b keep="this"></b></a>`,
name: "remove multiple attributes from single node",
document: `<a><b foo="bar" hello="world" keep="this"/></a>`,
xPath: "//@*[local-name() != 'keep']",
want: `<a><b keep="this"></b></a>`,
},
{
name: "remove text",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a>delete this</a>`, nil
},
},
xPath: "//text()['*delete*']",
want: `<a></a>`,
name: "remove text",
document: `<a>delete this</a>`,
xPath: "//text()['*delete*']",
want: `<a></a>`,
},
{
name: "remove comments",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b><c><!-- delete this --></c><!-- this too--></b></a>`, nil
},
},
xPath: "//comment()",
want: `<a><b><c></c></b></a>`,
name: "remove comments",
document: `<a><b><c><!-- delete this --></c><!-- this too--></b></a>`,
xPath: "//comment()",
want: `<a><b><c></c></b></a>`,
},
{
name: "remove CDATA",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<a><b><![CDATA[text content possibly containing literal <or &characters ]]></b></a>`, nil
},
},
xPath: "//text()['<![CDATA[*']",
want: `<a><b></b></a>`,
name: "remove CDATA",
document: `<a><b><![CDATA[text content possibly containing literal <or &characters ]]></b></a>`,
xPath: "//text()['<![CDATA[*']",
want: `<a><b></b></a>`,
},
{
name: "preserve declaration",
target: ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return `<?xml version="1.0" encoding="UTF-8"?><a>delete this</a>`, nil
},
},
xPath: "//text()['*delete*']",
want: `<?xml version="1.0" encoding="UTF-8"?><a></a>`,
name: "preserve declaration",
document: `<?xml version="1.0" encoding="UTF-8"?><a>delete this</a>`,
xPath: "//text()['*delete*']",
want: `<?xml version="1.0" encoding="UTF-8"?><a></a>`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc := removeXML(tt.target, tt.xPath)
target := ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return tt.document, nil
},
}
exprFunc := removeXML(target, tt.xPath)
result, err := exprFunc(context.Background(), nil)
assert.NoError(t, err)
assert.Equal(t, tt.want, result)
Expand Down