Skip to content

Commit

Permalink
Add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Zwick committed Oct 18, 2024
1 parent 3df669b commit c00a7c7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Source/ID3TagEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Foundation
*/
public class ID3TagEditor {
private let id3TagParser: ID3TagParser
private let id3TagCreator: ID3TagCreator
private let mp3FileReader: Mp3FileReader
private let mp3FileWriter: Mp3FileWriter
private let mp3WithID3TagBuilder: Mp3WithID3TagBuilder
Expand All @@ -21,9 +22,10 @@ public class ID3TagEditor {
*/
public init() {
self.id3TagParser = ID3TagParserFactory.make()
self.id3TagCreator = ID3TagCreatorFactory.make()
self.mp3FileReader = Mp3FileReaderFactory.make()
self.mp3FileWriter = Mp3FileWriter()
self.mp3WithID3TagBuilder = Mp3WithID3TagBuilder(id3TagCreator: ID3TagCreatorFactory.make(),
self.mp3WithID3TagBuilder = Mp3WithID3TagBuilder(id3TagCreator: id3TagCreator,
id3TagConfiguration: ID3TagConfiguration())
}

Expand Down Expand Up @@ -68,8 +70,8 @@ public class ID3TagEditor {
ID3 tag).
*/
public func write(tag: ID3Tag, to path: String, andSaveTo newPath: String? = nil) throws {
let newId3TagData = try mp3WithID3TagBuilder.build(mp3: Data(), newId3Tag: tag, currentId3Tag: nil)
let currentId3Tag = try read(from: path)
let newId3TagData = try id3TagCreator.create(id3Tag: tag)
try mp3FileWriter.write(newId3TagData: newId3TagData, currentId3Tag: currentId3Tag, fromPath: path, toPath: newPath ?? path)
}

Expand Down
1 change: 1 addition & 0 deletions Source/Mp3/Mp3FileReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Mp3FileReader {
}

inputStream.open()
defer { inputStream.close() }

let headerSize = id3TagConfiguration.headerSize()
let header = try read(bytesCount: headerSize, fromStream: inputStream)
Expand Down
37 changes: 37 additions & 0 deletions Tests/Mp3/Mp3FileWriterTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Mp3FileWriterTest.swift
// ID3TagEditor
//
// Created by Fabian Zwick on 18.10.24.
//

import Foundation
import Testing

@testable import ID3TagEditor

struct Mp3FileWriterTest {
@Test func testWritingWithCurrentTag() throws {
let newTag = ID32v3TagBuilder()
.title(frame: .init(content: "Test Title"))
.album(frame: .init(content: "Test Album"))
.build()

let id3TagCreator = ID3TagCreatorFactory.make()
let newId3TagData = try id3TagCreator.create(id3Tag: newTag)

let fromPath = PathLoader().pathFor(name: "example", fileType: "mp3")
let currentId3Tag = try ID3TagEditor().read(from: fromPath)

let pathMp3Generated = NSHomeDirectory() + "/testWritingWithCurrentTagAndEqualToPath.mp3"
#expect(throws: Never.self) { try Mp3FileWriter().write(newId3TagData: newId3TagData, currentId3Tag: currentId3Tag, fromPath: fromPath, toPath: pathMp3Generated) }

let readTag = try ID3TagEditor().read(from: pathMp3Generated)
#expect((readTag?.frames[.title] as? ID3FrameWithStringContent)?.content == "Test Title")
#expect((readTag?.frames[.album] as? ID3FrameWithStringContent)?.content == "Test Album")
}

static let allTests = [
("testWritingWithCurrentTag", testWritingWithCurrentTag)
]
}
8 changes: 7 additions & 1 deletion Tests/Parse/ID3TagSizeParserTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ struct ID3TagSizeParserTest {
#expect(ID3TagSizeParser().parse(data: mp3V23) == 245864)
}

@Test func parseTagSizeToBeModified() {
let mp3 = NSData(contentsOfFile: PathLoader().pathFor(name: "example-to-be-modified", fileType: "mp3"))!
#expect(ID3TagSizeParser().parse(data: mp3) < mp3.count)
}

static let allTests = [
("parseTagSizeV2", parseTagSizeV2),
("parseFrameContentSizeV3", parseTagSizeV3)
("parseFrameContentSizeV3", parseTagSizeV3),
("parseTagSizeToBeModified", parseTagSizeToBeModified)
]
}

0 comments on commit c00a7c7

Please sign in to comment.