Skip to content

Commit

Permalink
unit-test: some samples of what I did for guidance
Browse files Browse the repository at this point in the history
  • Loading branch information
devahmedshendy committed Aug 10, 2023
1 parent 36ffc69 commit 184a396
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Tests/ContributeWordPressTests/DynamicRedirectGeneratorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import XCTest
@testable import ContributeWordPress

final class DynamicRedirectGeneratorTests: XCTestCase {

private var generator: DynamicRedirectGenerator!

override func setUp() {
generator = DynamicRedirectGenerator()
}

func test() throws {
let site = try Self.makeWordPressSite(
posts: [try Self.myYearInReviewPost(), try Self.podcastingPost()]
)
let items = generator.redirects(fromSites: ["blogs" : site])

print(items[0])
print(items[1])
}

}
//URL(string: "https://leogdion.name")!
72 changes: 72 additions & 0 deletions Tests/ContributeWordPressTests/File.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Foundation
import SyndiKit
@testable import ContributeWordPress

let item: RSSItem = .init(
title: "diagram-for-goals",
link: URL(string: "https://leogdion.name/2018/01/08/from-goals-to-actions-2018/diagram-for-goals/")!,
description: "This is a description",
guid: .url(URL(string: "https://leogdion.name/wp-content/uploads/2018/01/diagram-for-goals.png")!),
pubDate: .distantPast,
contentEncoded: .init(),
categoryTerms: [.Uncategorized],
content: .init(),
itunesTitle: "itunes Title",
itunesEpisode: .init(10),
itunesAuthor: "itunesAuthor",
itunesSubtitle: "itunesSubtitle",
itunesSummary: "itunesSummary",
itunesExplicit: "itunesExplicit",
itunesDuration: 10,
itunesImage: nil,
enclosure: nil,
creators: ["Shendy"],
wpCommentStatus: .init(),
wpPingStatus: .init(),
wpStatus: .init(),
wpPostParent: .bitWidth,
wpMenuOrder: .bitWidth,
wpIsSticky: .bitWidth,
wpPostPassword: .init(),
wpPostID: 10,
wpPostDate: .distantPast,
wpPostDateGMT: .distantFuture,
wpModifiedDate: .distantFuture,
wpModifiedDateGMT: nil,
wpPostName: .init(),
wpPostType: "post",
wpPostMeta: nil,
wpAttachmentURL: nil,
mediaContent: nil,
mediaThumbnail: nil
)

internal extension WordPressElements.Category {
static let TopMenu: Self = .TopMenu
}

internal extension RSSItemCategory {
static let TopMenu: Self = .init(
value: "Top Menu",
domain: "nav_menu",
nicename: "top-menu"
)

static let Primary: Self = .init(
value: "Primary",
domain: "nav_menu",
nicename: "primary"
)

static let Uncategorized: Self = .init(
value: "Uncategorized",
domain: "category",
nicename: "uncategorized"
)

static let Podcasting: Self = .init(
value: "Podcasting",
domain: "post_tag",
nicename: "podcasting"
)
}
35 changes: 35 additions & 0 deletions Tests/ContributeWordPressTests/MarkdownProcessorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import XCTest
import Contribute
@testable import ContributeWordPress

// This seems an integration testing, than unit test.
//internal final class MarkdownProcessorTests: XCTestCase {
// internal func test() throws {
// let processor = MarkdownProcessor(
// redirectFromatter: <#T##RedirectFormatter#>,
// postFilters: <#T##[PostFilter]#>
// )
//
// try processor.begin(withSettings: <#T##ProcessorSettings#>)
// }
//}
//
//final class MockProcessorSettings: ProcessorSettings {
// var contentPathURL: URL
//
// var resourcesPathURL: URL
//
// var exportsDirectoryURL: URL
//
// var importAssetPathURL: URL?
//
// var overwriteAssets: Bool
//
// var skipDownload: Bool
//
// var assetRelativePath: String
//
// func markdownFrom(html: String) throws -> String {
// PandocMarkdownGenerator(shellOut: <#(String, [String]) throws -> String#>).markdown(fromHTML: html)
// }
//}
51 changes: 51 additions & 0 deletions Tests/ContributeWordPressTests/NetlifyRedirectFormatterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import XCTest
@testable import ContributeWordPress

