Skip to content

Commit

Permalink
[chore] Cleanup OTTL RemoveXML tests (open-telemetry#35422)
Browse files Browse the repository at this point in the history
This is just a minor cleanup of tests, following the pattern used in
open-telemetry#35421, open-telemetry#35328, and open-telemetry#35364
  • Loading branch information
djaglowski authored and jriguera committed Oct 4, 2024
1 parent d681d71 commit 451c544
Showing 1 changed file with 58 additions and 101 deletions.
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

0 comments on commit 451c544

Please sign in to comment.