-
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.
- Loading branch information
1 parent
9d5a946
commit 5b37da9
Showing
53 changed files
with
480 additions
and
1,181 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
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
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.
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" | ||
) | ||
|
||
// 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 Example2!") | ||
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,6 @@ | ||
// Package errors is the errors package for the application | ||
// | ||
// # It contains the errors for the application | ||
// | ||
// It is used to return comprehendable errors to the user of the package. | ||
package errors |
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
Oops, something went wrong.