forked from quire-io/SwiftyChrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChronoJSXCTestCase.swift
166 lines (140 loc) · 7.15 KB
/
ChronoJSXCTestCase.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// ChronoJSXCTestCase.swift
// SwiftyChrono
//
// Created by Jerry Chen on 2/6/17.
// Copyright © 2017 Potix. All rights reserved.
//
import XCTest
import JavaScriptCore
public protocol ChronoJSTestable {
func evalJS(_ script: String, fileName: String)
}
public class ChronoJSXCTestCase: XCTestCase, ChronoJSTestable {
let jsContext = JSContext()!
let chrono = Chrono()
var testTitle = ""
/// we assign each js call from chrono parse to those variables, so the value while test fail occur will be the corresponding value
var lastResultsForFailCase: [ParsedResult] = []
var lastTextForFailCase: String = ""
// current fail test in JS test file within same text source
var nthTest = 0
// used to calculate diff of [nthTest]
var _lastText = ""
public func evalJS(_ script: String, fileName: String) {
print("start to test: \(fileName)")
jsContext.evaluateScript(script)
}
override public func setUp() {
super.setUp()
Chrono.preferredLanguage = nil
/// Handle Error
jsContext.exceptionHandler = { context, exception in
let stacktrace = exception?.objectForKeyedSubscript("stack").toString()
let lineNumber = exception?.objectForKeyedSubscript("line").toString()
let column = exception?.objectForKeyedSubscript("column")
assertionFailure("error: \(String(describing: exception?.description))\n stack trace: \(String(describing: stacktrace))\n line number: \(lineNumber ?? "") \ncolumn: \(String(describing: column))")
}
/// register classs to js context
//jsContext.setObject(TestParsedResult.self, forKeyedSubscript: "ParsedResult" as (NSCopying & NSObjectProtocol)!)
//jsContext.setObject(TestParsedComponents.self, forKeyedSubscript: "ParsedComponents" as (NSCopying & NSObjectProtocol)!)
/// set function callbacks
// setTestTitle(title: String)
let setTestTitle: @convention(block) (String) -> Void = { title in
self.testTitle = title
}
jsContext.setObject(unsafeBitCast(setTestTitle, to: AnyObject.self), forKeyedSubscript: "setTestTitle" as (NSCopying & NSObjectProtocol))
/// set function callbacks
// clearTestTitle()
let clearTestTitle: @convention(block) () -> Void = { () in
self.testTitle = ""
}
jsContext.setObject(unsafeBitCast(clearTestTitle, to: AnyObject.self), forKeyedSubscript: "clearTestTitle" as (NSCopying & NSObjectProtocol))
/// set function callbacks
// ok(passed: Bool, message: String)
let ok: @convention(block) (Bool, String?) -> Void = { (passed, message) in
if self._lastText != self.lastTextForFailCase {
self._lastText = self.lastTextForFailCase
self.nthTest = 1
} else {
self.nthTest += 1
}
if !passed {
print("the \(self.nthTest) case of text:\"\(self.lastTextForFailCase)\", results: \(String(describing: self.lastResultsForFailCase.first?.start.date))")
}
if let message = message {
XCTAssert(passed, message)
} else {
XCTAssert(passed)
}
}
jsContext.setObject(unsafeBitCast(ok, to: AnyObject.self), forKeyedSubscript: "ok" as (NSCopying & NSObjectProtocol))
/// set function callbacks
// chronoParse()
let chronoParse: @convention(block) (String, NSDate, NSDictionary, String) -> NSArray = { (text, ref, opt, mode) -> NSArray in
let chrono = mode == "strict" ? Chrono.strict : mode == "casual" ? Chrono.casual : self.chrono
self.lastTextForFailCase = text
var opts = [OptionType: Int]()
for o in opt {
opts[OptionType(rawValue: o.key as! String)!] = o.value as? Int ?? 1
}
let parseResults = chrono.parse(text: text, refDate: ref as Date, opt: opts)
let results = parseResults.map{ TestParsedResult($0) }
self.lastResultsForFailCase = parseResults
return results as NSArray
}
jsContext.setObject(unsafeBitCast(chronoParse, to: AnyObject.self), forKeyedSubscript: "chronoParse" as (NSCopying & NSObjectProtocol))
/// set function callbacks
// chronoParseDate()
let chronoParseDate: @convention(block) (String, NSDate, NSDictionary, String) -> NSDate = { (text, ref, opt, mode) in
let chrono = mode == "strict" ? Chrono.strict : mode == "casual" ? Chrono.casual : self.chrono
self.lastTextForFailCase = text
var opts = [OptionType: Int]()
for o in opt {
opts[OptionType(rawValue: o.key as! String)!] = o.value as? Int ?? 1
}
//return (self.chrono.parseDate(text: text, refDate: ref as Date, opt: opts) ?? Date()) as NSDate
// for debugging, we get the raw parse result and get the first start date.
// make sure this logic didn't change in chrono's parseDate()
let results = chrono.parse(text: text, refDate: ref as Date, opt: opts)
self.lastResultsForFailCase = results
return (results.first?.start.date ?? Date()) as NSDate
}
jsContext.setObject(unsafeBitCast(chronoParseDate, to: AnyObject.self), forKeyedSubscript: "chronoParseDate" as (NSCopying & NSObjectProtocol))
/// expose test() at the top scope for using objectForKeyedSubscript
jsContext.evaluateScript(
"test = function(title, testFunc) {" +
" setTestTitle(title);" +
" testFunc();" +
" clearTestTitle();" +
"};"
)
/// expose test() at the top scope for using objectForKeyedSubscript
jsContext.evaluateScript(
"chrono = {" +
" parse: function(text, ref, opt) {" +
" return chronoParse(text, ref, opt);" +
" }," +
" parseDate: function(text, ref, opt) {" +
" return chronoParseDate(text, ref, opt);" +
" }," +
" strict: {" +
" parse: function(text, ref, opt) {" +
" return chronoParse(text, ref, opt, 'strict');" +
" }," +
" parseDate: function(text, ref, opt) {" +
" return chronoParseDate(text, ref, opt, 'strict');" +
" }" +
" }," +
" casual: {" +
" parse: function(text, ref, opt) {" +
" return chronoParse(text, ref, opt, 'casual');" +
" }," +
" parseDate: function(text, ref, opt) {" +
" return chronoParseDate(text, ref, opt, 'casual');" +
" }" +
" }" +
"};"
)
}
}