package ibd import ( "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/net/html" ) func Test_findClass(t *testing.T) { t.Parallel() tests := []struct { name string html string className string found bool expData string }{ { name: "class exists", html: `
`, className: "foo", found: true, expData: "div", }, { name: "class exists nested", html: `
`, className: "abc", found: true, expData: "a", }, { name: "class exists multiple", html: `
`, className: "foo", found: true, expData: "div", }, { name: "class missing", html: `
`, className: "foo", found: false, expData: "", }, { name: "class missing", html: `
`, className: "foo", found: false, expData: "", }, { name: "class exists multiple save div", html: `
`, className: "bar", found: true, expData: "div", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { node, err := html.Parse(strings.NewReader(tt.html)) require.NoError(t, err) got := findClass(node, tt.className) if !tt.found { require.Nil(t, got) return } require.NotNil(t, got) assert.Equal(t, tt.expData, got.Data) }) } }