-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from conneroisu/dev
Dev
- Loading branch information
Showing
45 changed files
with
1,147 additions
and
335 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
File renamed without changes.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 +1,139 @@ | ||
package seltabl | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// DecodeExStruct is a test struct | ||
type DecodeExStruct struct { | ||
A string `json:"a" seltabl:"a" hSel:"tr:nth-child(1) td:nth-child(1)" dSel:"tr td:nth-child(1)" cSel:"$text"` | ||
B string `json:"b" seltabl:"b" hSel:"tr:nth-child(1) td:nth-child(2)" dSel:"tr td:nth-child(2)" cSel:"$text"` | ||
} | ||
|
||
// TestDecoder_Decode tests the Decoder.Decode function | ||
func TestDecoder_Decode(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input string | ||
expected []DecodeExStruct | ||
hasError bool | ||
}{ | ||
{ | ||
name: "Valid input", | ||
input: ` | ||
<table> | ||
<tr> | ||
<td>a</td> | ||
<td>b</td> | ||
</tr> | ||
<tr> | ||
<td>1</td> | ||
<td>2</td> | ||
</tr> | ||
<tr> | ||
<td>3</td> | ||
<td>4</td> | ||
</tr> | ||
</table> | ||
`, | ||
expected: []DecodeExStruct{ | ||
{A: "1", B: "2"}, | ||
{A: "3", B: "4"}, | ||
}, | ||
hasError: false, | ||
}, | ||
{ | ||
name: "Invalid input", | ||
input: ` | ||
<table> | ||
<tr> | ||
<td>a</td> | ||
<td>b</td> | ||
</tr> | ||
<tr> | ||
<td>1</td> | ||
</tr> | ||
</table> | ||
`, | ||
expected: nil, | ||
hasError: true, | ||
}, | ||
{ | ||
name: "Invalid input with invalid html", | ||
input: ` | ||
<table> | ||
<tr> | ||
<td>a</td> | ||
<td>b</td> | ||
</tr> | ||
<tr> | ||
<td>1</td> | ||
</tr> | ||
</table> | ||
`, | ||
expected: nil, | ||
hasError: true, | ||
}, | ||
{ | ||
name: "Invalid input with invalid json", | ||
input: ` | ||
<table> | ||
<tr> | ||
<td>a</td> | ||
<td>b</td> | ||
</tr> | ||
<tr> | ||
<td>1</td> | ||
</tr> | ||
</table> | ||
`, | ||
expected: nil, | ||
hasError: true, | ||
}, | ||
{ | ||
name: "Invalid input with invalid json", | ||
input: ` | ||
<table> | ||
<tr> | ||
<td>a</td> | ||
<td>b</td> | ||
<td>1</td> | ||
</tr> | ||
</table> | ||
`, | ||
expected: nil, | ||
hasError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
r := io.NopCloser(strings.NewReader(tc.input)) | ||
decoder := NewDecoder[DecodeExStruct](r) | ||
result, err := decoder.Decode() | ||
|
||
if tc.hasError { | ||
if err == nil { | ||
t.Errorf("Expected an error, but got none") | ||
} | ||
return | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
if len(result) != len(tc.expected) { | ||
t.Errorf("Expected %d results, but got %d", len(tc.expected), len(result)) | ||
} | ||
|
||
for i, expected := range tc.expected { | ||
if result[i].A != expected.A || result[i].B != expected.B { | ||
t.Errorf("Expected %+v, but got %+v", expected, result[i]) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Package main shows how to use the seltabl package to scrape a table from a given url. | ||
// The table used in this example is from the huggingface llm leader board. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// main scrapes from: https://huggingface.co/spaces/HuggingFaceH4/LLM-Leaderboard | ||
func main() { | ||
if err := run(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// run runs the example | ||
func run() error { | ||
fmt.Println("Hello, World from llm leader board!") | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestRun tests the run function | ||
func TestRun(t *testing.T) { | ||
err := run() | ||
assert.Nil(t, err) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package main is the an example of how to use the seltabl package. | ||
// for the seltabl package | ||
package main |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Package main is the an example of how to use the seltabl package. | ||
// for the seltabl package to scrape a html table from a given url. | ||
// The table used in this example is from the wikipedia page for | ||
// penguins. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// main scrapes from: https://en.wikipedia.org/wiki/List_of_penguins | ||
func main() { | ||
if err := run(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// run runs the example | ||
func run() error { | ||
fmt.Println("Hello, World from list of penguins!") | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestRun tests the run function | ||
func TestRun(t *testing.T) { | ||
err := run() | ||
assert.Nil(t, err) | ||
} |
Oops, something went wrong.