|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:onesignal_flutter/src/utils.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('JSONStringRepresentable', () { |
| 6 | + test('convertToJsonString formats simple map correctly', () { |
| 7 | + final testObj = TestJSONStringRepresentable(); |
| 8 | + final map = {'key': 'value', 'number': 42}; |
| 9 | + |
| 10 | + final result = testObj.convertToJsonString(map); |
| 11 | + |
| 12 | + expect(result, contains('"key": "value"')); |
| 13 | + expect(result, contains('"number": 42')); |
| 14 | + expect(result, contains('\n')); |
| 15 | + }); |
| 16 | + |
| 17 | + test('convertToJsonString formats nested map correctly', () { |
| 18 | + final testObj = TestJSONStringRepresentable(); |
| 19 | + final map = { |
| 20 | + 'outer': { |
| 21 | + 'inner': 'value', |
| 22 | + 'nested': {'deep': 'data'} |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + final result = testObj.convertToJsonString(map); |
| 27 | + |
| 28 | + expect(result, contains('"outer"')); |
| 29 | + expect(result, contains('"inner": "value"')); |
| 30 | + expect(result, contains('"nested"')); |
| 31 | + expect(result, contains('"deep": "data"')); |
| 32 | + }); |
| 33 | + |
| 34 | + test('convertToJsonString formats list correctly', () { |
| 35 | + final testObj = TestJSONStringRepresentable(); |
| 36 | + final map = { |
| 37 | + 'items': [1, 2, 3], |
| 38 | + 'strings': ['a', 'b', 'c'] |
| 39 | + }; |
| 40 | + |
| 41 | + final result = testObj.convertToJsonString(map); |
| 42 | + |
| 43 | + expect(result, contains('"items"')); |
| 44 | + expect(result, contains('"strings"')); |
| 45 | + expect(result, contains('[')); |
| 46 | + expect(result, contains(']')); |
| 47 | + }); |
| 48 | + |
| 49 | + test('convertToJsonString handles null map', () { |
| 50 | + final testObj = TestJSONStringRepresentable(); |
| 51 | + |
| 52 | + final result = testObj.convertToJsonString(null); |
| 53 | + |
| 54 | + expect(result, equals('null')); |
| 55 | + }); |
| 56 | + |
| 57 | + test('convertToJsonString handles empty map', () { |
| 58 | + final testObj = TestJSONStringRepresentable(); |
| 59 | + final map = <String, dynamic>{}; |
| 60 | + |
| 61 | + final result = testObj.convertToJsonString(map); |
| 62 | + |
| 63 | + expect(result, equals('{}')); |
| 64 | + }); |
| 65 | + |
| 66 | + test('convertToJsonString removes escaped newlines', () { |
| 67 | + final testObj = TestJSONStringRepresentable(); |
| 68 | + final map = {'text': 'line1\nline2'}; |
| 69 | + |
| 70 | + final result = testObj.convertToJsonString(map); |
| 71 | + |
| 72 | + expect(result, contains('"text": "line1\nline2"')); |
| 73 | + }); |
| 74 | + |
| 75 | + test('convertToJsonString removes escaped backslashes', () { |
| 76 | + final testObj = TestJSONStringRepresentable(); |
| 77 | + final map = {'path': 'folder/file'}; |
| 78 | + |
| 79 | + final result = testObj.convertToJsonString(map); |
| 80 | + |
| 81 | + expect(result, contains('"path": "folder/file"')); |
| 82 | + }); |
| 83 | + |
| 84 | + test('convertToJsonString formats with proper indentation', () { |
| 85 | + final testObj = TestJSONStringRepresentable(); |
| 86 | + final map = { |
| 87 | + 'level1': {'level2': 'value'} |
| 88 | + }; |
| 89 | + |
| 90 | + final result = testObj.convertToJsonString(map); |
| 91 | + |
| 92 | + expect(result, contains(' ')); |
| 93 | + final lines = result.split('\n'); |
| 94 | + expect(lines.length, greaterThan(1)); |
| 95 | + }); |
| 96 | + |
| 97 | + test('convertToJsonString handles boolean values', () { |
| 98 | + final testObj = TestJSONStringRepresentable(); |
| 99 | + final map = {'isTrue': true, 'isFalse': false}; |
| 100 | + |
| 101 | + final result = testObj.convertToJsonString(map); |
| 102 | + |
| 103 | + expect(result, contains('"isTrue": true')); |
| 104 | + expect(result, contains('"isFalse": false')); |
| 105 | + }); |
| 106 | + |
| 107 | + test('convertToJsonString handles numeric values', () { |
| 108 | + final testObj = TestJSONStringRepresentable(); |
| 109 | + final map = {'integer': 42, 'double': 3.14, 'negative': -10}; |
| 110 | + |
| 111 | + final result = testObj.convertToJsonString(map); |
| 112 | + |
| 113 | + expect(result, contains('42')); |
| 114 | + expect(result, contains('3.14')); |
| 115 | + expect(result, contains('-10')); |
| 116 | + }); |
| 117 | + |
| 118 | + test('convertToJsonString handles mixed types', () { |
| 119 | + final testObj = TestJSONStringRepresentable(); |
| 120 | + final map = { |
| 121 | + 'string': 'text', |
| 122 | + 'number': 123, |
| 123 | + 'bool': true, |
| 124 | + 'null': null, |
| 125 | + 'list': [1, 2], |
| 126 | + 'map': {'nested': 'value'} |
| 127 | + }; |
| 128 | + |
| 129 | + final result = testObj.convertToJsonString(map); |
| 130 | + |
| 131 | + expect(result, contains('"string": "text"')); |
| 132 | + expect(result, contains('"number": 123')); |
| 133 | + expect(result, contains('"bool": true')); |
| 134 | + expect(result, contains('"null": null')); |
| 135 | + expect(result, contains('"list":')); |
| 136 | + expect(result, contains('"map":')); |
| 137 | + expect(result, contains('"nested": "value"')); |
| 138 | + expect(result, contains('{')); |
| 139 | + expect(result, contains('}')); |
| 140 | + }); |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +class TestJSONStringRepresentable extends JSONStringRepresentable { |
| 145 | + @override |
| 146 | + String jsonRepresentation() { |
| 147 | + return convertToJsonString({'test': 'data'}); |
| 148 | + } |
| 149 | +} |
0 commit comments