Skip to content

Commit

Permalink
Added support for ranges in parens to Free Coding, e.g. (1...100).
Browse files Browse the repository at this point in the history
  • Loading branch information
twostraws committed May 5, 2019
1 parent d0bd916 commit aaefe0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Unwrap/Extensions/String-Variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ extension String {
replaced = replaced.replacingOccurrences(of: "...", with: " ... ")
replaced = replaced.replacingOccurrences(of: "..<", with: " ..< ")

// Some folks, particularly those coming from Python, use Range(1...100) rather than just 1...100. This is legal, so we homogenize it to a regular Swift range.
// Some folks, particularly those coming from Python, use Range(1...100) or (1...100) rather than just 1...100. This is legal, so we homogenize it to a regular Swift range.
replaced = replaced.replacingOccurrences(of: #"Range\((\d+ \.\.\< \d+)\)"#, with: "$1", options: .regularExpression)
replaced = replaced.replacingOccurrences(of: #"Range\((\d+ \.\.\. \d+)\)"#, with: "$1", options: .regularExpression)
replaced = replaced.replacingOccurrences(of: #"\((\d+ \.\.\< \d+)\)"#, with: "$1", options: .regularExpression)
replaced = replaced.replacingOccurrences(of: #"\((\d+ \.\.\. \d+)\)"#, with: "$1", options: .regularExpression)

// Anonymize variable names.
replaced = replaced.anonymizingComponent("(?:let|var) ([A-Za-z_][A-Za-z0-9_]*)( =|:)", replacementWrapper: "&")
Expand Down
17 changes: 11 additions & 6 deletions UnwrapTests/ExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,19 @@ class ExtensionTests: XCTestCase {
let anonymizedString2 = "for @1@ in 1 ... 100 {"
XCTAssertEqual(cleanString2.toAnonymizedVariables(), anonymizedString2)

// watch out for explicit type annotation with an empty array initializer
let cleanString3 = "var array:[Int]=[]"
let anonymizedString3 = "var &1& = [Int]()"
// watch out for ranges in parens
let cleanString3 = "for i in (1...100) {"
let anonymizedString3 = "for @1@ in 1 ... 100 {"
XCTAssertEqual(cleanString3.toAnonymizedVariables(), anonymizedString3)

// check that a simple (albeit bad) function definition doesn't cause problems
let cleanString4 = "func a"
let anonymizedString4 = "func #1#"
// watch out for explicit type annotation with an empty array initializer
let cleanString4 = "var array:[Int]=[]"
let anonymizedString4 = "var &1& = [Int]()"
XCTAssertEqual(cleanString4.toAnonymizedVariables(), anonymizedString4)

// check that a simple (albeit bad) function definition doesn't cause problems
let cleanString5 = "func a"
let anonymizedString5 = "func #1#"
XCTAssertEqual(cleanString5.toAnonymizedVariables(), anonymizedString5)
}
}

0 comments on commit aaefe0b

Please sign in to comment.