Search code examples
xcodeunit-testingsonarqubetest-coverage

XCode test coverage reports uncovered line with only braces


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 in this way, if its still reported as uncovered:

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

Is there anything I missed?


Solution

  • 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
    }