Skip to content

Commit

Permalink
fix(InteractionOutput): adjust implementation to spec
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 27, 2024
1 parent 457a113 commit 115b0ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/src/core/implementation/interaction_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "dart:typed_data";

import "../definitions/data_schema.dart";
import "../definitions/form.dart";
import "../exceptions.dart";
import "../scripting_api.dart" as scripting_api;
import "content.dart";
import "content_serdes.dart";
Expand Down Expand Up @@ -38,6 +39,10 @@ class InteractionOutput implements scripting_api.InteractionOutput {

@override
Future<ByteBuffer> arrayBuffer() async {
if (dataUsed) {
throw NotReadableException("Data has already been read");
}

_dataUsed = true;
return _content.byteBuffer;
}
Expand All @@ -52,8 +57,11 @@ class InteractionOutput implements scripting_api.InteractionOutput {
return existingValue.value;
}

// TODO(JKRhb): Should a NotReadableError be thrown if schema is null?
// C.f. https://w3c.github.io/wot-scripting-api/#the-value-function
if (schema == null) {
throw NotReadableException(
"Can't convert data to a value because no DataSchema is present.",
);
}

final value = await _contentSerdes.contentToValue(
_content,
Expand Down
14 changes: 12 additions & 2 deletions test/core/interaction_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ void main() {
]),
);

final interactionOutput = InteractionOutput(content, contentSerdes);
final interactionOutput = InteractionOutput(
content,
contentSerdes,
null,
const DataSchema(),
);

final value1 = await interactionOutput.value();
expect(value1, inputValue);
Expand All @@ -46,7 +51,12 @@ void main() {
]),
);

final interactionOutput = InteractionOutput(content, contentSerdes);
final interactionOutput = InteractionOutput(
content,
contentSerdes,
null,
const DataSchema(),
);

final value1 = await interactionOutput.value();
expect(value1, inputValue);
Expand Down

0 comments on commit 115b0ff

Please sign in to comment.