Skip to content

Commit

Permalink
Merge pull request #4 from conneroisu/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
conneroisu authored Jun 7, 2024
2 parents 2c8c44e + 59f11c6 commit 0108919
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Go
name: Test Go

on:
push:
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ lint:
test:
@+sh ./scripts/makefile.test.sh

.PHONY: dev.requirements
dev.requirements:
@sh ./scripts/makefile.dev.requirements.sh
.PHONY: install
install:
@sh ./scripts/makefile.install.sh

.PHONY: clean
clean:
Expand Down
18 changes: 11 additions & 7 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
version: '3'

tasks:
run:
cmds:
- task install

clean:
cmds:
- sh ./scripts/makefile.clean.sh
- sh ./scripts/taskfile.clean.sh

fmt:
cmds:
- sh ./scripts/makefile.fmt.sh
- sh ./scripts/taskfile.fmt.sh

lint:
cmds:
- sh ./scripts/makefile.lint.sh
- sh ./scripts/taskfile.lint.sh

test:
cmds:
- sh ./scripts/makefile.test.sh
- sh ./scripts/taskfile.test.sh

database:
cmds:
- sh ./scripts/makefile.database.sh
- sh ./scripts/taskfile.database.sh

coverage:
cmds:
- sh ./scripts/makefile.coverage.sh
- sh ./scripts/taskfile.coverage.sh

install:
cmds:
- sh ./scripts/makefile.install.sh
- sh ./scripts/taskfile.install.sh
6 changes: 5 additions & 1 deletion decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ func TestDecoder_Decode(t *testing.T) {
}

if len(result) != len(tc.expected) {
t.Errorf("Expected %d results, but got %d", len(tc.expected), len(result))
t.Errorf(
"Expected %d results, but got %d",
len(tc.expected),
len(result),
)
}

