|
| 1 | +import 'dart:convert' show Encoding, utf8; |
| 2 | + |
| 3 | +import 'package:qs_dart/src/enums/format.dart'; |
| 4 | +import 'package:qs_dart/src/enums/list_format.dart'; |
| 5 | +import 'package:qs_dart/src/models/encode_config.dart'; |
| 6 | +import 'package:qs_dart/src/models/encode_options.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +String _encoder(dynamic value, {Encoding? charset, Format? format}) => |
| 10 | + value.toString(); |
| 11 | +String _serializeDate(DateTime date) => date.toIso8601String(); |
| 12 | +int _sort(dynamic a, dynamic b) => 0; |
| 13 | + |
| 14 | +void main() { |
| 15 | + group('EncodeConfig', () { |
| 16 | + test('copyWith returns same instance when unchanged', () { |
| 17 | + final config = _baseConfig(); |
| 18 | + |
| 19 | + expect(identical(config.copyWith(), config), isTrue); |
| 20 | + }); |
| 21 | + |
| 22 | + test('withEncoder delegates to copyWith and can clear encoder', () { |
| 23 | + final config = _baseConfig(encoder: _encoder); |
| 24 | + |
| 25 | + final cleared = config.withEncoder(null); |
| 26 | + |
| 27 | + expect(cleared, equals(config.copyWith(encoder: null))); |
| 28 | + expect(cleared.encoder, isNull); |
| 29 | + expect(identical(cleared, config), isFalse); |
| 30 | + }); |
| 31 | + |
| 32 | + test('copyWith can clear nullable fields', () { |
| 33 | + final config = _baseConfig( |
| 34 | + encoder: _encoder, |
| 35 | + serializeDate: _serializeDate, |
| 36 | + sort: _sort, |
| 37 | + filter: const ['a', 'b'], |
| 38 | + ); |
| 39 | + |
| 40 | + final copy = config.copyWith( |
| 41 | + encoder: null, |
| 42 | + serializeDate: null, |
| 43 | + sort: null, |
| 44 | + filter: null, |
| 45 | + ); |
| 46 | + |
| 47 | + expect(copy.encoder, isNull); |
| 48 | + expect(copy.serializeDate, isNull); |
| 49 | + expect(copy.sort, isNull); |
| 50 | + expect(copy.filter, isNull); |
| 51 | + expect(copy.generateArrayPrefix, same(config.generateArrayPrefix)); |
| 52 | + expect(copy.formatter, same(config.formatter)); |
| 53 | + }); |
| 54 | + |
| 55 | + test('copyWith treats const Object filter as explicit override', () { |
| 56 | + const marker = Object(); |
| 57 | + final config = _baseConfig(filter: null); |
| 58 | + |
| 59 | + final copy = config.copyWith(filter: marker); |
| 60 | + |
| 61 | + expect(identical(copy.filter, marker), isTrue); |
| 62 | + }); |
| 63 | + |
| 64 | + test('equatable props compare all fields', () { |
| 65 | + final a = _baseConfig(); |
| 66 | + final b = _baseConfig(); |
| 67 | + |
| 68 | + expect(a, equals(b)); |
| 69 | + expect(a.copyWith(skipNulls: true), isNot(equals(b))); |
| 70 | + }); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +EncodeConfig _baseConfig({ |
| 75 | + ListFormatGenerator? generateArrayPrefix, |
| 76 | + bool commaRoundTrip = false, |
| 77 | + bool commaCompactNulls = false, |
| 78 | + bool allowEmptyLists = false, |
| 79 | + bool strictNullHandling = false, |
| 80 | + bool skipNulls = false, |
| 81 | + bool encodeDotInKeys = false, |
| 82 | + Encoder? encoder, |
| 83 | + DateSerializer? serializeDate, |
| 84 | + Sorter? sort, |
| 85 | + dynamic filter, |
| 86 | + bool allowDots = false, |
| 87 | + Format format = Format.rfc3986, |
| 88 | + Formatter? formatter, |
| 89 | + bool encodeValuesOnly = false, |
| 90 | +}) { |
| 91 | + return EncodeConfig( |
| 92 | + generateArrayPrefix: generateArrayPrefix ?? ListFormat.indices.generator, |
| 93 | + commaRoundTrip: commaRoundTrip, |
| 94 | + commaCompactNulls: commaCompactNulls, |
| 95 | + allowEmptyLists: allowEmptyLists, |
| 96 | + strictNullHandling: strictNullHandling, |
| 97 | + skipNulls: skipNulls, |
| 98 | + encodeDotInKeys: encodeDotInKeys, |
| 99 | + encoder: encoder, |
| 100 | + serializeDate: serializeDate, |
| 101 | + sort: sort, |
| 102 | + filter: filter, |
| 103 | + allowDots: allowDots, |
| 104 | + format: format, |
| 105 | + formatter: formatter ?? format.formatter, |
| 106 | + encodeValuesOnly: encodeValuesOnly, |
| 107 | + charset: utf8, |
| 108 | + ); |
| 109 | +} |
0 commit comments