-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathWorkbook.swift
84 lines (73 loc) · 2.96 KB
/
Workbook.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2019-2020 CoreOffice contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Max Desiatov on 23/11/2018.
//
@testable import CoreXLSX
import XCTest
import XMLCoder
private let parsedSheet = [
Workbook.Sheet(name: "Sheet 1",
id: "1",
relationship: "rId4"),
]
// swiftlint:disable line_length
private let workbookNoViews =
"""
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main"><workbookPr/><sheets><sheet state="visible" name="Summary" sheetId="1" r:id="rId4"/><sheet state="visible" name="General" sheetId="2" r:id="rId5"/></sheets><definedNames/><calcPr/></workbook>
""".data(using: .utf8)!
// swiftlint:enable line_length
private let expectedWorkbook =
Workbook(views: nil, sheets: .init(items: [
.init(
name: "Summary",
id: "1",
relationship: "rId4"
),
.init(
name: "General",
id: "2",
relationship: "rId5"
),
]))
final class WorkbookTests: XCTestCase {
func testWorkbook() throws {
guard let file =
XLSXFile(filepath: "\(fixturesPath)/categories.xlsx")
else {
XCTFail("failed to open the file")
return
}
let wbs = try file.parseWorkbooks()
XCTAssertEqual(wbs[0].sheets.items, parsedSheet)
}
func testWorksheetPathsAndNames() throws {
guard let file =
XLSXFile(filepath: "\(fixturesPath)/categories.xlsx")
else {
XCTFail("failed to open the file")
return
}
let wbs = try file.parseWorkbooks()
let sheets = try file.parseWorksheetPathsAndNames(workbook: wbs[0])
XCTAssertEqual(sheets.map { $0.name }, ["Sheet 1"])
}
func testWorkbookNoViews() throws {
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = true
let decoded = try decoder.decode(Workbook.self, from: workbookNoViews)
XCTAssertEqual(decoded, expectedWorkbook)
}
}