Skip to content

Commit

Permalink
@ F r remove dart mirror impl, added flutter sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed Jun 29, 2024
1 parent 96f3b72 commit 50f566d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 35 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include: package:sizzle_lints/sizzle_lints.yaml

analyzer:
errors:
comment_references: ignore
depend_on_referenced_packages: ignore
invalid_use_of_visible_for_testing_member: ignore
linter:
Expand Down
1 change: 0 additions & 1 deletion lib/approval_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:mirrors';

import 'package:diff_match_patch2/diff_match_patch.dart';
import 'package:talker/talker.dart';
Expand Down
52 changes: 24 additions & 28 deletions lib/src/core/utils/converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ part of '../../../approval_tests.dart';

class ApprovalConverter {
static String convert(String jsonString) {
// Decode the JSON string to a dynamic object
final decodedJson = jsonDecode(jsonString);
// Use JsonEncoder with custom indentation
const encoder = JsonEncoder.withIndent(' ');
// Convert the dynamic object back to a string with indentation
return encoder.convert(decodedJson);
}

/// `encodeReflectively` is a method that encodes an object to JSON format using reflection.
static String encodeReflectively(
Object? object, {
bool includeClassName = false,
Expand All @@ -20,51 +16,51 @@ class ApprovalConverter {
}

if (object is List) {
// Handle lists of objects by iterating through them
return '[${object.map((item) => encodeReflectively(item)).join(', ')}]';
}

if (object is Map) {
// Handle maps directly
return '{${object.entries.map((e) => '"${e.key}": ${encodeReflectively(e.value)}').join(', ')}}';
}

// Reflect the object
final InstanceMirror mirror = reflect(object);
final ClassMirror classMirror = mirror.type;

if (object is String) {
// JSON encode strings with proper escaping
return '"${object.replaceAll('"', '\\"')}"';
} else if (object is num || object is bool) {
// Numbers and booleans can be added directly
return object.toString();
}

// Handling custom objects
final Map<String, String> jsonMap = {};
// Attempt to convert custom objects to a map
final Map<String, dynamic>? jsonMap = _convertObjectToMap(object);

// Iterate over the instance variables of the class
for (final value in classMirror.declarations.values) {
if (value is VariableMirror && !value.isStatic) {
final String key = MirrorSystem.getName(value.simpleName);
final reflectee = mirror.getField(value.simpleName).reflectee;
jsonMap[key] = encodeReflectively(reflectee);
}
if (jsonMap == null) {
throw UnsupportedError('Cannot serialize object of type ${object.runtimeType}');
}

// Format the map into JSON
final String jsonBody = jsonMap.entries
.map((entry) => '"${entry.key}": ${entry.value}')
.join(', ');
final String jsonBody =
jsonMap.entries.map((entry) => '"${entry.key}": ${encodeReflectively(entry.value)}').join(', ');

if (includeClassName) {
final String className = MirrorSystem.getName(classMirror.simpleName);
final String capitalizedClassName =
'${className[0].toLowerCase()}${className.substring(1)}';
final String className = object.runtimeType.toString();
final String capitalizedClassName = '${className[0].toLowerCase()}${className.substring(1)}';
return '{"$capitalizedClassName": {$jsonBody}}';
}

return '{$jsonBody}';
}

// Function to dynamically convert an object to a map if it has a toJson method
static Map<String, dynamic>? _convertObjectToMap(Object object) {
try {
// Check if the object has a `toJson` method
// ignore: avoid_dynamic_calls
final jsonMap = (object as dynamic).toJson();
if (jsonMap is Map<String, dynamic>) {
return jsonMap;
}
} catch (e) {
// If the object doesn't have a `toJson` method, return null
return null;
}
return null;
}
}
12 changes: 9 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
test: ^1.25.5
talker: ^4.2.0
flutter:
sdk: flutter
flutter_test:
sdk: flutter

test: ^1.25.2
talker: ^4.3.2
diff_match_patch2: ^0.5.0
test_api: ^0.7.1
test_api: ^0.7.0


dev_dependencies:
sizzle_lints: ^2.0.2
4 changes: 1 addition & 3 deletions test/approval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import 'dart:io';
import 'package:approval_tests/approval_tests.dart';
import 'package:test/test.dart';

import 'groups/approval_tests.dart' as verify_tests;
import 'groups/diff_tools_tests.dart' as diff_tools_tests;
import 'groups/exception_tests.dart' as exception_tests;
import 'groups/minor_tests.dart' as minor_tests;
import 'groups/verify_tests.dart' as verify_tests;

import 'models/item.dart';
import 'queries/db_request_query.dart';

part 'constants/lines.dart';

part 'mock/testable_file_path_extractor.dart';
part 'mock/testable_platform_wrapper.dart';
part 'utils/helper.dart';
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions test/models/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class JsonItem {
required this.subItem,
required this.anotherItem,
});

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'subItem': subItem,
'anotherItem': anotherItem,
};
}

/// Sub item class for testing
Expand All @@ -24,6 +31,12 @@ class SubItem {
required this.name,
required this.anotherItems,
});

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'anotherItems': anotherItems.map((e) => e.toJson()).toList(),
};
}

/// Another item class for testing
Expand All @@ -32,4 +45,9 @@ class AnotherItem {
final String name;

const AnotherItem({required this.id, required this.name});

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
};
}

0 comments on commit 50f566d

Please sign in to comment.