Skip to content

Commit 7c74158

Browse files
committed
change NSAssert to custom assert
which always throw exception when VFLEnableAssert = true
1 parent 953f15d commit 7c74158

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

AutoLayoutVisualFormat/AutoLayoutFormatAnalyzer.m

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ - (instancetype)init {
8282

8383
#endif
8484

85+
#define Assert(_condition_, ...) \
86+
if (__builtin_expect(!(_condition_), 0)) { WARN(__VA_ARGS__); }
87+
8588
#define SkipSpace(charPtr) while( isspace(*(charPtr)) ) {++(charPtr);}
8689

8790
#define kDefaultSpace 8
@@ -154,7 +157,7 @@ static inline NSLayoutAttribute getAttr(char attrChar){
154157
if (*format == '$') {
155158
++format;
156159
it = _tryGetIndexValue(format, env, out);
157-
NSCAssert1(*out, @"can't found indexValue at %s", format);
160+
Assert(*out, @"can't found indexValue at %s", format);
158161
return it;
159162
} else if (!env->envIsArray) { // dict $ may omit
160163
it = _tryGetIndexValue(format, env, out);
@@ -183,13 +186,13 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
183186

184187
if (leftView == SUPER_TOKEN) { // [V]-|
185188
leftView = [rightView superview];
186-
NSCAssert(leftView, @"superview not exist!");
189+
Assert(leftView, @"superview not exist!");
187190

188191
defAttr1 = defAttr2 = vertical ?
189192
NSLayoutAttributeBottom : NSLayoutAttributeRight;
190193
} else if (rightView == SUPER_TOKEN) { // |-[V]
191194
rightView = [leftView superview];
192-
NSCAssert(rightView, @"superview not exist!");
195+
Assert(rightView, @"superview not exist!");
193196

194197
defAttr1 = defAttr2 = vertical ?
195198
NSLayoutAttributeTop : NSLayoutAttributeLeft;
@@ -349,7 +352,7 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
349352
}
350353
// it's attr1
351354
(*outPredicate)->attr1 = getAttr(*format);
352-
NSCAssert((*outPredicate)->attr1 != 0, @"format error: unexpect attr type %c", *format);
355+
Assert((*outPredicate)->attr1 != 0, @"format error: unexpect attr type %c", *format);
353356
format = identifierEnd;
354357
}
355358
}
@@ -387,7 +390,7 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
387390
++format;
388391
SkipSpace(format);
389392
identifierEnd = analyzeConstant(format, env, &((*outPredicate)->multiplier));
390-
NSCAssert( identifierEnd != format, @"* should follow metric. at %s", format);
393+
Assert( identifierEnd != format, @"* should follow metric. at %s", format);
391394
format = identifierEnd;
392395
}
393396

@@ -404,7 +407,7 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
404407
++format;
405408
SkipSpace(format);
406409
identifierEnd = analyzeConstant(format, env, &((*outPredicate)->priority));
407-
NSCAssert( identifierEnd != format, @"@ should follow priority. at %s", format);
410+
Assert( identifierEnd != format, @"@ should follow priority. at %s", format);
408411
format = identifierEnd;
409412
}
410413

@@ -431,7 +434,7 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
431434
if (*format == '$') ++format;
432435
format = _tryGetIndexValue(format, env, outView);
433436
// outView should be UIView or layoutGuide
434-
NSCAssert(*outView, @"can't found identifier at %s!", format);
437+
Assert(*outView, @"can't found identifier at %s!", format);
435438

436439
SkipSpace(format);
437440
if (*format == '!') { (*outView).translatesAutoresizingMaskIntoConstraints = NO; ++format; SkipSpace(format); }
@@ -457,11 +460,11 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
457460
// set H or V according to label. if not set, don't change.(init default to H)
458461
if (*format == 'V') {
459462
env->vertical = true;
460-
NSCAssert(*(format+1) == ':', @"V should followed by :!");
463+
Assert(*(format+1) == ':', @"V should followed by :!");
461464
format += 2;
462465
} else if (*format == 'H') {
463466
env->vertical = false;
464-
NSCAssert(*(format+1) == ':', @"H should followed by :!");
467+
Assert(*(format+1) == ':', @"H should followed by :!");
465468
format += 2;
466469
}
467470

@@ -512,7 +515,7 @@ static void buildConstraints(id leftView, NSArray* predicates, id rightView, boo
512515
}
513516
goto CONTINUE_LOOP;
514517
}
515-
NSCAssert(NO, @"shouldn't happen!");
518+
Assert(NO, @"shouldn't happen!");
516519
}
517520
case '[': { // view statement
518521
View:

Tests/Tests.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ - (void)testVFL {
248248
XCTAssertEqualObjects(layouts, compareLayouts);
249249
}
250250

251+
- (void)testException {
252+
VFLEnableAssert = true;
253+
XCTAssertThrows(VFLConstraints(@"[$1]", [NSDictionary dictionary]));
254+
}
255+
251256
//- (void)testPerformanceExample {
252257
// // This is an example of a performance test case.
253258
// [self measureBlock:^{

0 commit comments

Comments
 (0)