Swift Package Module name conflicting with struct, class or any type defined in other Modules - swift

created Swift Package Module with name OSLogBoolFormat and then I have to import os in the code. when I try to access types defined in OSLogBoolFormat module it's not being recognized and its only pointing to os.OSLogBoolFormat.
import UIKit;
import os;
import OSLogBoolFormat;
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let name = OSLogBoolFormat.PrcoessKT.name
print(name)
}
}
OSLogBoolFormat package is conflicting with OSLogBoolFormat enum defined in os Module.
Code Example: https://github.com/sreexamus/NewOneTest

Related

Inherit tests from a common XCTestCase from a Swift package

Consider the following:
A Swift package Feature has a protocol Component and a couple of implementations FooComponent, BarComponent, etc.
The package has its own tests FeatureTests and the Component's are tested in a way, that individual implementations inherit a single test case in order to test the common expectations of a Component:
import XCTest
#testable import Feature
class ComponentTests: XCTestCase {
var sut: Component!
override class var defaultTestSuite: XCTestSuite {
XCTestSuite(name: "EventStorageService Interface Tests Excluded")
}
func test_common() { }
}
...
class FooComponentTests: ComponentTests {
override func setUpWithError() throws {
try super.setUpWithError()
sut = FooComponent()
}
override class var defaultTestSuite: XCTestSuite {
XCTestSuite(forTestCaseClass: Self.self)
}
func test_specific() { }
}
class BarComponentTests: ComponentTests {
override func setUpWithError() throws {
try super.setUpWithError()
sut = BarComponent()
}
...
This all works really nicely within the Swift package.
Now let's consider the app App injects its own custom Component implementation and also wants to test it and benefit from the already written common tests.
import XCTest
#testable import App
#testable import Feature
class CustomComponentTests: ComponentTests { // Error: Cannot find type 'ComponentTests' in scope
...
Of course the above does not work as the compiler does not find ComponentTests. And importing FeatureTests also fails.
import XCTest
#testable import App
#testable import FeatureTests // Error: No such module 'FeatureTests'
class CustomComponentTests: ComponentTests {
...
Does anyone have an idea how to achieve the desired outcome (i.e. inherit common tests for the protocol defined within the lib in order to not duplicate the test code)?

Swift Error - Method Does not override any method from its superclass

I am running into an issue where when I am attempting to override a function it is telling me that there is no method in the superclass. The subclass is in an XCTest. When I subclass in the regular project it works perfectly but the XCTest does not work for some reason Below is the Code
SuperClass
class BackupServerCell: UITableViewCell {
#IBOutlet var serverNameLabel: UILabel!
#IBOutlet var serverDescriptionLabel: UILabel!
func configCell(with server: VeeamBackupServer) {
}
}
Subclass - Located in the XCTest file
class MockBackupServerCell : BackupServerCell {
var configCellGotCalled = false
override func configCell(with server: VeeamBackupServer) {
configCellGotCalled = true
}
}
When you are working on a Test target, its sometime needed to import your main project.
Let's say your project is named 'MyAwesomeProject', just need to add this line in your test file:
#testable import MyAwesomeProject
To obtain something like this:
import XCTest
#testable import MyAwesomeProject
class TestStaticQueries: XCTestCase {
// Add your test methods here.
}
#DDelforge, was partially correct. I had to add the BackupServerCell class, to the Test Target. Not sure why this class is different, but hey, it works.
Thank you all

Issue with swift framework import

I have created a swift framework.
In this framework i have some swift file and one category in objective-C (so there is the .h and .m files).
I successfully build my framework, but when i import it into another project, only the method from my category (written in objective-C) are visible. If i try to use any swift class i got an error "Use of unresolved identifier".
I have checked the following points in my framework project :
all my swift class are public public class Toto
i have set the build settings - packaging - defines module param to yes
In my project where the framework is imported, i have the following code :
import UIKit
import myframework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
toto()
}
func toto() {
let data = NSData()
data.isGzippedData()
var client = Client ()
NSLog("ok");
}
}
I have an error "Use of undeclared identifier" for the line var toto = Client (), but in my swift framework the class Client is public and its default constructor is public.
However, if i comment this line, the code works fine, even if the method isGzippedData is declared and implement in my framework (but in objective-C).
How can i use the swift class from my framework into my project ?

Xcode UI test - swipeRight() not working after tearDown

I have a logout function that gets called during every tearDown(), but does not work when called this way. If I call the same logout function during the test, it works fine. I'm wondering what are the behaviors of XCUI testing during teardown, are there limitations? I tried debugging and calling app.swipeRight() using the lldb (espression->write code)...
-------
Navbar.swift
-------
import XCTest
import Foundation
class NavbarTest: XCTestCaseLib{
override func setUp()
{
super.setUp()
continueAfterFailure = false
}
override func tearDown()
{
logout()
super.tearDown()
}
func testSideBar_STAGING(){
//...<test code that executes no problem>
//...
}
}
-----
XCTestCaseLib.swift
------
import XCTest
import Foundation
class XCTestCaseLib: XCTestCase {
let app = XCUIApplication()
func logout() {
app.swipeRight()
...
}
From the code you've posted, it appears to be your imports (I'm assuming here that these classes are in different files, otherwise your inheritance is ambiguous). If I'm mistaken please update your question to include your file structure. Play around with your imports and inheritance.
I believe you just need to import XCTest on your NavbarTest class

How to write code in Swift file that executes when module is loaded?

Say I have a registry defined inside of a module called Registry:
class Registry {
static let sharedInstance = Registry()
registerImplementation(implementation: AnyClass, forInterface interface: Protocol) {...}
implementationForInterface(interface: Protocol) -> AnyClass {...}
}
And an API and implementation respectively in the modules FooServiceAPI and FooServiceImplementation respectively.
// In FooServiceAPI target:
protocol FooService {
...
}
// In FooServiceImplementation target:
import FooServiceAPI
import Registry
// This line causes an error saying that code cannot be executed at the top level.
Registry.sharedInstance.registerInterface(FooService, forImplementation: FooServiceImplementation)
class FooServiceImplementation: FooService {
...
}
I would like FooServiceImplementation to be automatically registered in the Registry for the FooServiceAPI interface, so that other classes can use them like so:
import Registry
import FooServiceAPI
class Bar {
func baz() {
let fooService: FooService = Registry.sharedInstance.implementationForInterface(FooService)
...
}
}
How can I accomplish this without forcing the application to individually register the implementations in the AppDelegate? Thanks!