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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
|
//===--- LegacyPlaygroundLoggerTests.swift --------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014-2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file contains code ported from the legacy/original implementation of
// PlaygroundLogger. This is to support porting the tests from the legacy
// PlaygroundLogger as-is (i.e. using the original test decoder) in order to
// validate the new implementation.
//
//===----------------------------------------------------------------------===//
import Foundation
#if os(macOS)
import AppKit
typealias ImageType = NSImage
#elseif os(iOS) || os(tvOS)
import UIKit
typealias ImageType = UIImage
#endif
import SpriteKit
import XCTest
@testable import PlaygroundLogger
// MARK: - TestCases.swift (adapted to XCTest)
class LegacyPlaygroundLoggerTests: XCTestCase {
override class func setUp() {
super.setUp()
legacyInitializePlaygroundLogger()
}
func testVersionDecoding() {
let logdata = legacyLog(instance: 1, name: "", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
XCTAssertEqual(10, decoded.version)
}
func testSourceRanges() {
let myrange = SourceRange(begin: (line: 12, col: 3), end: (line: 13, col: 2))
let logdata = legacyLog(instance: 1,
name: "",
id: 0,
startLine: Int(myrange.begin.line),
endLine: Int(myrange.end.line),
startColumn: Int(myrange.begin.col),
endColumn: Int(myrange.end.col)) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
XCTAssertEqual(myrange.begin.line, decoded.range.begin.line)
XCTAssertEqual(myrange.begin.col, decoded.range.begin.col)
XCTAssertEqual(myrange.end.line, decoded.range.end.line)
XCTAssertEqual(myrange.end.col, decoded.range.end.col)
}
func testTIDSent() {
let logdata = legacyLog(instance: 1, name: "", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
var found = false
for (key,value) in decoded.header {
if key == "tid" {
if Int(value) != nil {
found = true
}
}
}
XCTAssertTrue(found)
}
func testNameDecoding() {
struct S { var a = 1; var b = 2; }
let logdata = legacyLog(instance: S(), name: "s", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
let s = decoded.object
XCTAssertEqual("s", s.name)
guard let structured = s as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual(2, structured.count)
let child0 = structured[0]
let child1 = structured[1]
XCTAssertEqual(child0.name,"a")
XCTAssertEqual(child1.name,"b")
}
func testBaseTypesDecoding() {
struct S { var a = 1; var b = "hello world"; var c: Double = 12.15; var d: Float = 12.15 }
let logdata = legacyLog(instance: S(), name: "s", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let structured = decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual(4, structured.count)
let child0 = structured[0]
let child1 = structured[1]
let child2 = structured[2]
let child3 = structured[3]
guard let child0_iderepr = child0 as? PlaygroundDecodedObject_IDERepr else {
XCTFail("child0 is not IDERepr")
return
}
guard let child1_iderepr = child1 as? PlaygroundDecodedObject_IDERepr else {
XCTFail("child1 is not IDERepr")
return
}
guard let child2_iderepr = child2 as? PlaygroundDecodedObject_IDERepr else {
XCTFail("child2 is not IDERepr")
return
}
guard let child3_iderepr = child3 as? PlaygroundDecodedObject_IDERepr else {
XCTFail("child3 is not IDERepr")
return
}
guard let child0Payload = child0_iderepr.payload, let aInt64 = child0Payload as? Int64 else {
XCTFail("child0 does not contain expected payload type")
return
}
let a = Int(aInt64)
guard let child1Payload = child1_iderepr.payload, let b = child1Payload as? String else {
XCTFail("child1 does not contain expected payload type")
return
}
guard let child2Payload = child2_iderepr.payload, let c = child2Payload as? Double else {
XCTFail("child2 does not contain expected payload type")
return
}
guard let child3Payload = child3_iderepr.payload, let d = child3Payload as? Float else {
XCTFail("child3 does not contain expected payload type")
return
}
let realS = S()
XCTAssertEqual(a, realS.a)
XCTAssertEqual(b, realS.b)
XCTAssertEqual(c, realS.c)
XCTAssertEqual(d, realS.d)
}
func testStructuredTypesDecoding() {
struct S {}
class C {}
let s_logdata = legacyLog(instance: S(), name: "s", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let c_logdata = legacyLog(instance: C(), name: "c", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let t_logdata = legacyLog(instance: (1, 1), name: "t", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let s_decoded = legacyLogDecode(s_logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let c_decoded = legacyLogDecode(c_logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let t_decoded = legacyLogDecode(t_logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let s = s_decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
guard let c = c_decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
guard let t = t_decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual(s.type, PlaygroundRepresentation.Struct.description)
XCTAssertEqual(c.type, PlaygroundRepresentation.Class.description)
XCTAssertEqual(t.type, PlaygroundRepresentation.Tuple.description)
}
func testNSNumberDecoding() {
let num: NSNumber = NSNumber(value: 12345)
let logdata = legacyLog(instance: num, name: "num", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let iderepr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
guard let data = iderepr.payload else {
XCTFail("IDERepr is missing payload")
return
}
XCTAssertEqual("\(data)", "12345")
}
func testOnePlusOneDecoding() {
let logdata = legacyLog(instance: 1+1, name: "num", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let iderepr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
let summary = iderepr.summary
XCTAssertEqual(summary, "2")
}
func testMetatypeLogging() {
struct S {}
let logdata = legacyLog(instance: S.self, name: "S", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
XCTAssert(decoded.object is PlaygroundDecodedObject_Structured)
}
// testExceptionSafety() is excluded, as it tests functionality handled differently in the new implementation.
#if os(macOS)
func testNSViewLogging() {
let button = NSButton(frame: NSRect(x: 0,y: 0,width: 100,height: 100))
let logdata = legacyLog(instance: button, name: "button", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let iderepr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(iderepr.tag, "VIEW")
guard let payloadImage = iderepr.payload as? NSImage else {
XCTFail("Decoded payload is not an image")
return
}
XCTAssertEqual(payloadImage.size, NSSize(width: 100, height: 100))
}
#endif
#if os(iOS) || os(tvOS)
func testUIViewLogging() {
let button = UIButton(type: .system)
button.setTitle("Button", for: .normal)
button.sizeToFit()
let logdata = legacyLog(instance: button, name: "button", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let iderepr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(iderepr.tag, "VIEW")
guard let payloadImage = iderepr.payload as? UIImage else {
XCTFail("Decoded payload is not an image")
return
}
XCTAssertEqual(payloadImage.size, CGSize(width: button.bounds.size.width * UIScreen.main.scale, height: button.bounds.size.height * UIScreen.main.scale))
}
#endif
func testImageLogging() {
let size = CGSize(width: 30, height: 30)
#if os(macOS)
let image = NSImage(size: size, flipped: false) { rect -> Bool in
NSColor.white.setFill()
NSBezierPath(rect: rect).fill()
NSColor.orange.setFill()
NSBezierPath(roundedRect: rect.insetBy(dx: 5, dy: 5), xRadius: 3, yRadius: 3).fill()
return true
}
#elseif os(iOS) || os(tvOS)
let rendererFormat: UIGraphicsImageRendererFormat
if #available(iOS 11.0, tvOS 11.0, *) {
rendererFormat = .preferred()
}
else {
rendererFormat = .default()
}
rendererFormat.scale = 1
rendererFormat.opaque = true
let image = UIGraphicsImageRenderer(size: size, format: rendererFormat).image { context in
UIColor.white.setFill()
UIBezierPath(rect: context.format.bounds).fill()
UIColor.orange.setFill()
UIBezierPath(roundedRect: context.format.bounds.insetBy(dx: 5, dy: 5), cornerRadius: 3).fill()
}
#endif
let logdata = legacyLog(instance: image, name: "image", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let iderepr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(iderepr.tag, "IMAG")
guard let payloadImage = iderepr.payload as? ImageType else {
XCTFail("Decoded payload is not an image")
return
}
let expectedSize: CGSize
#if os(macOS)
// On macOS, the image we create above is rendered at a scale factor. We get the best-available scale factor from all screens and use that when creating our expectation.
let scaleFactor = NSScreen.screens.reduce(1.0) { previousBestScaleFactor, screen in
return max(previousBestScaleFactor, screen.backingScaleFactor)
}
expectedSize = CGSize(width: size.width * scaleFactor, height: size.height * scaleFactor)
#elseif os(iOS) || os(tvOS)
// On iOS and tvOS, we expect the output image to be the same size as the input image.
expectedSize = size
#endif
XCTAssertEqual(payloadImage.size, expectedSize)
}
// testSpriteKitLogging() is excluded, as it cannot be trivially ported.
func testOptionalGetsStripped() {
let some: String?? = "hello"
let none: String?? = nil
let some_logged = legacyLog(instance: some, name: "some", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let none_logged = legacyLog(instance: none, name: "none", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let some_decoded = legacyLogDecode(some_logged) else {
XCTFail("Failed to decode log data")
return
}
guard let none_decoded = legacyLogDecode(none_logged) else {
XCTFail("Failed to decode log data")
return
}
guard let some_iderepr = some_decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
guard let none_structured = none_decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual(none_structured.summary, "nil")
XCTAssertEqual(some_iderepr.tag, "STRN")
XCTAssertEqual(some_iderepr.summary, "hello")
}
// testStackWorks(), testNeverLoggingPolicy(), and testAdaptiveLoggingPolicy() are excluded, as they test functionality omitted from the new implementation.
func testSetIsMembershipContainer() {
let object = Set([1,2,3])
let logdata = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let set_structured = decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual("MembershipContainer", set_structured.type)
}
func testTypenameManagement() {
struct SomeStruct {
var a = 12
var b = 24
}
var object: Any = SomeStruct()
var logdata = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
var decoded: PlaygroundDecodedLogEntry!
var structured: PlaygroundDecodedObject_Structured!
func decodeLogdata() {
guard let decodedTemp = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
decoded = decodedTemp
guard let structuredTemp = decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
structured = structuredTemp
}
decodeLogdata()
XCTAssert(structured.typeName.hasSuffix(".SomeStruct"))
object = (1,2,2,4)
logdata = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
decodeLogdata()
XCTAssertEqual("(Int, Int, Int, Int)", structured.typeName)
object = [1: "1", 2: "2"]
logdata = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
decodeLogdata()
XCTAssertEqual("Dictionary<Int, String>", structured.typeName)
class Foo { class Swift { class Bar { class Baz { } } } }
object = Foo.Swift.Bar.Baz()
logdata = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
decodeLogdata()
XCTAssert(structured.typeName.hasSuffix(".Foo.Swift.Bar.Baz"))
}
func testFloatDoubleDecoding() {
let f: Float = 1.25
let d: Double = 1.25
let f_ld = legacyLog(instance: f, name: "f", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let d_ld = legacyLog(instance: d, name: "d", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let f_dc = legacyLogDecode(f_ld) else {
XCTFail("Failed to decode log data")
return
}
guard let d_dc = legacyLogDecode(d_ld) else {
XCTFail("Failed to decode log data")
return
}
guard let f_repr = f_dc.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
guard let d_repr = d_dc.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(f_repr.tag, "FLOT")
XCTAssertEqual(d_repr.tag, "DOBL")
guard let f2 = f_repr.payload as? Float else {
XCTFail("Payload is not expected type")
return
}
guard let d2 = d_repr.payload as? Double else {
XCTFail("Payload is not expected type")
return
}
XCTAssertEqual(f, f2)
XCTAssertEqual(d, d2)
}
func testSKShapeNode() {
let blahNode = SKShapeNode(circleOfRadius: 30.0)
let logdata = legacyLog(instance: blahNode, name: "blahNode", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let bn_repr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(bn_repr.tag, "SKIT")
XCTAssertEqual(bn_repr.typeName, "SKShapeNode")
XCTAssert(bn_repr.payload is ImageType, "We expect the payload to be an image")
}
func testBaseClassLogging() {
class Parent { var a = 1; var b = 2 }
class Child : Parent { var c = 3 }
let object = Child()
let logdata = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata) else {
XCTFail("Failed to decode log data")
return
}
guard let structured = decoded.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
var seen_parent = false
var seen_a = false
var seen_b = false
var seen_c = false
for child in structured.children {
if let structured_child = child as? PlaygroundDecodedObject_Structured {
if structured_child.name == "super" {
seen_parent = true
for parent_child in structured_child.children {
if parent_child.name == "a" { seen_a = true }
if parent_child.name == "b" { seen_b = true }
}
}
}
if child.name == "c" {
seen_c = true
}
}
XCTAssert(seen_parent && seen_a && seen_b && seen_c)
}
func testEnumSummary_Generic() {
typealias GEither = EnumSummaryTestCase_GEither<Int,String>
let t1 = GEither.First(1)
let t2 = GEither.Second("A")
let t3 = GEither.Neither
let logdata_t1 = legacyLog(instance: t1, name: "t1", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let logdata_t2 = legacyLog(instance: t2, name: "t2", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let logdata_t3 = legacyLog(instance: t3, name: "t3", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded_t1 = legacyLogDecode(logdata_t1) else {
XCTFail("Failed to decode log data")
return
}
guard let decoded_t2 = legacyLogDecode(logdata_t2) else {
XCTFail("Failed to decode log data")
return
}
guard let decoded_t3 = legacyLogDecode(logdata_t3) else {
XCTFail("Failed to decode log data")
return
}
guard let structured_t1 = decoded_t1.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
guard let structured_t2 = decoded_t2.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
guard let structured_t3 = decoded_t3.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual(structured_t1.summary, "First(1)")
XCTAssertEqual(structured_t2.summary, "Second(\"A\")")
XCTAssertEqual(structured_t3.summary, "Neither")
}
func testEnumSummary_NotGeneric() {
typealias Either = EnumSummaryTestCase_Either
let t1 = Either.First(1)
let t2 = Either.Second("A")
let t3 = Either.Neither
let logdata_t1 = legacyLog(instance: t1, name: "t1", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let logdata_t2 = legacyLog(instance: t2, name: "t2", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
let logdata_t3 = legacyLog(instance: t3, name: "t3", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded_t1 = legacyLogDecode(logdata_t1) else {
XCTFail("Failed to decode log data")
return
}
guard let decoded_t2 = legacyLogDecode(logdata_t2) else {
XCTFail("Failed to decode log data")
return
}
guard let decoded_t3 = legacyLogDecode(logdata_t3) else {
XCTFail("Failed to decode log data")
return
}
guard let structured_t1 = decoded_t1.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
guard let structured_t2 = decoded_t2.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
guard let structured_t3 = decoded_t3.object as? PlaygroundDecodedObject_Structured else {
XCTFail("Decoded object is not structured")
return
}
XCTAssertEqual(structured_t1.summary, "First(1)")
XCTAssertEqual(structured_t2.summary, "Second(\"A\")")
XCTAssertEqual(structured_t3.summary, "Neither")
}
func testPrintHook() {
printHook(string: "hello world")
let logdata_1 = legacyLogPostPrint(startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded_1 = legacyLogDecode(logdata_1), let iderepr_1 = decoded_1.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(iderepr_1.summary, "hello world")
printHook(string: "not this one")
printHook(string: "but this one")
let logdata_2 = legacyLogPostPrint(startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded_2 = legacyLogDecode(logdata_2), let iderepr_2 = decoded_2.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(iderepr_2.summary, "but this one")
}
func testPlaygroundQuickLookCalledOnce() {
class MyObject : _CustomPlaygroundQuickLookable {
var numCalls = 0
var customPlaygroundQuickLook: _PlaygroundQuickLook {
get {
numCalls = numCalls + 1
return .text("Hello world")
}
}
}
let object = MyObject()
_ = legacyLog(instance: object, name: "object", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0)
XCTAssertEqual(object.numCalls, 1)
}
// testUInt64EightBytesEncoding(), testNSColorLogging(), and testStructLogging() are excluded, as in their current form they test implementation details of the legacy logger.
// MARK: - New Tests Using Legacy Infrastructure
// These are tests which did not exist in the previous test suite, but which use the legacy infrastructure to run.
// They should be migrated to new test infrastructure once such a thing exists.
func testCGFloatLogging() {
let cgFloat: CGFloat = 2.0
let logdata = legacyLog(instance: cgFloat, name: "cgFloat", id: 0, startLine: 0, endLine: 0, startColumn: 0, endColumn: 0) as! NSData
guard let decoded = legacyLogDecode(logdata), let iderepr = decoded.object as? PlaygroundDecodedObject_IDERepr else {
XCTFail("Decoded object is not IDERepr")
return
}
XCTAssertEqual(iderepr.name, "cgFloat")
XCTAssertEqual(iderepr.typeName, "CoreGraphics.CGFloat")
XCTAssertEqual(iderepr.summary, "2.0")
if CGFloat.NativeType.self == Double.self {
XCTAssertEqual(iderepr.tag, "DOBL")
guard iderepr.payload is Double else {
XCTFail("Expected a Double as payload but did not have one")
return
}
XCTAssertEqual(iderepr.payload as! Double, 2.0 as Double)
}
else if CGFloat.NativeType.self == Float.self {
XCTAssertEqual(iderepr.tag, "FLOT")
guard iderepr.payload is Float else {
XCTFail("Expected a Float as payload but did not have one")
return
}
XCTAssertEqual(iderepr.payload as! Float, 2.0 as Float)
}
else {
XCTFail("Unknown CGFloat.NativeType: \(CGFloat.NativeType.self)")
}
}
}
// generic so can't be nested in the test case itself
fileprivate enum EnumSummaryTestCase_GEither<T1,T2> { case First(T1), Second(T2), Neither }
fileprivate enum EnumSummaryTestCase_Either { case First(Int), Second(String), Neither }
// MARK: - Common.swift
typealias SourceLocation = (line: UInt64, col: UInt64)
typealias SourceRange = (begin: SourceLocation, end: SourceLocation)
// MARK: - BytesStorage.swift
final class BytesStorage {
let data: NSData // hold on to the NSData so it doesn't go away from under us
let bytes: UnsafeMutablePointer<UInt8> // but a pointer is good enough to actually index bytes by
var index: Int
init(_ _bytes: NSData) {
data = _bytes
bytes = UnsafeMutablePointer(mutating: data.bytes.bindMemory(to: UInt8.self, capacity: data.length))
index = 0
}
func get() -> UInt8 {
let i = index
index += 1
return bytes[i]
}
func peek() -> UInt8 {
return bytes[index]
}
func eof() -> Bool {
return index >= count
}
var count: Int {
get { return data.length }
}
subscript (i: UInt64) -> UInt8 {
get {
return bytes[index+Int(i)]
}
}
func has(_ nBytes: UInt64) -> Bool {
return (index+Int(nBytes) <= count)
}
func dumpBytes() {
for idx in 0..<count {
let byte : UInt8 = self.bytes[idx]
print("\(byte) ", terminator: "")
}
print("")
}
func subset(len: UInt64, consume: Bool) -> BytesStorage {
let copydata = NSData(bytesNoCopy:bytes+index, length: Int(len), freeWhenDone: false)
if consume {
index = index + Int(len)
}
return BytesStorage(copydata)
}
}
// MARK: - PlaygroundRepresentation.swift
enum PlaygroundRepresentation : UInt8, Hashable, CustomStringConvertible, Equatable {
case Class = 1
case Struct = 2
case Tuple = 3
case Enum = 4
case Aggregate = 5
case Container = 6
case IDERepr = 7
case Gap = 8
case ScopeEntry = 9
case ScopeExit = 10
case Error = 11
case IndexContainer = 12
case KeyContainer = 13
case MembershipContainer = 14
case Unknown = 0xFF
init (byte: UInt8) {
if let repr = PlaygroundRepresentation(rawValue: byte) {
self = repr
} else {
self = .Unknown
}
}
init? (storage : BytesStorage) {
self = PlaygroundRepresentation(byte: storage.get())
}
var hashValue : Int {
return Int(self.rawValue)
}
var description: String {
switch self {
case .Class: return "Class"
case .Struct: return "Struct"
case .Tuple: return "Tuple"
case .Enum: return "Enum"
case .Aggregate: return "Aggregate"
case .Container: return "Container"
case .IDERepr: return "IDERepr"
case .Gap: return "Gap"
case .ScopeEntry: return "ScopeEntry"
case .ScopeExit: return "ScopeExit"
case .Error: return "Error"
case .IndexContainer: return "IndexContainer"
case .KeyContainer: return "KeyContainer"
case .MembershipContainer: return "MembershipContainer"
default: return "Unknown"
}
}
init(_ x: Mirror.DisplayStyle)
{
switch (x) {
case .`class`: self = .Class
case .`struct`: self = .Struct
case .tuple: self = .Tuple
case .`enum`: self = .Enum
case .optional: self = .Aggregate
case .collection: self = .IndexContainer
case .dictionary: self = .KeyContainer
case .set: self = .MembershipContainer
@unknown default: self = .Container
}
}
}
// MARK: - KeyedUnarchiver.swift
final class LoggerUnarchiver {
var unarchiver : NSKeyedUnarchiver
let storage : BytesStorage
init(_ stg : BytesStorage) {
storage = stg
unarchiver = NSKeyedUnarchiver(forReadingWith: NSData(bytes: storage.bytes + storage.index, length: storage.count - storage.index) as Data)
}
func get(double: String) -> Double {
return unarchiver.decodeDouble(forKey: double)
}
func get(bool : String) -> Bool {
return unarchiver.decodeBool(forKey: bool)
}
func get(int64 : String) -> Int64 {
return unarchiver.decodeInt64(forKey: int64)
}
func get(uint64 : String) -> UInt64 {
return UInt64(unarchiver.decodeInt64(forKey: uint64))
}
func get(object : String) -> Any! {
return unarchiver.decodeObject(forKey: object)
}
func has(_ key : String) -> Bool {
switch unarchiver.decodeObject(forKey: key) {
case nil: return false
default: return true
}
}
}
// MARK: - ExtensionUInt64.swift
extension UInt64 {
static let largeNumMarker : UInt8 = 0xFF
init? (storage : BytesStorage) {
let byte0 = storage.get()
if (byte0 == UInt64.largeNumMarker) {
if let x = UInt64(eightBytesStorage: storage) {
self = x
} else {
return nil
}
} else {
self = UInt64(byte0)
}
}
init? (eightBytesStorage: BytesStorage) {
if !eightBytesStorage.has(8) { return nil }
let up_byte = UnsafeMutablePointer<UInt8>.allocate(capacity: 8)
defer { up_byte.deallocate() }
for idx in 0..<8 {
up_byte[idx] = eightBytesStorage.get()
}
let up_int: UnsafePointer<UInt64> = UnsafeRawPointer(up_byte).bindMemory(
to: UInt64.self, capacity: 1)
self = up_int.pointee
}
}
// MARK: - ExtensionString.swift
extension String {
init (rawBytes: [UInt8]) {
self = String(decoding: rawBytes, as: UTF8.self)
}
init? (storage: BytesStorage) {
var str_bytes = Array<UInt8>()
guard let count = UInt64(storage: storage) else { return nil }
if count == 0 {
self = ""
} else {
if !storage.has(count) { return nil }
for _ in 0..<count {
let byte = storage.get()
str_bytes.append(byte)
}
self = String(rawBytes: str_bytes)
}
}
init? (fullBytesStorage: BytesStorage) {
var str_bytes = Array<UInt8>()
while fullBytesStorage.has(1) {
let byte = fullBytesStorage.get()
str_bytes.append(byte)
}
self = String(rawBytes: str_bytes)
}
}
extension String {
var byteLength: Int {
return self.utf8.count
}
}
// MARK: - ExtensionBool.swift
extension Bool {
init? (storage: BytesStorage) {
let b = storage.get()
if b == 1 { self = true }
else if b == 0 { self = false }
else { return nil }
}
}
// MARK: - ExtensionFloat.swift
extension Float {
init? (storage: BytesStorage) {
let ubPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: 4)
defer { ubPtr.deallocate() }
for idx in 0..<4 {
ubPtr[idx] = storage.get()
}
let udPtr = UnsafeMutableRawPointer(ubPtr).bindMemory(
to: Float.self, capacity: 1)
self = udPtr.pointee
}
}
// MARK: - ExtensionDouble.swift
extension Double {
init? (storage: BytesStorage) {
let ubPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: 8)
defer { ubPtr.deallocate() }
for idx in 0..<8 {
ubPtr[idx] = storage.get()
}
let udPtr = UnsafeMutableRawPointer(ubPtr).bindMemory(
to: Double.self, capacity: 1)
self = udPtr.pointee
}
}
// MARK: - LoggerDecoderAPI.swift
// LoggerDecoder is not meant for public consumption, is not complete
// and is distinct from the decoder logic used by Xcode - the purpose
// of this decoding API is to enable testing of PlaygroundLogger
public class PlaygroundDecodedLogEntry {
let version: UInt64
let range: SourceRange
let header: [String: String]
let object: PlaygroundDecodedObject
init (version: UInt64,
startLine: UInt64,
startColumn: UInt64,
endLine: UInt64,
endColumn: UInt64,
header: [String: String],
object: PlaygroundDecodedObject) {
self.version = version
self.range = SourceRange(begin: (line: startLine, col: startColumn), end: (line: endLine, col: endColumn))
self.header = header
self.object = object
}
func print<T: TextOutputStream>(to stream: inout T) {
Swift.print("Version: \(version)", to: &stream)
Swift.print("\(header.count) header entries", to: &stream)
for (key,value) in header {
Swift.print("\t\(key) = \(value)", to: &stream)
}
object.print(&stream,0)
}
public func toString() -> String {
var s = ""
print(to: &s)
return s
}
}
func legacyLogDecode(_ object : NSData) -> PlaygroundDecodedLogEntry? {
return PlaygroundDecoder(BytesStorage(object)).decode()
}
// MARK: - LoggerDecoderImpl.swift
func * (lhs : String, rhs : Int) -> String {
if (rhs <= 0) { return "" }
if (rhs == 1) { return lhs }
var str = lhs
for _ in 1..<rhs
{
str += lhs
}
return str
}
protocol PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject?
}
class PlaygroundDecodedObject_Structured: PlaygroundDecodedObject {
let typeName: String
let summary: String
let totalCount: UInt64
let storedCount: UInt64
let type: String
var children: [PlaygroundDecodedObject]
init (_ name: String, _ brief: String, _ long: String, _ total: UInt64, _ stored: UInt64, _ type: String) {
self.typeName = brief
self.summary = long
self.totalCount = total
self.storedCount = stored
self.children = [PlaygroundDecodedObject]()
self.type = type
super.init(name)
}
func addChild(_ x: PlaygroundDecodedObject) {
self.children.append(x)
}
var count: Int { return children.count }
subscript(x: Int) -> PlaygroundDecodedObject { return children[x] }
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Typename: \(typeName)", to: &stream)
Swift.print("\(prefix)Summary: \(summary)", to: &stream)
Swift.print("\(prefix)Total count: \(totalCount)", to: &stream)
Swift.print("\(prefix)Stored count: \(storedCount)", to: &stream)
Swift.print("\(prefix)Type: \(type)", to: &stream)
for child in children {
child.print(&stream, depth+1)
}
}
}
class PlaygroundObjectDecoder_Structured: PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject? {
guard let brief = String(storage: bytes) else { return nil }
guard let long = String(storage: bytes) else { return nil }
guard let total = UInt64(storage: bytes) else { return nil }
guard let stored: UInt64 = ((total > 0) ? UInt64(storage: bytes) : 0) else { return nil }
let object = PlaygroundDecodedObject_Structured(name, brief, long, total, stored, kind.description)
for _ in 0..<stored {
object.addChild(decoder.decodeObject(bytes)!)
}
return object
}
}
class PlaygroundDecodedObject_Gap: PlaygroundDecodedObject {
override init(_ name: String) {
super.init(name)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Type: Gap", to: &stream)
}
}
class PlaygroundObjectDecoder_Gap: PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject? {
return PlaygroundDecodedObject_Gap(name)
}
}
class PlaygroundDecodedObject_ScopeEntry: PlaygroundDecodedObject {
override init(_ name: String) {
super.init(name)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Type: Scope Entry", to: &stream)
}
}
class PlaygroundObjectDecoder_ScopeEntry: PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject? {
return PlaygroundDecodedObject_ScopeEntry(name)
}
}
class PlaygroundDecodedObject_ScopeExit: PlaygroundDecodedObject {
override init(_ name: String) {
super.init(name)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Type: Scope Exit", to: &stream)
}
}
class PlaygroundObjectDecoder_ScopeExit: PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject? {
return PlaygroundDecodedObject_ScopeExit(name)
}
}
class PlaygroundDecodedObject_Error: PlaygroundDecodedObject {
let error: String
init(_ name: String, _ error: String) {
self.error = error
super.init(name)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Type: Error", to: &stream)
Swift.print("\(prefix)Message: \(error)", to: &stream)
}
}
class PlaygroundObjectDecoder_Error: PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject? {
guard let message = String(storage: bytes) else { return nil }
return PlaygroundDecodedObject_Error(name,message)
}
}
class PlaygroundObjectDecoder_IDERepr: PlaygroundObjectDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ kind: PlaygroundRepresentation) -> PlaygroundDecodedObject? {
guard let preferSummary = Bool(storage: bytes) else { return nil }
guard let brief = String(storage: bytes) else { return nil }
guard let long = String(storage: bytes) else { return nil }
guard let tag = String(storage: bytes) else { return nil }
guard let size = UInt64(storage: bytes) else { return nil }
let subset = bytes.subset(len: size, consume: true)
return decoder.getIDEReprDecoder(tag).decodeObject(decoder, subset, name, preferSummary, brief, long, tag)
}
}
protocol PlaygroundIDEReprDecoder {
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr?
}
class PlaygroundIDEReprDecoder_String: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_String: PlaygroundDecodedObject_IDERepr {
let data: String
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: String) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = String(fullBytesStorage: bytes) else { return nil }
return PlaygroundDecodedObject_IDERepr_String(name, psum ,brief, long, tag, data)
}
}
class PlaygroundIDEReprDecoder_Int: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Int: PlaygroundDecodedObject_IDERepr {
let data: Int64
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: Int64) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = String(fullBytesStorage: bytes) else { return nil }
guard let idata = Int(data) else { return nil }
return PlaygroundDecodedObject_IDERepr_Int(name, psum ,brief, long, tag, Int64(idata))
}
}
class PlaygroundIDEReprDecoder_UInt: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_UInt: PlaygroundDecodedObject_IDERepr {
let data: UInt64
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: UInt64) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = String(fullBytesStorage: bytes) else { return nil }
return PlaygroundDecodedObject_IDERepr_UInt(name, psum, brief, long, tag, strtoull(data, nil, 10))
}
}
class PlaygroundIDEReprDecoder_Float: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Float: PlaygroundDecodedObject_IDERepr {
let data: Float
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: Float) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = Float(storage: bytes) else { return nil }
return PlaygroundDecodedObject_IDERepr_Float(name, psum, brief, long, tag, data)
}
}
class PlaygroundIDEReprDecoder_Double: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Double: PlaygroundDecodedObject_IDERepr {
let data: Double
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: Double) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = Double(storage: bytes) else { return nil }
return PlaygroundDecodedObject_IDERepr_Double(name, psum, brief, long, tag, data)
}
}
class PlaygroundIDEReprDecoder_Point: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Point: PlaygroundDecodedObject_IDERepr {
let data: CGPoint
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: CGPoint) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
let decoder = LoggerUnarchiver(bytes)
if !(decoder.has("x") && decoder.has("y")) { return nil }
return PlaygroundDecodedObject_IDERepr_Point(name, psum, brief, long, tag, CGPoint(x: CGFloat(decoder.get(double: "x")), y: CGFloat(decoder.get(double: "y"))))
}
}
class PlaygroundIDEReprDecoder_Size: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Size: PlaygroundDecodedObject_IDERepr {
let data: CGSize
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: CGSize) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
let decoder = LoggerUnarchiver(bytes)
if !(decoder.has("w") && decoder.has("h")) { return nil }
return PlaygroundDecodedObject_IDERepr_Size(name, psum, brief, long, tag, CGSize(width: CGFloat(decoder.get(double: "w")), height: CGFloat(decoder.get(double: "h"))))
}
}
class PlaygroundIDEReprDecoder_Rect: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Rect: PlaygroundDecodedObject_IDERepr {
let data: CGRect
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: CGRect) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
let decoder = LoggerUnarchiver(bytes)
if !(decoder.has("x") && decoder.has("y") && decoder.has("w") && decoder.has("h")) { return nil }
return PlaygroundDecodedObject_IDERepr_Rect(name,
psum,
brief,
long,
tag,
CGRect(x: CGFloat(decoder.get(double: "x")),
y: CGFloat(decoder.get(double: "y")),
width: CGFloat(decoder.get(double:"w")),
height: CGFloat(decoder.get(double: "h"))))
}
}
class PlaygroundIDEReprDecoder_Range: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Range: PlaygroundDecodedObject_IDERepr {
let data: NSRange
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: NSRange) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
let decoder = LoggerUnarchiver(bytes)
if !(decoder.has("loc") && decoder.has("len")) { return nil }
return PlaygroundDecodedObject_IDERepr_Range(name, psum, brief, long, tag, NSRange(location: Int(decoder.get(int64: "loc")), length: Int(decoder.get(int64: "len"))))
}
}
class PlaygroundIDEReprDecoder_Bool: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Bool: PlaygroundDecodedObject_IDERepr {
let data: Bool
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: Bool) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = Bool(storage: bytes) else { return nil }
return PlaygroundDecodedObject_IDERepr_Bool(name, psum, brief, long, tag, data)
}
}
class PlaygroundIDEReprDecoder_URL: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_URL: PlaygroundDecodedObject_IDERepr {
let data: NSURL
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: NSURL) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
guard let data = String(fullBytesStorage: bytes) else { return nil }
guard let url = NSURL(string: data) else { return nil }
return PlaygroundDecodedObject_IDERepr_URL(name, psum ,brief, long, tag, url)
}
}
class PlaygroundIDEReprDecoder_Image: PlaygroundIDEReprDecoder {
class PlaygroundDecodedObject_IDERepr_Image: PlaygroundDecodedObject_IDERepr {
let data: ImageType
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: ImageType) {
self.data = data
super.init(name, psum, brief, long, tag)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Data: \(data)", to: &stream)
}
override var payload: Any? { return data }
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
let data = bytes.data
guard let image = ImageType(data: data as Data) else { return nil }
return PlaygroundDecodedObject_IDERepr_Image(name, psum ,brief, long, tag, image)
}
}
class PlaygroundDecoder {
var bytes: BytesStorage
var obj_decoders: [PlaygroundRepresentation: PlaygroundObjectDecoder]
var iderepr_decoders: [String: PlaygroundIDEReprDecoder]
init (_ bytes: BytesStorage) {
self.bytes = bytes
self.obj_decoders = [PlaygroundRepresentation: PlaygroundObjectDecoder]()
self.iderepr_decoders = [String: PlaygroundIDEReprDecoder]()
self.obj_decoders[.Class] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.Struct] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.Tuple] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.Enum] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.Aggregate] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.Container] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.IndexContainer] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.KeyContainer] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.MembershipContainer] = PlaygroundObjectDecoder_Structured()
self.obj_decoders[.Gap] = PlaygroundObjectDecoder_Gap()
self.obj_decoders[.ScopeEntry] = PlaygroundObjectDecoder_ScopeEntry()
self.obj_decoders[.ScopeExit] = PlaygroundObjectDecoder_ScopeExit()
self.obj_decoders[.Error] = PlaygroundObjectDecoder_Error()
self.obj_decoders[.IDERepr] = PlaygroundObjectDecoder_IDERepr()
self.iderepr_decoders["STRN"] = PlaygroundIDEReprDecoder_String()
self.iderepr_decoders["SINT"] = PlaygroundIDEReprDecoder_Int()
self.iderepr_decoders["UINT"] = PlaygroundIDEReprDecoder_UInt()
self.iderepr_decoders["FLOT"] = PlaygroundIDEReprDecoder_Float()
self.iderepr_decoders["DOBL"] = PlaygroundIDEReprDecoder_Double()
self.iderepr_decoders["RECT"] = PlaygroundIDEReprDecoder_Rect()
self.iderepr_decoders["PONT"] = PlaygroundIDEReprDecoder_Point()
self.iderepr_decoders["SIZE"] = PlaygroundIDEReprDecoder_Size()
self.iderepr_decoders["RANG"] = PlaygroundIDEReprDecoder_Range()
self.iderepr_decoders["BOOL"] = PlaygroundIDEReprDecoder_Bool()
self.iderepr_decoders["URL"] = PlaygroundIDEReprDecoder_URL()
self.iderepr_decoders["IMAG"] = PlaygroundIDEReprDecoder_Image()
self.iderepr_decoders["VIEW"] = PlaygroundIDEReprDecoder_Image()
self.iderepr_decoders["SKIT"] = PlaygroundIDEReprDecoder_Image()
}
func getIDEReprDecoder(_ tag: String) -> PlaygroundIDEReprDecoder {
if let decoder = iderepr_decoders[tag] {
return decoder
}
class DefaultIDEReprDecoder: PlaygroundIDEReprDecoder {
class DefaultIDEReprDecoder_Object: PlaygroundDecodedObject_IDERepr {
let data: String
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String, _ data: String) {
self.data = data
super.init(name, psum, brief, long, tag)
}
}
func decodeObject(_ decoder: PlaygroundDecoder, _ bytes: BytesStorage, _ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) -> PlaygroundDecodedObject_IDERepr? {
let len_to_dump = 128
var buffer = ""
for i in 0..<bytes.count {
if i > len_to_dump {
buffer += "..."
break
} else {
buffer = buffer + "\(bytes.get()) "
}
}
return DefaultIDEReprDecoder_Object(name, psum, brief, long, tag, buffer)
}
}
return DefaultIDEReprDecoder()
}
func decodeObject(_ bytes: BytesStorage) -> PlaygroundDecodedObject? {
guard let name = String(storage: bytes) else { return nil }
let kind = PlaygroundRepresentation(byte: bytes.get())
return obj_decoders[kind]?.decodeObject(self, bytes, name, kind)
}
func decode () -> PlaygroundDecodedLogEntry? {
guard let version = UInt64(storage: bytes) else { return nil }
guard let startline = UInt64(eightBytesStorage: bytes) else { return nil }
guard let startcol = UInt64(eightBytesStorage: bytes) else { return nil }
guard let endline = UInt64(eightBytesStorage: bytes) else { return nil }
guard let endcol = UInt64(eightBytesStorage: bytes) else { return nil }
guard let header_count = UInt64(storage: bytes) else { return nil }
var header = [String: String]()
for _ in 0..<header_count {
guard let key = String(storage: bytes) else { return nil }
guard let value = String(storage: bytes) else { return nil }
header[key] = value
}
guard let object = decodeObject(self.bytes) else { return nil }
return PlaygroundDecodedLogEntry(version: version,
startLine: startline,
startColumn: startcol,
endLine: endline,
endColumn: endcol,
header: header,
object: object)
}
}
class PlaygroundDecodedObject {
let name: String
init(_ name: String) {
self.name = name
}
func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
Swift.print("\(prefix)Name: \(name)", to: &stream)
}
}
class PlaygroundDecodedObject_IDERepr: PlaygroundDecodedObject {
let shouldPreferSummary: Bool
let typeName: String
let summary: String
let tag: String
init (_ name: String, _ psum: Bool, _ brief: String, _ long: String, _ tag: String) {
self.shouldPreferSummary = psum
self.typeName = brief
self.summary = long
self.tag = tag
super.init(name)
}
override func print<T: TextOutputStream>(_ stream: inout T, _ depth: Int) {
let prefix = "\t"*depth
super.print(&stream, depth)
Swift.print("\(prefix)Typename: \(typeName)", to: &stream)
Swift.print("\(prefix)Summary: \(summary)", to: &stream)
Swift.print("\(prefix)Tag: \(tag)", to: &stream)
}
var payload: Any? { return nil }
}
|