0001    //
0002    //  StringTests.swift
0003    //  SourceKitten
0004    //
0005    //  Created by JP Simard on 2015-01-03.
0006    //  Copyright (c) 2015 SourceKitten. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    import SourceKittenFramework
0011    import XCTest
0012    
0013    class StringTests
main_.swift:18
    StringTests(),
: XCTestCase { 0014 0015 // protocol XCTestCaseProvider 0016 lazy var allTests: [(String, () throws -> Void)] = [ 0017 ("testStringByRemovingCommonLeadingWhitespaceFromLines", 0018 self.testStringByRemovingCommonLeadingWhitespaceFromLines), 0019 ("testStringByTrimmingTrailingCharactersInSet", 0020 self.testStringByTrimmingTrailingCharactersInSet), 0021 ("testCommentBody", self.testCommentBody), 0022 ("testIsSwiftFile", self.testIsSwiftFile), 0023 ("testIsObjectiveCHeaderFile", self.testIsObjectiveCHeaderFile), 0024 ("testAbsolutePath", self.testAbsolutePath), 0025 ("testIsTokenDocumentable", self.testIsTokenDocumentable), 0026 ("testParseDeclaration", self.testParseDeclaration), 0027 ("testGenerateDocumentedTokenOffsets", self.testGenerateDocumentedTokenOffsets), 0028 ("testDocumentedTokenOffsetsWithSubscript", self.testDocumentedTokenOffsetsWithSubscript), 0029 ("testGenerateDocumentedTokenOffsetsEmpty", self.testGenerateDocumentedTokenOffsetsEmpty), 0030 ("testSubstringWithByteRange", self.testSubstringWithByteRange), 0031 ("testSubstringLinesWithByteRange", self.testSubstringLinesWithByteRange), 0032 ("testLineRangeWithByteRange", self.testLineRangeWithByteRange), 0033 ] 0034 0035 func testStringByRemovingCommonLeadingWhitespaceFromLines
StringTests.swift:18
            self.testStringByRemovingCommonLeadingWhitespaceFromLines),
() { 0036 var input = "a\n b\n c" 0037 XCTAssertEqual(input.stringByRemovingCommonLeadingWhitespaceFromLines(), input) 0038 0039 input = " a\n b\n c" 0040 XCTAssertEqual(input.stringByRemovingCommonLeadingWhitespaceFromLines(), "a\n b\n c") 0041 } 0042 0043 func testStringByTrimmingTrailingCharactersInSet
StringTests.swift:20
            self.testStringByTrimmingTrailingCharactersInSet),
() { 0044 XCTAssertEqual("".stringByTrimmingTrailingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), "") 0045 XCTAssertEqual(" a ".stringByTrimmingTrailingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), " a") 0046 XCTAssertEqual(" ".stringByTrimmingTrailingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), "") 0047 XCTAssertEqual("a".stringByTrimmingTrailingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()), "a") 0048 } 0049 0050 func testCommentBody
StringTests.swift:21
        ("testCommentBody", self.testCommentBody),
