 class NSRegExNamedCaptureGroupTests: XCTestCase {
 
   class TestSamples_Group1 {
-    static var phoneNumber = "202-555-0136"
-    static var USAPhoneNumberPattern = try! NSRegularExpression(
-        pattern: "\\b(?<Area>\\d\\d\\d)-(?:\\d\\d\\d)-(?<Num>\\d\\d\\d\\d)\\b"
+    static let phoneNumber = "202-555-0136"
+
+    static let areaPattern = "(?<Area>\\d\\d\\d)"
+    static let exchPattern = "(?:\\d\\d\\d)"
+    static let numPattern = "(?<Num>\\d\\d\\d\\d)"
+
+    static let USAPhoneNumberPattern = try! NSRegularExpression(
+        pattern: "\\b\(areaPattern)-\(exchPattern)-\(numPattern)\\b"
       , options: NSRegularExpression.commonOptions
       )
     }
 
-  func test_array_based_api_01() {
+  func _compareRange(
+      in checkingResult: NSTextCheckingResult
+    , byGroupName groupName: String?
+    , with index: Int ) -> Bool {
+
+    let rangeByGroupName = checkingResult.range( withGroupName: groupName )
+    let rangeByIndex = checkingResult.rangeAt( index )
+
+    return
+      rangeByGroupName.location == rangeByIndex.location
+        && rangeByGroupName.length == rangeByIndex.length
+    }
+
+  func _isRangeInvalid(
+      in checkingResult: NSTextCheckingResult
+    , byGroupName groupName: String? ) -> Bool {
+    
+    let rangeByGroupName = checkingResult.range( withGroupName: groupName )
+    return
+      rangeByGroupName.location == NSNotFound
+        && rangeByGroupName.length == 0
+    }
+
+  func testArrayBasedAPI_01() {
     let matches = TestSamples_Group1.USAPhoneNumberPattern.matches(
         in: TestSamples_Group1.phoneNumber
       , options: []

      ) { checkingResult, _, stopToken in

      XCTAssertNotNil( checkingResult )
      guard let checkingResult = checkingResult else { stopToken.pointee = true; return }

      do {
        let rangesOfCaptured = try TestSamples_Group1.USAPhoneNumberPattern.rangesOfNamedCaptureGroups( inMatch: checkingResult )
        XCTAssertEqual( rangesOfCaptured.count, checkingResult.numberOfRanges - 1 )

        // Tests whether NSRegExNamedCaptureGroup can extract 
        // all the NCG expressions properly
        for ( ncgSubexpr, nsRange ) in rangesOfCaptured {
          let pattern = TestSamples_Group1.USAPhoneNumberPattern.pattern
          let targetText = TestSamples_Group1.phoneNumber
          XCTAssertNotEqual( pattern.range( of: ncgSubexpr ), nil )

          // let substringCapturedByNCG = targetText[ targetText.range( from: nsRange )! ]
          // let substringCapturedByOrdi = targetText[ targetText.range( from: checkingResult.range )! ]
          // XCTAssertEqual( substringCapturedByNCG, substringCapturedByOrdi )
          }

        } catch {
          fatalError( error.localizedDescription )
        }
      }
    }

  static var allTests = [
      ( "test_array_based_api_01", test_array_based_api_01 )
    , ( "test_block_enumeration_based_api_01", test_block_enumeration_based_api_01 )
    ]
  }

      )

    for match in matches {
      XCTAssert( _compareRange( in: match, byGroupName: nil, with: 0 ) )
      XCTAssert( _compareRange( in: match, byGroupName: TestSamples_Group1.areaPattern, with: 1 ) )
      XCTAssert( _isRangeInvalid( in: match, byGroupName: TestSamples_Group1.exchPattern ) )
      XCTAssert( _compareRange( in: match, byGroupName: TestSamples_Group1.numPattern, with: 2 ) )
      }
    }

  func testBlockEnumerationBasedAPI_01() {
    TestSamples_Group1.USAPhoneNumberPattern.enumerateMatches(
        in: TestSamples_Group1.phoneNumber
      , options: []
