Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ContribtorTests to use SwiftTesting #322

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 47 additions & 45 deletions Basic-Car-Maintenance-Tests/Shared/Models/ContributorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,65 @@
// Created by Drag0ndust on 07.10.23.
//

import XCTest
import Testing
import Foundation
Comment on lines +8 to +9
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import Testing
import Foundation
import Foundation
import Testing

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just switching these to be in alphabetical order

@testable import Basic_Car_Maintenance

final class ContributorTests: XCTestCase {
private var jsonDecoder: JSONDecoder!
private var jsonEncoder: JSONEncoder!
private let testContributorJson = """
{
"login": "Drag0ndust",
"id": 12915108,
"node_id": "MDQ6VXNlcjEyOTE1MTA4",
"avatar_url": "https://avatars.githubusercontent.com/u/12915108?v=4",
"url": "https://api.github.com/users/Drag0ndust",
"html_url": "https://github.com/Drag0ndust",
"contributions": 0
}
"""

override func setUpWithError() throws {
jsonDecoder = JSONDecoder()
struct ContributorTests {
private let jsonDecoder = {
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase

jsonEncoder = JSONEncoder()
return jsonDecoder
}()

private let jsonEncoder = {
let jsonEncoder = JSONEncoder()
jsonEncoder.keyEncodingStrategy = .convertToSnakeCase
return jsonEncoder
}()

@Test()
func contributorDecoding() throws {
let testContributorJson = """
{
"login": "Drag0ndust",
"id": 12915108,
"node_id": "MDQ6VXNlcjEyOTE1MTA4",
"avatar_url": "https://avatars.githubusercontent.com/u/12915108?v=4",
"url": "https://api.github.com/users/Drag0ndust",
"html_url": "https://github.com/Drag0ndust",
"contributions": 0
}

func testContributorDecoding() throws {
guard let data = testContributorJson.data(using: .utf8) else {
XCTFail("Can't decode JSON string")
return
}

"""
let data = try #require(testContributorJson.data(using: .utf8))
let sut = try jsonDecoder.decode(Contributor.self, from: data)

XCTAssertEqual(sut.login, "Drag0ndust")
XCTAssertEqual(sut.id, 12915108)
XCTAssertEqual(sut.nodeID, "MDQ6VXNlcjEyOTE1MTA4")
XCTAssertEqual(sut.avatarURL, "https://avatars.githubusercontent.com/u/12915108?v=4")
XCTAssertEqual(sut.url, "https://api.github.com/users/Drag0ndust")
XCTAssertEqual(sut.htmlURL, "https://github.com/Drag0ndust")
XCTAssertEqual(sut.contributions, 0)
#expect(sut.login == "Drag0ndust")
#expect(sut.id == 12915108)
#expect(sut.nodeID == "MDQ6VXNlcjEyOTE1MTA4")
#expect(sut.avatarURL == "https://avatars.githubusercontent.com/u/12915108?v=4")
#expect(sut.url == "https://api.github.com/users/Drag0ndust")
#expect(sut.htmlURL == "https://github.com/Drag0ndust")
#expect(sut.contributions == 0)
}

func testContributorEncoding() throws {
let contributor = Contributor(login: "Drag0ndust",
id: 12915108,
nodeID: "MDQ6VXNlcjEyOTE1MTA4",
avatarURL: "https://avatars.githubusercontent.com/u/12915108?v=4",
url: "https://api.github.com/users/Drag0ndust",
htmlURL: "https://github.com/Drag0ndust",
contributions: 0)
@Test()
func contributorEncoding() throws {
let contributor = Contributor(
login: "Drag0ndust",
id: 12915108,
nodeID: "MDQ6VXNlcjEyOTE1MTA4",
avatarURL: "https://avatars.githubusercontent.com/u/12915108?v=4",
url: "https://api.github.com/users/Drag0ndust",
htmlURL: "https://github.com/Drag0ndust",
contributions: 0
)

let contributorData = try jsonEncoder.encode(contributor)

// now decode it again and check if the objects are equal
// now decode it again and check if the objects are equal
let decodedContributor = try jsonDecoder.decode(Contributor.self, from: contributorData)

XCTAssertEqual(contributor, decodedContributor)
#expect(contributor == decodedContributor)
}
}
Loading