Skip to content

Commit

Permalink
[objective_c] Rename NSString.toString() to .toDartString() (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe authored Jan 19, 2025
1 parent 2d3fc60 commit 962ab98
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 58 deletions.
12 changes: 7 additions & 5 deletions pkgs/ffigen/test/native_objc_test/bad_override_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';
import '../test_utils.dart';
import 'bad_override_bindings.dart';
Expand Down Expand Up @@ -53,10 +54,10 @@ void main() {
// contravariant.
// https://github.com/dart-lang/native/issues/1220
Polygon parentResult = BadOverrideParent.new1().contravariantReturn();
expect(parentResult.name().toString(), 'Rectangle');
expect(parentResult.name().toDartString(), 'Rectangle');

Polygon childResult = BadOverrideChild.new1().contravariantReturn();
expect(childResult.name().toString(), 'Triangle');
expect(childResult.name().toDartString(), 'Triangle');
});

test('Covariant args', () {
Expand All @@ -67,11 +68,12 @@ void main() {
final triangle = Triangle.new1();

var parent = BadOverrideParent.new1();
expect(parent.covariantArg_(square).toString(), 'Polygon: Square');
expect(parent.covariantArg_(triangle).toString(), 'Polygon: Triangle');
expect(parent.covariantArg_(square).toDartString(), 'Polygon: Square');
expect(
parent.covariantArg_(triangle).toDartString(), 'Polygon: Triangle');

parent = BadOverrideChild.new1();
expect(parent.covariantArg_(square).toString(), 'Rectangle: Square');
expect(parent.covariantArg_(square).toDartString(), 'Rectangle: Square');
expect(() => parent.covariantArg_(triangle), throwsA(isA<TypeError>()));
});
});
Expand Down
10 changes: 5 additions & 5 deletions pkgs/ffigen/test/native_objc_test/block_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,16 @@ void main() {
test('Nullable string block', () {
// Regression test for https://github.com/dart-lang/native/issues/1537.
final block = NullableStringBlock.fromFunction(
(NSString? x) => '$x Cat'.toNSString());
(NSString? x) => '${x?.toDartString()} Cat'.toNSString());

final result1 = block('Dog'.toNSString());
expect(result1.toString(), 'Dog Cat');
expect(result1?.toDartString(), 'Dog Cat');

final result2 = block(null);
expect(result2.toString(), 'null Cat');
expect(result2?.toDartString(), 'null Cat');

final result3 = BlockTester.callNullableStringBlock_(block);
expect(result3.toString(), 'Lizard Cat');
expect(result3?.toDartString(), 'Lizard Cat');
});

