Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart: do not derive from classes annotated as 'visibleForTesting' #1642

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Gluecodium project Release Notes

## Unreleased
### Bug fixes:
* Dart: removed inheritance from base types annotated as `visibleForTesting`. It caused linter warnings.

## 13.10.1
Release date 2024-12-12
### Bug fixes:
Expand Down
3 changes: 3 additions & 0 deletions functional-tests/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,18 @@ feature(Defaults cpp android swift dart SOURCES
)

feature(Inheritance cpp android swift dart SOURCES
input/src/cpp/BaseClassWithStaticMethodsImpl.cpp
input/src/cpp/ChildClassImpl.cpp
input/src/cpp/ChildClassImpl.h
input/src/cpp/DerivedClassWithStaticMethodsImpl.cpp
input/src/cpp/GrandchildClassImpl.cpp
input/src/cpp/GrandchildClassImpl.h
input/src/cpp/ListenerInheritance.cpp
input/src/cpp/Teacher.cpp
input/src/cpp/Inheritance.cpp
input/src/cpp/CrossPackageInheritance.cpp

input/lime/DartVisibleForTestingInheritance.lime
input/lime/Inheritance.lime
input/lime/InheritanceNameClash.lime
input/lime/ListenerInheritance.lime
Expand Down
33 changes: 33 additions & 0 deletions functional-tests/functional/dart/test/Inheritance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@
//
// -------------------------------------------------------------------------------------------------

import 'dart:ffi';
import "package:test/test.dart";
import "package:functional/test.dart";
import "../test_suite.dart";

final _testSuite = TestSuite("Inheritance");

// Prototype class, which will be injected to 'DerivedClassWithStaticMethods'.
class MockDerivedClassWithStaticMethods extends DerivedClassWithStaticMethods$Impl {
MockDerivedClassWithStaticMethods(Pointer<Void> handle) : super(handle);

@override
int getRandomInt() => 37;

@override
double getRandomDouble() => 33.33;
}

void main() {
_testSuite.test("Create child class instance", () {
final ChildClass result = ChildClass.createChildClass();
Expand Down Expand Up @@ -64,4 +76,25 @@ void main() {

expect(result, isA<ConcreteGrandChild>());
});
_testSuite.test("Class using \$HiddenImpl properly executes function calls and allows mocking", () {
// Call functions.
final BaseClassWithStaticMethods baseObj = BaseClassWithStaticMethods();
expect(baseObj.nonstaticGetInt(), 14);
expect(BaseClassWithStaticMethods.getRandomInt(), 7);

final DerivedClassWithStaticMethods derivedObj = DerivedClassWithStaticMethods();
expect(derivedObj.nonstaticGetInt(), 21);
expect(derivedObj.nonstaticGetDouble(), closeTo(21.21, 0.000001));
expect(DerivedClassWithStaticMethods.getRandomDouble(), closeTo(77.77, 0.000001));

// Mock-ability of static functions from Base and Derived.
BaseClassWithStaticMethods.$prototype = MockDerivedClassWithStaticMethods(Pointer<Void>.fromAddress(0));
expect(baseObj.nonstaticGetInt(), 14);
expect(BaseClassWithStaticMethods.getRandomInt(), 37);

DerivedClassWithStaticMethods.$prototype = MockDerivedClassWithStaticMethods(Pointer<Void>.fromAddress(0));
expect(derivedObj.nonstaticGetInt(), 21);
expect(derivedObj.nonstaticGetDouble(), closeTo(21.21, 0.000001));
expect(DerivedClassWithStaticMethods.getRandomDouble(), closeTo(33.33, 0.000001));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (C) 2016-2024 HERE Europe B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE

package test

@Skip(Java) @Skip(Swift)
open class BaseClassWithStaticMethods {
// A constructor, which creates the base class.
constructor create()

// A static function defined in the base class.
static fun getRandomInt(): Int

// A nonstatic function defined in the base class.
fun nonstaticGetInt(): Int
}

@Skip(Java) @Skip(Swift)
open class DerivedClassWithStaticMethods : BaseClassWithStaticMethods {
// A constructor, which creates the derived class.
constructor create()

// A static function defined in the derived class.
static fun getRandomDouble(): Double

// A nonstatic function defined in the derived class.
fun nonstaticGetDouble(): Double
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2016-2024 HERE Europe B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// License-Filename: LICENSE
//
// -------------------------------------------------------------------------------------------------

#include "test/BaseClassWithStaticMethods.h"

namespace test
{

class BaseClassWithStaticMethodsImpl : public BaseClassWithStaticMethods {
public:
BaseClassWithStaticMethodsImpl() = default;
~BaseClassWithStaticMethodsImpl() override = default;

int nonstatic_get_int() override {
return 14;
}
};

int BaseClassWithStaticMethods::get_random_int() {
return 7;
}

std::shared_ptr<BaseClassWithStaticMethods> BaseClassWithStaticMethods::create() {
return std::make_shared<BaseClassWithStaticMethodsImpl>();
}

} // namespace test
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2016-2024 HERE Europe B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// License-Filename: LICENSE
//
// -------------------------------------------------------------------------------------------------

#include "test/DerivedClassWithStaticMethods.h"

namespace test
{

class DerivedClassWithStaticMethodsImpl : public DerivedClassWithStaticMethods {
public:
DerivedClassWithStaticMethodsImpl() = default;
~DerivedClassWithStaticMethodsImpl() override = default;

int nonstatic_get_int() override {
return 21;
}

double nonstatic_get_double() override {
return 21.21;
}
};

double DerivedClassWithStaticMethods::get_random_double() {
return 77.77;
}

std::shared_ptr<DerivedClassWithStaticMethods> DerivedClassWithStaticMethods::create() {
return std::make_shared<DerivedClassWithStaticMethodsImpl>();
}

} // namespace test
21 changes: 20 additions & 1 deletion gluecodium/src/main/resources/templates/dart/DartClass.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ final _{{resolveName "Ffi"}}GetTypeId = __lib.catchArgumentError(() => __lib.nat
/// @nodoc
@visibleForTesting
{{/ifPredicate}}
class {{resolveName}}$Impl extends {{#if this.parentClass}}{{resolveName this.parentClass}}$Impl{{/if}}{{!!
class {{resolveName}}$Impl extends {{!!
}}{{#if this.parentClass}}{{!!
}}{{#unlessPredicate this.parentClass "hasStaticFunctions"}}{{!!
}}{{resolveName this.parentClass}}$Impl{{!!
}}{{/unless}}{{!!
}}{{#ifPredicate this.parentClass "hasStaticFunctions"}}{{!!
}}{{resolveName this.parentClass}}$HiddenImpl{{!!
}}{{/ifPredicate}}{{!!
}}{{/if}}{{!!
}}{{#unless this.parentClass}}__lib.NativeBase{{/unless}} implements {{resolveName}} {

{{resolveName}}$Impl(Pointer<Void> handle) : super(handle);
Expand Down Expand Up @@ -126,7 +134,18 @@ class {{resolveName}}$Impl extends {{#if this.parentClass}}{{resolveName this.pa
{{/set}}{{/set}}
{{#if attributes.equatable}}{{prefixPartial "dart/DartEqualityOperator" " "}}{{/if}}{{!!
}}{{#if attributes.pointerEquatable}}{{prefixPartial "dart/DartEqualityOperator" " "}}{{/if}}
}{{!!
}}{{#if this.isOpen}}{{!!
}}{{#ifPredicate "hasStaticFunctions"}}

/// @nodoc
class {{resolveName}}$HiddenImpl extends {{resolveName}}$Impl {

{{resolveName}}$HiddenImpl(Pointer<Void> handle) : super(handle);

}
{{/ifPredicate}}{{!!
}}{{/if}}

Pointer<Void> {{resolveName "Ffi"}}ToFfi({{resolveName}} value) =>
_{{resolveName "Ffi"}}CopyHandle((value as __lib.NativeBase).handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ class Constructors$Impl extends __lib.NativeBase implements Constructors {
}


}
/// @nodoc
class Constructors$HiddenImpl extends Constructors$Impl {

Constructors$HiddenImpl(Pointer<Void> handle) : super(handle);

}

Pointer<Void> smokeConstructorsToFfi(Constructors value) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (C) 2016-2024 HERE Europe B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE

package smoke

@Skip(Java) @Skip(Swift)
open class BaseClassWithStaticMethods {
constructor create()

// A static function defined in the base class.
static fun getRandomInt(): Int
}

@Skip(Java) @Skip(Swift)
open class DerivedClassWithStaticMethods : BaseClassWithStaticMethods {
// A constructor, which creates the derived class.
constructor create()

// A static function defined in the derived class.
static fun getRandomDouble(): Double
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@


import 'package:library/src/smoke/base_class_with_static_methods.dart';
import 'package:library/src/smoke/child_class_from_class.dart';
import 'package:library/src/smoke/child_class_from_interface.dart';
import 'package:library/src/smoke/child_class_name_clash.dart';
Expand All @@ -6,6 +9,7 @@ import 'package:library/src/smoke/child_class_with_imports.dart';
import 'package:library/src/smoke/child_class_with_lambda.dart';
import 'package:library/src/smoke/child_interface.dart';
import 'package:library/src/smoke/child_with_parent_class_references.dart';
import 'package:library/src/smoke/derived_class_with_static_methods.dart';
import 'package:library/src/smoke/grand_child_interface.dart';
import 'package:library/src/smoke/interface_with_lambda.dart';
import 'package:library/src/smoke/interface_with_overloads.dart';
Expand All @@ -16,23 +20,45 @@ import 'package:library/src/smoke/parent_class_with_imports.dart';
import 'package:library/src/smoke/parent_interface.dart';
import 'package:library/src/smoke/parent_interface_with_bool.dart';
import 'package:library/src/smoke/parent_with_class_references.dart';
final Map<String, Function> typeRepository = {

final Map<String, Function> typeRepository = {
"smoke_BaseClassWithStaticMethods": (handle) => BaseClassWithStaticMethods$Impl(handle),

"smoke_ChildClassFromClass": (handle) => ChildClassFromClass$Impl(handle),

"smoke_ChildClassFromInterface": (handle) => ChildClassFromInterface$Impl(handle),

"smoke_ChildClassNameClash": (handle) => ChildClassNameClash$Impl(handle),

"smoke_ChildClassWithBool": (handle) => ChildClassWithBool$Impl(handle),

"smoke_ChildClassWithImports": (handle) => ChildClassWithImports$Impl(handle),

"smoke_ChildClassWithLambda": (handle) => ChildClassWithLambda$Impl(handle),

"smoke_ChildInterface": (handle) => ChildInterface$Impl(handle),

"smoke_ChildWithParentClassReferences": (handle) => ChildWithParentClassReferences$Impl(handle),

"smoke_DerivedClassWithStaticMethods": (handle) => DerivedClassWithStaticMethods$Impl(handle),

"smoke_GrandChildInterface": (handle) => GrandChildInterface$Impl(handle),

"smoke_InterfaceWithLambda": (handle) => InterfaceWithLambda$Impl(handle),

"smoke_InterfaceWithOverloads": (handle) => InterfaceWithOverloads$Impl(handle),

"smoke_InternalChild": (handle) => InternalChild$Impl(handle),

"smoke_InternalParent": (handle) => InternalParent$Impl(handle),

"smoke_ParentClass": (handle) => ParentClass$Impl(handle),

"smoke_ParentClassWithImports": (handle) => ParentClassWithImports$Impl(handle),

"smoke_ParentInterface": (handle) => ParentInterface$Impl(handle),

"smoke_ParentInterfaceWithBool": (handle) => ParentInterfaceWithBool$Impl(handle),

"smoke_ParentWithClassReferences": (handle) => ParentWithClassReferences$Impl(handle),
};
Loading
Loading