Skip to content

Commit

Permalink
test JSON and HTML parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bjesus committed Sep 21, 2024
1 parent 67e36d4 commit 73cf378
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
8 changes: 6 additions & 2 deletions parsers/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ func ParseHTMLQueries(htmlData []byte, queries []string, nextPage string) ([]int

})

result = append(result, subresult)
// result = append(result, subresult)
// Only append non-empty results
if len(subresult) > 0 {
result = append(result, subresult)
}
} else if indentation == 0 {
parts := strings.Split(line, "|")
elements := doc.Find(" " + parts[0])
elements := doc.Find(parts[0])
value := elements.First()

html := ""
Expand Down
42 changes: 42 additions & 0 deletions parsers/html_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package parsers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseHTMLQueries(t *testing.T) {
htmlData := []byte(`
<ul><li>one</li><li>two</li><li>three</li></ul>
<p><ol><li>foo</li><li>bar</li></ol>
<table>
<tr><td>penny</td><td>lane</td></tr>
<tr><td>strawberry</td><td>fields</td></tr>
<tr><td>everybody's got something to hide</td><td>except for me and my monkey</td></tr>
</table>
<table>
<tr><td>fool</td><td>on</td><td>the</td><td>hill</td></tr>
</table>
<a href="/more">next</a>
`)

queries := []string{
"ul li",
" li",
"ol li",
"ol li | wc -c",
"table",
" tr",
" td",
" td",
}

result, nextPage, err := ParseHTMLQueries(htmlData, queries, "a")

expectedResult := []interface{}{[]interface{}{"one", "two", "three"}, "foo", "12", []interface{}{[]interface{}{[]interface{}{"penny", "lane"}, []interface{}{"strawberry", "fields"}, []interface{}{"everybody's got something to hide", "except for me and my monkey"}}, []interface{}{[]interface{}{"fool", "on", "the", "hill"}}}}

assert.NoError(t, err)
assert.Equal(t, expectedResult, result)
assert.Equal(t, "/more", nextPage)
}
33 changes: 33 additions & 0 deletions parsers/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parsers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseJSONQueries(t *testing.T) {
JSONData := []byte(`
{"person": {"name": "bjesus", "friends": [{ "name": "bob", "nickname": "rocky"}, {"name":"alice", "nickname": "racoon"}]}}
`)

queries := []string{
"person.name",
"person.name | wc -c",
"person.friends",
" name",
" nickname",
}

result, nextPage, err := ParseJSONQueries(JSONData, queries)

expectedResult := []interface{}{
"bjesus",
float64(6),
[]interface{}{[]interface{}{"bob", "rocky"}, []interface{}{"alice", "racoon"}},
}

assert.NoError(t, err)
assert.Equal(t, expectedResult, result)
assert.Equal(t, "", nextPage)
}

0 comments on commit 73cf378

Please sign in to comment.