Skip to content

Commit

Permalink
<Description> : Adding NSAttributed string data extension.
Browse files Browse the repository at this point in the history
<Type> : feature/test
  • Loading branch information
Khalian authored and Sanyal committed Feb 13, 2017
1 parent c9a2469 commit 849232a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
19 changes: 11 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,38 @@ Add subsequent changes that you make in this section.
3. **CGRect**
- `var area` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/366) by *Khalian*

3. **Date**
4. **Data**
- `var attributedString` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/379) by *Khalian*

5. **Date**
- `var isFuture` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/343) by *Khalian*
- `var isPast` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/343) by *Khalian*
- `var era` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/374) by *Khalian*
- `var iso8601` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/375) by *Khalian*

4. **Double**
6. **Double**
- `var abs` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/365) by *Khalian*

5. **Float**
7. **Float**
- `static func random() -> Float` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/351) by *Khalian*
- `static func random(within: Range<Float>) -> Float` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/351) by *Khalian*

6. **Int**
8. **Int**
- `var digitArray` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/373) by *Khalian*
- `static func random(within: Range<Int>) -> Int` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/360) by *Khalian*

7. **String**
9. **String**
- `func urlDecoded() -> String` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/358) by *Khalian*
- `mutating func urlDecode() -> String` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/358) by *Khalian*

8. **UInt**
10. **UInt**
- `static gcd(_ firstNum:UInt, _ secondNum:UInt) -> UInt` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/347) by *Khalian*
- `static lcm(_ firstNum:UInt, _ secondNum:UInt) -> UInt` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/347)by *Khalian*

