-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: normalize input types of CSS middleware (#152)
- Loading branch information
Showing
6 changed files
with
177 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="red_050e"> | ||
Red text | ||
</div> |
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,81 @@ | ||
package testcssmiddleware | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"net/http/httptest" | ||
"strings" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/a-h/htmlformat" | ||
"github.com/a-h/templ" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
//go:embed expected.html | ||
var expected string | ||
|
||
var expectedCSS = `.red_050e{color:red;} | ||
` | ||
|
||
func Test(t *testing.T) { | ||
var errs []error | ||
var wg sync.WaitGroup | ||
wg.Add(3) | ||
|
||
// Format the expected value. | ||
go func() { | ||
defer wg.Done() | ||
e := new(strings.Builder) | ||
err := htmlformat.Fragment(e, strings.NewReader(expected)) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("expected html formatting error: %w", err)) | ||
} | ||
expected = e.String() | ||
}() | ||
|
||
component := render("Red text") | ||
h := templ.Handler(component) | ||
cssmw := templ.NewCSSMiddleware(h, red()) | ||
|
||
// Create the actual value. | ||
var actual string | ||
go func() { | ||
defer wg.Done() | ||
|
||
w := httptest.NewRecorder() | ||
cssmw.ServeHTTP(w, httptest.NewRequest("GET", "/", nil)) | ||
|
||
a := new(strings.Builder) | ||
err := htmlformat.Fragment(a, w.Body) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("actual html formatting error: %w", err)) | ||
} | ||
actual = a.String() | ||
}() | ||
|
||
var actualCSS string | ||
go func() { | ||
defer wg.Done() | ||
|
||
w := httptest.NewRecorder() | ||
cssmw.ServeHTTP(w, httptest.NewRequest("GET", "/styles/templ.css", nil)) | ||
|
||
a := new(strings.Builder) | ||
err := htmlformat.Fragment(a, w.Body) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("actual html formatting error: %w", err)) | ||
} | ||
actualCSS = a.String() | ||
}() | ||
|
||
wg.Wait() | ||
|
||
if diff := cmp.Diff(expected, actual); diff != "" { | ||
t.Error(diff) | ||
} | ||
if diff := cmp.Diff(expectedCSS, actualCSS); diff != "" { | ||
t.Error(diff) | ||
} | ||
} |
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,10 @@ | ||
package testcssmiddleware | ||
|
||
css red() { | ||
color: red; | ||
} | ||
|
||
templ render(s string) { | ||
<div class={ red }>{ s }</div> | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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