() { 0051 var commentString = "/// Single line comment.\n\n" 0052 XCTAssertEqual(commentString.commentBody()!, "Single line comment.") 0053 0054 commentString = "/// Multiple\n/// single line comments.\n\n" 0055 XCTAssertEqual(commentString.commentBody()!, "Multiple\nsingle line comments.") 0056 0057 commentString = "/**\nMultiple\nline\ncomments.\n*/" 0058 XCTAssertEqual(commentString.commentBody()!, "Multiple\nline\ncomments.") 0059 0060 commentString = "/*\nNot a documentation comment.\n*/" 0061 XCTAssertNil(commentString.commentBody()) 0062 0063 commentString = "// Not a documentation comment." 0064 XCTAssertNil(commentString.commentBody()) 0065 0066 commentString = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n /// Multiple\n /// single line comments.\n\n" 0067 XCTAssertEqual(commentString.commentBody()!, "Multiple\n single line comments.") 0068 0069 commentString = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n /**\n Multiple\n line\n comments.\n */" 0070 XCTAssertEqual(commentString.commentBody()!, "Multiple\n line\ncomments.") 0071 } 0072 0073 func testIsSwiftFile
StringTests.swift:22
        ("testIsSwiftFile", self.testIsSwiftFile),
() { 0074 let good = ["good.swift"] 0075 let bad = [ 0076 "bad.swift.", 0077 ".swift.bad", 0078 "badswift", 0079 "bad.Swift" 0080 ] 0081 XCTAssertEqual((good + bad).filter({ $0.isSwiftFile() }), good, "should parse Swift files in an Array") 0082 } 0083 0084 func testIsObjectiveCHeaderFile
StringTests.swift:23
        ("testIsObjectiveCHeaderFile", self.testIsObjectiveCHeaderFile),
() { 0085 let good = [ 0086 "good.h", 0087 "good.hpp", 0088 "good.hh" 0089 ] 0090 let bad = [ 0091 "bad.h.", 0092 ".hpp.bad", 0093 "badshh", 0094 "bad.H" 0095 ] 0096 XCTAssertEqual((good + bad).filter({ $0.isObjectiveCHeaderFile() }), good, "should parse Objective-C header files in an Array") 0097 } 0098 0099 func testAbsolutePath
StringTests.swift:24
        ("testAbsolutePath", self.testAbsolutePath),
() { 0100 XCTAssert(("LICENSE".absolutePathRepresentation() as NSString).absolutePath, "absolutePathRepresentation() of a relative path should be an absolute path") 0101 XCTAssertEqual(__FILE__.absolutePathRepresentation(), __FILE__, "absolutePathRepresentation() should return the caller if it's already an absolute path") 0102 } 0103 0104 func testIsTokenDocumentable
StringTests.swift:25
        ("testIsTokenDocumentable", self.testIsTokenDocumentable),
() { 0105 let source = "struct A { subscript(key: String) -> Void { return () } }" 0106 let actual = SyntaxMap(file: File(contents: source)).tokens.filter(source.isTokenDocumentable) 0107 let expected = [ 0108 SyntaxToken(type: SyntaxKind.Identifier.rawValue, offset: 7, length: 1), // `A` 0109 SyntaxToken(type: SyntaxKind.Keyword.rawValue, offset: 11, length: 9), // `subscript` 0110 SyntaxToken(type: SyntaxKind.Identifier.rawValue, offset: 21, length: 3) // `key` 0111 ] 0112 XCTAssertEqual(actual, expected, "should detect documentable tokens") 0113 } 0114 0115 func testParseDeclaration
StringTests.swift:26
        ("testParseDeclaration", self.testParseDeclaration),
() { 0116 let dict: [String: SourceKitRepresentable] = [ 0117 "key.kind": "source.lang.swift.decl.class", 0118 "key.offset": Int64(24), 0119 "key.bodyoffset": Int64(32), 0120 "key.annotated_decl": "", 0121 "key.typename": "ClassA.Type" 0122 ] 0123 // This string is a regression test for https://github.com/jpsim/SourceKitten/issues/35 . 0124 let file = File(contents: "/**\nใ€€ใปใ’\n*/\nclass ClassA {\n}\n") 0125 XCTAssertEqual("class ClassA", file.parseDeclaration(dict)!, "should extract declaration from source text") 0126 } 0127 0128 func testGenerateDocumentedTokenOffsets
StringTests.swift:27
        ("testGenerateDocumentedTokenOffsets", self.testGenerateDocumentedTokenOffsets),
() { 0129 let fileContents = "/// Comment\nlet global = 0" 0130 let syntaxMap = SyntaxMap(file: File(contents: fileContents)) 0131 XCTAssertEqual(fileContents.documentedTokenOffsets(syntaxMap), [16], "should generate documented token offsets") 0132 } 0133 0134 func testDocumentedTokenOffsetsWithSubscript
StringTests.swift:28
        ("testDocumentedTokenOffsetsWithSubscript", self.testDocumentedTokenOffsetsWithSubscript),
() { 0135 let file = File(path: fixturesDirectory + "Subscript.swift")! 0136 let syntaxMap = SyntaxMap(file: file) 0137 XCTAssertEqual(file.contents.documentedTokenOffsets(syntaxMap), [54], "should generate documented token offsets") 0138 } 0139 0140 func testGenerateDocumentedTokenOffsetsEmpty
StringTests.swift:29
        ("testGenerateDocumentedTokenOffsetsEmpty", self.testGenerateDocumentedTokenOffsetsEmpty),
() { 0141 let fileContents = "// Comment\nlet global = 0" 0142 let syntaxMap = SyntaxMap(file: File(contents: fileContents)) 0143 XCTAssertEqual(fileContents.documentedTokenOffsets(syntaxMap).count, 0, "shouldn't detect any documented token offsets when there are none") 0144 } 0145 0146 func testSubstringWithByteRange
StringTests.swift:30
        ("testSubstringWithByteRange", self.testSubstringWithByteRange),
() { 0147 let string = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง123" 0148 XCTAssertEqual(string.substringWithByteRange(start: 0, length: 25)!, "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง") 0149 XCTAssertEqual(string.substringWithByteRange(start: 25, length: 1)!, "1") 0150 } 0151 0152 func testSubstringLinesWithByteRange
StringTests.swift:31
        ("testSubstringLinesWithByteRange", self.testSubstringLinesWithByteRange),
() { 0153 let string = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n123" 0154 XCTAssertEqual(string.substringLinesWithByteRange(start: 0, length: 0)!, "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n") 0155 XCTAssertEqual(string.substringLinesWithByteRange(start: 0, length: 25)!, "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n") 0156 XCTAssertEqual(string.substringLinesWithByteRange(start: 0, length: 26)!, "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n") 0157 XCTAssertEqual(string.substringLinesWithByteRange(start: 0, length: 27)!, string) 0158 XCTAssertEqual(string.substringLinesWithByteRange(start: 27, length: 0)!, "123") 0159 } 0160 0161 func testLineRangeWithByteRange
StringTests.swift:32
        ("testLineRangeWithByteRange", self.testLineRangeWithByteRange),
() { 0162 XCTAssert("".lineRangeWithByteRange(start: 0, length: 0) == nil) 0163 let string = "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง\n123" 0164 XCTAssert(string.lineRangeWithByteRange(start: 0, length: 0)! == (1, 1)) 0165 XCTAssert(string.lineRangeWithByteRange(start: 0, length: 25)! == (1, 1)) 0166 XCTAssert(string.lineRangeWithByteRange(start: 0, length: 26)! == (1, 2)) 0167 XCTAssert(string.lineRangeWithByteRange(start: 0, length: 27)! == (1, 2)) 0168 XCTAssert(string.lineRangeWithByteRange(start: 27, length: 0)! == (2, 2)) 0169 } 0170 } 0171 0172 typealias LineRangeType
StringTests.swift:174
func ==(lhs: LineRangeType, rhs: LineRangeType) -> Bool {
StringTests.swift:174
func ==(lhs: LineRangeType, rhs: LineRangeType) -> Bool {
= (start: Int, end: Int) 0173 0174 func ==(lhs: LineRangeType, rhs: LineRangeType) -> Bool { 0175 return lhs.start == rhs.start && lhs.end == rhs.end 0176 } 0177