Tests/OrgCoreTests/PathsTests.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Tests/OrgCoreTests/PathsTests.swift history · blame · raw

41 lines · 2027 bytes

6 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct PathsTests {
 6    /// Unsandboxed, the real home folder is the one Foundation gives.
 7    @Test func homeIsFoundationsOutsideTheSandbox() {
 8        #expect(OrgPaths.home == NSHomeDirectory())
 9        #expect(OrgPaths.expandingTilde("~/org/a.org") == ("~/org/a.org" as NSString).expandingTildeInPath)
10    }
11
12    @Test func tildeExpandsToTheGivenHome() {
13        let home = "/Users/someone"
14        #expect(OrgPaths.expandingTilde("~", home: home) == home)
15        #expect(OrgPaths.expandingTilde("~/", home: home) == home)
16        #expect(OrgPaths.expandingTilde("~/org//notes/", home: home) == "/Users/someone/org/notes")
17        #expect(OrgPaths.expandingTilde("/srv/org/", home: home) == "/srv/org")
18        #expect(OrgPaths.expandingTilde("org/a.org", home: home) == "org/a.org")
19        #expect(OrgPaths.expandingTilde("~~", home: home) == "~~")
20        #expect(OrgPaths.expandingTilde("", home: home) == "")
21    }
22
23    @Test func otherUsersHomesAreFoundations() {
24        #expect(OrgPaths.expandingTilde("~root/x", home: "/Users/someone") == ("~root/x" as NSString).expandingTildeInPath)
25    }
26
27    @Test func abbreviatesTheGivenHome() {
28        let home = "/Users/someone"
29        #expect(OrgPaths.abbreviatingWithTilde("/Users/someone", home: home) == "~")
30        #expect(OrgPaths.abbreviatingWithTilde("/Users/someone/", home: home) == "~")
31        #expect(OrgPaths.abbreviatingWithTilde("/Users/someone/org//a.org", home: home) == "~/org/a.org")
32        #expect(OrgPaths.abbreviatingWithTilde("/Users/someoneelse/a.org", home: home) == "/Users/someoneelse/a.org")
33        #expect(OrgPaths.abbreviatingWithTilde("~/a.org", home: home) == "~/a.org")
34    }
35
36    @Test func abbreviatingMatchesFoundationOutsideTheSandbox() {
37        for path in [NSHomeDirectory(), NSHomeDirectory() + "/org//a.org/", "/tmp/a.org", "relative/a"] {
38            #expect(OrgPaths.abbreviatingWithTilde(path) == (path as NSString).abbreviatingWithTildeInPath)
39        }
40    }
41}