Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
farshadBB committed Jan 18, 2023
2 parents 89f12aa + 17d46dd commit 17c746c
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct DefaultScenarioProcessor: ScenarioProcessing {
scenarioFileInformation.steps.forEach { item in
let overriddenPath = globalConfigs?.hasMap(for: item.folder)?.to
let mockKey = overriddenPath != nil ? overriddenPath! + item.folder : item.folder
guard let cleanedName = try? JSONFileNameParser().parse(.init(name: item.responseFileName, filePath: URL(string: "/api/")!, relativePath: "")),
guard let cleanedName = try? mockFileNameParse.parse(.init(name: item.responseFileName, filePath: URL(string: "/api/")!, relativePath: "")),
let mocks = requestToMockMapper(mockKey),
let mock = mocks.mocks.first(where: {$0.name.caseInsensitiveCompare(cleanedName.displayName) == .orderedSame})
else {
Expand All @@ -39,14 +39,18 @@ struct DefaultScenarioProcessor: ScenarioProcessing {

private let _process: (String) throws -> Dictionary<DirectoryRequestPath, [Self.Information]>
private let scenarioDecoder: PropertyListDecoder

private let mockFileNameParse: FileNameParsing
init<DS>(directoryProcessor: DS = FileDirectoryProcessor(),
scenarioDecoder: PropertyListDecoder = PropertyListDecoder()
scenarioDecoder: PropertyListDecoder = PropertyListDecoder(),
mockFileNameParse: FileNameParsing = JSONFileNameParser()
)

where DS: DirectoryProcessing, DS.Information == Self.Information {
self._process = directoryProcessor.process
self.scenarioDecoder = scenarioDecoder
self.mockFileNameParse = mockFileNameParse
}

func process(_ path: String) throws -> Dictionary<DirectoryRequestPath, [FileInformation]> {
try _process(path)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
protocol DirectoryProcessing {
public protocol DirectoryProcessing {
associatedtype Information: FileInformationBasic, Comparable
/// Analysed the directory and sub directories and creates a dictionary of them
func process(_ path: String) throws -> Dictionary<DirectoryRequestPath, [Information]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import Foundation

struct DirectoryRequestPath: Hashable, Comparable {
static func < (lhs: DirectoryRequestPath, rhs: DirectoryRequestPath) -> Bool {
lhs.path < rhs.path
public struct DirectoryRequestPath: Hashable, Comparable {
public static func < (lhs: DirectoryRequestPath, rhs: DirectoryRequestPath) -> Bool {
lhs.path.absoluteString < rhs.path.absoluteString
}

let path: URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct FileDirectoryProcessor: DirectoryProcessing {
}
}

struct GlobalMockDirectoryConfig: Decodable, Equatable {
public struct GlobalMockDirectoryConfig: Decodable, Equatable {
struct Map: Decodable, Equatable {
let folder: String
let to: String
Expand All @@ -89,7 +89,7 @@ struct GlobalMockDirectoryConfig: Decodable, Equatable {
case maps
}

init(from decoder: Decoder) throws {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.maps = try container.decode([GlobalMockDirectoryConfig.Map].self, forKey: .maps).sorted(by: {$0.folder > $1.folder})
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/LetSee/Core/DirectoryProcessor/FileInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
//

import Foundation
struct FileInformation: Equatable, Comparable, FileInformationBasic {
var url: URL {
public struct FileInformation: Equatable, Comparable, FileInformationBasic {
public var url: URL {
self.filePath
}

static func < (lhs: FileInformation, rhs: FileInformation) -> Bool {
lhs.filePath < rhs.filePath
public static func < (lhs: FileInformation, rhs: FileInformation) -> Bool {
lhs.filePath.absoluteString < rhs.filePath.absoluteString
}

/// File name
let name: String
public let name: String
/// Original path
let filePath: URL
public let filePath: URL
/// Relative to the top mock folder
let relativePath: String
public let relativePath: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
//

import Foundation
protocol FileInformationBasic {
public protocol FileInformationBasic {
var url: URL {get}
}

extension URL: Comparable, FileInformationBasic {
var url: URL {
self
struct FileURL: Comparable, FileInformationBasic {
var url: URL
init(url: URL) {
self.url = url
}

public static func < (lhs: URL, rhs: URL) -> Bool {
lhs.absoluteString < rhs.absoluteString
static func < (lhs: Self, rhs: Self) -> Bool {
lhs.url.absoluteString < rhs.url.absoluteString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
//

import Foundation
protocol FileNameParsing {
public protocol FileNameParsing {
func parse(_ filePath: FileInformation) throws -> MockFileInformation
}
16 changes: 12 additions & 4 deletions Sources/LetSee/Core/DirectoryProcessor/MockFileInformation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
//

import Foundation
struct MockFileInformation: Equatable, Comparable, FileInformationBasic {
static func < (lhs: MockFileInformation, rhs: MockFileInformation) -> Bool {
public struct MockFileInformation: Equatable, Comparable, FileInformationBasic {
public static func < (lhs: MockFileInformation, rhs: MockFileInformation) -> Bool {
lhs.fileInformation < rhs.fileInformation
}

var url: URL {
public var url: URL {
fileInformation.url
}

enum MockStatus {
public enum MockStatus {
case success
case failure
}
Expand All @@ -29,4 +29,12 @@ struct MockFileInformation: Equatable, Comparable, FileInformationBasic {
let status: MockStatus?

let displayName: String

public init(fileInformation: FileInformation, statusCode: Int?, delay: TimeInterval?, status: MockStatus?, displayName: String) {
self.fileInformation = fileInformation
self.statusCode = statusCode
self.delay = delay
self.status = status
self.displayName = displayName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
protocol MockProcessing: DirectoryProcessing where Information == MockFileInformation {
public protocol MockProcessing: DirectoryProcessing where Information == MockFileInformation {
func process(_ path: String) throws -> Dictionary<DirectoryRequestPath, [Information]>
func buildMocks(_ path: String) throws -> Dictionary<String, Set<LetSeeMock>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation
struct RawDirectoryProcessor: DirectoryProcessing {
typealias Information = URL
typealias Information = FileURL
let fileManager: FileManager

init(fileManager: FileManager = .default) {
Expand All @@ -16,14 +16,14 @@ struct RawDirectoryProcessor: DirectoryProcessing {

func process(_ path: String) throws -> Dictionary<DirectoryRequestPath, [Information]> {
let url = URL(fileURLWithPath: path)
var files = Dictionary<DirectoryRequestPath, [URL]>()
var files = Dictionary<DirectoryRequestPath, [FileURL]>()
if let enumerator = fileManager.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey, .parentDirectoryURLKey], options: [ .skipsPackageDescendants]) {
for case let fileURL as URL in enumerator {
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey, .parentDirectoryURLKey])
if fileAttributes.isRegularFile!, let parentDirectory = fileAttributes.parentDirectory {
let directory = DirectoryRequestPath(path: parentDirectory, relativePath: "")
let fileURLs = (files[directory] ?? []) + [fileURL]
let fileURLs = (files[directory] ?? []) + [FileURL(url: fileURL) ]
files.updateValue(fileURLs , forKey: directory)
}
} catch { print(error, fileURL) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
protocol ScenarioProcessing: DirectoryProcessing where Information == FileInformation {
public protocol ScenarioProcessing: DirectoryProcessing where Information == FileInformation {
func process(_ path: String) throws -> Dictionary<DirectoryRequestPath, [Information]>
func buildScenarios(for path: String,
requestToMockMapper: (String) -> CategorisedMocks?,
Expand Down
27 changes: 15 additions & 12 deletions Sources/LetSee/Core/LetSee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ extension LetSee {
}
/// The LetSee object that serves as the entry point to the library and provides access to the features.
final public class LetSee: LetSeeProtocol {

internal(set) public var configuration: Configuration = .default
/// All available mocks that LetSee have found on the given mock directory
internal(set) public var mocks: Dictionary<String, Set<LetSeeMock>> = [:]
/// All available scenarios that LetSee have found on the given scenario directory
internal(set) public var scenarios: [Scenario] = []
/// a closure that is called when the mock state of the LetSee object changes. It takes a single argument, a Bool value indicating whether mock is enabled or not. It can be set or retrieved using the set and get functions.
public var onMockStateChanged: ((Bool) -> Void)?
public let fileToMockMapper: FileToLetSeeMockMapping
public var jsonFileParser: FileNameParsing
public let interceptor: LetSeeInterceptor
let mockProcessor: any MockProcessing
let fileManager: FileManager
let requestToMockMapper: RequestToMockMapper
var globalMockDirectoryConfigs: GlobalMockDirectoryConfig?
let scenarioProcessor: any ScenarioProcessing
init(configuration: Configuration = .default, fileManager: FileManager = .default,
fileToMockMapper: FileToLetSeeMockMapping = DefaultFileToLetSeeMockMapping(),
interceptor: LetSeeInterceptor = .init(),
mockProcessor: any MockProcessing = DefaultMockProcessor(),
scenarioProcessor: any ScenarioProcessing = DefaultScenarioProcessor(),
requestToMockMapper: @escaping RequestToMockMapper = DefaultRequestToMockMapper.transform
public init(configuration: Configuration = .default,
fileManager: FileManager = .default,
jsonFileParser: (any FileNameParsing)? = nil,
interceptor: LetSeeInterceptor? = nil,
mockProcessor: (any MockProcessing)? = nil,
scenarioProcessor: (any ScenarioProcessing)? = nil,
requestToMockMapper: RequestToMockMapper? = nil
) {
self.configuration = configuration
self.fileManager = fileManager
self.fileToMockMapper = fileToMockMapper
self.interceptor = interceptor
self.mockProcessor = mockProcessor
self.requestToMockMapper = requestToMockMapper
self.scenarioProcessor = scenarioProcessor
self.jsonFileParser = jsonFileParser ?? JSONFileNameParser()
self.interceptor = interceptor ?? LetSeeInterceptor()
self.mockProcessor = mockProcessor ?? DefaultMockProcessor(directoryProcessor: MockDirectoryProcessor(fileNameParser: self.jsonFileParser))
self.scenarioProcessor = scenarioProcessor ?? DefaultScenarioProcessor(mockFileNameParse: self.jsonFileParser)
self.requestToMockMapper = requestToMockMapper ?? DefaultRequestToMockMapper.transform

}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/LetSee/Core/Services/CategorisedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public struct CategorisedMocks: Hashable {
public var mocks: [LetSeeMock]
}

typealias RequestToMockMapper = ((_: URL, _ mocks: Dictionary<String, Set<LetSeeMock>>) -> CategorisedMocks?)
public typealias RequestToMockMapper = ((_: URL, _ mocks: Dictionary<String, Set<LetSeeMock>>) -> CategorisedMocks?)
2 changes: 1 addition & 1 deletion Sources/LetSee/Core/Services/LetSeeProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol LetSeeProtocol: AnyObject {

/// A closure that is called when the mock state of the LetSee object changes. It takes a single argument, a Bool value indicating whether mock is enabled or not. It can be set or retrieved using the set and get functions.
var onMockStateChanged: ((Bool) -> Void)? {set get}
var fileToMockMapper: FileToLetSeeMockMapping {get}
var jsonFileParser: FileNameParsing {get}
var interceptor: LetSeeInterceptor {get}

/// Sets the given `Configuration` for LetSee.
Expand Down
44 changes: 23 additions & 21 deletions Sources/LetSee/InAppView/ScenariosListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@ struct ScenariosListView: View {
}
public var body: some View {
DisclosureGroup(isExpanded: $isScenariosCollapsed, content: {
VStack(alignment: .leading, spacing: 16) {
HStack(spacing: 24){
if configs.isMockEnabled {
// ProgressView()
ScrollView{
VStack(alignment: .leading, spacing: 16) {
HStack(spacing: 24){
if configs.isMockEnabled {
// ProgressView()
}
}
}
if !self.viewModel.scenarios.isEmpty {
ForEach(self.viewModel.scenarios, id: \.name) { item in
Button {
self.viewModel.toggleScenario(item)
} label: {
ScenarioRow(isSelected: .constant(item == viewModel.selectedScenario), scenario: item)
if !self.viewModel.scenarios.isEmpty {
ForEach(self.viewModel.scenarios, id: \.name) { item in
Button {
self.viewModel.toggleScenario(item)
} label: {
ScenarioRow(isSelected: .constant(item == viewModel.selectedScenario), scenario: item)
}

Divider()
}

Divider()
} else {
Spacer()
Text("No Scenario is available.")
.font(.subheadline)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
Spacer()
}

} else {
Spacer()
Text("No Scenario is available.")
.font(.subheadline)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
Spacer()
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.frame(maxWidth: .infinity, alignment: .leading)
}.frame(maxHeight: 300)
}, label: {
VStack(alignment: .leading) {
if let selectedScenario = self.viewModel.selectedScenario, !isScenariosCollapsed {
Expand Down
2 changes: 1 addition & 1 deletion Tests/LetSeeTests/FileDirectoryProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class FileDirectoryProcessorTests: XCTestCase {
let mockFolderWithConfigs = try! sut.process(MockFileManager.defaultMocksDirectoryPath + "/FolderWithConfig")
XCTAssertEqual(expectedDirectoryNames, mockFolderWithConfigs.map({$0.key.path.lastPathComponent}).sorted())

expectedFileNames = [".pathconfigs.json", "error_rejectedPayment.json", "success_arrangementSingleItem.json", "success_validatedPayment.json"].sorted()
expectedFileNames = ["error_rejectedPayment.json", "success_arrangementSingleItem.json", "success_validatedPayment.json"].sorted()
XCTAssertEqual(expectedFileNames, mockFolderWithConfigs.flatMap({$0.value}).map({$0.url.lastPathComponent}).sorted())
}

Expand Down
Loading

0 comments on commit 17c746c

Please sign in to comment.