Skip to content

Commit 9bcca68

Browse files
committed
Improve Debugging for Undefined Structs
1 parent 00e4c1c commit 9bcca68

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

OCRunner/RunEnv/MFValue.mm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,24 @@ - (MFValue *)fieldForKey:(NSString *)key copied:(BOOL)copied{
563563
NSCAssert(self.type == OCTypeStruct, @"must be struct");
564564
NSString *structName = self.typeName;
565565
ORStructDeclare *declare = [[ORTypeSymbolTable shareInstance] symbolItemForTypeName:structName].declare;
566+
#if DEBUG
567+
if (!declare) {
568+
NSLog(@"\
569+
\n---------OCRunner Error---------\n\
570+
Unknown struct: '%@'\n\
571+
Tried to access the field '%@' of the struct '%@', but the struct '%@' was not defined in advance\n\
572+
You need to add the missing struct in OCRunner scripts.\n\
573+
For example:\n\
574+
If it is 'NSDirectionalEdgeInsets', then add the following struct:\n\
575+
```\n\
576+
typedef struct {\n\
577+
CGFloat top, leading, bottom, trailing;\n\
578+
} NSDirectionalEdgeInsets;\n\
579+
```\n\
580+
-----------------------------------", structName, key, structName, structName);
581+
NSAssert(false, @"As mentioned above");
582+
}
583+
#endif
566584
NSUInteger offset = declare.keyOffsets[key].unsignedIntegerValue;
567585
MFValue *result = [MFValue defaultValueWithTypeEncoding:declare.keyTypeEncodes[key].UTF8String];
568586
if (copied) {

OCRunnerTests/ORTestWithObjc.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,23 @@ - (void)testStructGetValue{
373373
XCTAssert(aValue.type == OCTypeDouble);
374374
XCTAssert(aValue.doubleValue == 4);
375375
}
376+
- (void)testStructGetValueNoDefine{
377+
// NSString *source = @"\
378+
// struct NSDirectionalEdgeInsets {\
379+
// CGFloat top, leading, bottom, trailing;\
380+
// };\
381+
// CGFloat top = UIApplication.sharedApplication.keyWindow.directionalLayoutMargins.top;\
382+
// ";
383+
NSString *source = @"CGFloat top = UIApplication.sharedApplication.keyWindow.directionalLayoutMargins.top;";
384+
MFScopeChain *scope = self.currentScope;
385+
AST *ast = [_parser parseSource:source];
386+
for (id <OCExecute> exp in ast.globalStatements) {
387+
[exp execute:scope];
388+
}
389+
if (@available(iOS 11.0, *)) {
390+
XCTAssert(UIApplication.sharedApplication.keyWindow.directionalLayoutMargins.top == [scope getValueWithIdentifier:@"top"].doubleValue);
391+
}
392+
}
376393
- (void)testDetectStructMemeryLayoutCode{
377394
NSString *result = detectStructMemeryLayoutEncodeCode("{CGRect={CGPoint=ff{CGPoint=dd}}{CGSize=dd}}");
378395
XCTAssert([result isEqualToString:@"ffdddd"]);

0 commit comments

Comments
 (0)