forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoped_method_swizzler_unittest.mm
66 lines (46 loc) · 1.93 KB
/
scoped_method_swizzler_unittest.mm
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/testing/scoped_method_swizzler.h"
#import <Foundation/Foundation.h>
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// Class containing methods that will be swizzled by the unittests.
@interface ScopedMethodSwizzlerTestClass : NSObject
- (NSString*)instanceMethodToSwizzle;
- (NSString*)swizzledInstanceMethod;
@end
namespace {
NSString* const kOriginalInstanceValue = @"Bizz";
NSString* const kSwizzledInstanceValue = @"Buzz";
using ScopedMethodSwizzlerTest = PlatformTest;
// Tests that swizzling an instance method works properly.
TEST_F(ScopedMethodSwizzlerTest, SwizzlingInstanceMethod) {
ScopedMethodSwizzlerTestClass* target =
[[ScopedMethodSwizzlerTestClass alloc] init];
EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]);
EXPECT_NSEQ(kSwizzledInstanceValue, [target swizzledInstanceMethod]);
{
ScopedMethodSwizzler swizzler([ScopedMethodSwizzlerTestClass class],
@selector(instanceMethodToSwizzle),
@selector(swizzledInstanceMethod));
EXPECT_NSEQ(kSwizzledInstanceValue, [target instanceMethodToSwizzle]);
EXPECT_NSEQ(kOriginalInstanceValue, [target swizzledInstanceMethod]);
}
EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]);
EXPECT_NSEQ(kSwizzledInstanceValue, [target swizzledInstanceMethod]);
}
} // namespace
#pragma mark - ScopedMethodSwizzlerTestClass
@implementation ScopedMethodSwizzlerTestClass
- (NSString*)instanceMethodToSwizzle {
return kOriginalInstanceValue;
}
- (NSString*)swizzledInstanceMethod {
return kSwizzledInstanceValue;
}
@end