forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDTypeLexerUnitTest.m
177 lines (144 loc) · 5.04 KB
/
CDTypeLexerUnitTest.m
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
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-2019 Steve Nygard.
#import "CDTypeLexerUnitTest.h"
#import <Foundation/Foundation.h>
#import "CDTypeLexer.h"
struct tokenValuePair {
int token;
NSString *value;
int nextState;
};
@implementation CDTypeLexerUnitTest
- (void)setUp;
{
}
- (void)tearDown;
{
}
- (void)_setupLexerForString:(NSString *)str;
{
lexer = [[CDTypeLexer alloc] initWithString:str];
}
- (void)_cleanupLexer;
{
[lexer release];
lexer = nil;
}
- (void)_showScannedTokens;
{
int token;
NSLog(@"----------------------------------------");
STAssertNotNil(lexer, @"");
NSLog(@"str: %@", [lexer string]);
[lexer setShouldShowLexing:YES];
token = [lexer scanNextToken];
while (token != TK_EOS)
token = [lexer scanNextToken];
NSLog(@"----------------------------------------");
}
- (void)showScannedTokensForString:(NSString *)str;
{
[self _setupLexerForString:str];
[self _showScannedTokens];
[self _cleanupLexer];
}
// The last token in expectedResults must be TK_EOS.
- (void)testLexingString:(NSString *)str expectedResults:(struct tokenValuePair *)expectedResults;
{
int token;
[self _setupLexerForString:str];
//NSLog(@"str: %@", [lexer string]);
//[lexer setShouldShowLexing:YES];
while (expectedResults->token != TK_EOS) {
token = [lexer scanNextToken];
STAssertEquals(expectedResults->token, token, @"");
if (expectedResults->value != nil)
STAssertEqualObjects(expectedResults->value, [lexer lexText], @"");
if (expectedResults->nextState != -1)
[lexer setState:expectedResults->nextState];
expectedResults++;
}
token = [lexer scanNextToken];
STAssertEquals(TK_EOS, token, @"");
[self _cleanupLexer];
}
- (void)testSimpleTokens;
{
NSString *str = @"i^@";
struct tokenValuePair tokens[] = {
{ 'i', nil, -1 },
{ '^', nil, -1 },
{ '@', nil, -1 },
{ TK_EOS, nil, -1 },
};
[self testLexingString:str expectedResults:tokens];
}
- (void)testQuotedStringToken;
{
NSString *str = @"@\"NSObject\"";
struct tokenValuePair tokens[] = {
{ '@', nil, -1 },
{ TK_QUOTED_STRING, @"NSObject", -1 },
{ TK_EOS, nil, -1 },
};
[self testLexingString:str expectedResults:tokens];
}
- (void)testEmptyQuotedStringToken;
{
NSString *str = @"@\"\"";
struct tokenValuePair tokens[] = {
{ '@', nil, -1 },
{ TK_QUOTED_STRING, @"", -1 },
{ TK_EOS, nil, -1 },
};
[self testLexingString:str expectedResults:tokens];
}
- (void)testUnterminatedQuotedString;
{
NSString *str = @"@\"NSObject";
struct tokenValuePair tokens[] = {
{ '@', nil, -1 },
{ TK_QUOTED_STRING, @"NSObject", -1 },
{ TK_EOS, nil, -1 },
};
[self testLexingString:str expectedResults:tokens];
}
// The lexer should automatically switch back to normal mode after scanning one identifier.
- (void)testIdentifierToken;
{
NSString *str = @"iii)ii";
struct tokenValuePair tokens[] = {
{ 'i', nil, CDTypeLexerState_Identifier },
{ TK_IDENTIFIER, @"ii", -1 },
{ ')', nil, -1 },
{ 'i', nil, -1 },
{ 'i', nil, -1 },
{ TK_EOS, nil, -1 },
};
[self testLexingString:str expectedResults:tokens];
}
// This tests a more complicated C++ template type, and makes sure the space between the '>'s is ignored.
- (void)testTemplateTokens;
{
NSString *str = @"{vector<IPPhotoInfo*,std::allocator<IPPhotoInfo*> >=iic}";
struct tokenValuePair tokens[] = {
{ '{', nil, CDTypeLexerState_Identifier },
{ TK_IDENTIFIER, @"vector", -1 },
{ '<', nil, CDTypeLexerState_TemplateTypes },
{ TK_TEMPLATE_TYPE, @"IPPhotoInfo*", -1 },
{ ',', nil, -1 },
{ TK_TEMPLATE_TYPE, @"std::allocator", -1 },
{ '<', nil, CDTypeLexerState_TemplateTypes },
{ TK_TEMPLATE_TYPE, @"IPPhotoInfo*", -1 },
{ '>', nil, -1 },
{ '>', nil, CDTypeLexerState_Normal },
{ '=', nil, -1 },
{ 'i', nil, -1 },
{ 'i', nil, -1 },
{ 'c', nil, -1 },
{ '}', nil, -1 },
{ TK_EOS, nil, -1 },
};
[self testLexingString:str expectedResults:tokens];
}
@end