Why this line is not covered? Xcode code coverage - swift

I'm experiencing an issue with the report of code coverage in Xcode.
As you can see from this screenshot:
On the left tab, line 58 is "touched" from the break-point, and on the right tab, the test passes. While on the right tab, I'm running only the test on line 37.
Why does Xcode sign the line 58 in red, as not covered?
Line 53 is not "touched" (if I set a break-point there).
Using SQLite as a database.
The entire project is available here.
EDIT: Adding code:
Test.swift
func testAddFuelFail() {
fuelsManager.dropTable()
XCTAssertEqual(addFuel(), -1)
}
FuelsManager.swift
func addFuel(dateOfFuel: Date, mileageOnSave: Int, quantityOfFuel: Double, pricePerUnitOfFuel: Double) -> Int64 {
let insertFuel = fuelsTable.insert(date <- dateOfFuel, mileage <- mileageOnSave, quantity <- quantityOfFuel, pricePerUnit <- pricePerUnitOfFuel)
do {
let id = try database!.run(insertFuel)
return id
} catch {
print(error)
}
return -1
}

Actually its a bug reported here.
Coverage number varies between multiple runs on XC 10 on the same binary. Like in first run it shows x% vs in another run it will show y% keeping the same code.
Coverage number/Number of tests varies while running on 11.4 simulator and 12.0 simulator, both ran on XC 10
Number of tests also a little different like in some run it was 5507 tests vs in some runs it was 5506.
XC 10 certainly came with lots of bugs.

Take the breakpoint out and run the test. Once the test has finished running the coverage information should change.

Related

One Line of Swift code works one day and doesn't the next (Only Xcode)

Very simply I need to get the power of an Integer and I have this code to do so below
import Darwin
var x = Int(pow(Double(3),Double(3)))
print(x)
For whatever reason this line of code worked one day and the next day when I came back to the project it did not work and instead of getting 27 I keep getting 3...?
What confuses me more is if I go into Swift Playgrounds(The App) it works fine but put it in Xcode and it doesn't work. I haven't changed the code at all and are just copy/paste into different programs with different results

how to restart dafny in VS code when it gets frozen

Before I start moaning let me state I am very impressed with Dafny.
I have managed to get Dafny-VScode frozen on several occasions and would like to know if there is a better way to "reboot" Dafny with out just shutting down VS code. The example below is a cut down version of what I am working on so it may seem a little pointless:
method sortOfImp(s:seq<int>) returns (r:seq<int>)
{
var i:int:= 1;
r:=[];
if |s|>= 2 {
while (i<|s|)
decreases |s| - i
invariant i<=|s|
// invariant forall j:nat :: 0<j<i ==> ( s[j]<=s[j-1] ==>
// (exists ii:nat :: ii<|r| && r[ii]==s[j]) )
{
if s[|s|-i-1] <= s[|s|-i] { r:= [s[|s|-i]]+r;}
i:=i+1;
}
}
}
loads and behaves nicely. But if you uncomment the two lines an error is reported (no problem there) now restore the comments and the errors do not go away plus other errors are not reported.
The only solution I have found is to quit VS code and when restarted the errors are no longer reported and everything works as you would expect.
The other part of the question is:
what part of the invariant does dafny object?

Dart how to drop a breakpoint or debugger

In Python, I can insert a breakpoint() keyword on any line of code, and when I run the script from the command-line, it will stop when it reaches that line, and I'll have an opportunity to interact with or access any previously-defined variables.
I was looking for a way to do this in the Dart language but so far without success.
I've seen references to the debugger keyword provided by the dart:developer library, but instead of allowing me to interact, the script just hangs:
// bin/my_script.dart
import "dart:developer"; // source of debugger();
main() {
var x = 5;
print("X: ${x}"); //> X: 5
debugger(); // ... just hangs
print("END");
}
I've also seen references to the console package, but I'm not seeing it do anything:
// bin/my_script.dart
import "package:console/console.dart"; // source of Console.init()
main() {
var x = 5;
print("X: ${x}"); //> X: 5
Console.init(); // ... nothing happens
print("END");
}
FYI: I'm running this script via dart bin/my_script.dart, and a command-line solution would be ideal, but a solution using the VS Code text editor would also be sufficient.
You can't debug without a debugger attached, that's why running from the command line will hang in the breakpoint, since you can step on the next instruction.
If you use VS Code or Intellij and use the debugger() while in debug mode Shift+Cmd+R, it will trigger a breakpoint there and you can analyze your variables and step forward to the next instructions.

os_log repeating log entry multiple times

Using Unified Logging, I'm getting multiple copies of each log entry (23 to be exact). I'm wondering what I might be doing wrong.
I'm using Swift 4.1, Xcode 9.4.1, Console Version 1.0 (2.0.52), and building an iOS 11.0 app.
The relevant code looks like:
import os.log
...
// property
let log = OSLog(subsystem: Constants.subsystem, category: Constants.category)
override func viewDidLoad() {
super.viewDidLoad()
os_log("Useless message...", log: log, type: .debug)
}
And, in the Console (only showing the Message column), I get:
Subsystem, Category, Time, and Process values are identical for every entry.
And, the output only shows once in Xcode's console window.
What silly thing have I done now? (Or, is this a 🤪 bug?)
Update: I set a breakpoint at the log statement to test. It's only hit once, but as soon as it executes, the 23 entries show in the Console.
Update 2: added UUID to log for test: same result: UUID was the same in all 23 entries.
os_log("Useless message... %#", log: log, type: .debug, String(describing: UUID()))
Results:
Update 3: Fixed...
Cleaned the project, deleted derived data, erased the content in the simulator - all of which I'd done multiple times before.
This time, though, I also shut down, then restarted Xcode - and now it's working fine! Weirdness!

Xcode takes long time to print debug results.

When I debug on Xcode it takes around 30 seconds or more to print results of po on Xcode console.
Unfortunately, this is only few information I have on the issue.
However, there is another point to consider. This issue is very specific to a project. This is because when I use po for other projects on same Macbook, it works immediately. Also, this particular project is slow on all other Macbook, and for all team.
I googled it but no relevant answer found. I find it easy to use print(...) rather than using debugging on Xcode console. However, it's more work and requires lots of rebuilds.
I have several explanations:
There is a lot of code (Xcode slows down after a certain amount of code) Also make sure your print statement is towards the top of your page. Xocde goes from top to bottom.
Your Mac is slow. Some Macs after a certain amount of usage slow down. Also if you have a Mac mini or air they are slower than others.
Xcode Beta. If you are using Xcode beta then there might just be a bug.
If none of those answer you provide me with more info and I provide other solutions.
Swift:
Try this solution which helps to reduce log time in debug mode.
Step 1: Create a new file called Utils.swift (File name based on your preference)
Step 2: Add following code in file
import Foundation
import UIKit
struct Utils { }
public func PrintLogs(_ message: Any, file: String = #file, function: String = #function, line: Int = #line) {
#if DEBUG
let className = file.components(separatedBy: "/").last ?? ""
let classNameArr = className.components(separatedBy: ".")
NSLog("\n\n--> Class Name: \(classNameArr[0]) \n--> Function Name: \(function) \n--> Line: \(line)")
print("--> Log Message: \(message)")
#endif
}
Usage: Call PrintLogs("Hello") instead of print("Hello")
Sample Output:
--> Class Name: HomeViewController
--> Function Name: logTest()
--> Line: 81
--> Log Message: Hello