on master help sign in
ADDED swift test file for syntax highlighting
fd01f0aAkshat Gadhwal
tests/syntax-tests/highlighted/Swift/test.swiftnew file+268
@@ -0,0 +1,268 @@
1+class Person {
2+// We can define class property here 
3+var age = 25
4+// Implement Class initializer. Initializers are called when a new object of this class is created 
5+init() { 
6+ print(“A new instance of this class Person is created.”) 
7+ } 
8+} 
9+// We can now create an instance of class Person - an object - by putting parentheses after the class name 
10+let personObj = Person()
11+// Once an instance of Person class is created we can access its properties using the dot “.” syntax. 
12+print(“This person age is \(personObj.age)”)
13+
14+import Foundation
15+class Friend : Comparable {
16+ let name : String
17+ let age : Int
18+ 
19+ init(name : String, age: Int) {
20+ self.name = name
21+ self.age = age
22+ }
23+}
24+func < (lhs: Friend, rhs: Friend) -> Bool {
25+ return lhs.age < rhs.age } func > (lhs: Friend, rhs: Friend) -> Bool {
26+ return lhs.age > rhs.age
27+}
28+func == (lhs: Friend, rhs: Friend) -> Bool {
29+ var returnValue = false
30+ if (lhs.name == rhs.name) && (lhs.age == rhs.age)
31+ {
32+ returnValue = true
33+ }
34+ return returnValue
35+}
36+
37+ let friend1 = Friend(name: "Sergey", age: 35)
38+ let friend2 = Friend(name: "Sergey", age: 30)
39+ 
40+ print("\Compare Friend object. Same person? (friend1 == friend2)")
41+
42+func sayHelloWorld() {
43+ print("Hello World")
44+}
45+// Call function 
46+sayHelloWorld()
47+
48+func printOutFriendNames(names: String...) {
49+ 
50+ for name in names {
51+ 
52+ print(name)
53+ }
54+ 
55+}
56+// Call the printOutFriendNames with two parameters 
57+printOutFriendNames("Sergey", "Bill")
58+// Call the function with more parameters 
59+printOutFriendNames("Sergey", "Bill", "Max")
60+
61+let simpleClosure = {
62+ print("From a simpleClosure")
63+}
64+// Call closure 
65+simpleClosure() 
66+
67+let fullName = { (firstName:String, lastName:String)->String in
68+ return firstName + " " + lastName
69+}
70+// Call Closure 
71+let myFullName = fullName("Sergey", "Kargopolov")
72+print("My full name is \(myFullName)")
73+
74+let myDictionary = [String:String]()
75+// Another way to create an empty dictionary 
76+let myDictionary2:[String:String] = [:]
77+// Keys in dictionary can also be of type Int 
78+let myDictionary3 = [Int:String]()
79+
80+var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
81+// print to preview 
82+print(myDictionary)
83+// Add a new key with a value 
84+myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"
85+// We should now have 3 key value pairs printed 
86+print(myDictionary)
87+
88+var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
89+// Loop through dictionary keys and print values 
90+for (key,value) in myDictionary {
91+ print("\(key) = \(value)")
92+}
93+ class Friend {
94+ let name : String
95+ let age : Int
96+ 
97+ init(name : String, age: Int) {
98+ self.name = name
99+ self.age = age
100+ }
101+}
102+
103+ var friends:[Friend] = []
104+ 
105+ let friend1 = Friend(name: "Sergey", age: 30)
106+ let friend2 = Friend(name: "Bill", age: 35)
107+ let friend3 = Friend(name: "Michael", age: 21)
108+ 
109+ friends.append(friend1)
110+ friends.append(friend2)
111+ friends.append(friend3)
112+ 
113+ printFriends(friends: friends)
114+ 
115+ // Get sorted array in descending order (largest to the smallest number) 
116+ let sortedFriends = friends.sorted(by: { $0.age > $1.age })
117+ printFriends(friends: sortedFriends)
118+ 
119+ // Get sorted array in ascending order (smallest to the largest number) 
120+ let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })
121+ printFriends(friends: sortedFriendsAscendingOrder)
122+
123+
124+ func printFriends(friends: [Friend])
125+ {
126+ for friendEntry in friends {
127+ print("Name: \(friendEntry.name), age: \(friendEntry.age)")
128+ }
129+ }
130+
131+import UIKit
132+class ViewController: UIViewController {
133+override func viewDidLoad() {
134+ super.viewDidLoad()
135+}
136+override func viewWillAppear(_ animated: Bool) {
137+ super.viewWillAppear(animated)
138+ 
139+ // Create destination URL  
140+ let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
141+ let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")
142+ 
143+ //Create URL to the source file you want to download 
144+ let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")
145+ 
146+ let sessionConfig = URLSessionConfiguration.default
147+ let session = URLSession(configuration: sessionConfig)
148+ 
149+ let request = URLRequest(url:fileURL!)
150+ 
151+ let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
152+ if let tempLocalUrl = tempLocalUrl, error == nil {
153+ // Success 
154+ if let statusCode = (response as? HTTPURLResponse)?.statusCode {
155+ print("Successfully downloaded. Status code: \(statusCode)")
156+ }
157+ 
158+ do {
159+ try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
160+ } catch (let writeError) {
161+ print("Error creating a file \(destinationFileUrl) : \(writeError)")
162+ }
163+ 
164+ } else {
165+ print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
166+ }
167+ }
168+ task.resume()
169+ 
170+ }
171+}
172+
173+ do {
174+ 
175+ // Convert JSON Object received from server side into Swift NSArray. 
176+ // Note the use "try" 
177+ if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
178+ }
179+ 
180+ } catch let error as NSError {
181+ print(error.localizedDescription)
182+ }
183+
184+DispatchQueue.global(qos: .userInitiated).async {
185+ // Do some time consuming task in this background thread 
186+ // Mobile app will remain to be responsive to user actions 
187+ 
188+ print("Performing time consuming task in this background thread")
189+ 
190+ DispatchQueue.main.async {
191+ // Task consuming task has completed 
192+ // Update UI from this block of code 
193+ print("Time consuming task has completed. From here we are allowed to update user interface.")
194+ }
195+ }
196+
197+import UIKit
198+class ViewController: UIViewController {
199+ 
200+ override func viewDidLoad() {
201+ super.viewDidLoad()
202+ // Do any additional setup after loading the view, typically from a nib. 
203+ 
204+ let button = UIButton(type: UIButtonType.system) as UIButton
205+ 
206+ let xPostion:CGFloat = 50
207+ let yPostion:CGFloat = 100
208+ let buttonWidth:CGFloat = 150
209+ let buttonHeight:CGFloat = 45
210+ 
211+ button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
212+ 
213+ button.backgroundColor = UIColor.lightGray
214+ button.setTitle("Tap me", for: UIControlState.normal)
215+ button.tintColor = UIColor.black
216+ button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)
217+ 
218+ self.view.addSubview(button)
219+ }
220+ 
221+ func buttonAction(_ sender:UIButton!)
222+ {
223+ print("Button tapped")
224+ }
225+ 
226+ override func didReceiveMemoryWarning() {
227+ super.didReceiveMemoryWarning()
228+ // Dispose of any resources that can be recreated. 
229+ }
230+ 
231+ 
232+}
233+
234+import UIKit
235+class ViewController: UIViewController {
236+ 
237+ override func viewDidLoad() {
238+ super.viewDidLoad()
239+ 
240+ //Create Activity Indicator 
241+ let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
242+ 
243+ // Position Activity Indicator in the center of the main view 
244+ myActivityIndicator.center = view.center
245+ 
246+ // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called 
247+ myActivityIndicator.hidesWhenStopped = false
248+ 
249+ // Start Activity Indicator 
250+ myActivityIndicator.startAnimating()
251+ 
252+ // Call stopAnimating() when need to stop activity indicator 
253+ //myActivityIndicator.stopAnimating() 
254+ 
255+ 
256+ view.addSubview(myActivityIndicator)
257+ }
258+ 
259+ override func didReceiveMemoryWarning() {
260+ super.didReceiveMemoryWarning()
261+ }
262+ 
263+}
264+
265+
266+
267+
268+
tests/syntax-tests/source/Swift/test.swiftnew file+268
@@ -0,0 +1,268 @@
1+class Person {
2+// We can define class property here
3+var age = 25
4+// Implement Class initializer. Initializers are called when a new object of this class is created
5+init() {
6+ print(“A new instance of this class Person is created.”)
7+ }
8+}
9+// We can now create an instance of class Person - an object - by putting parentheses after the class name
10+let personObj = Person()
11+// Once an instance of Person class is created we can access its properties using the dot “.” syntax.
12+print(“This person age is \(personObj.age)”)
13+
14+import Foundation
15+class Friend : Comparable {
16+ let name : String
17+ let age : Int
18+
19+ init(name : String, age: Int) {
20+ self.name = name
21+ self.age = age
22+ }
23+}
24+func < (lhs: Friend, rhs: Friend) -> Bool {
25+ return lhs.age < rhs.age } func > (lhs: Friend, rhs: Friend) -> Bool {
26+ return lhs.age > rhs.age
27+}
28+func == (lhs: Friend, rhs: Friend) -> Bool {
29+ var returnValue = false
30+ if (lhs.name == rhs.name) && (lhs.age == rhs.age)
31+ {
32+ returnValue = true
33+ }
34+ return returnValue
35+}
36+
37+ let friend1 = Friend(name: "Sergey", age: 35)
38+ let friend2 = Friend(name: "Sergey", age: 30)
39+
40+ print("\Compare Friend object. Same person? (friend1 == friend2)")
41+
42+func sayHelloWorld() {
43+ print("Hello World")
44+}
45+// Call function
46+sayHelloWorld()
47+
48+func printOutFriendNames(names: String...) {
49+
50+ for name in names {
51+
52+ print(name)
53+ }
54+
55+}
56+// Call the printOutFriendNames with two parameters
57+printOutFriendNames("Sergey", "Bill")
58+// Call the function with more parameters
59+printOutFriendNames("Sergey", "Bill", "Max")
60+
61+let simpleClosure = {
62+ print("From a simpleClosure")
63+}
64+// Call closure
65+simpleClosure()
66+
67+let fullName = { (firstName:String, lastName:String)->String in
68+ return firstName + " " + lastName
69+}
70+// Call Closure
71+let myFullName = fullName("Sergey", "Kargopolov")
72+print("My full name is \(myFullName)")
73+
74+let myDictionary = [String:String]()
75+// Another way to create an empty dictionary
76+let myDictionary2:[String:String] = [:]
77+// Keys in dictionary can also be of type Int
78+let myDictionary3 = [Int:String]()
79+
80+var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
81+// print to preview
82+print(myDictionary)
83+// Add a new key with a value
84+myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"
85+// We should now have 3 key value pairs printed
86+print(myDictionary)
87+
88+var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
89+// Loop through dictionary keys and print values
90+for (key,value) in myDictionary {
91+ print("\(key) = \(value)")
92+}
93+ class Friend {
94+ let name : String
95+ let age : Int
96+
97+ init(name : String, age: Int) {
98+ self.name = name
99+ self.age = age
100+ }
101+}
102+
103+ var friends:[Friend] = []
104+
105+ let friend1 = Friend(name: "Sergey", age: 30)
106+ let friend2 = Friend(name: "Bill", age: 35)
107+ let friend3 = Friend(name: "Michael", age: 21)
108+
109+ friends.append(friend1)
110+ friends.append(friend2)
111+ friends.append(friend3)
112+
113+ printFriends(friends: friends)
114+
115+ // Get sorted array in descending order (largest to the smallest number)
116+ let sortedFriends = friends.sorted(by: { $0.age > $1.age })
117+ printFriends(friends: sortedFriends)
118+
119+ // Get sorted array in ascending order (smallest to the largest number)
120+ let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })
121+ printFriends(friends: sortedFriendsAscendingOrder)
122+
123+
124+ func printFriends(friends: [Friend])
125+ {
126+ for friendEntry in friends {
127+ print("Name: \(friendEntry.name), age: \(friendEntry.age)")
128+ }
129+ }
130+
131+import UIKit
132+class ViewController: UIViewController {
133+override func viewDidLoad() {
134+ super.viewDidLoad()
135+}
136+override func viewWillAppear(_ animated: Bool) {
137+ super.viewWillAppear(animated)
138+
139+ // Create destination URL
140+ let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
141+ let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")
142+
143+ //Create URL to the source file you want to download
144+ let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")
145+
146+ let sessionConfig = URLSessionConfiguration.default
147+ let session = URLSession(configuration: sessionConfig)
148+
149+ let request = URLRequest(url:fileURL!)
150+
151+ let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
152+ if let tempLocalUrl = tempLocalUrl, error == nil {
153+ // Success
154+ if let statusCode = (response as? HTTPURLResponse)?.statusCode {
155+ print("Successfully downloaded. Status code: \(statusCode)")
156+ }
157+
158+ do {
159+ try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
160+ } catch (let writeError) {
161+ print("Error creating a file \(destinationFileUrl) : \(writeError)")
162+ }
163+
164+ } else {
165+ print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
166+ }
167+ }
168+ task.resume()
169+
170+ }
171+}
172+
173+ do {
174+
175+ // Convert JSON Object received from server side into Swift NSArray.
176+ // Note the use "try"
177+ if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
178+ }
179+
180+ } catch let error as NSError {
181+ print(error.localizedDescription)
182+ }
183+
184+DispatchQueue.global(qos: .userInitiated).async {
185+ // Do some time consuming task in this background thread
186+ // Mobile app will remain to be responsive to user actions
187+
188+ print("Performing time consuming task in this background thread")
189+
190+ DispatchQueue.main.async {
191+ // Task consuming task has completed
192+ // Update UI from this block of code
193+ print("Time consuming task has completed. From here we are allowed to update user interface.")
194+ }
195+ }
196+
197+import UIKit
198+class ViewController: UIViewController {
199+
200+ override func viewDidLoad() {
201+ super.viewDidLoad()
202+ // Do any additional setup after loading the view, typically from a nib.
203+
204+ let button = UIButton(type: UIButtonType.system) as UIButton
205+
206+ let xPostion:CGFloat = 50
207+ let yPostion:CGFloat = 100
208+ let buttonWidth:CGFloat = 150
209+ let buttonHeight:CGFloat = 45
210+
211+ button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
212+
213+ button.backgroundColor = UIColor.lightGray
214+ button.setTitle("Tap me", for: UIControlState.normal)
215+ button.tintColor = UIColor.black
216+ button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)
217+
218+ self.view.addSubview(button)
219+ }
220+
221+ func buttonAction(_ sender:UIButton!)
222+ {
223+ print("Button tapped")
224+ }
225+
226+ override func didReceiveMemoryWarning() {
227+ super.didReceiveMemoryWarning()
228+ // Dispose of any resources that can be recreated.
229+ }
230+
231+
232+}
233+
234+import UIKit
235+class ViewController: UIViewController {
236+
237+ override func viewDidLoad() {
238+ super.viewDidLoad()
239+
240+ //Create Activity Indicator
241+ let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
242+
243+ // Position Activity Indicator in the center of the main view
244+ myActivityIndicator.center = view.center
245+
246+ // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called
247+ myActivityIndicator.hidesWhenStopped = false
248+
249+ // Start Activity Indicator
250+ myActivityIndicator.startAnimating()
251+
252+ // Call stopAnimating() when need to stop activity indicator
253+ //myActivityIndicator.stopAnimating()
254+
255+
256+ view.addSubview(myActivityIndicator)
257+ }
258+
259+ override func didReceiveMemoryWarning() {
260+ super.didReceiveMemoryWarning()
261+ }
262+
263+}
264+
265+
266+
267+
268+