Skip to content

Commit

Permalink
Merge pull request #2 from ghv/fix-issue-001
Browse files Browse the repository at this point in the history
Fixing issue #1 in removeGeneratedLines
  • Loading branch information
ghv committed Apr 29, 2020
2 parents c082e54 + cf2493a commit 470ec43
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Sources/SVMPrefsKit/Parser/ParserMetaData.swift
Expand Up @@ -201,9 +201,9 @@ class ParserMetaData {
if linesToDelete > 0 {
lines.removeSubrange(start..<end)
totalLinesDeleted += linesToDelete
codeMark.begin.clearedOffset = start - 1
codeMark.end.clearedOffset = start
}
codeMark.begin.clearedOffset = start - 1
codeMark.end.clearedOffset = start
}
}

Expand Down
53 changes: 53 additions & 0 deletions Tests/SVMPrefsKitTests/SwiftCodeGeneratorTests.swift
Expand Up @@ -136,21 +136,69 @@ final class SwiftCodeGeneratorTests: XCTestCase {
SVMPREFS*/
class MyMigrationTests {
// MARK: BEGIN main
// this will be deleted
// MARK: END main
// MARK: BEGIN copy
// this will be deleted
// MARK: END copy
// MARK: BEGIN migrate
// MARK: END migrate
}
"""

assertSnapshot(matching: code, as: .svmPrefs())
}

func testMigrationWithCodeThatFollows() {
let code = """
import Foundation
import AppKit
/*SVMPREFS
S main | | RALL
V Bool | boolVar1 | boolVar2 | IS |
V Bool | hasBoolVar2 | boolVar2 | |
V Bool | boolVar3 | boolVar3 | |
V Bool | boolVar4 | boolVar4 | |
S copy | | RALL
V Bool | boolVar3 | boolVar3 | |
V Bool | boolVar4 | boolVar4 | |
M main | copy | boolVar3 | boolVar3
M main | copy | boolVar4 | boolVar4
M main | delete | hasBoolVar2
SVMPREFS*/
class MyMigrationTests {
// MARK: BEGIN main
// this will be deleted
// MARK: END main
// MARK: BEGIN copy
// this will be deleted
// MARK: END copy
// MARK: BEGIN migrate
// MARK: END migrate
}
// These lines here attempt to creat the scenario
// where there are enough extra lines (3) than being
// removed (2) to prepare for the generated code to be inserted
"""

assertSnapshot(matching: code, as: .svmPrefs())
}


func testDate() {
let code = """
import Foundation
Expand Down Expand Up @@ -198,12 +246,15 @@ final class SwiftCodeGeneratorTests: XCTestCase {
}
// MARK: BEGIN main
// this will be deleted
// MARK: END main
// MARK: BEGIN main1
// this will be deleted
// MARK: END main1
// MARK: BEGIN main2
// this will be deleted
// MARK: END main2
}
Expand All @@ -228,6 +279,7 @@ final class SwiftCodeGeneratorTests: XCTestCase {
class MyCompoundCodeMarakTests {
// MARK: BEGIN main,main1,main2
// this will be deleted
// MARK: END main,main1,main2
}
Expand All @@ -249,6 +301,7 @@ final class SwiftCodeGeneratorTests: XCTestCase {
class MyNoRemoveAllTests {
// MARK: BEGIN main
// this will be deleted
// MARK: END main
}
Expand Down
Expand Up @@ -21,22 +21,6 @@ M main | delete | hasBoolVar2
SVMPREFS*/

class MyMigrationTests {
// MARK: BEGIN migrate ⬇⬇⬇ AUTO-GENERATED CODE - DO NOT EDIT ⬇⬇⬇
func migrate() {
if let value = main.object(forKey: MainKeys.boolVar3) {
copy.set(value, forKey: CopyKeys.boolVar3)
main.removeObject(forKey: MainKeys.boolVar3)
}
if let value = main.object(forKey: MainKeys.boolVar4) {
copy.set(value, forKey: CopyKeys.boolVar4)
main.removeObject(forKey: MainKeys.boolVar4)
}
if main.object(forKey: MainKeys.hasBoolVar2) != nil {
main.removeObject(forKey: MainKeys.hasBoolVar2)
}
}
// MARK: END migrate ⬆⬆⬆ AUTO-GENERATED CODE - DO NOT EDIT ⬆⬆⬆

// MARK: BEGIN main ⬇⬇⬇ AUTO-GENERATED CODE - DO NOT EDIT ⬇⬇⬇
var main = UserDefaults.standard

Expand Down Expand Up @@ -95,4 +79,20 @@ class MyMigrationTests {
copy.removeObject(forKey: CopyKeys.boolVar4)
}
// MARK: END copy ⬆⬆⬆ AUTO-GENERATED CODE - DO NOT EDIT ⬆⬆⬆

// MARK: BEGIN migrate ⬇⬇⬇ AUTO-GENERATED CODE - DO NOT EDIT ⬇⬇⬇
func migrate() {
if let value = main.object(forKey: MainKeys.boolVar3) {
copy.set(value, forKey: CopyKeys.boolVar3)
main.removeObject(forKey: MainKeys.boolVar3)
}
if let value = main.object(forKey: MainKeys.boolVar4) {
copy.set(value, forKey: CopyKeys.boolVar4)
main.removeObject(forKey: MainKeys.boolVar4)
}
if main.object(forKey: MainKeys.hasBoolVar2) != nil {
main.removeObject(forKey: MainKeys.hasBoolVar2)
}
}
// MARK: END migrate ⬆⬆⬆ AUTO-GENERATED CODE - DO NOT EDIT ⬆⬆⬆
}
@@ -0,0 +1,101 @@
import Foundation
import AppKit

/*SVMPREFS [code generated using svmprefs version 2.0.0]
S main | | RALL
V Bool | boolVar1 | boolVar2 | IS |
V Bool | hasBoolVar2 | boolVar2 | |
V Bool | boolVar3 | boolVar3 | |
V Bool | boolVar4 | boolVar4 | |
S copy | | RALL
V Bool | boolVar3 | boolVar3 | |
V Bool | boolVar4 | boolVar4 | |
M main | copy | boolVar3 | boolVar3
M main | copy | boolVar4 | boolVar4
M main | delete | hasBoolVar2
SVMPREFS*/

class MyMigrationTests {
// MARK: BEGIN main ⬇⬇⬇ AUTO-GENERATED CODE - DO NOT EDIT ⬇⬇⬇
var main = UserDefaults.standard

enum MainKeys {
static let isBoolVar1 = "boolVar2"
static let hasBoolVar2 = "boolVar2"
static let boolVar3 = "boolVar3"
static let boolVar4 = "boolVar4"
}

var isBoolVar1: Bool {
get {
return main.bool(forKey: MainKeys.isBoolVar1)
}
set {
main.set(newValue, forKey: MainKeys.isBoolVar1)
}
}

func mainRemoveAll() {
main.removeObject(forKey: MainKeys.isBoolVar1)
main.removeObject(forKey: MainKeys.hasBoolVar2)
main.removeObject(forKey: MainKeys.boolVar3)
main.removeObject(forKey: MainKeys.boolVar4)
}
// MARK: END main ⬆⬆⬆ AUTO-GENERATED CODE - DO NOT EDIT ⬆⬆⬆

// MARK: BEGIN copy ⬇⬇⬇ AUTO-GENERATED CODE - DO NOT EDIT ⬇⬇⬇
var copy = UserDefaults.standard

enum CopyKeys {
static let boolVar3 = "boolVar3"
static let boolVar4 = "boolVar4"
}

var boolVar3: Bool {
get {
return copy.bool(forKey: CopyKeys.boolVar3)
}
set {
copy.set(newValue, forKey: CopyKeys.boolVar3)
}
}

var boolVar4: Bool {
get {
return copy.bool(forKey: CopyKeys.boolVar4)
}
set {
copy.set(newValue, forKey: CopyKeys.boolVar4)
}
}

func copyRemoveAll() {
copy.removeObject(forKey: CopyKeys.boolVar3)
copy.removeObject(forKey: CopyKeys.boolVar4)
}
// MARK: END copy ⬆⬆⬆ AUTO-GENERATED CODE - DO NOT EDIT ⬆⬆⬆

// MARK: BEGIN migrate ⬇⬇⬇ AUTO-GENERATED CODE - DO NOT EDIT ⬇⬇⬇
func migrate() {
if let value = main.object(forKey: MainKeys.boolVar3) {
copy.set(value, forKey: CopyKeys.boolVar3)
main.removeObject(forKey: MainKeys.boolVar3)
}
if let value = main.object(forKey: MainKeys.boolVar4) {
copy.set(value, forKey: CopyKeys.boolVar4)
main.removeObject(forKey: MainKeys.boolVar4)
}
if main.object(forKey: MainKeys.hasBoolVar2) != nil {
main.removeObject(forKey: MainKeys.hasBoolVar2)
}
}
// MARK: END migrate ⬆⬆⬆ AUTO-GENERATED CODE - DO NOT EDIT ⬆⬆⬆
}
// These lines here attempt to creat the scenario
// where there are enough extra lines (3) than being
// removed (2) to prepare for the generated code to be inserted

0 comments on commit 470ec43

Please sign in to comment.