for i, expected := range tc.expected {
Expand Down
18 changes: 14 additions & 4 deletions internal/errors/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ type SelectionNotFound[T any] struct {
// not found.
//
// It is used by the HeaderNotFoundError struct.
func selectionStructHighlight[T any](structPtr *T, selector string) (string, error) {
func selectionStructHighlight[T any](
structPtr *T,
selector string,
) (string, error) {
val := reflect.ValueOf(structPtr)
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
return "", fmt.Errorf("expected struct pointer, got %s", val.Kind())
Expand All @@ -38,9 +41,14 @@ func selectionStructHighlight[T any](structPtr *T, selector string) (string, err
fieldValue := val.Field(i)
skv, err := genStructKeyString(field, selector)
if err != nil {
return "", fmt.Errorf("failed to generate struct key string: %w", err)
return "", fmt.Errorf(
"failed to generate struct key string: %w",
err,
)
}
_, err = result.WriteString(fmt.Sprintf("\t%s %v %s\n", field.Name, fieldValue.Type(), *skv))
_, err = result.WriteString(
fmt.Sprintf("\t%s %v %s\n", field.Name, fieldValue.Type(), *skv),
)
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
Expand Down Expand Up @@ -71,7 +79,9 @@ func genStructKeyString(
key := match[1]
value := match[2]
if strings.Contains(value, highlightSelector) {
_, err = result.WriteString(fmt.Sprintf(" %s:%s", key, "==\""+value+"\"=="))
_, err = result.WriteString(
fmt.Sprintf(" %s:%s", key, "==\""+value+"\"=="),
)
if err != nil {
return nil, fmt.Errorf("failed to write string: %w", err)
}
Expand Down
13 changes: 8 additions & 5 deletions internal/errors/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ import (
// SuperNovaStruct is a test struct
type SuperNovaStruct struct {
Supernova string `json:"Supernova" seltabl:"Supernova" hSel:"tr:nth-child(1) th:nth-child(1)" dSel:"tr td:nth-child(1)" cSel:"$text"`
Year string `json:"Year" seltabl:"Year" hSel:"tr:nth-child(1) th:nth-child(2)" dSel:"tr td:nth-child(2)" cSel:"$text"`
Type string `json:"Type" seltabl:"Type" hSel:"tr:nth-child(1) th:nth-child(3)" dSel:"tr td:nth-child(3)" cSel:"$text"`
Distance string `seltabl:"Distance" hSel:"tr:nth-child(1) th:nth-child(4)" dSel:"tr td:nth-child(4)" json:"Distance" `
Notes string `json:"Notes" seltabl:"Notes" hSel:"tr:nth-child(1) th:nth-child(5)" dSel:"tr td:nth-child(5)"`
Year string `json:"Year" seltabl:"Year" hSel:"tr:nth-child(1) th:nth-child(2)" dSel:"tr td:nth-child(2)" cSel:"$text"`
Type string `json:"Type" seltabl:"Type" hSel:"tr:nth-child(1) th:nth-child(3)" dSel:"tr td:nth-child(3)" cSel:"$text"`
Distance string `json:"Distance" seltabl:"Distance" hSel:"tr:nth-child(1) th:nth-child(4)" dSel:"tr td:nth-child(4)"`
Notes string `json:"Notes" seltabl:"Notes" hSel:"tr:nth-child(1) th:nth-child(5)" dSel:"tr td:nth-child(5)"`
}

// TestStructErrors tests the errors in the package
func TestStructErrors(t *testing.T) {
stc := SuperNovaStruct{}
output, err := selectionStructHighlight(&stc, "tr:nth-child(1) th:nth-child(1)")
output, err := selectionStructHighlight(
&stc,
"tr:nth-child(1) th:nth-child(1)",
)
assert.Nil(t, err)
assert.NotEmpty(t, output)
assert.True(t, strings.Contains(output, "Supernova"))
Expand Down
26 changes: 20 additions & 6 deletions internal/errors/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ type SelectorNotFound[T any] struct {
// not found.
//
// It is used by the HeaderNotFoundError struct.
func selectorStructHighlight[T any](structPtr T, selector string) (string, error) {
func selectorStructHighlight[T any](
structPtr T,
selector string,
) (string, error) {
val := reflect.ValueOf(structPtr)
if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Struct {
return "", fmt.Errorf("expected struct pointer, got %s", val.Kind())
Expand All @@ -38,9 +41,14 @@ func selectorStructHighlight[T any](structPtr T, selector string) (string, error
fieldValue := val.Field(i)
skv, err := GenStructTagString(field, selector)
if err != nil {
return "", fmt.Errorf("failed to generate struct tag string: %w", err)
return "", fmt.Errorf(
"failed to generate struct tag string: %w",
err,
)
}
result.WriteString(fmt.Sprintf("\t%s %v %s\n", field.Name, fieldValue.Type(), *skv))
result.WriteString(
fmt.Sprintf("\t%s %v %s\n", field.Name, fieldValue.Type(), *skv),
)
}
_, err := result.WriteString("}")
if err != nil {
Expand All @@ -54,7 +62,10 @@ func selectorStructHighlight[T any](structPtr T, selector string) (string, error
// for a struct field.
//
// It is used by the HeaderNotFoundError struct.
func GenStructTagString(field reflect.StructField, highlightSelector string) (*string, error) {
func GenStructTagString(
field reflect.StructField,
highlightSelector string,
) (*string, error) {
var result strings.Builder
var err error
result.WriteString("`")
Expand All @@ -66,7 +77,9 @@ func GenStructTagString(field reflect.StructField, highlightSelector string) (*s
key := match[1]
value := match[2]
if strings.Contains(value, highlightSelector) {
_, err = result.WriteString(fmt.Sprintf(" %s:%s", key, "==\""+value+"==\""))
_, err = result.WriteString(
fmt.Sprintf(" %s:%s", key, "==\""+value+"==\""),
)
if err != nil {
return nil, fmt.Errorf("failed to write struct tag: %w", err)
}
Expand All @@ -89,7 +102,8 @@ func GenStructTagString(field reflect.StructField, highlightSelector string) (*s
func (e *SelectorNotFound[T]) Error() string {
structString, err := selectorStructHighlight(e.Struct, e.Selector)
if err != nil {
return fmt.Errorf("failed to generate struct string while reporting that a selector was not found: %w", err).Error()
return fmt.Errorf("failed to generate struct string while reporting that a selector was not found: %w", err).
Error()
}
return fmt.Sprintf(
"selector %s not found for field %s\n%s",
Expand Down
30 changes: 24 additions & 6 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ func TestNewFromString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(tt.args.htmlInput))
doc, err := goquery.NewDocumentFromReader(
strings.NewReader(tt.args.htmlInput),
)
assert.Nil(t, err)
got, err := New[testdata.SuperNovaStruct](doc)
if (err != nil) != tt.wantErr {
t.Errorf("NewFromString() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"NewFromString() error = %v, wantErr %v",
err,
tt.wantErr,
)
return
}
if tt.wantErr {
Expand Down Expand Up @@ -98,11 +104,17 @@ func TestNewFromString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(tt.args.htmlInput))
doc, err := goquery.NewDocumentFromReader(
strings.NewReader(tt.args.htmlInput),
)
assert.Nil(t, err)
got, err := New[testdata.NumberedStruct](doc)
if (err != nil) != tt.wantErr {
t.Errorf("NewFromString() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"NewFromString() error = %v, wantErr %v",
err,
tt.wantErr,
)
return
}
if tt.wantErr {
Expand Down Expand Up @@ -149,11 +161,17 @@ func TestNewFromString(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(tt.args.htmlInput))
doc, err := goquery.NewDocumentFromReader(
strings.NewReader(tt.args.htmlInput),
)
assert.Nil(t, err)
got, err := New[testdata.FixtureStruct](doc)
if (err != nil) != tt.wantErr {
t.Errorf("NewFromString() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"NewFromString() error = %v, wantErr %v",
err,
tt.wantErr,
)
return
}
if tt.wantErr {
Expand Down
56 changes: 46 additions & 10 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,38 @@ func TestSetStructField(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt := tt
t.Parallel()
doc, err := goquery.NewDocumentFromReader(strings.NewReader(tt.cellHTML))
doc, err := goquery.NewDocumentFromReader(
strings.NewReader(tt.cellHTML),
)
if err != nil {
t.Fatalf("failed to create document: %v", err)
}
cellValue := doc.Find("div")
err = SetStructField(tt.structPtr, tt.fieldName, cellValue, tt.selector)
err = SetStructField(
tt.structPtr,
tt.fieldName,
cellValue,
tt.selector,
)
if (err != nil) != tt.wantErr {
t.Errorf("SetStructField() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"SetStructField() error = %v, wantErr %v",
err,
tt.wantErr,
)
return
}
if err == nil {
v := reflect.ValueOf(tt.structPtr).Elem().FieldByName(tt.fieldName).Interface()
v := reflect.ValueOf(tt.structPtr).
Elem().
FieldByName(tt.fieldName).
Interface()
if !reflect.DeepEqual(v, tt.expected) {
t.Errorf("SetStructField() = %v, expected %v", v, tt.expected)
t.Errorf(
"SetStructField() = %v, expected %v",
v,
tt.expected,
)
}
}
})
Expand Down Expand Up @@ -683,20 +701,38 @@ func BenchSetStructField(t *testing.B) {
for _, tt := range Benchs {
t.Run(tt.name, func(t *testing.B) {
tt := tt
doc, err := goquery.NewDocumentFromReader(strings.NewReader(tt.cellHTML))
doc, err := goquery.NewDocumentFromReader(
strings.NewReader(tt.cellHTML),
)
if err != nil {
t.Fatalf("failed to create document: %v", err)
}
cellValue := doc.Find("div")
err = SetStructField(tt.structPtr, tt.fieldName, cellValue, tt.selector)
err = SetStructField(
tt.structPtr,
tt.fieldName,
cellValue,
tt.selector,
)
if (err != nil) != tt.wantErr {
t.Errorf("SetStructField() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"SetStructField() error = %v, wantErr %v",
err,
tt.wantErr,
)
return
}
if err == nil {
v := reflect.ValueOf(tt.structPtr).Elem().FieldByName(tt.fieldName).Interface()
v := reflect.ValueOf(tt.structPtr).
Elem().
FieldByName(tt.fieldName).
Interface()
if !reflect.DeepEqual(v, tt.expected) {
t.Errorf("SetStructField() = %v, expected %v", v, tt.expected)
t.Errorf(
"SetStructField() = %v, expected %v",
v,
tt.expected,
)
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion scripts/makefile.fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

gofmt -w .

goline -w --max-len=79 .
golines -w --max-len=79 .
2 changes: 1 addition & 1 deletion scripts/taskfile.fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ gum spin --spinner dot --title "Formatting Go Files with 'go fmt' in ." --show-o
gofmt -w .

gum spin --spinner dot --title "Formatting Go Files with 'golines' in ." --show-output -- \
goline -w --max-len=79 .
golines -w --max-len=79 .
6 changes: 4 additions & 2 deletions scripts/taskfile.test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# file: makefile.test.sh
# url: https://github.com/conneroisu/seltabl/scripts/makefile.test.sh
# file: taskfile.test.sh
# url: https://github.com/conneroisu/blob/main/seltabl/scripts/taskfile.test.sh
# title: Test Script
# description: This script runs the test for the project.
#
Expand All @@ -11,3 +11,5 @@ gum spin --spinner dot --title "Running Tests" --show-output -- \

gum spin --spinner dot --title "Generating Coverage" --show-output -- \
go test -coverprofile=coverage.out ./...

gocovsh coverage.out
14 changes: 8 additions & 6 deletions test/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ func TestNewFromURLWithRandomData(t *testing.T) {

// httpTestServer sets up a test HTTP server
func httpTestServer(t *testing.T, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(body))
if err != nil {
t.Fatalf("Failed to write response: %v", err)
}
}))
return httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(body))
if err != nil {
t.Fatalf("Failed to write response: %v", err)
}
}),
)
}
Loading

0 comments on commit 0108919

Please sign in to comment.