Skip to content

Commit

Permalink
test: add tests for JSON schema validation of values
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Mar 21, 2022
1 parent 529d6f4 commit f1b9584
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions test/content_serdes_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022 The NAMIB Project Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause

import 'package:dart_wot/src/core/content.dart';
import 'package:dart_wot/src/core/content_serdes.dart';
import 'package:dart_wot/src/definitions/data_schema.dart';
import 'package:test/test.dart';

Content _getTestContent() {
return Content("application/json", Stream<List<int>>.value('42'.codeUnits));
}

void main() {
group('Content Serdes Tests', () {
setUp(() {
// Additional setup goes here.
});
});

test('Content Validation', () async {
final contentSerdes = ContentSerdes();

final testContent1 = _getTestContent();
final successfulSchema =
DataSchema.fromJson(<String, dynamic>{"type": "number"});

expect(
await contentSerdes.contentToValue(testContent1, successfulSchema), 42);

final testContent2 = _getTestContent();
final failingSchema =
DataSchema.fromJson(<String, dynamic>{"type": "string"});

expect(contentSerdes.contentToValue(testContent2, failingSchema),
throwsA(TypeMatcher<ContentSerdesException>()));

expect(
() =>
contentSerdes.valueToContent(42, failingSchema, "application/json"),
throwsA(TypeMatcher<ContentSerdesException>()));
});
}

0 comments on commit f1b9584

Please sign in to comment.