9. **UIEdgeInsets**
11. **UIEdgeInsets**
- `init(inset: CGFloat)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/368) by *furuyan*

10. **UILabel**
12. **UILabel**
- `set(text _text: String?, duration: TimeInterval)` in [[PR]](https://github.com/goktugyil/EZSwiftExtensions/pull/340) by *vilapuigvila*

## [Version 1.8]
Expand Down
8 changes: 8 additions & 0 deletions EZSwiftExtensions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
31F868DB1C2B6E5E00542250 /* DoubleExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31F868DA1C2B6E5E00542250 /* DoubleExtensions.swift */; };
5A3292F01DC5BA6F0078F7F3 /* CGPointExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3292EF1DC5BA6F0078F7F3 /* CGPointExtensions.swift */; };
5A3292F41DC5BCC50078F7F3 /* CGPointTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5A3292F31DC5BCC50078F7F3 /* CGPointTests.swift */; };
5AB87AC71E50C205008A0B86 /* DataExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AB87AC61E50C205008A0B86 /* DataExtensions.swift */; };
5AB87AC91E50C2F4008A0B86 /* DataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AB87AC81E50C2F4008A0B86 /* DataTests.swift */; };
7605D3121C81FC180046FAC3 /* ArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7605D3021C81FBB80046FAC3 /* ArrayTests.swift */; };
7605D3131C81FC180046FAC3 /* BlockButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7605D3031C81FBB80046FAC3 /* BlockButtonTests.swift */; };
7605D3141C81FC180046FAC3 /* BoolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7605D3041C81FBB80046FAC3 /* BoolTests.swift */; };
Expand Down Expand Up @@ -233,6 +235,8 @@
31F868DA1C2B6E5E00542250 /* DoubleExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DoubleExtensions.swift; sourceTree = "<group>"; };
5A3292EF1DC5BA6F0078F7F3 /* CGPointExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPointExtensions.swift; sourceTree = "<group>"; };
5A3292F31DC5BCC50078F7F3 /* CGPointTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGPointTests.swift; sourceTree = "<group>"; };
5AB87AC61E50C205008A0B86 /* DataExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataExtensions.swift; sourceTree = "<group>"; };
5AB87AC81E50C2F4008A0B86 /* DataTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTests.swift; sourceTree = "<group>"; };
7605D3021C81FBB80046FAC3 /* ArrayTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayTests.swift; sourceTree = "<group>"; };
7605D3031C81FBB80046FAC3 /* BlockButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlockButtonTests.swift; sourceTree = "<group>"; };
7605D3041C81FBB80046FAC3 /* BoolTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BoolTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -415,6 +419,7 @@
5A3292F31DC5BCC50078F7F3 /* CGPointTests.swift */,
E10A79651D6F65AF00735549 /* CGRectTests.swift */,
E10A79661D6F65AF00735549 /* CharacterTests.swift */,
5AB87AC81E50C2F4008A0B86 /* DataTests.swift */,
7605D3081C81FBB80046FAC3 /* DateTests.swift */,
7605D3061C81FBB80046FAC3 /* DictionaryTests.swift */,
E10A79671D6F65AF00735549 /* DoubleTests.swift */,
Expand Down Expand Up @@ -472,6 +477,7 @@
5A3292EF1DC5BA6F0078F7F3 /* CGPointExtensions.swift */,
B5DC87091C0ED34300972D0A /* CGRectExtensions.swift */,
E178781F1C867E9400BC05AA /* CharacterExtensions.swift */,
5AB87AC61E50C205008A0B86 /* DataExtensions.swift */,
E19AC0E81DCBA26500908905 /* DateExtensions.swift */,
B5DC870A1C0ED34300972D0A /* DictionaryExtensions.swift */,
31F868DA1C2B6E5E00542250 /* DoubleExtensions.swift */,
Expand Down Expand Up @@ -715,6 +721,7 @@
E15484E21CA2BCF0003B030E /* ErrorTypeExtensions.swift in Sources */,
31F868DB1C2B6E5E00542250 /* DoubleExtensions.swift in Sources */,
FADFAA631D9D8C2F00CF9F7A /* FloatingPointExtensions.swift in Sources */,
5AB87AC71E50C205008A0B86 /* DataExtensions.swift in Sources */,
E19AC0E61DCBA25100908905 /* BundleExtensions.swift in Sources */,
B5DC871C1C0ED34300972D0A /* ArrayExtensions.swift in Sources */,
E10A79461D6F506400735549 /* UIStoryboardExtensions.swift in Sources */,
Expand Down Expand Up @@ -814,6 +821,7 @@
E10A79AD1D6F65AF00735549 /* UISliderTests.swift in Sources */,
E10A79991D6F65AF00735549 /* UIApplicationTests.swift in Sources */,
7605D3151C81FC180046FAC3 /* CGFloatTests.swift in Sources */,
5AB87AC91E50C2F4008A0B86 /* DataTests.swift in Sources */,
E10A79A11D6F65AF00735549 /* UIColorTests.swift in Sources */,
7605D3181C81FC180046FAC3 /* DateTests.swift in Sources */,
E10A79AF1D6F65AF00735549 /* UIStoryboardTests.swift in Sources */,
Expand Down
41 changes: 41 additions & 0 deletions EZSwiftExtensionsTests/DataTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// DataTests.swift
// EZSwiftExtensions
//
// Created by Sanyal, Arunav on 2/12/17.
// Copyright © 2017 Goktug Yilmaz. All rights reserved.
//

import XCTest
import EZSwiftExtensions

class DataTests: XCTestCase {

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

func testNSAttributedStringSanityTest() {
let data = Data()
XCTAssertEqual(data.attributedString, NSAttributedString())
}

func testNSAttributedStringTest() {

// We can alternatively store the existing google.com contents on a file
// to stabilize this test case.
let url = URL(string:"http://www.google.com")
do {
let data = try Data(contentsOf: url!)
XCTAssertEqual(data.attributedString?.length, 217)

// TODO : Write additional tests on attrs, e.g. the font family
} catch _ {
XCTFail()
}
}
}
23 changes: 23 additions & 0 deletions Sources/DataExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// DataExtensions.swift
// EZSwiftExtensions
//
// Created by Sanyal, Arunav on 2/12/17.
// Copyright © 2017 Goktug Yilmaz. All rights reserved.
//

import Foundation

extension Data {

/// EZSE : Attributed string from data
/// Found it here : http://stackoverflow.com/questions/39248092/nsattributedstring-extension-in-swift-3
public var attributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch let error {
NSLog(error.localizedDescription)
}
return nil
}
}

0 comments on commit 849232a

Please sign in to comment.