final class NetlifyRedirectFormatterTests: XCTestCase {

private var formatter: NetlifyRedirectFormatter!

override func setUp() {
formatter = NetlifyRedirectFormatter()
}

func testEmptyRedirectItemsShouldReturnEmptyString() {
let redirects = formatter.formatRedirects([])

XCTAssertTrue(redirects.isEmpty)
}

func testNumberOfRedirectItemsShouldReturn() {
let redirectItems: [RedirectItem] = [
.init(
fromURLPath: "/blog/2014/04/01/hello-emscripten",
redirectURLPath: "/articles/hello-emscripten"
),
.init(
fromURLPath: "/blog/2018/10/11/new-apple-watch-4",
redirectURLPath: "/articles/new-apple-watch-4"
)
]

let redirects = formatter.formatRedirects(redirectItems)
let lines = redirects.split(separator: "\n")

XCTAssertEqual(lines.count, redirectItems.count)
}

func testNumberOfRedirectItemsShouldReturnCorrectLinePerRedirect() {
let redirectItems: [RedirectItem] = [
.init(
fromURLPath: "/blog/2014/04/01/hello-emscripten",
redirectURLPath: "/articles/hello-emscripten"
)
]

let redirects = formatter.formatRedirects(redirectItems)

let firstLine = redirects.split(separator: "\n").first!
let lineParts = firstLine.split(separator: "\t")

XCTAssertTrue(lineParts.count == 2)
}
}
49 changes: 49 additions & 0 deletions Tests/ContributeWordPressTests/RegexKeyPostFilterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import XCTest
import SyndiKit
@testable import ContributeWordPress

final class RegexKeyPostFilterTests: XCTestCase {

func testNoFiltersShouldMatchEverything() throws {
let filters: [RegexKeyPostFilter] = []

let posts: [WordPressPost] = [
try Self.makeWordPressPost(title: "1st-post", type: "attachment"),
try Self.makeWordPressPost(title: "2d-post", type: "post"),
try Self.makeWordPressPost(title: "3rd-post", type: "post")
]

let matched = posts.filter { post in
filters.allSatisfy { $0.include(post) }
}

XCTAssertEqual(matched.count, posts.count)
}

func testSingleFilterShouldMatchSinglePost() throws {
let filter = try RegexKeyPostFilter(pattern: "post", keyPath: \.type)

let post = try Self.makeWordPressPost(title: "single-post")

XCTAssertTrue(filter.include(post))
}

func testMultipleFiltersShouldOnlyMatchPublishedPosts() throws {
let filters: [RegexKeyPostFilter] = [
try RegexKeyPostFilter(pattern: "post", keyPath: \.type),
try RegexKeyPostFilter(pattern: "publish", keyPath: \.status),
]

let posts: [WordPressPost] = [
try Self.makeWordPressPost(title: "1st-post", type: "attachment"),
try Self.makeWordPressPost(title: "2d-post", type: "post"),
try Self.makeWordPressPost(title: "3rd-post", type: "post")
]

let matched = posts.filter { post in
filters.allSatisfy { $0.include(post) }
}

XCTAssertTrue(matched.count == 2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import XCTest
import SyndiKit
@testable import ContributeWordPress

final class SpecFrontMatterTranslatorTests: XCTestCase {

private var translator: SpecFrontMatterTranslator!

override func setUp() {
translator = .init()
}

func test() throws {
let featuredImage = "/media/wp-assets/leogdion/2019/01/image-e1547230562842-1024x682.jpg"
let source = Source(
sectionName: "blogs",
post: try Self.makeWordPressPost(title: "diagram-for-goals"),
featuredImage: featuredImage
)

let specs = translator.frontMatter(from: source)

XCTAssertEqual(specs.title, source.post.title)
// TODO: Not sure how to test this
// XCTAssertEqual(specs.date, String(describing: source.post.pubDate))
XCTAssertEqual(specs.description, nil)
XCTAssertEqual(specs.featuredImage, featuredImage)
}
}
22 changes: 22 additions & 0 deletions Tests/ContributeWordPressTests/Temporary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation

// This is the same one used in PandocMarkdownGenerator.Temporary,
// Not sure where I should
public enum Temporary {
/// The URL of the temporary directory.
private static let temporaryDirURL = URL(
fileURLWithPath: NSTemporaryDirectory(),
isDirectory: true
)

/// Creates a temporary file from the given content.
///
/// - Parameter content: The content of the temporary file.
/// - Returns: The URL of the created temporary file.
/// - Throws: An error if the temporary file creation fails.
public static func file(fromContent content: String) throws -> URL {
let temporaryFileURL = temporaryDirURL.appendingPathComponent(UUID().uuidString)
try content.write(to: temporaryFileURL, atomically: true, encoding: .utf8)
return temporaryFileURL
}
}
Loading

0 comments on commit 184a396

Please sign in to comment.