test('Object listener block', () async {
Expand Down Expand Up @@ -345,7 +345,7 @@ void main() {
test('NSString listener block', () async {
final hasRun = Completer<void>();
final block = NSStringListenerBlock.listener((NSString s) {
expect(s.toString(), "Foo 123");
expect(s.toDartString(), "Foo 123");
hasRun.complete();
});

Expand Down
6 changes: 3 additions & 3 deletions pkgs/ffigen/test/native_objc_test/category_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ void main() {
test('Category on built-in type', () {
final str = 'Hello'.toNSString();

expect(str.method().toString(), 'HelloWorld!');
expect(InterfaceOnBuiltInType.staticMethod().method().toString(),
expect(str.method().toDartString(), 'HelloWorld!');
expect(InterfaceOnBuiltInType.staticMethod().method().toDartString(),
'GoodbyeWorld!');

NSString str2 = str.instancetypeMethod();
expect(str2.toString(), 'Hello');
expect(str2.toDartString(), 'Hello');
});
});
}
4 changes: 2 additions & 2 deletions pkgs/ffigen/test/native_objc_test/global_native_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ void main() {
});

test('Global string', () {
expect(globalString.toString(), 'Hello World');
expect(globalString.toDartString(), 'Hello World');
globalString = 'Something else'.toNSString();
expect(globalString.toString(), 'Something else');
expect(globalString.toDartString(), 'Something else');
globalString = 'Hello World'.toNSString();
});

Expand Down
4 changes: 2 additions & 2 deletions pkgs/ffigen/test/native_objc_test/global_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ void main() {
});

test('Global string', () {
expect(lib.globalString.toString(), 'Hello World');
expect(lib.globalString.toDartString(), 'Hello World');
lib.globalString = 'Something else'.toNSString();
expect(lib.globalString.toString(), 'Something else');
expect(lib.globalString.toDartString(), 'Something else');
lib.globalString = 'Hello World'.toNSString();
});

Expand Down
3 changes: 2 additions & 1 deletion pkgs/ffigen/test/native_objc_test/nullable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void main() {
test('Nullable typealias', () {
// Regression test for https://github.com/dart-lang/native/issues/1701
expect(NullableInterface.returnNullableAlias_(true), isNull);
expect(NullableInterface.returnNullableAlias_(false)?.toString(), "Hi");
expect(
NullableInterface.returnNullableAlias_(false)?.toDartString(), "Hi");
});
});
}
29 changes: 15 additions & 14 deletions pkgs/ffigen/test/native_objc_test/protocol_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(protocolImpl);
expect(result.toString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14');
expect(
result.toDartString(), 'ObjCProtocolImpl: Hello from ObjC: 3.14');

// Optional instance method.
final intResult = consumer.callOptionalMethod_(protocolImpl);
Expand All @@ -57,7 +58,7 @@ void main() {
// Required instance method.
final result =
protocolImpl.instanceMethod_withDouble_("abc".toNSString(), 123);
expect(result.toString(), 'ObjCProtocolImpl: abc: 123.00');
expect(result.toDartString(), 'ObjCProtocolImpl: abc: 123.00');

// Optional instance method.
final structPtr = calloc<SomeStruct>();
Expand Down Expand Up @@ -110,7 +111,7 @@ void main() {

final myProtocol = MyProtocol.implement(
instanceMethod_withDouble_: (NSString s, double x) {
return 'MyProtocol: $s: $x'.toNSString();
return 'MyProtocol: ${s.toDartString()}: $x'.toNSString();
},
optionalMethod_: (SomeStruct s) {
return s.y - s.x;
Expand All @@ -119,7 +120,7 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(myProtocol);
expect(result.toString(), 'MyProtocol: Hello from ObjC: 3.14');
expect(result.toDartString(), 'MyProtocol: Hello from ObjC: 3.14');

// Optional instance method.
final intResult = consumer.callOptionalMethod_(myProtocol);
Expand All @@ -132,7 +133,7 @@ void main() {
final protocolBuilder = ObjCProtocolBuilder();
MyProtocol.addToBuilder(protocolBuilder,
instanceMethod_withDouble_: (NSString s, double x) {
return 'ProtocolBuilder: $s: $x'.toNSString();
return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString();
});
SecondaryProtocol.addToBuilder(protocolBuilder,
otherMethod_b_c_d_: (int a, int b, int c, int d) {
Expand All @@ -142,7 +143,7 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(protocolImpl);
expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14');

// Required instance method from secondary protocol.
final otherIntResult = consumer.callOtherMethod_(protocolImpl);
Expand All @@ -155,7 +156,7 @@ void main() {
final protocolBuilder = ObjCProtocolBuilder();
MyProtocol.instanceMethod_withDouble_.implement(protocolBuilder,
(NSString s, double x) {
return 'ProtocolBuilder: $s: $x'.toNSString();
return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString();
});
SecondaryProtocol.otherMethod_b_c_d_.implement(protocolBuilder,
(int a, int b, int c, int d) {
Expand All @@ -165,7 +166,7 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(protocolImpl);
expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14');

// Required instance method from secondary protocol.
final otherIntResult = consumer.callOtherMethod_(protocolImpl);
Expand All @@ -192,7 +193,7 @@ void main() {
final listenerCompleter = Completer<int>();
final myProtocol = MyProtocol.implementAsListener(
instanceMethod_withDouble_: (NSString s, double x) {
return 'MyProtocol: $s: $x'.toNSString();
return 'MyProtocol: ${s.toDartString()}: $x'.toNSString();
},
optionalMethod_: (SomeStruct s) {
return s.y - s.x;
Expand All @@ -204,7 +205,7 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(myProtocol);
expect(result.toString(), 'MyProtocol: Hello from ObjC: 3.14');
expect(result.toDartString(), 'MyProtocol: Hello from ObjC: 3.14');

// Optional instance method.
final intResult = consumer.callOptionalMethod_(myProtocol);
Expand All @@ -223,7 +224,7 @@ void main() {
MyProtocol.addToBuilderAsListener(
protocolBuilder,
instanceMethod_withDouble_: (NSString s, double x) {
return 'ProtocolBuilder: $s: $x'.toNSString();
return 'ProtocolBuilder: ${s.toDartString()}: $x'.toNSString();
},
voidMethod_: (int x) {
listenerCompleter.complete(x);
Expand All @@ -237,7 +238,7 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(protocolImpl);
expect(result.toString(), 'ProtocolBuilder: Hello from ObjC: 3.14');
expect(result.toDartString(), 'ProtocolBuilder: Hello from ObjC: 3.14');

// Required instance method from secondary protocol.
final otherIntResult = consumer.callOtherMethod_(protocolImpl);
Expand Down Expand Up @@ -324,7 +325,7 @@ void main() {
isRequired: true, isInstanceMethod: true)!;
final block = InstanceMethodBlock.fromFunction(
(Pointer<Void> p, NSString s, double x) {
return 'DartProxy: $s: $x'.toNSString();
return 'DartProxy: ${s.toDartString()}: $x'.toNSString();
});
proxyBuilder.implementMethod_withSignature_andBlock_(
sel, signature, block.ref.pointer.cast());
Expand Down Expand Up @@ -354,7 +355,7 @@ void main() {

// Required instance method.
final result = consumer.callInstanceMethod_(proxy);
expect(result.toString(), "DartProxy: Hello from ObjC: 3.14");
expect(result.toDartString(), "DartProxy: Hello from ObjC: 3.14");

// Optional instance method.
final intResult = consumer.callOptionalMethod_(proxy);
Expand Down
3 changes: 2 additions & 1 deletion pkgs/ffigen/test/native_objc_test/rename_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import 'dart:ffi';
import 'dart:io';

import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';
import '../test_utils.dart';
import 'rename_bindings.dart';
Expand Down Expand Up @@ -36,7 +37,7 @@ void main() {

renamed.property = 123;
expect(renamed.toString(), "Instance of 'Renamed'");
expect(renamed.toString1().toString(), "123");
expect(renamed.toString1().toDartString(), "123");
});

test('Method with the same name as a type', () {
Expand Down
10 changes: 5 additions & 5 deletions pkgs/ffigen/test/native_objc_test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ void main() {
test('NSString to/from Dart string [$s]', () {
final ns1 = NSString(s);
expect(ns1.length, s.length);
expect(ns1.toString().length, s.length);
expect(ns1.toString(), s);
expect(ns1.toDartString().length, s.length);
expect(ns1.toDartString(), s);

final ns2 = s.toNSString();
expect(ns2.length, s.length);
expect(ns2.toString().length, s.length);
expect(ns2.toString(), s);
expect(ns2.toDartString().length, s.length);
expect(ns2.toDartString(), s);
});
}

Expand All @@ -46,7 +46,7 @@ void main() {

final str3 = StringUtil.strConcat_with_(str1, str2);
expect(str3.length, 11);
expect(str3.toString(), "HelloWorld!");
expect(str3.toDartString(), "HelloWorld!");
});
});
}
2 changes: 2 additions & 0 deletions pkgs/objective_c/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 5.0.0-wip

- __Breaking change__: Rename the `NSString` to `String` conversion method from
`toString()` to `toDartString()`.
- Add various ObjC categories (extension methods) to the built in classes.
- Make all visible API types public.

Expand Down
9 changes: 9 additions & 0 deletions pkgs/objective_c/lib/src/ns_string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:ffi/ffi.dart';
import 'objective_c_bindings_generated.dart';

extension StringToNSString on String {
NSString toNSString() => NSString(this);
}

extension NSStringToString on NSString {
String toDartString() {
final data =
dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */);
return data!.bytes.cast<Utf16>().toDartString(length: length);
}
}
7 changes: 0 additions & 7 deletions pkgs/objective_c/lib/src/objective_c_bindings_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7541,13 +7541,6 @@ class NSString extends NSObject {
return nsstr;
}

@override
String toString() {
final data =
dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */);
return data!.bytes.cast<pkg_ffi.Utf16>().toDartString(length: length);
}

NSString._(ffi.Pointer<objc.ObjCObject> pointer,
{bool retain = false, bool release = false})
: super.castFromPointer(pointer, retain: retain, release: release);
Expand Down
4 changes: 2 additions & 2 deletions pkgs/objective_c/test/ns_input_stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ void main() {
expect(
error2,
isA<NSError>()
.having((e) => e.localizedDescription.toString(),
.having((e) => e.localizedDescription.toDartString(),
'localizedDescription', contains('some exception message'))
.having((e) => e.domain.toString(), 'domain', 'DartError'));
.having((e) => e.domain.toDartString(), 'domain', 'DartError'));
});

group('delegate', () {
Expand Down
8 changes: 4 additions & 4 deletions pkgs/objective_c/test/nsstring_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ void main() {
test('NSString to/from Dart string [$s]', () {
final ns1 = NSString(s);
expect(ns1.length, s.length);
expect(ns1.toString().length, s.length);
expect(ns1.toString(), s);
expect(ns1.toDartString().length, s.length);
expect(ns1.toDartString(), s);

final ns2 = s.toNSString();
expect(ns2.length, s.length);
expect(ns2.toString().length, s.length);
expect(ns2.toString(), s);
expect(ns2.toDartString().length, s.length);
expect(ns2.toDartString(), s);
});
}
});
Expand Down
7 changes: 0 additions & 7 deletions pkgs/objective_c/tool/data/extra_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ class NSString extends NSObject {
pkg_ffi.calloc.free(cstr);
return nsstr;
}

@override
String toString() {
final data =
dataUsingEncoding_(0x94000100 /* NSUTF16LittleEndianStringEncoding */);
return data!.bytes.cast<pkg_ffi.Utf16>().toDartString(length: length);
}
}

0 comments on commit 962ab98

Please sign in to comment.