oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1import RastrilloApple
2import RastrilloNative
3import XCTest
4@testable import DocsKit
5
6/// Spec §6: every subtitle row, its words, style and announcement.
7final class DocsSubtitleTests: XCTestCase {
8 private let en = DocsStrings(locale: Locale(identifier: "en"))
9
10 private func row(_ status: SyncStatus, access: DocsAccess = .writable, saving: Bool = false, loading: Bool = false) -> DocsSubtitleRow {
11 DocsSubtitleRow(access: access, status: status, savingShown: saving, loading: loading)
12 }
13
14 func testEveryRowInPrecedenceOrder() {
15 let held = SyncStatus.failed(pending: 1, reason: .held(.accessLost))
16 // 1 beats everything.
17 XCTAssertEqual(row(held, access: .unavailable), .unavailable)
18 XCTAssertEqual(row(held), .failedHeld)
19 XCTAssertEqual(row(.failed(pending: 0, reason: .cannotSaveOnDevice)), .cannotSaveOnDevice)
20 XCTAssertEqual(row(.failed(pending: 2, reason: .tooManyUnsent)), .tooManyUnsent)
21 XCTAssertEqual(row(.needsSignIn(pending: 3)), .needsSignIn(pending: 3))
22 XCTAssertEqual(row(.needsSignIn(pending: 0)), .signedOut)
23 XCTAssertEqual(row(.offline(pending: 2)), .offline(pending: 2))
24 XCTAssertEqual(row(.retrying(pending: 1)), .retrying(pending: 1))
25 // 9 only once the debounce says so.
26 XCTAssertEqual(row(.saving), .saved)
27 XCTAssertEqual(row(.saving, saving: true), .saving)
28 XCTAssertEqual(row(.saving, access: .readOnly), .viewOnly)
29 XCTAssertEqual(row(.saved, access: .readOnly), .viewOnly)
30 XCTAssertEqual(row(.saved), .saved)
31 // 13: the team alone while the document loads.
32 XCTAssertEqual(row(.saved, loading: true), .loading)
33 // A negative row still shows while loading: it is news.
34 XCTAssertEqual(row(.offline(pending: 1), loading: true), .offline(pending: 1))
35 }
36
37 func testWordsAndJoin() {
38 XCTAssertEqual(en.subtitle(team: "Tito", row: .saved), "Tito · Saved")
39 XCTAssertEqual(en.subtitle(team: "Tito", row: .viewOnly), "Tito · View only")
40 XCTAssertEqual(en.subtitle(team: "Tito", row: .unavailable), "Tito · No longer available")
41 XCTAssertEqual(en.subtitle(team: "Tito", row: .signedOut), "Tito · Signed out")
42 XCTAssertEqual(en.subtitle(team: nil, row: .saved), "Saved", "no team: the state alone, no separator")
43 XCTAssertEqual(en.subtitle(team: "", row: .saved), "Saved")
44 XCTAssertEqual(en.subtitle(team: "Tito", row: .loading), "Tito", "loading: the team only")
45 let sync = NativeSyncStrings.kit(localization: "en")
46 XCTAssertEqual(en.state(.offline(pending: 2)), sync.offline(2))
47 XCTAssertEqual(en.state(.needsSignIn(pending: 3)), sync.needsSignIn(3))
48 XCTAssertEqual(en.state(.retrying(pending: 1)), sync.retrying)
49 XCTAssertEqual(en.state(.saving), sync.saving)
50 XCTAssertEqual(en.state(.failedHeld), sync.failed(.held(.tooLarge)))
51 XCTAssertEqual(en.state(.cannotSaveOnDevice), sync.failed(.cannotSaveOnDevice))
52 XCTAssertEqual(en.state(.tooManyUnsent), sync.failed(.tooManyUnsent))
53 }
54
55 func testStyles() {
56 for negative in [DocsSubtitleRow.unavailable, .failedHeld, .cannotSaveOnDevice, .tooManyUnsent, .needsSignIn(pending: 1), .signedOut] {
57 XCTAssertTrue(negative.isNegative, "\(negative)")
58 }
59 for muted in [DocsSubtitleRow.offline(pending: 1), .retrying(pending: 1), .saving, .viewOnly, .saved, .loading] {
60 XCTAssertFalse(muted.isNegative, "\(muted)")
61 }
62 XCTAssertTrue(DocsSubtitleRow.failedHeld.isEmphasised)
63 XCTAssertFalse(DocsSubtitleRow.unavailable.isEmphasised)
64 }
65
66 func testAnnouncements() {
67 // Entering offline, needs sign-in, failed or unavailable is news.
68 for new in [DocsSubtitleRow.offline(pending: 1), .needsSignIn(pending: 1), .signedOut, .failedHeld, .cannotSaveOnDevice, .tooManyUnsent, .unavailable] {
69 XCTAssertTrue(DocsSubtitleRow.announces(from: .saved, to: new), "\(new)")
70 }
71 // A count changing inside one kind is not; saving never is.
72 XCTAssertFalse(DocsSubtitleRow.announces(from: .offline(pending: 1), to: .offline(pending: 2)))
73 XCTAssertFalse(DocsSubtitleRow.announces(from: .needsSignIn(pending: 2), to: .signedOut))
74 XCTAssertFalse(DocsSubtitleRow.announces(from: .saved, to: .saving))
75 XCTAssertFalse(DocsSubtitleRow.announces(from: .saving, to: .saved))
76 XCTAssertFalse(DocsSubtitleRow.announces(from: .saved, to: .retrying(pending: 1)))
77 // View only is news mid-session, not as a reader's window opens.
78 XCTAssertTrue(DocsSubtitleRow.announces(from: .saved, to: .viewOnly))
79 XCTAssertFalse(DocsSubtitleRow.announces(from: .loading, to: .viewOnly))
80 }
81
82 /// Spec §8: every key the new views use resolves in all twelve tables,
83 /// and no value is its own key.
84 func testEveryNewKeyResolvesInEveryLanguage() throws {
85 let keys = ["docs.editor.toolbar.history", "docs.editor.toolbar.group.style", "docs.editor.toolbar.group.text",
86 "docs.editor.toolbar.group.align", "docs.editor.toolbar.group.lists", "docs.editor.inspector.label",
87 "docs.editor.inspector.toggle", "docs.share.action", "native.subtitle.saved", "native.subtitle.viewOnly",
88 "native.subtitle.joined", "native.subtitle.unavailable", "native.unavailable.body", "native.subtitle.signedOut",
89 "native.format.show", "native.format.hide",
90 // Reused by the header, inspector and menus.
91 "docs.editor.toolbar.undo", "docs.editor.toolbar.redo", "docs.editor.toolbar.bold", "docs.editor.toolbar.italic",
92 "docs.editor.toolbar.code", "docs.editor.toolbar.align.left", "docs.editor.toolbar.align.center",
93 "docs.editor.toolbar.align.right", "docs.editor.toolbar.bulletlist", "docs.editor.toolbar.orderedlist",
94 "docs.editor.toolbar.quote", "docs.editor.toolbar.block", "docs.editor.toolbar.block.paragraph",
95 "docs.editor.toolbar.font", "docs.editor.toolbar.size", "docs.editor.menu.format.indent",
96 "docs.editor.menu.format.outdent", "docs.editor.menu.format.clear", "docs.editor.menu.format",
97 "docs.editor.menu.tools", "docs.editor.menu.tools.wordcount", "docs.editor.menu.tools.shortcuts",
98 "docs.editor.menu.view.paged", "docs.editor.menu.view.infinite", "docs.editor.menu.file.open",
99 "docs.document.rename", "native.close", "native.retry", "native.discard.action"]
100 let languages = DocsResources.bundle.localizations.filter { $0 != "Base" }
101 XCTAssertEqual(Set(languages), ["ar", "bn", "en", "es", "ga", "hi", "ja", "pt", "ru", "vi", "yue", "zh-Hans"])
102 for language in languages {
103 let strings = DocsStrings(locale: Locale(identifier: language))
104 XCTAssertEqual(strings.localization, language)
105 for key in keys {
106 let value = strings(key)
107 XCTAssertNotEqual(value, key, "\(language): \(key) has no value")
108 XCTAssertFalse(value.isEmpty, "\(language): \(key) is empty")
109 }
110 let joined = strings("native.subtitle.joined")
111 XCTAssertTrue(joined.contains("%1$@") && joined.contains("%2$@"), "\(language): the join keeps both placeholders")
112 XCTAssertFalse(strings.subtitle(team: "T", row: .saved).contains("%"), language)
113 }
114 }
115
116 /// S3's keys (the iPhone document, spec §8): every one resolves in all
117 /// twelve tables, and no value is its own key.
118 func testEveryIPhoneDocumentKeyResolvesInEveryLanguage() {
119 let keys = ["native.more", "native.keyboard.hide", "native.format.size.smaller", "native.format.size.larger", "native.back", "native.rename.tooLong",
120 "docs.editor.menu.help", "native.colophon", "docs.editor.menu.tools.wordcount", "docs.editor.menu.tools.shortcuts",
121 "docs.editor.menu.file.open", "docs.editor.menu.view.paged", "docs.editor.menu.view.infinite", "native.export",
122 "docs.export.pdf.label", "docs.export.markdown.label", "docs.editor.menu.file.print", "docs.document.rename",
123 "docs.editor.menu.file.delete", "docs.share.action", "docs.editor.inspector.label", "docs.editor.inspector.toggle",
124 "docs.editor.toolbar.font", "docs.editor.toolbar.size", "native.close.pending.cancel", "docs.list.create.placeholder"]
125 for language in DocsResources.bundle.localizations.filter({ $0 != "Base" }) {
126 let strings = DocsStrings(locale: Locale(identifier: language))
127 for key in keys {
128 let value = strings(key)
129 XCTAssertNotEqual(value, key, "\(language): \(key) has no value")
130 XCTAssertFalse(value.isEmpty, "\(language): \(key) is empty")
131 }
132 }
133 }
134}
135