Home > Net >  XCode test coverage reports uncovered line with only braces
XCode test coverage reports uncovered line with only braces

Time:10-04

I have the code below:

var property: Type {
  if condition {
    return value1
  } else {
    return value2
  }
}

and I have unit tests coverage both the if and else conditions, however, XCode coverage reports that the last line isn't covered, where there's only a }.

SonarQube report

Report from SonarQube, which depends on the coverage report from XCode.

I haven't tried if I write it this way, if it still reported as uncovered:

var property: Type {
  if condition {
    return value1
  }
  return value2
}

Is there anything I missed?

Thanks!

CodePudding user response:

Here are possible variants:

a) preferred one (IMO)

var property: Type {
  condition ? value1 : value2
}

b) and if you like explicit branches (or last your snapshot)

var property: Type {
  var result = value2
  if condition {
    result = value1
  }
  return result
}
  • Related