2015-10-26 20:31:09 +03:00
|
|
|
import Assertions
|
|
|
|
import Doubt
|
|
|
|
import Prelude
|
|
|
|
import Madness
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
final class JSONParserTests: XCTestCase {
|
|
|
|
func testCapturesKeysInKeyedElements() {
|
2015-10-26 22:05:58 +03:00
|
|
|
let dictWithArray = "{ \"hello\":\n [\"world\",\n \"sailor\"\n ]}"
|
|
|
|
let array: Cofree<JSONLeaf, Range<Int>> = Cofree(15..<41, .Indexed([
|
|
|
|
Cofree(16..<23, .Leaf(.String("world"))),
|
|
|
|
Cofree(29..<37, .Leaf(.String("sailor")))
|
|
|
|
]))
|
2015-10-26 20:31:09 +03:00
|
|
|
|
2015-10-27 20:28:35 +03:00
|
|
|
let fixedPairs: Cofree<JSONLeaf, Range<Int>> = Cofree(2..<41, .Fixed([Cofree(2..<9, .Leaf(.String("hello"))), array]))
|
2015-10-26 20:31:09 +03:00
|
|
|
|
2015-10-27 18:23:12 +03:00
|
|
|
let expected: Cofree<JSONLeaf, Range<Int>> = Cofree(0..<42, .Keyed(["hello": fixedPairs]))
|
2015-10-26 22:05:58 +03:00
|
|
|
let actual = Madness.parse(json, input: dictWithArray).right!
|
2015-11-09 23:19:34 +03:00
|
|
|
let startRange = actual.extract.2
|
2015-11-06 19:01:11 +03:00
|
|
|
let new: Cofree<JSONLeaf, Range<Int>> = actual.map({ tuple in
|
2015-11-09 23:19:34 +03:00
|
|
|
let startI: Int = startRange.startIndex.distanceTo(tuple.2.startIndex)
|
|
|
|
let endI: Int = startRange.startIndex.distanceTo(tuple.2.endIndex)
|
2015-10-26 22:05:58 +03:00
|
|
|
return Range(start: startI, end: endI)
|
|
|
|
})
|
|
|
|
|
|
|
|
assert(new, == , expected)
|
2015-10-26 20:31:09 +03:00
|
|
|
}
|
2015-10-26 22:05:58 +03:00
|
|
|
}
|