Kaynağa Gözat

Add parameterized configuration to autobahn tests. (#365)

Nikita Lutsenko 9 yıl önce
ebeveyn
işleme
ba8252279e

+ 4 - 0
SocketRocket.xcodeproj/project.pbxproj

@@ -22,6 +22,7 @@
 		8105E4821CDD67BD00AA12DB /* SRTWebSocketOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E4771CDD679A00AA12DB /* SRTWebSocketOperation.m */; };
 		8105E4841CDD67CE00AA12DB /* XCTestCase+SRTAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E47E1CDD679A00AA12DB /* XCTestCase+SRTAdditions.m */; };
 		8105E4AE1CDD6E6200AA12DB /* SRAutobahnOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 8105E4AD1CDD6E6200AA12DB /* SRAutobahnOperation.m */; };
+		8105E5281CDD98E100AA12DB /* autobahn_configuration.json in Resources */ = {isa = PBXBuildFile; fileRef = 8105E5271CDD98E100AA12DB /* autobahn_configuration.json */; };
 		811934BC1CDAF725003AB243 /* SocketRocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 555E0EB11C51E56D00E6BB92 /* SocketRocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		811934BE1CDAF725003AB243 /* SocketRocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 555E0EB11C51E56D00E6BB92 /* SocketRocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		811934C01CDAF726003AB243 /* SocketRocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 555E0EB11C51E56D00E6BB92 /* SocketRocket.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -110,6 +111,7 @@
 		8105E47E1CDD679A00AA12DB /* XCTestCase+SRTAdditions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "XCTestCase+SRTAdditions.m"; sourceTree = "<group>"; };
 		8105E4AC1CDD6E6200AA12DB /* SRAutobahnOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SRAutobahnOperation.h; sourceTree = "<group>"; };
 		8105E4AD1CDD6E6200AA12DB /* SRAutobahnOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRAutobahnOperation.m; sourceTree = "<group>"; };
+		8105E5271CDD98E100AA12DB /* autobahn_configuration.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = autobahn_configuration.json; sourceTree = "<group>"; };
 		811934B11CDAF711003AB243 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		81B31C0F1CDC404100D86D43 /* SRIOConsumer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SRIOConsumer.h; sourceTree = "<group>"; };
 		81B31C101CDC404100D86D43 /* SRIOConsumer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRIOConsumer.m; sourceTree = "<group>"; };
@@ -245,6 +247,7 @@
 		8105E4781CDD679A00AA12DB /* Resources */ = {
 			isa = PBXGroup;
 			children = (
+				8105E5271CDD98E100AA12DB /* autobahn_configuration.json */,
 				8105E4791CDD679A00AA12DB /* SRWebSocketTests-Info.plist */,
 			);
 			path = Resources;
@@ -637,6 +640,7 @@
 			isa = PBXResourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				8105E5281CDD98E100AA12DB /* autobahn_configuration.json in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 15 - 0
Tests/Resources/autobahn_configuration.json

@@ -0,0 +1,15 @@
+{
+    "UNIMPLEMENTED": [
+                      "12",
+                      "13"
+                      ],
+    "NON-STRICT": [
+                   "6.4.2",
+                   "6.4.4",
+                   ],
+    "INFORMATIONAL": [
+                      "7.1.6",
+                      "7.13.1",
+                      "7.13.2"
+                      ]
+}

+ 41 - 27
Tests/SRTAutobahnTests.m

@@ -23,19 +23,34 @@
 @interface SRTAutobahnTests : XCTestCase
 @end
 
-@interface NSInvocation (SRTBlockInvocation)
-
-+ (NSInvocation *)invocationWithBlock:(dispatch_block_t)block;
-
-@end
-
-@interface SRTBlockInvoker
-
-- (instancetype)initWithBlock:(dispatch_block_t)block;
+static NSDictionary<NSString *, id> *SRAutobahnTestConfiguration() {
+    static NSDictionary *configuration;
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        NSURL *configurationURL = [[NSBundle bundleForClass:[SRTAutobahnTests class]] URLForResource:@"autobahn_configuration"
+                                                                                       withExtension:@"json"];
+        NSInputStream *readStream = [NSInputStream inputStreamWithURL:configurationURL];
+        [readStream open];
+        configuration = [NSJSONSerialization JSONObjectWithStream:readStream options:0 error:nil];
+        [readStream close];
+    });
+    return configuration;
+}
 
-- (void)invoke;
+static BOOL SRAutobahnIsValidResultBehavior(NSString *caseIdentifier, NSString *behavior)
+{
+    if ([behavior isEqualToString:@"OK"]) {
+        return YES;
+    }
 
-@end
+    NSArray *cases = SRAutobahnTestConfiguration()[behavior];
+    for (NSString *caseId in cases) {
+        if ([caseIdentifier hasPrefix:caseId]) {
+            return YES;
+        }
+    }
+    return NO;
+}
 
 @implementation SRTAutobahnTests {
     SRWebSocket *_curWebSocket;
@@ -46,13 +61,15 @@
     NSURL *_prefixURL;
     NSString *_agent;
     NSString *_description;
+    NSString *_identifier;
 }
 
-- (instancetype)initWithInvocation:(NSInvocation *)anInvocation description:(NSString *)description;
+- (instancetype)initWithInvocation:(NSInvocation *)anInvocation description:(NSString *)description identifier:(NSString *)identifier
 {
     self = [self initWithInvocation:anInvocation];
     if (self) {
         _description = description;
+        _identifier = identifier;
     }
     return self;
 }
@@ -88,11 +105,6 @@
     return count;
 }
 
-- (BOOL)isEmpty;
-{
-    return NO;
-}
-
 - (void)performTest:(XCTestCaseRun *) aRun
 {
     if (self.invocation) {
@@ -108,13 +120,14 @@
         invocation.target = self;
 
         [invocation setArgument:&i atIndex:2];
+        
+        NSDictionary *caseInfo = [self caseInfoForCaseNumber:i];
+        NSString *identifier = caseInfo[@"id"];
+        NSString *description = [NSString stringWithFormat:@"%@ - %@", caseInfo[@"id"], caseInfo[@"description"]];
 
-        NSString *description = [self caseDescriptionForCaseNumber:i];
-
-        XCTestCase *testCase = [[[self class] alloc] initWithInvocation:invocation description:description];
+        XCTestCase *testCase = [[[self class] alloc] initWithInvocation:invocation description:description identifier:identifier];
 
         XCTestCaseRun *run = [[XCTestCaseRun alloc] initWithTest:testCase];
-
         [testCase performTest:run];
     }
     [aRun stop];
@@ -129,11 +142,11 @@
     return i;
 }
 
-- (NSString *)caseDescriptionForCaseNumber:(NSInteger)caseNumber;
+- (NSDictionary *)caseInfoForCaseNumber:(NSInteger)caseNumber;
 {
     __block NSDictionary *caseInfo = nil;
-    SRAutobahnOperation *testInfoOperation = SRAutobahnTestCaseInfoOperation(_prefixURL, caseNumber, ^(NSDictionary * _Nullable caseInfo) {
-        caseInfo = caseInfo;
+    SRAutobahnOperation *testInfoOperation = SRAutobahnTestCaseInfoOperation(_prefixURL, caseNumber, ^(NSDictionary * _Nullable info) {
+        caseInfo = info;
     });
 
     [testInfoOperation start];
@@ -143,8 +156,7 @@
     } timeout:60 * 60];
 
     XCTAssertNil(testInfoOperation.error, @"Updating the report should not have errored");
-
-    return [NSString stringWithFormat:@"%@ - %@", caseInfo[@"id"], caseInfo[@"description"]];
+    return caseInfo;
 }
 
 - (NSString *)description;
@@ -185,7 +197,9 @@
     } timeout:60 * 60];
 
     XCTAssertTrue(!testOp.error, @"Test operation should not have failed");
-    XCTAssertEqualObjects(@"OK", resultInfo[@"behavior"], @"Test behavior should be OK");
+    if (!SRAutobahnIsValidResultBehavior(_identifier, resultInfo[@"behavior"])) {
+        XCTFail(@"Invalid test behavior %@ for %@.", resultInfo[@"behavior"], _identifier);
+    }
 }
 
 - (void)updateReports