|
| 1 | +// Copyright 2023 Contributors to the Eclipse Foundation. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: BSD-3-Clause |
| 6 | + |
| 7 | +import 'dart:convert'; |
| 8 | + |
| 9 | +import 'package:dart_wot/scripting_api.dart'; |
| 10 | +import 'package:dart_wot/src/core/codecs/cbor_codec.dart'; |
| 11 | +import 'package:dart_wot/src/core/codecs/json_codec.dart' as json_codec; |
| 12 | +import 'package:dart_wot/src/core/codecs/text_codec.dart'; |
| 13 | +import 'package:test/test.dart'; |
| 14 | + |
| 15 | +void main() { |
| 16 | + group('TextCodec should', () { |
| 17 | + test('convert bytes to values and back', () { |
| 18 | + final textCodec = TextCodec(); |
| 19 | + |
| 20 | + const testValue = 'foo'; |
| 21 | + final testInput = utf8.encode(testValue); |
| 22 | + |
| 23 | + final convertedValue = textCodec.bytesToValue(testInput, null, {}); |
| 24 | + expect(convertedValue?.value, testValue); |
| 25 | + |
| 26 | + final convertedBytes = textCodec.valueToBytes(convertedValue, null, {}); |
| 27 | + |
| 28 | + expect(convertedBytes, testInput); |
| 29 | + |
| 30 | + final convertedNullValue = textCodec.valueToBytes(null, null, {}); |
| 31 | + expect(convertedNullValue, []); |
| 32 | + }); |
| 33 | + |
| 34 | + test('reject unknown charsets', () { |
| 35 | + final textCodec = TextCodec(); |
| 36 | + |
| 37 | + const charsetParameters = {'charset': 'foobar'}; |
| 38 | + |
| 39 | + expect( |
| 40 | + () => |
| 41 | + textCodec.bytesToValue(utf8.encode('foo'), null, charsetParameters), |
| 42 | + throwsFormatException, |
| 43 | + ); |
| 44 | + |
| 45 | + expect( |
| 46 | + () => textCodec.valueToBytes( |
| 47 | + DataSchemaValue.fromNull(), |
| 48 | + null, |
| 49 | + charsetParameters, |
| 50 | + ), |
| 51 | + throwsFormatException, |
| 52 | + ); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + group('JsonCodec should', () { |
| 57 | + test('convert bytes to values and back', () { |
| 58 | + final jsonCodec = json_codec.JsonCodec(); |
| 59 | + |
| 60 | + const testValue = 'foo'; |
| 61 | + final testInput = utf8.encode('"$testValue"'); |
| 62 | + |
| 63 | + final convertedValue = jsonCodec.bytesToValue(testInput, null, {}); |
| 64 | + expect(convertedValue?.value, testValue); |
| 65 | + |
| 66 | + final convertedBytes = jsonCodec.valueToBytes(convertedValue, null, {}); |
| 67 | + |
| 68 | + expect(convertedBytes, testInput); |
| 69 | + |
| 70 | + final convertedNullValue = jsonCodec.valueToBytes(null, null, {}); |
| 71 | + expect(convertedNullValue, []); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + group('CborCodec should', () { |
| 76 | + test('convert bytes to values and back', () { |
| 77 | + final cborCodec = CborCodec(); |
| 78 | + const testValue = { |
| 79 | + 'foo': ['bar', 'baz'], |
| 80 | + }; |
| 81 | + |
| 82 | + final convertedBytes = cborCodec |
| 83 | + .valueToBytes(DataSchemaValue.fromObject(testValue), null, {}); |
| 84 | + |
| 85 | + expect( |
| 86 | + convertedBytes, |
| 87 | + [161, 99, 102, 111, 111, 130, 99, 98, 97, 114, 99, 98, 97, 122], |
| 88 | + ); |
| 89 | + |
| 90 | + final convertedValue = cborCodec.bytesToValue(convertedBytes, null, {}); |
| 91 | + expect(convertedValue?.value, testValue); |
| 92 | + |
| 93 | + final convertedNullValue = cborCodec.valueToBytes(null, null, {}); |
| 94 | + expect(convertedNullValue, []); |
| 95 | + }); |
| 96 | + }); |
| 97 | +} |
0 commit comments