Skip to content

Commit

Permalink
latest
Browse files Browse the repository at this point in the history
  • Loading branch information
conneroisu committed Jun 3, 2024
1 parent 0648c4e commit 83fe4a4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 70 deletions.
8 changes: 4 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (

// NewFromString parses a string into a slice of structs.
//
// The struct must have a field with the tag `seltabl`, a header selector with
// the tag `hSel`, and a data selector with the tag `dSel`.
// The struct must have a field with the tag seltabl, a header selector with
// the tag hSel, and a data selector with the tag dSel.
//
// The selectors responsibilties:
//
Expand Down Expand Up @@ -92,15 +92,15 @@ func NewFromString[T any](htmlInput string) ([]T, error) {
headSelector := field.Tag.Get(headerSelectorTag)
if headSelector == "" {
return nil, fmt.Errorf(
"no header selector (%s) for field %s",
"no header selector (%s) defined for field %s",
headerSelectorTag,
headName,
)
}
headerRow := doc.Find(headSelector)
if headerRow.Length() == 0 {
return nil, fmt.Errorf(
"no header for field %s with selector (%s)",
"selector (%s) found no header for field %s",
headName,
headSelector,
)
Expand Down
85 changes: 24 additions & 61 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package seltabl

import (
"fmt"
"testing"

"github.com/bytedance/sonic"
"github.com/conneroisu/seltabl/testdata"
"github.com/stretchr/testify/assert"
)
Expand All @@ -13,9 +11,6 @@ import (
func TestFixtureTables(t *testing.T) {
p, err := NewFromString[testdata.FixtureStruct](testdata.FixtureABNumTable)
assert.Nil(t, err)
for _, pp := range p {
fmt.Printf("pp %+v\n", pp)
}
assert.Equal(t, "1", p[0].A)
assert.Equal(t, "2", p[0].B)
assert.Equal(t, "3", p[1].A)
Expand All @@ -31,7 +26,6 @@ func TestNumberedTable(t *testing.T) {
p, err := NewFromString[testdata.NumberedStruct](testdata.NumberedTable)
assert.Nil(t, err)
assert.NoError(t, err)

assert.Equal(t, "Row 1, Cell 1", p[0].Header1)
assert.Equal(t, "Row 1, Cell 2", p[0].Header2)
assert.Equal(t, "Row 1, Cell 3", p[0].Header3)
Expand All @@ -43,63 +37,32 @@ func TestNumberedTable(t *testing.T) {
assert.Equal(t, "Row 3, Cell 3", p[2].Header3)
}

// <table>
// <tr>
// <th>Supernova</th>
// <th>Year</th>
// <th>Type</th>
// <th>Distance (light-years)</th>
// <th>Notes</th>
// </tr>
// <tr>
// <td>SN 1006</td>
// <td>1006</td>
// <td>Type Ia</td>
// <td>7,200</td>
// <td>Brightest recorded supernova in history</td>
// </tr>
// <tr>
// <td>SN 1054 (Crab Nebula)</td>
// <td>1054</td>
// <td>Type II</td>
// <td>6,500</td>
// <td>Formed the Crab Nebula and pulsar</td>
// </tr>
// <tr>
// <td>SN 1572 (Tycho's Supernova)</td>
// <td>1572</td>
// <td>Type Ia</td>
// <td>8,000-10,000</td>
// <td>Observed by Tycho Brahe</td>
// </tr>
// <tr>
// <td>SN 1604 (Kepler's Supernova)</td>
// <td>1604</td>
// <td>Type Ia</td>
// <td>20,000</td>
// <td>Last observed supernova in the Milky Way</td>
// </tr>
// <tr>
// <td>SN 1987A</td>
// <td>1987</td>
// <td>Type II</td>
// <td>168,000</td>
// <td>Closest observed supernova since 1604</td>
// </tr>
// <tr>
// <td>SN 1993J</td>
// <td>1993</td>
// <td>Type IIb</td>
// <td>11,000,000</td>
// <td>In the galaxy M81</td>
// </tr>
// </table>

// TestSuperNovaTable tests the parsing of a table with supernova data.
func TestSuperNovaTable(t *testing.T) {
p, err := NewFromString[testdata.SuperNovaStruct](testdata.SuperNovaTable)
assert.Nil(t, err)
assert.Equal(t, "SN 1006", p[0].Supernova)
assert.Equal(t, "1006", p[0].Year)
assert.Equal(t, "Type Ia", p[0].Type)
assert.Equal(t, "7,200", p[0].Distance)
assert.Equal(t, "Brightest recorded supernova in history", p[0].Notes)

assert.Equal(t, "SN 1054 (Crab Nebula)", p[1].Supernova)
assert.Equal(t, "1054", p[1].Year)
assert.Equal(t, "Type II", p[1].Type)
assert.Equal(t, "6,500", p[1].Distance)
assert.Equal(t, "Formed the Crab Nebula and pulsar", p[1].Notes)

assert.Equal(t, "SN 1572 (Tycho's Supernova)", p[2].Supernova)
assert.Equal(t, "1572", p[2].Year)
assert.Equal(t, "Type Ia", p[2].Type)
assert.Equal(t, "8,000-10,000", p[2].Distance)
assert.Equal(t, "Observed by Tycho Brahe", p[2].Notes)

assert.Equal(t, "SN 1604 (Kepler's Supernova)", p[3].Supernova)
assert.Equal(t, "1604", p[3].Year)
assert.Equal(t, "Type Ia", p[3].Type)
assert.Equal(t, "20,000", p[3].Distance)
assert.Equal(t, "Last observed supernova in the Milky Way", p[3].Notes)

var data testdata.SuperNovaStruct
// Marshal
output, err := sonic.Marshal(&data)
}
File renamed without changes.
1 change: 0 additions & 1 deletion specify.go

This file was deleted.

3 changes: 0 additions & 3 deletions testdata/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ type SuperNovaStruct struct {

//go:embed supernova.html
var SuperNovaTable string

//go:embed supernova.json
var SuperNovaJSON string
1 change: 0 additions & 1 deletion tt.json

This file was deleted.

0 comments on commit 83fe4a4

Please sign in to comment.