Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: WritableUtils to simplify tests #983

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class PreactViewRenderSpec extends Specification {
void "views can be rendered with basic props and no request"() {
when:
Writable writable = renderer.render("App", TestProps.basic, null)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).orElseThrow()

then:
result.contains("/static/client.preact.js")
Expand All @@ -40,10 +37,7 @@ class PreactViewRenderSpec extends Specification {

when:
Writable writable = renderer.render("App", TestProps.basic, req)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).orElseThrow()

then:
result.contains("/static/client.preact.js")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ class ReactViewRenderSpec extends Specification {
void "views can be rendered with basic props"() {
when:
Writable writable = renderer.render("App", TestProps.basic, null)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).orElseThrow()

then:
result.contains("Hello there")
Expand All @@ -38,10 +35,7 @@ class ReactViewRenderSpec extends Specification {

when:
Writable writable = renderer.render("App", TestProps.basic, req)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).orElseThrow()

then:
result.contains("/static/client.js")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class SandboxReactRenderSpec extends Specification {
void "views can be rendered with sandboxing enabled"() {
when:
Writable writable = renderer.render("App", TestProps.basic, null)
String resultAsString = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String resultAsString = WritableUtils.writableToString(writable).orElseThrow()

String dataJSON = resultAsString.find(~/var Micronaut = (\{[^;]+});/).replace("var Micronaut = ", "")
def data = new JsonSlurper().parseText(dataJSON)
Expand All @@ -37,10 +34,7 @@ class SandboxReactRenderSpec extends Specification {
void "host types are inaccessible with the sandbox enabled"() {
when:
Writable writable = renderer.render("App", TestProps.triggerSandbox, null)
new StringWriter().with {
writable.writeTo(it)
it.toString()
}
WritableUtils.writableToString(writable)

then:
def t = thrown(MessageBodyException)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.micronaut.views.react;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.io.Writable;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Optional;

public final class WritableUtils {
private WritableUtils() {
}

@NonNull
public static Optional<String> writableToString(@NonNull Writable writable) {
if (writable == null) {
return Optional.empty();
}
try {
StringWriter stringWriter = new StringWriter();
writable.writeTo(stringWriter);
return Optional.of(stringWriter.toString());
} catch (IOException e) {
return Optional.empty();
}
}
}
Loading