#define nil ((id)0)
#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
[bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
[bundle localizedStringForKey:(key) value:(val) table:(tbl)]
#define CGFLOAT_TYPE double
typedef CGFLOAT_TYPE CGFloat;
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
@interface NSObject
+ (id)alloc;
- (id)init;
@end
@class NSDictionary;
@interface NSString : NSObject
- (void)drawAtPoint:(CGPoint)point withAttributes:(NSDictionary *)attrs;
+ (instancetype)localizedStringWithFormat:(NSString *)format, ...;
@end
@interface NSBundle : NSObject
+ (NSBundle *)mainBundle;
- (NSString *)localizedStringForKey:(NSString *)key
value:(NSString *)value
table:(NSString *)tableName;
@end
@protocol UIAccessibility
- (void)accessibilitySetIdentification:(NSString *)ident;
- (void)setAccessibilityLabel:(NSString *)label;
@end
@interface UILabel : NSObject <UIAccessibility>
@property(nullable, nonatomic, copy) NSString *text;
@end
@interface TestObject : NSObject
@property(strong) NSString *text;
@end
@interface NSView : NSObject
@property (strong) NSString *toolTip;
@end
@interface NSViewSubclass : NSView
@end
@interface LocalizationTestSuite : NSObject
NSString *ForceLocalized(NSString *str)
__attribute__((annotate("returns_localized_nsstring")));
CGPoint CGPointMake(CGFloat x, CGFloat y);
int random(void);
NSString *CFNumberFormatterCreateStringWithNumber(float x);
+ (NSString *)forceLocalized:(NSString *)str
__attribute__((annotate("returns_localized_nsstring")));
+ (NSString *)takesLocalizedString:
(NSString *)__attribute__((annotate("takes_localized_nsstring")))str;
@end
NSString *
takesLocalizedString(NSString *str
__attribute__((annotate("takes_localized_nsstring")))) {
return str;
}
@implementation LocalizationTestSuite
NSString *ForceLocalized(NSString *str) { return str; }
+ (NSString *)forceLocalized:(NSString *)str {
return str;
}
+ (NSString *) takesLocalizedString:(NSString *)str { return str; }
+ (NSString *)unLocalizedStringMethod {
return @"UnlocalizedString";
}
- (void)testLocalizationErrorDetectedOnPathway {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[testLabel setText:bar]; }
- (void)testLocalizationErrorDetectedOnNSString {
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[bar drawAtPoint:CGPointMake(0, 0) withAttributes:nil]; }
- (void)testNoLocalizationErrorDetectedFromCFunction {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = CFNumberFormatterCreateStringWithNumber(1);
[testLabel setText:bar]; }
- (void)testAnnotationAddsLocalizedStateForCFunction {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[testLabel setText:ForceLocalized(bar)]; }
- (void)testAnnotationAddsLocalizedStateForObjCMethod {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
if (random()) {
bar = @"Unlocalized string";
}
[testLabel setText:[LocalizationTestSuite forceLocalized:bar]]; }
- (void)testEmptyStringLiteralHasLocalizedState {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = @"";
[testLabel setText:bar]; }
- (void)testInlineEmptyStringLiteralHasLocalizedState {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:@""]; }
- (void)testInlineStringLiteralHasLocalizedState {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:@"Hello"]; }
- (void)testNilStringIsNotMarkedAsUnlocalized {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:nil]; }
- (void)testLocalizedStringArgument {
UILabel *testLabel = [[UILabel alloc] init];
NSString *localizedString = NSLocalizedString(@"Hello", @"Comment");
NSString *combinedString =
[NSString localizedStringWithFormat:@"%@", localizedString];
[testLabel setText:combinedString]; }
- (void)testLocalizedStringAsArgument:(NSString *)argumentString {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:argumentString]; }
- (void)testLocalizedStringAsArgumentOtherMethod:(NSString *)argumentString {
[self localizedStringAsArgument:@"UnlocalizedString"];
}
- (void)localizedStringAsArgument:(NSString *)argumentString {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setText:argumentString]; }
- (void)testUnLocalizedStringMethod {
UILabel *testLabel = [[UILabel alloc] init];
NSString *bar = NSLocalizedString(@"Hello", @"Comment");
[testLabel setText:[LocalizationTestSuite unLocalizedStringMethod]]; }
- (void)testMethodNotInRequiresLocalizedStringMethods {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel accessibilitySetIdentification:@"UnlocalizedString"]; }
- (void)testRequiresLocalizationMethodFromSuperclass {
NSViewSubclass *s = [[NSViewSubclass alloc] init];
NSString *bar = @"UnlocalizedString";
[s setToolTip:bar]; }
- (void)testRequiresLocalizationMethodFromProtocol {
UILabel *testLabel = [[UILabel alloc] init];
[testLabel setAccessibilityLabel:@"UnlocalizedString"]; }
#define HOM(s) YOLOC(s)
#define YOLOC(x) NSLocalizedString(x, nil)
- (void)testNilLocalizationContext {
NSString *string = NSLocalizedString(@"LocalizedString", nil); NSString *string2 = NSLocalizedString(@"LocalizedString", nil); NSString *string3 = NSLocalizedString(@"LocalizedString", nil); }
- (void)testEmptyLocalizationContext {
NSString *string = NSLocalizedString(@"LocalizedString", @""); NSString *string2 = NSLocalizedString(@"LocalizedString", @" "); NSString *string3 = NSLocalizedString(@"LocalizedString", @" "); }
- (void)testNSLocalizedStringVariants {
NSString *string = NSLocalizedStringFromTable(@"LocalizedString", nil, @""); NSString *string2 = NSLocalizedStringFromTableInBundle(@"LocalizedString", nil, [[NSBundle alloc] init],@""); NSString *string3 = NSLocalizedStringWithDefaultValue(@"LocalizedString", nil, [[NSBundle alloc] init], nil,@""); }
- (void)testMacroExpansionNilString {
NSString *string = YOLOC(@"Hello"); NSString *string2 = HOM(@"Hello"); NSString *string3 = NSLocalizedString((0 ? @"Critical" : @"Current"),nil); }
- (void)testMacroExpansionDefinedInPCH {
NSString *string = MyLocalizedStringInPCH(@"Hello"); }
#define KCLocalizedString(x,comment) NSLocalizedString(x, comment)
#define POSSIBLE_FALSE_POSITIVE(s,other) KCLocalizedString(s,@"Comment")
- (void)testNoWarningForNilCommentPassedIntoOtherMacro {
NSString *string = KCLocalizedString(@"Hello",@""); NSString *string2 = KCLocalizedString(@"Hello",nil); NSString *string3 = KCLocalizedString(@"Hello",@"Comment"); }
- (void)testPossibleFalsePositiveSituationAbove {
NSString *string = POSSIBLE_FALSE_POSITIVE(@"Hello", nil); NSString *string2 = POSSIBLE_FALSE_POSITIVE(@"Hello", @"Hello"); }
- (void)testTakesLocalizedString {
NSString *localized = NSLocalizedString(@"Hello", @"World");
NSString *alsoLocalized = [LocalizationTestSuite takesLocalizedString:localized]; NSString *stillLocalized = [LocalizationTestSuite takesLocalizedString:alsoLocalized]; takesLocalizedString(stillLocalized);
[LocalizationTestSuite takesLocalizedString:@"not localized"]; takesLocalizedString(@"not localized"); }
@end
@interface SynthesizedAccessors : NSObject
@property (assign) NSObject *obj;
@end
@implementation SynthesizedAccessors
@end