From d6018fd849000490a1ec9ae22b8eec46b58afff7 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Wed, 3 May 2017 22:10:19 +0200 Subject: [PATCH] GH-433 Add UrlCommonsTest --- VimR.xcodeproj/project.pbxproj | 8 ++ VimR/FoundationCommons.swift | 26 +++---- VimRTests/UrlCommonsTest.swift | 78 +++++++++++++++++++ .../resources/UrlCommonsTest/.dot-hidden-file | 0 .../UrlCommonsTest/dummy.rtfd/TXT.rtf | 8 ++ 5 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 VimRTests/UrlCommonsTest.swift create mode 100644 VimRTests/resources/UrlCommonsTest/.dot-hidden-file create mode 100644 VimRTests/resources/UrlCommonsTest/dummy.rtfd/TXT.rtf diff --git a/VimR.xcodeproj/project.pbxproj b/VimR.xcodeproj/project.pbxproj index 139df324..3e8aeb84 100644 --- a/VimR.xcodeproj/project.pbxproj +++ b/VimR.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1929B0032058D7A00000234A /* UrlCommonsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BB6C37013C2F120279B6 /* UrlCommonsTest.swift */; }; 1929B00BA624DA8DC75F7E02 /* SerializableStates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BA42AB6F1BF631B57399 /* SerializableStates.swift */; }; 1929B05B9D664052EC2D23EF /* FileOutlineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BCE3E156C06EDF1F2806 /* FileOutlineView.swift */; }; 1929B08C6230B9C5AB72DAF1 /* Pref128ToCurrentConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B5046239709E33516F5C /* Pref128ToCurrentConverter.swift */; }; @@ -106,6 +107,7 @@ 4B4192181D0C52D700A0BEB2 /* Grid.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B4192171D0C52D700A0BEB2 /* Grid.swift */; }; 4B5011F81EBA67EB00F76C46 /* RxTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B5011F71EBA67EB00F76C46 /* RxTest.framework */; }; 4B5011F91EBA69E200F76C46 /* RxTest.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4B5011F71EBA67EB00F76C46 /* RxTest.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 4B5011FB1EBA6FD700F76C46 /* UrlCommonsTest in Resources */ = {isa = PBXBuildFile; fileRef = 4B5011FA1EBA6FD700F76C46 /* UrlCommonsTest */; }; 4B56F2931D29903F00C1F92E /* SwiftNeoVimTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B56F2921D29903F00C1F92E /* SwiftNeoVimTests.swift */; }; 4B56F2951D29903F00C1F92E /* SwiftNeoVim.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B2A2BF71D0351810074CE9A /* SwiftNeoVim.framework */; }; 4B56F29D1D29926600C1F92E /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B56F29B1D29926600C1F92E /* Nimble.framework */; }; @@ -384,6 +386,7 @@ 1929BB2AD21A10A0ECA66A5E /* ToolsPref.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToolsPref.swift; sourceTree = ""; }; 1929BB55946DAEBF55D24048 /* GeneralPref.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralPref.swift; sourceTree = ""; }; 1929BB6608B4F0E037CA0F4C /* States.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = States.swift; sourceTree = ""; }; + 1929BB6C37013C2F120279B6 /* UrlCommonsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UrlCommonsTest.swift; sourceTree = ""; }; 1929BB6CFF4CC0B5E8B00C62 /* DataWrapper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DataWrapper.m; sourceTree = ""; }; 1929BB6EB0EC313E228EBC9C /* DictionaryCommonsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryCommonsTest.swift; sourceTree = ""; }; 1929BBC84557C8351EC6183E /* FileItemIgnorePatternTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileItemIgnorePatternTest.swift; sourceTree = ""; }; @@ -431,6 +434,7 @@ 4B401B191D046E0600D99EDC /* NeoVimViewDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NeoVimViewDelegate.swift; sourceTree = ""; }; 4B4192171D0C52D700A0BEB2 /* Grid.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Grid.swift; sourceTree = ""; }; 4B5011F71EBA67EB00F76C46 /* RxTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxTest.framework; path = Carthage/Build/Mac/RxTest.framework; sourceTree = SOURCE_ROOT; }; + 4B5011FA1EBA6FD700F76C46 /* UrlCommonsTest */ = {isa = PBXFileReference; lastKnownFileType = folder; path = UrlCommonsTest; sourceTree = ""; }; 4B56F2901D29903F00C1F92E /* SwiftNeoVimTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftNeoVimTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 4B56F2921D29903F00C1F92E /* SwiftNeoVimTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftNeoVimTests.swift; sourceTree = ""; }; 4B56F2941D29903F00C1F92E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -623,6 +627,7 @@ 1929B41F745CDCDFE09ACDCF /* resources */ = { isa = PBXGroup; children = ( + 4B5011FA1EBA6FD700F76C46 /* UrlCommonsTest */, 4BDF500B1D760A3500D8FBC3 /* FileUtilsTest */, ); path = resources; @@ -963,6 +968,7 @@ 1929BB6EB0EC313E228EBC9C /* DictionaryCommonsTest.swift */, 1929BE20FFBB2D828DB30D81 /* StringCommonsTest.swift */, 1929BCB25C82D93EA66C5FA5 /* RxSwiftCommonsTest.swift */, + 1929BB6C37013C2F120279B6 /* UrlCommonsTest.swift */, ); path = VimRTests; sourceTree = ""; @@ -1311,6 +1317,7 @@ buildActionMask = 2147483647; files = ( 4BDF500C1D760A3500D8FBC3 /* FileUtilsTest in Resources */, + 4B5011FB1EBA6FD700F76C46 /* UrlCommonsTest in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1522,6 +1529,7 @@ 1929B9FDD1FFCC81C83EFA69 /* DictionaryCommonsTest.swift in Sources */, 1929B87BEB0B00619DEC2A7F /* StringCommonsTest.swift in Sources */, 1929B7BDD88EEE171D452595 /* RxSwiftCommonsTest.swift in Sources */, + 1929B0032058D7A00000234A /* UrlCommonsTest.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/VimR/FoundationCommons.swift b/VimR/FoundationCommons.swift index 4331ec51..bd6096c1 100644 --- a/VimR/FoundationCommons.swift +++ b/VimR/FoundationCommons.swift @@ -64,6 +64,18 @@ extension URL { return self.deletingLastPathComponent() } + var isDir: Bool { + return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue) + } + + var isHidden: Bool { + return self.resourceValue(URLResourceKey.isHiddenKey.rawValue) + } + + var isPackage: Bool { + return self.resourceValue(URLResourceKey.isPackageKey.rawValue) + } + /// Wrapper function for NSURL.getResourceValue for Bool values. /// Returns also `false` when /// - there is no value for the given `key` or @@ -71,7 +83,7 @@ extension URL { /// /// - parameters: /// - key: The `key`-parameter of `NSURL.getResourceValue`. - func resourceValue(_ key: String) -> Bool { + fileprivate func resourceValue(_ key: String) -> Bool { var rsrc: AnyObject? do { @@ -88,16 +100,4 @@ extension URL { return false } - - var isDir: Bool { - return self.resourceValue(URLResourceKey.isDirectoryKey.rawValue) - } - - var isHidden: Bool { - return self.resourceValue(URLResourceKey.isHiddenKey.rawValue) - } - - var isPackage: Bool { - return self.resourceValue(URLResourceKey.isPackageKey.rawValue) - } } diff --git a/VimRTests/UrlCommonsTest.swift b/VimRTests/UrlCommonsTest.swift new file mode 100644 index 00000000..17c81b9f --- /dev/null +++ b/VimRTests/UrlCommonsTest.swift @@ -0,0 +1,78 @@ +/** + * Tae Won Ha - http://taewon.de - @hataewon + * See LICENSE + */ + +import XCTest +import Nimble +@testable import VimR + +class UrlCommonsTest: XCTestCase { + + func testIsDirectParent() { + let parent = URL(fileURLWithPath: "/some/path") + let child = URL(fileURLWithPath: "/some/path/text.txt") + let noChild1 = URL(fileURLWithPath: "/some/where/else/text.txt") + let noChild2 = URL(fileURLWithPath: "/some/text.txt") + + expect(parent.isDirectParent(of: child)).to(beTrue()) + expect(parent.isDirectParent(of: noChild1)).to(beFalse()) + expect(parent.isDirectParent(of: noChild2)).to(beFalse()) + } + + func testIsParent() { + let parent = URL(fileURLWithPath: "/some/path") + let child1 = URL(fileURLWithPath: "/some/path/text.txt") + let child2 = URL(fileURLWithPath: "/some/path/deep/text.txt") + let noChild1 = URL(fileURLWithPath: "/some/where/else/text.txt") + let noChild2 = URL(fileURLWithPath: "/some/text.txt") + + expect(parent.isParent(of: child1)).to(beTrue()) + expect(parent.isParent(of: child2)).to(beTrue()) + expect(parent.isParent(of: noChild1)).to(beFalse()) + expect(parent.isParent(of: noChild2)).to(beFalse()) + } + + func testIsContained() { + let parent = URL(fileURLWithPath: "/some/path") + let child1 = URL(fileURLWithPath: "/some/path/text.txt") + let child2 = URL(fileURLWithPath: "/some/path/deep/text.txt") + let noChild1 = URL(fileURLWithPath: "/some/where/else/text.txt") + let noChild2 = URL(fileURLWithPath: "/some/text.txt") + + expect(child1.isContained(in: parent)).to(beTrue()) + expect(child2.isContained(in: parent)).to(beTrue()) + expect(noChild1.isContained(in: parent)).to(beFalse()) + expect(noChild2.isContained(in: parent)).to(beFalse()) + } + + func testParent() { + expect(URL(fileURLWithPath: "/some/path/").parent).to(equal(URL(fileURLWithPath: "/some/"))) + expect(URL(fileURLWithPath: "/some/path/text.txt").parent).to(equal(URL(fileURLWithPath: "/some/path/"))) + expect(URL(fileURLWithPath: "/").parent).to(equal(URL(fileURLWithPath: "/"))) + } + + func testIsDir() { + let resourceUrl = Bundle.init(for: type(of: self)).url(forResource: "UrlCommonsTest", withExtension: "")! + let hidden = resourceUrl.appendingPathComponent(".dot-hidden-file") + + expect(resourceUrl.isDir).to(beTrue()) + expect(hidden.isDir).to(beFalse()) + } + + func testIsHidden() { + let resourceUrl = Bundle.init(for: type(of: self)).url(forResource: "UrlCommonsTest", withExtension: "")! + let hidden = resourceUrl.appendingPathComponent(".dot-hidden-file") + + expect(hidden.isHidden).to(beTrue()) + expect(resourceUrl.isHidden).to(beFalse()) + } + + func testIsPackage() { + let resourceUrl = Bundle.init(for: type(of: self)).url(forResource: "UrlCommonsTest", withExtension: "")! + let package = resourceUrl.appendingPathComponent("dummy.rtfd") + + expect(package.isPackage).to(beTrue()) + expect(resourceUrl.isPackage).to(beFalse()) + } +} diff --git a/VimRTests/resources/UrlCommonsTest/.dot-hidden-file b/VimRTests/resources/UrlCommonsTest/.dot-hidden-file new file mode 100644 index 00000000..e69de29b diff --git a/VimRTests/resources/UrlCommonsTest/dummy.rtfd/TXT.rtf b/VimRTests/resources/UrlCommonsTest/dummy.rtfd/TXT.rtf new file mode 100644 index 00000000..c7248945 --- /dev/null +++ b/VimRTests/resources/UrlCommonsTest/dummy.rtfd/TXT.rtf @@ -0,0 +1,8 @@ +{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf820 +{\fonttbl\f0\fswiss\fcharset0 Helvetica;} +{\colortbl;\red255\green255\blue255;} +{\*\expandedcolortbl;;} +\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0 +\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0 + +\f0\fs24 \cf0 Dummy RTFD} \ No newline at end of file