Skip to content

Commit

Permalink
Lint. Add test to assert new rule
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayterDev committed Dec 5, 2023
1 parent dfe029e commit 30394ce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/TrackerRadarKit/ContentBlockerRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public struct ContentBlockerRule: Codable, Hashable {
case loadContext = "load-context"
}

private init(urlFilter: String, unlessDomain: [String]?, ifDomain: [String]?, resourceType: [ResourceType]?, loadType: [LoadType]?, loadContext: [LoadContext]?) {
private init(urlFilter: String, unlessDomain: [String]?, ifDomain: [String]?,
resourceType: [ResourceType]?, loadType: [LoadType]?, loadContext: [LoadContext]?) {
self.urlFilter = urlFilter
self.unlessDomain = unlessDomain
self.ifDomain = ifDomain
Expand Down Expand Up @@ -106,7 +107,8 @@ public struct ContentBlockerRule: Codable, Hashable {
resourceType types: [ResourceType]?,
loadTypes: [LoadType]? = [ .thirdParty ],
loadContext: [LoadContext]? = nil) -> Trigger {
return Trigger(urlFilter: filter, unlessDomain: nil, ifDomain: domains, resourceType: types, loadType: loadTypes, loadContext: loadContext)
return Trigger(urlFilter: filter, unlessDomain: nil, ifDomain: domains,
resourceType: types, loadType: loadTypes, loadContext: loadContext)
}
}

Expand Down
15 changes: 15 additions & 0 deletions Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ class ContentBlockerRulesBuilderTests: XCTestCase {
}
}

func testDefaultIgnoreGeneratesPopupIgnoreRules() throws {
let rules = ContentBlockerRulesBuilder(trackerData: trackerData).buildRules(withExceptions: ["duckduckgo.com"],
andTemporaryUnprotectedDomains: [])

if let idx = rules.firstIndexOfExactFilter(filter: "^(https?)?(wss?)?://([a-z0-9-]+\\.)*xvideos-cdn\\.com\\/v-c19d94e7937\\/v3\\/js\\/skins\\/min\\/default\\.header\\.static\\.js") {

Check failure on line 54 in Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Line Length Violation: Line should be 150 characters or less; currently it has 190 characters (line_length)
let nextRule = rules[idx + 1]
XCTAssertNotNil(nextRule, "Missing ignore-previous popup type rule")
XCTAssert(nextRule.action == .ignorePreviousRules())
XCTAssert(nextRule.trigger.loadContext?.first == .topFrame)
XCTAssert(nextRule.trigger.resourceType?.first == .popup)
} else {
XCTFail("Missing rule for testing")
}
}

func testLoadingUnsupportedRules() throws {
let data = JSONTestDataLoader.mockTrackerData
guard let mockData = try? JSONDecoder().decode(TrackerData.self, from: data) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ extension Array where Element == ContentBlockerRule {

return nil
}

func firstIndexOfExactFilter(filter: String) -> Int? {
self.firstIndex { $0.trigger.urlFilter == filter }
}
}

0 comments on commit 30394ce

Please sign in to comment.