FSTSpecTests.mm 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Example/Tests/SpecTests/FSTSpecTests.h"
  17. #import <FirebaseFirestore/FIRFirestoreErrors.h>
  18. #include <algorithm>
  19. #include <cstddef>
  20. #include <limits>
  21. #include <map>
  22. #include <memory>
  23. #include <set>
  24. #include <string>
  25. #include <unordered_map>
  26. #include <utility>
  27. #include <vector>
  28. #import "Firestore/Source/API/FSTUserDataConverter.h"
  29. #import "Firestore/Example/Tests/SpecTests/FSTSyncEngineTestDriver.h"
  30. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  31. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  32. #include "Firestore/core/src/auth/user.h"
  33. #include "Firestore/core/src/core/field_filter.h"
  34. #include "Firestore/core/src/local/persistence.h"
  35. #include "Firestore/core/src/local/target_data.h"
  36. #include "Firestore/core/src/model/delete_mutation.h"
  37. #include "Firestore/core/src/model/document.h"
  38. #include "Firestore/core/src/model/document_key.h"
  39. #include "Firestore/core/src/model/document_key_set.h"
  40. #include "Firestore/core/src/model/field_value.h"
  41. #include "Firestore/core/src/model/maybe_document.h"
  42. #include "Firestore/core/src/model/no_document.h"
  43. #include "Firestore/core/src/model/patch_mutation.h"
  44. #include "Firestore/core/src/model/resource_path.h"
  45. #include "Firestore/core/src/model/set_mutation.h"
  46. #include "Firestore/core/src/model/snapshot_version.h"
  47. #include "Firestore/core/src/model/types.h"
  48. #include "Firestore/core/src/nanopb/nanopb_util.h"
  49. #include "Firestore/core/src/remote/existence_filter.h"
  50. #include "Firestore/core/src/remote/serializer.h"
  51. #include "Firestore/core/src/remote/watch_change.h"
  52. #include "Firestore/core/src/util/async_queue.h"
  53. #include "Firestore/core/src/util/comparison.h"
  54. #include "Firestore/core/src/util/filesystem.h"
  55. #include "Firestore/core/src/util/hard_assert.h"
  56. #include "Firestore/core/src/util/log.h"
  57. #include "Firestore/core/src/util/path.h"
  58. #include "Firestore/core/src/util/status.h"
  59. #include "Firestore/core/src/util/string_apple.h"
  60. #include "Firestore/core/src/util/to_string.h"
  61. #include "Firestore/core/test/unit/testutil/testutil.h"
  62. #include "absl/types/optional.h"
  63. namespace objc = firebase::firestore::objc;
  64. namespace testutil = firebase::firestore::testutil;
  65. namespace util = firebase::firestore::util;
  66. using firebase::firestore::Error;
  67. using firebase::firestore::auth::User;
  68. using firebase::firestore::core::DocumentViewChange;
  69. using firebase::firestore::core::Query;
  70. using firebase::firestore::local::Persistence;
  71. using firebase::firestore::local::TargetData;
  72. using firebase::firestore::local::QueryPurpose;
  73. using firebase::firestore::model::Document;
  74. using firebase::firestore::model::DocumentKey;
  75. using firebase::firestore::model::DocumentKeySet;
  76. using firebase::firestore::model::DocumentState;
  77. using firebase::firestore::model::FieldValue;
  78. using firebase::firestore::model::MaybeDocument;
  79. using firebase::firestore::model::MutationResult;
  80. using firebase::firestore::model::NoDocument;
  81. using firebase::firestore::model::ObjectValue;
  82. using firebase::firestore::model::ResourcePath;
  83. using firebase::firestore::model::SnapshotVersion;
  84. using firebase::firestore::model::TargetId;
  85. using firebase::firestore::nanopb::ByteString;
  86. using firebase::firestore::nanopb::MakeByteString;
  87. using firebase::firestore::remote::ExistenceFilter;
  88. using firebase::firestore::remote::DocumentWatchChange;
  89. using firebase::firestore::remote::ExistenceFilterWatchChange;
  90. using firebase::firestore::remote::WatchTargetChange;
  91. using firebase::firestore::remote::WatchTargetChangeState;
  92. using firebase::firestore::util::MakeNSString;
  93. using firebase::firestore::util::MakeString;
  94. using firebase::firestore::util::Path;
  95. using firebase::firestore::util::Status;
  96. using firebase::firestore::util::TimerId;
  97. using testutil::Doc;
  98. using testutil::Filter;
  99. using testutil::OrderBy;
  100. NS_ASSUME_NONNULL_BEGIN
  101. // Whether to run the benchmark spec tests.
  102. // TODO(mrschmidt): Make this configurable via the tests schema.
  103. static BOOL kRunBenchmarkTests = NO;
  104. // The name of an environment variable whose value is a filter that specifies which tests to
  105. // execute. The value of this environment variable is a regular expression that is matched against
  106. // the name of each test. Using this environment variable is an alternative to setting the
  107. // kExclusiveTag tag, which requires modifying the JSON file. When this environment variable is set
  108. // to a non-empty value, a test will be executed if and only if its name matches this regular
  109. // expression. In this context, a test's "name" is the result of appending its "itName" to its
  110. // "describeName", separated by a space character.
  111. static NSString *const kTestFilterEnvKey = @"SPEC_TEST_FILTER";
  112. // Disables all other tests; useful for debugging. Multiple tests can have this tag and they'll all
  113. // be run (but all others won't).
  114. static NSString *const kExclusiveTag = @"exclusive";
  115. // A tag for tests that should be excluded from execution (on iOS), useful to allow the platforms
  116. // to temporarily diverge.
  117. static NSString *const kNoIOSTag = @"no-ios";
  118. // A tag for tests that exercise the multi-client behavior of the Web client. These tests are
  119. // ignored on iOS.
  120. static NSString *const kMultiClientTag = @"multi-client";
  121. // A tag for tests that is assigned to the perf tests in "perf_spec.json". These tests are only run
  122. // if `kRunBenchmarkTests` is set to 'YES'.
  123. static NSString *const kBenchmarkTag = @"benchmark";
  124. NSString *const kEagerGC = @"eager-gc";
  125. NSString *const kDurablePersistence = @"durable-persistence";
  126. namespace {
  127. std::vector<TargetId> ConvertTargetsArray(NSArray<NSNumber *> *from) {
  128. std::vector<TargetId> result;
  129. for (NSNumber *targetID in from) {
  130. result.push_back(targetID.intValue);
  131. }
  132. return result;
  133. }
  134. ByteString MakeResumeToken(NSString *specString) {
  135. return MakeByteString([specString dataUsingEncoding:NSUTF8StringEncoding]);
  136. }
  137. NSString *ToDocumentListString(const std::set<DocumentKey> &keys) {
  138. std::vector<std::string> strings;
  139. strings.reserve(keys.size());
  140. for (const auto &key : keys) {
  141. strings.push_back(key.ToString());
  142. }
  143. std::sort(strings.begin(), strings.end());
  144. return MakeNSString(absl::StrJoin(strings, ", "));
  145. }
  146. NSString *ToDocumentListString(const std::map<DocumentKey, TargetId> &map) {
  147. std::set<DocumentKey> keys;
  148. for (const auto &kv : map) {
  149. keys.insert(kv.first);
  150. }
  151. return ToDocumentListString(keys);
  152. }
  153. NSString *ToTargetIdListString(const ActiveTargetMap &map) {
  154. std::vector<model::TargetId> targetIds;
  155. targetIds.reserve(map.size());
  156. for (const auto &kv : map) {
  157. targetIds.push_back(kv.first);
  158. }
  159. std::sort(targetIds.begin(), targetIds.end());
  160. return MakeNSString(absl::StrJoin(targetIds, ", "));
  161. }
  162. } // namespace
  163. @interface FSTSpecTests ()
  164. @property(nonatomic, strong, nullable) FSTSyncEngineTestDriver *driver;
  165. @end
  166. @implementation FSTSpecTests {
  167. BOOL _gcEnabled;
  168. size_t _maxConcurrentLimboResolutions;
  169. BOOL _networkEnabled;
  170. FSTUserDataConverter *_converter;
  171. }
  172. #define FSTAbstractMethodException() \
  173. [NSException exceptionWithName:NSInternalInconsistencyException \
  174. reason:[NSString stringWithFormat:@"You must override %s in a subclass", \
  175. __func__] \
  176. userInfo:nil];
  177. - (std::unique_ptr<Persistence>)persistenceWithGCEnabled:(__unused BOOL)GCEnabled {
  178. @throw FSTAbstractMethodException(); // NOLINT
  179. }
  180. - (BOOL)shouldRunWithTags:(NSArray<NSString *> *)tags {
  181. if ([tags containsObject:kNoIOSTag]) {
  182. return NO;
  183. } else if ([tags containsObject:kMultiClientTag]) {
  184. return NO;
  185. } else if (!kRunBenchmarkTests && [tags containsObject:kBenchmarkTag]) {
  186. return NO;
  187. }
  188. return YES;
  189. }
  190. - (void)setUpForSpecWithConfig:(NSDictionary *)config {
  191. _converter = FSTTestUserDataConverter();
  192. // Store GCEnabled so we can re-use it in doRestart.
  193. NSNumber *GCEnabled = config[@"useGarbageCollection"];
  194. _gcEnabled = [GCEnabled boolValue];
  195. NSNumber *maxConcurrentLimboResolutions = config[@"maxConcurrentLimboResolutions"];
  196. _maxConcurrentLimboResolutions = (maxConcurrentLimboResolutions == nil)
  197. ? std::numeric_limits<size_t>::max()
  198. : maxConcurrentLimboResolutions.unsignedIntValue;
  199. NSNumber *numClients = config[@"numClients"];
  200. if (numClients) {
  201. XCTAssertEqualObjects(numClients, @1, @"The iOS client does not support multi-client tests");
  202. }
  203. std::unique_ptr<Persistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  204. self.driver =
  205. [[FSTSyncEngineTestDriver alloc] initWithPersistence:std::move(persistence)
  206. initialUser:User::Unauthenticated()
  207. outstandingWrites:{}
  208. maxConcurrentLimboResolutions:_maxConcurrentLimboResolutions];
  209. [self.driver start];
  210. }
  211. - (void)tearDownForSpec {
  212. [self.driver shutdown];
  213. // Help ARC realize that everything here can be collected earlier.
  214. _driver = nil;
  215. }
  216. /**
  217. * Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
  218. * FSTSpecTests since it is incomplete without the implementations supplied by its subclasses.
  219. */
  220. - (BOOL)isTestBaseClass {
  221. return [self class] == [FSTSpecTests class];
  222. }
  223. #pragma mark - Methods for constructing objects from specs.
  224. - (Query)parseQuery:(id)querySpec {
  225. if ([querySpec isKindOfClass:[NSString class]]) {
  226. return testutil::Query(util::MakeString((NSString *)querySpec));
  227. } else if ([querySpec isKindOfClass:[NSDictionary class]]) {
  228. NSDictionary *queryDict = (NSDictionary *)querySpec;
  229. NSString *path = queryDict[@"path"];
  230. ResourcePath resource_path = ResourcePath::FromString(util::MakeString(path));
  231. std::shared_ptr<const std::string> collectionGroup =
  232. util::MakeStringPtr(queryDict[@"collectionGroup"]);
  233. Query query(std::move(resource_path), std::move(collectionGroup));
  234. if (queryDict[@"limit"]) {
  235. NSNumber *limitNumber = queryDict[@"limit"];
  236. auto limit = static_cast<int32_t>(limitNumber.integerValue);
  237. NSString *limitType = queryDict[@"limitType"];
  238. if ([limitType isEqualToString:@"LimitToFirst"]) {
  239. query = query.WithLimitToFirst(limit);
  240. } else {
  241. query = query.WithLimitToLast(limit);
  242. }
  243. }
  244. if (queryDict[@"filters"]) {
  245. NSArray<NSArray<id> *> *filters = queryDict[@"filters"];
  246. for (NSArray<id> *filter in filters) {
  247. std::string key = util::MakeString(filter[0]);
  248. std::string op = util::MakeString(filter[1]);
  249. FieldValue value = [_converter parsedQueryValue:filter[2]];
  250. query = query.AddingFilter(Filter(key, op, value));
  251. }
  252. }
  253. if (queryDict[@"orderBys"]) {
  254. NSArray *orderBys = queryDict[@"orderBys"];
  255. for (NSArray<NSString *> *orderBy in orderBys) {
  256. std::string field_path = util::MakeString(orderBy[0]);
  257. std::string direction = util::MakeString(orderBy[1]);
  258. query = query.AddingOrderBy(OrderBy(field_path, direction));
  259. }
  260. }
  261. return query;
  262. } else {
  263. XCTFail(@"Invalid query: %@", querySpec);
  264. return Query();
  265. }
  266. }
  267. - (SnapshotVersion)parseVersion:(NSNumber *_Nullable)version {
  268. return testutil::Version(version.longLongValue);
  269. }
  270. - (DocumentViewChange)parseChange:(NSDictionary *)jsonDoc ofType:(DocumentViewChange::Type)type {
  271. NSNumber *version = jsonDoc[@"version"];
  272. NSDictionary *options = jsonDoc[@"options"];
  273. DocumentState documentState = [options[@"hasLocalMutations"] isEqualToNumber:@YES]
  274. ? DocumentState::kLocalMutations
  275. : ([options[@"hasCommittedMutations"] isEqualToNumber:@YES]
  276. ? DocumentState::kCommittedMutations
  277. : DocumentState::kSynced);
  278. XCTAssert([jsonDoc[@"key"] isKindOfClass:[NSString class]]);
  279. FieldValue data = [_converter parsedQueryValue:jsonDoc[@"value"]];
  280. Document doc = Doc(util::MakeString((NSString *)jsonDoc[@"key"]), version.longLongValue, data,
  281. documentState);
  282. return DocumentViewChange{doc, type};
  283. }
  284. #pragma mark - Methods for doing the steps of the spec test.
  285. - (void)doListen:(NSArray *)listenSpec {
  286. Query query = [self parseQuery:listenSpec[1]];
  287. TargetId actualID = [self.driver addUserListenerWithQuery:std::move(query)];
  288. TargetId expectedID = [listenSpec[0] intValue];
  289. XCTAssertEqual(actualID, expectedID, @"targetID assigned to listen");
  290. }
  291. - (void)doUnlisten:(NSArray *)unlistenSpec {
  292. Query query = [self parseQuery:unlistenSpec[1]];
  293. [self.driver removeUserListenerWithQuery:std::move(query)];
  294. }
  295. - (void)doSet:(NSArray *)setSpec {
  296. [self.driver writeUserMutation:FSTTestSetMutation(setSpec[0], setSpec[1])];
  297. }
  298. - (void)doPatch:(NSArray *)patchSpec {
  299. [self.driver
  300. writeUserMutation:FSTTestPatchMutation(util::MakeString(patchSpec[0]), patchSpec[1], {})];
  301. }
  302. - (void)doDelete:(NSString *)key {
  303. [self.driver writeUserMutation:FSTTestDeleteMutation(key)];
  304. }
  305. - (void)doWaitForPendingWrites {
  306. [self.driver waitForPendingWrites];
  307. }
  308. - (void)doAddSnapshotsInSyncListener {
  309. [self.driver addSnapshotsInSyncListener];
  310. }
  311. - (void)doRemoveSnapshotsInSyncListener {
  312. [self.driver removeSnapshotsInSyncListener];
  313. }
  314. - (void)doWatchAck:(NSArray<NSNumber *> *)ackedTargets {
  315. WatchTargetChange change{WatchTargetChangeState::Added, ConvertTargetsArray(ackedTargets)};
  316. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  317. }
  318. - (void)doWatchCurrent:(NSArray<id> *)currentSpec {
  319. NSArray<NSNumber *> *currentTargets = currentSpec[0];
  320. ByteString resumeToken = MakeResumeToken(currentSpec[1]);
  321. WatchTargetChange change{WatchTargetChangeState::Current, ConvertTargetsArray(currentTargets),
  322. resumeToken};
  323. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  324. }
  325. - (void)doWatchRemove:(NSDictionary *)watchRemoveSpec {
  326. Status error;
  327. NSDictionary *cause = watchRemoveSpec[@"cause"];
  328. if (cause) {
  329. int code = ((NSNumber *)cause[@"code"]).intValue;
  330. NSDictionary *userInfo = @{
  331. NSLocalizedDescriptionKey : @"Error from watchRemove.",
  332. };
  333. error = Status{static_cast<Error>(code), MakeString([userInfo description])};
  334. }
  335. WatchTargetChange change{WatchTargetChangeState::Removed,
  336. ConvertTargetsArray(watchRemoveSpec[@"targetIds"]), error};
  337. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  338. // Unlike web, the FSTMockDatastore detects a watch removal with cause and will remove active
  339. // targets
  340. }
  341. - (void)doWatchEntity:(NSDictionary *)watchEntity {
  342. if (watchEntity[@"docs"]) {
  343. HARD_ASSERT(!watchEntity[@"doc"], "Exactly one of |doc| or |docs| needs to be set.");
  344. NSArray *docs = watchEntity[@"docs"];
  345. for (NSDictionary *doc in docs) {
  346. NSMutableDictionary *watchSpec = [NSMutableDictionary dictionary];
  347. watchSpec[@"doc"] = doc;
  348. if (watchEntity[@"targets"]) {
  349. watchSpec[@"targets"] = watchEntity[@"targets"];
  350. }
  351. if (watchEntity[@"removedTargets"]) {
  352. watchSpec[@"removedTargets"] = watchEntity[@"removedTargets"];
  353. }
  354. [self doWatchEntity:watchSpec];
  355. }
  356. } else if (watchEntity[@"doc"]) {
  357. NSDictionary *docSpec = watchEntity[@"doc"];
  358. DocumentKey key = FSTTestDocKey(docSpec[@"key"]);
  359. absl::optional<ObjectValue> value = [docSpec[@"value"] isKindOfClass:[NSNull class]]
  360. ? absl::optional<ObjectValue>{}
  361. : FSTTestObjectValue(docSpec[@"value"]);
  362. SnapshotVersion version = [self parseVersion:docSpec[@"version"]];
  363. MaybeDocument doc;
  364. if (value) {
  365. doc = Document(*std::move(value), key, version, DocumentState::kSynced);
  366. } else {
  367. doc = NoDocument(key, version, /* has_committed_mutations= */ false);
  368. }
  369. DocumentWatchChange change{ConvertTargetsArray(watchEntity[@"targets"]),
  370. ConvertTargetsArray(watchEntity[@"removedTargets"]), std::move(key),
  371. std::move(doc)};
  372. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  373. } else if (watchEntity[@"key"]) {
  374. DocumentKey docKey = FSTTestDocKey(watchEntity[@"key"]);
  375. DocumentWatchChange change{
  376. {}, ConvertTargetsArray(watchEntity[@"removedTargets"]), docKey, absl::nullopt};
  377. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  378. } else {
  379. HARD_FAIL("Either key, doc or docs must be set.");
  380. }
  381. }
  382. - (void)doWatchFilter:(NSArray *)watchFilter {
  383. NSArray<NSNumber *> *targets = watchFilter[0];
  384. HARD_ASSERT(targets.count == 1, "ExistenceFilters currently support exactly one target only.");
  385. int keyCount = watchFilter.count == 0 ? 0 : (int)watchFilter.count - 1;
  386. ExistenceFilter filter{keyCount};
  387. ExistenceFilterWatchChange change{filter, targets[0].intValue};
  388. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  389. }
  390. - (void)doWatchReset:(NSArray<NSNumber *> *)watchReset {
  391. WatchTargetChange change{WatchTargetChangeState::Reset, ConvertTargetsArray(watchReset)};
  392. [self.driver receiveWatchChange:change snapshotVersion:SnapshotVersion::None()];
  393. }
  394. - (void)doWatchSnapshot:(NSDictionary *)watchSnapshot {
  395. // The client will only respond to watchSnapshots if they are on a target change with an empty
  396. // set of target IDs.
  397. NSArray<NSNumber *> *targetIDs =
  398. watchSnapshot[@"targetIds"] ? watchSnapshot[@"targetIds"] : [NSArray array];
  399. ByteString resumeToken = MakeResumeToken(watchSnapshot[@"resumeToken"]);
  400. WatchTargetChange change{WatchTargetChangeState::NoChange, ConvertTargetsArray(targetIDs),
  401. resumeToken};
  402. [self.driver receiveWatchChange:change
  403. snapshotVersion:[self parseVersion:watchSnapshot[@"version"]]];
  404. }
  405. - (void)doWatchStreamClose:(NSDictionary *)closeSpec {
  406. NSDictionary *errorSpec = closeSpec[@"error"];
  407. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  408. NSNumber *runBackoffTimer = closeSpec[@"runBackoffTimer"];
  409. // TODO(b/72313632): Incorporate backoff in iOS Spec Tests.
  410. HARD_ASSERT(runBackoffTimer.boolValue, "iOS Spec Tests don't support backoff.");
  411. [self.driver receiveWatchStreamError:code userInfo:errorSpec];
  412. }
  413. - (void)doWriteAck:(NSDictionary *)spec {
  414. SnapshotVersion version = [self parseVersion:spec[@"version"]];
  415. NSNumber *keepInQueue = spec[@"keepInQueue"];
  416. XCTAssertTrue(keepInQueue == nil || keepInQueue.boolValue == NO,
  417. @"'keepInQueue=true' is not supported on iOS and should only be set in "
  418. @"multi-client tests");
  419. MutationResult mutationResult(version, absl::nullopt);
  420. [self.driver receiveWriteAckWithVersion:version mutationResults:{mutationResult}];
  421. }
  422. - (void)doFailWrite:(NSDictionary *)spec {
  423. NSDictionary *errorSpec = spec[@"error"];
  424. NSNumber *keepInQueue = spec[@"keepInQueue"];
  425. int code = ((NSNumber *)(errorSpec[@"code"])).intValue;
  426. [self.driver receiveWriteError:code userInfo:errorSpec keepInQueue:keepInQueue.boolValue];
  427. }
  428. - (void)doDrainQueue {
  429. [self.driver drainQueue];
  430. }
  431. - (void)doRunTimer:(NSString *)timer {
  432. TimerId timerID;
  433. if ([timer isEqualToString:@"all"]) {
  434. timerID = TimerId::All;
  435. } else if ([timer isEqualToString:@"listen_stream_idle"]) {
  436. timerID = TimerId::ListenStreamIdle;
  437. } else if ([timer isEqualToString:@"listen_stream_connection_backoff"]) {
  438. timerID = TimerId::ListenStreamConnectionBackoff;
  439. } else if ([timer isEqualToString:@"write_stream_idle"]) {
  440. timerID = TimerId::WriteStreamIdle;
  441. } else if ([timer isEqualToString:@"write_stream_connection_backoff"]) {
  442. timerID = TimerId::WriteStreamConnectionBackoff;
  443. } else if ([timer isEqualToString:@"online_state_timeout"]) {
  444. timerID = TimerId::OnlineStateTimeout;
  445. } else {
  446. HARD_FAIL("runTimer spec step specified unknown timer: %s", timer);
  447. }
  448. [self.driver runTimer:timerID];
  449. }
  450. - (void)doDisableNetwork {
  451. _networkEnabled = NO;
  452. [self.driver disableNetwork];
  453. }
  454. - (void)doEnableNetwork {
  455. _networkEnabled = YES;
  456. [self.driver enableNetwork];
  457. }
  458. - (void)doChangeUser:(nullable id)UID {
  459. if ([UID isEqual:[NSNull null]]) {
  460. UID = nil;
  461. }
  462. [self.driver changeUser:User::FromUid(UID)];
  463. }
  464. - (void)doRestart {
  465. // Any outstanding user writes should be automatically re-sent, so we want to preserve them
  466. // when re-creating the driver.
  467. FSTOutstandingWriteQueues outstandingWrites = self.driver.outstandingWrites;
  468. User currentUser = self.driver.currentUser;
  469. [self.driver shutdown];
  470. std::unique_ptr<Persistence> persistence = [self persistenceWithGCEnabled:_gcEnabled];
  471. self.driver =
  472. [[FSTSyncEngineTestDriver alloc] initWithPersistence:std::move(persistence)
  473. initialUser:currentUser
  474. outstandingWrites:outstandingWrites
  475. maxConcurrentLimboResolutions:_maxConcurrentLimboResolutions];
  476. [self.driver start];
  477. }
  478. - (void)doStep:(NSDictionary *)step {
  479. NSNumber *clientIndex = step[@"clientIndex"];
  480. XCTAssertNil(clientIndex, @"The iOS client does not support switching clients");
  481. if (step[@"userListen"]) {
  482. [self doListen:step[@"userListen"]];
  483. } else if (step[@"userUnlisten"]) {
  484. [self doUnlisten:step[@"userUnlisten"]];
  485. } else if (step[@"userSet"]) {
  486. [self doSet:step[@"userSet"]];
  487. } else if (step[@"userPatch"]) {
  488. [self doPatch:step[@"userPatch"]];
  489. } else if (step[@"userDelete"]) {
  490. [self doDelete:step[@"userDelete"]];
  491. } else if (step[@"addSnapshotsInSyncListener"]) {
  492. [self doAddSnapshotsInSyncListener];
  493. } else if (step[@"removeSnapshotsInSyncListener"]) {
  494. [self doRemoveSnapshotsInSyncListener];
  495. } else if (step[@"drainQueue"]) {
  496. [self doDrainQueue];
  497. } else if (step[@"watchAck"]) {
  498. [self doWatchAck:step[@"watchAck"]];
  499. } else if (step[@"watchCurrent"]) {
  500. [self doWatchCurrent:step[@"watchCurrent"]];
  501. } else if (step[@"watchRemove"]) {
  502. [self doWatchRemove:step[@"watchRemove"]];
  503. } else if (step[@"watchEntity"]) {
  504. [self doWatchEntity:step[@"watchEntity"]];
  505. } else if (step[@"watchFilter"]) {
  506. [self doWatchFilter:step[@"watchFilter"]];
  507. } else if (step[@"watchReset"]) {
  508. [self doWatchReset:step[@"watchReset"]];
  509. } else if (step[@"watchSnapshot"]) {
  510. [self doWatchSnapshot:step[@"watchSnapshot"]];
  511. } else if (step[@"watchStreamClose"]) {
  512. [self doWatchStreamClose:step[@"watchStreamClose"]];
  513. } else if (step[@"watchProto"]) {
  514. // watchProto isn't yet used, and it's unclear how to create arbitrary protos from JSON.
  515. HARD_FAIL("watchProto is not yet supported.");
  516. } else if (step[@"writeAck"]) {
  517. [self doWriteAck:step[@"writeAck"]];
  518. } else if (step[@"failWrite"]) {
  519. [self doFailWrite:step[@"failWrite"]];
  520. } else if (step[@"waitForPendingWrites"]) {
  521. [self doWaitForPendingWrites];
  522. } else if (step[@"runTimer"]) {
  523. [self doRunTimer:step[@"runTimer"]];
  524. } else if (step[@"enableNetwork"]) {
  525. if ([step[@"enableNetwork"] boolValue]) {
  526. [self doEnableNetwork];
  527. } else {
  528. [self doDisableNetwork];
  529. }
  530. } else if (step[@"changeUser"]) {
  531. [self doChangeUser:step[@"changeUser"]];
  532. } else if (step[@"restart"]) {
  533. [self doRestart];
  534. } else if (step[@"applyClientState"]) {
  535. XCTFail(@"'applyClientState' is not supported on iOS and should only be used in multi-client "
  536. @"tests");
  537. } else {
  538. XCTFail(@"Unknown step: %@", step);
  539. }
  540. }
  541. - (void)validateEvent:(FSTQueryEvent *)actual matches:(NSDictionary *)expected {
  542. Query expectedQuery = [self parseQuery:expected[@"query"]];
  543. XCTAssertEqual(actual.query, expectedQuery);
  544. if ([expected[@"errorCode"] integerValue] != 0) {
  545. XCTAssertNotNil(actual.error);
  546. XCTAssertEqual(actual.error.code, [expected[@"errorCode"] integerValue]);
  547. } else {
  548. std::vector<DocumentViewChange> expectedChanges;
  549. NSMutableArray *removed = expected[@"removed"];
  550. for (NSDictionary *changeSpec in removed) {
  551. expectedChanges.push_back([self parseChange:changeSpec
  552. ofType:DocumentViewChange::Type::Removed]);
  553. }
  554. NSMutableArray *added = expected[@"added"];
  555. for (NSDictionary *changeSpec in added) {
  556. expectedChanges.push_back([self parseChange:changeSpec
  557. ofType:DocumentViewChange::Type::Added]);
  558. }
  559. NSMutableArray *modified = expected[@"modified"];
  560. for (NSDictionary *changeSpec in modified) {
  561. expectedChanges.push_back([self parseChange:changeSpec
  562. ofType:DocumentViewChange::Type::Modified]);
  563. }
  564. NSMutableArray *metadata = expected[@"metadata"];
  565. for (NSDictionary *changeSpec in metadata) {
  566. expectedChanges.push_back([self parseChange:changeSpec
  567. ofType:DocumentViewChange::Type::Metadata]);
  568. }
  569. XCTAssertEqual(actual.viewSnapshot.value().document_changes().size(), expectedChanges.size());
  570. for (size_t i = 0; i != expectedChanges.size(); ++i) {
  571. XCTAssertTrue((actual.viewSnapshot.value().document_changes()[i] == expectedChanges[i]));
  572. }
  573. BOOL expectedHasPendingWrites =
  574. expected[@"hasPendingWrites"] ? [expected[@"hasPendingWrites"] boolValue] : NO;
  575. BOOL expectedIsFromCache = expected[@"fromCache"] ? [expected[@"fromCache"] boolValue] : NO;
  576. XCTAssertEqual(actual.viewSnapshot.value().has_pending_writes(), expectedHasPendingWrites,
  577. @"hasPendingWrites");
  578. XCTAssertEqual(actual.viewSnapshot.value().from_cache(), expectedIsFromCache, @"isFromCache");
  579. }
  580. }
  581. - (void)validateExpectedSnapshotEvents:(NSArray *_Nullable)expectedEvents {
  582. NSArray<FSTQueryEvent *> *events = self.driver.capturedEventsSinceLastCall;
  583. if (!expectedEvents) {
  584. XCTAssertEqual(events.count, 0u);
  585. for (FSTQueryEvent *event in events) {
  586. XCTFail(@"Unexpected event: %@", event);
  587. }
  588. return;
  589. }
  590. XCTAssertEqual(events.count, expectedEvents.count);
  591. events =
  592. [events sortedArrayUsingComparator:^NSComparisonResult(FSTQueryEvent *q1, FSTQueryEvent *q2) {
  593. return util::WrapCompare(q1.query.CanonicalId(), q2.query.CanonicalId());
  594. }];
  595. expectedEvents = [expectedEvents
  596. sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *left, NSDictionary *right) {
  597. Query leftQuery = [self parseQuery:left[@"query"]];
  598. Query rightQuery = [self parseQuery:right[@"query"]];
  599. return util::WrapCompare(leftQuery.CanonicalId(), rightQuery.CanonicalId());
  600. }];
  601. NSUInteger i = 0;
  602. for (; i < expectedEvents.count && i < events.count; ++i) {
  603. [self validateEvent:events[i] matches:expectedEvents[i]];
  604. }
  605. for (; i < expectedEvents.count; ++i) {
  606. XCTFail(@"Missing event: %@", expectedEvents[i]);
  607. }
  608. for (; i < events.count; ++i) {
  609. XCTFail(@"Unexpected event: %@", events[i]);
  610. }
  611. }
  612. - (void)validateExpectedState:(nullable NSDictionary *)expectedState {
  613. if (expectedState) {
  614. if (expectedState[@"numOutstandingWrites"]) {
  615. XCTAssertEqual([self.driver sentWritesCount],
  616. [expectedState[@"numOutstandingWrites"] intValue]);
  617. }
  618. if (expectedState[@"writeStreamRequestCount"]) {
  619. XCTAssertEqual([self.driver writeStreamRequestCount],
  620. [expectedState[@"writeStreamRequestCount"] intValue]);
  621. }
  622. if (expectedState[@"watchStreamRequestCount"]) {
  623. XCTAssertEqual([self.driver watchStreamRequestCount],
  624. [expectedState[@"watchStreamRequestCount"] intValue]);
  625. }
  626. if (expectedState[@"activeLimboDocs"]) {
  627. DocumentKeySet expectedActiveLimboDocuments;
  628. NSArray *docNames = expectedState[@"activeLimboDocs"];
  629. for (NSString *name in docNames) {
  630. expectedActiveLimboDocuments = expectedActiveLimboDocuments.insert(FSTTestDocKey(name));
  631. }
  632. // Update the expected active limbo documents
  633. [self.driver setExpectedActiveLimboDocuments:std::move(expectedActiveLimboDocuments)];
  634. }
  635. if (expectedState[@"enqueuedLimboDocs"]) {
  636. DocumentKeySet expectedEnqueuedLimboDocuments;
  637. NSArray *docNames = expectedState[@"enqueuedLimboDocs"];
  638. for (NSString *name in docNames) {
  639. expectedEnqueuedLimboDocuments = expectedEnqueuedLimboDocuments.insert(FSTTestDocKey(name));
  640. }
  641. // Update the expected enqueued limbo documents
  642. [self.driver setExpectedEnqueuedLimboDocuments:std::move(expectedEnqueuedLimboDocuments)];
  643. }
  644. if (expectedState[@"activeTargets"]) {
  645. __block ActiveTargetMap expectedActiveTargets;
  646. [expectedState[@"activeTargets"]
  647. enumerateKeysAndObjectsUsingBlock:^(NSString *targetIDString, NSDictionary *queryData,
  648. BOOL *) {
  649. TargetId targetID = [targetIDString intValue];
  650. ByteString resumeToken = MakeResumeToken(queryData[@"resumeToken"]);
  651. NSArray *queriesJson = queryData[@"queries"];
  652. std::vector<TargetData> queries;
  653. for (id queryJson in queriesJson) {
  654. Query query = [self parseQuery:queryJson];
  655. // TODO(mcg): populate the purpose of the target once it's possible to encode that in
  656. // the spec tests. For now, hard-code that it's a listen despite the fact that it's
  657. // not always the right value.
  658. queries.push_back(TargetData(query.ToTarget(), targetID, 0, QueryPurpose::Listen,
  659. SnapshotVersion::None(), SnapshotVersion::None(),
  660. std::move(resumeToken)));
  661. }
  662. expectedActiveTargets[targetID] = std::make_pair(std::move(queries), resumeToken);
  663. }];
  664. [self.driver setExpectedActiveTargets:std::move(expectedActiveTargets)];
  665. }
  666. }
  667. // Always validate the we received the expected number of callbacks.
  668. [self validateUserCallbacks:expectedState];
  669. // Always validate that the expected limbo docs match the actual limbo docs.
  670. [self validateActiveLimboDocuments];
  671. [self validateEnqueuedLimboDocuments];
  672. // Always validate that the expected active targets match the actual active targets.
  673. [self validateActiveTargets];
  674. }
  675. - (void)validateWaitForPendingWritesEvents:(int)expectedWaitForPendingWritesEvents {
  676. XCTAssertEqual(expectedWaitForPendingWritesEvents, [self.driver waitForPendingWritesEvents]);
  677. [self.driver resetWaitForPendingWritesEvents];
  678. }
  679. - (void)validateSnapshotsInSyncEvents:(int)expectedSnapshotInSyncEvents {
  680. XCTAssertEqual(expectedSnapshotInSyncEvents, [self.driver snapshotsInSyncEvents]);
  681. [self.driver resetSnapshotsInSyncEvents];
  682. }
  683. - (void)validateUserCallbacks:(nullable NSDictionary *)expected {
  684. NSDictionary *expectedCallbacks = expected[@"userCallbacks"];
  685. NSArray<NSString *> *actualAcknowledgedDocs =
  686. [self.driver capturedAcknowledgedWritesSinceLastCall];
  687. NSArray<NSString *> *actualRejectedDocs = [self.driver capturedRejectedWritesSinceLastCall];
  688. if (expectedCallbacks) {
  689. XCTAssertTrue([actualAcknowledgedDocs isEqualToArray:expectedCallbacks[@"acknowledgedDocs"]]);
  690. XCTAssertTrue([actualRejectedDocs isEqualToArray:expectedCallbacks[@"rejectedDocs"]]);
  691. } else {
  692. XCTAssertEqual([actualAcknowledgedDocs count], 0u);
  693. XCTAssertEqual([actualRejectedDocs count], 0u);
  694. }
  695. }
  696. - (void)validateActiveLimboDocuments {
  697. // Make a copy so it can modified while checking against the expected limbo docs.
  698. std::map<DocumentKey, TargetId> actualLimboDocs = self.driver.activeLimboDocumentResolutions;
  699. // Validate that each active limbo doc has an expected active target
  700. for (const auto &kv : actualLimboDocs) {
  701. const auto &expected = [self.driver expectedActiveTargets];
  702. XCTAssertTrue(expected.find(kv.second) != expected.end(),
  703. @"Found limbo doc %s, but its target ID %d was not in the "
  704. @"set of expected active target IDs %@",
  705. kv.first.ToString().c_str(), kv.second, ToTargetIdListString(expected));
  706. }
  707. for (const DocumentKey &expectedLimboDoc : self.driver.expectedActiveLimboDocuments) {
  708. XCTAssert(actualLimboDocs.find(expectedLimboDoc) != actualLimboDocs.end(),
  709. @"Expected doc to be in limbo, but was not: %s", expectedLimboDoc.ToString().c_str());
  710. actualLimboDocs.erase(expectedLimboDoc);
  711. }
  712. XCTAssertTrue(actualLimboDocs.empty(), @"Unexpected active docs in limbo: %@",
  713. ToDocumentListString(actualLimboDocs));
  714. }
  715. - (void)validateEnqueuedLimboDocuments {
  716. std::set<DocumentKey> actualLimboDocs;
  717. for (const auto &key : self.driver.enqueuedLimboDocumentResolutions) {
  718. actualLimboDocs.insert(key);
  719. }
  720. std::set<DocumentKey> expectedLimboDocs;
  721. for (const auto &key : self.driver.expectedEnqueuedLimboDocuments) {
  722. expectedLimboDocs.insert(key);
  723. }
  724. for (const auto &key : actualLimboDocs) {
  725. XCTAssertTrue(expectedLimboDocs.find(key) != expectedLimboDocs.end(),
  726. @"Found enqueued limbo doc %s, but it was not in the set of "
  727. @"expected enqueued limbo documents (%@)",
  728. key.ToString().c_str(), ToDocumentListString(expectedLimboDocs));
  729. }
  730. for (const auto &key : expectedLimboDocs) {
  731. XCTAssertTrue(actualLimboDocs.find(key) != actualLimboDocs.end(),
  732. @"Expected doc %s to be enqueued for limbo resolution, "
  733. @"but it was not in the queue (%@)",
  734. key.ToString().c_str(), ToDocumentListString(actualLimboDocs));
  735. }
  736. }
  737. - (void)validateActiveTargets {
  738. if (!_networkEnabled) {
  739. return;
  740. }
  741. // Create a copy so we can modify it below
  742. std::unordered_map<TargetId, TargetData> actualTargets = [self.driver activeTargets];
  743. for (const auto &kv : [self.driver expectedActiveTargets]) {
  744. TargetId targetID = kv.first;
  745. const std::pair<std::vector<TargetData>, ByteString> &queries = kv.second;
  746. const TargetData &targetData = queries.first[0];
  747. auto found = actualTargets.find(targetID);
  748. XCTAssertNotEqual(found, actualTargets.end(), @"Expected active target not found: %s",
  749. targetData.ToString().c_str());
  750. // TODO(mcg): validate the purpose of the target once it's possible to encode that in the
  751. // spec tests. For now, only validate properties that can be validated.
  752. // XCTAssertEqualObjects(actualTargets[targetID], TargetData);
  753. const TargetData &actual = found->second;
  754. XCTAssertEqual(actual.target(), targetData.target());
  755. XCTAssertEqual(actual.target_id(), targetData.target_id());
  756. XCTAssertEqual(actual.snapshot_version(), targetData.snapshot_version());
  757. XCTAssertEqual(actual.resume_token(), targetData.resume_token());
  758. actualTargets.erase(targetID);
  759. }
  760. XCTAssertTrue(actualTargets.empty(), "Unexpected active targets: %s",
  761. util::ToString(actualTargets).c_str());
  762. }
  763. - (void)runSpecTestSteps:(NSArray *)steps config:(NSDictionary *)config {
  764. @autoreleasepool {
  765. @try {
  766. [self setUpForSpecWithConfig:config];
  767. for (NSDictionary *step in steps) {
  768. LOG_DEBUG("Doing step %s", step);
  769. [self doStep:step];
  770. [self validateExpectedSnapshotEvents:step[@"expectedSnapshotEvents"]];
  771. [self validateExpectedState:step[@"expectedState"]];
  772. int expectedSnapshotsInSyncEvents = [step[@"expectedSnapshotsInSyncEvents"] intValue];
  773. [self validateSnapshotsInSyncEvents:expectedSnapshotsInSyncEvents];
  774. int expectedWaitForPendingWritesEvents =
  775. [step[@"expectedWaitForPendingWritesEvents"] intValue];
  776. [self validateWaitForPendingWritesEvents:expectedWaitForPendingWritesEvents];
  777. }
  778. [self.driver validateUsage];
  779. } @finally {
  780. // Ensure that the driver is torn down even if the test is failing due to a thrown exception
  781. // so that any resources held by the driver are released. This is important when the driver is
  782. // backed by LevelDB because LevelDB locks its database. If -tearDownForSpec were not called
  783. // after an exception then subsequent attempts to open the LevelDB will fail, making it harder
  784. // to zero in on the spec tests as a culprit.
  785. [self tearDownForSpec];
  786. }
  787. }
  788. }
  789. #pragma mark - The actual test methods.
  790. - (void)testSpecTests {
  791. if ([self isTestBaseClass]) return;
  792. // Enumerate the .json files containing the spec tests.
  793. NSMutableArray<NSString *> *specFiles = [NSMutableArray array];
  794. NSMutableArray<NSDictionary *> *parsedSpecs = [NSMutableArray array];
  795. BOOL exclusiveMode = NO;
  796. // TODO(wilhuff): Fix this when running spec tests using a real device
  797. auto source_file = Path::FromUtf8(__FILE__);
  798. Path json_ext = Path::FromUtf8(".json");
  799. auto spec_dir = source_file.Dirname();
  800. auto json_dir = spec_dir.AppendUtf8("json");
  801. auto iter = util::DirectoryIterator::Create(json_dir);
  802. for (; iter->Valid(); iter->Next()) {
  803. Path entry = iter->file();
  804. if (!entry.HasExtension(json_ext)) {
  805. continue;
  806. }
  807. // Read and parse the JSON from the file.
  808. NSString *path = entry.ToNSString();
  809. NSData *json = [NSData dataWithContentsOfFile:path];
  810. XCTAssertNotNil(json);
  811. NSError *error = nil;
  812. id _Nullable parsed = [NSJSONSerialization JSONObjectWithData:json options:0 error:&error];
  813. XCTAssertNil(error, @"%@", error);
  814. XCTAssertTrue([parsed isKindOfClass:[NSDictionary class]]);
  815. NSDictionary *testDict = (NSDictionary *)parsed;
  816. exclusiveMode = exclusiveMode || [self anyTestsAreMarkedExclusive:testDict];
  817. [specFiles addObject:entry.Basename().ToNSString()];
  818. [parsedSpecs addObject:testDict];
  819. }
  820. NSString *testNameFilterFromEnv = NSProcessInfo.processInfo.environment[kTestFilterEnvKey];
  821. NSRegularExpression *testNameFilter;
  822. if (testNameFilterFromEnv.length == 0) {
  823. testNameFilter = nil;
  824. } else {
  825. exclusiveMode = YES;
  826. NSError *error;
  827. testNameFilter =
  828. [NSRegularExpression regularExpressionWithPattern:testNameFilterFromEnv
  829. options:NSRegularExpressionAnchorsMatchLines
  830. error:&error];
  831. XCTAssertNotNil(testNameFilter, @"Invalid regular expression: %@ (%@)", testNameFilterFromEnv,
  832. error);
  833. }
  834. // Now iterate over them and run them.
  835. __block int testPassCount = 0;
  836. __block int testSkipCount = 0;
  837. __block bool ranAtLeastOneTest = NO;
  838. for (NSUInteger i = 0; i < specFiles.count; i++) {
  839. NSLog(@"Spec test file: %@", specFiles[i]);
  840. // Iterate over the tests in the file and run them.
  841. [parsedSpecs[i] enumerateKeysAndObjectsUsingBlock:^(id, id obj, BOOL *) {
  842. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  843. NSDictionary *testDescription = (NSDictionary *)obj;
  844. NSString *describeName = testDescription[@"describeName"];
  845. NSString *itName = testDescription[@"itName"];
  846. NSString *name = [NSString stringWithFormat:@"%@ %@", describeName, itName];
  847. NSDictionary *config = testDescription[@"config"];
  848. NSArray *steps = testDescription[@"steps"];
  849. NSArray<NSString *> *tags = testDescription[@"tags"];
  850. BOOL runTest;
  851. if (![self shouldRunWithTags:tags]) {
  852. runTest = NO;
  853. } else if (!exclusiveMode) {
  854. runTest = YES;
  855. } else if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  856. runTest = YES;
  857. } else if (testNameFilter != nil) {
  858. NSRange testNameFilterMatchRange =
  859. [testNameFilter rangeOfFirstMatchInString:name
  860. options:0
  861. range:NSMakeRange(0, [name length])];
  862. runTest = !NSEqualRanges(testNameFilterMatchRange, NSMakeRange(NSNotFound, 0));
  863. } else {
  864. runTest = NO;
  865. }
  866. if (runTest) {
  867. NSLog(@" Spec test: %@", name);
  868. [self runSpecTestSteps:steps config:config];
  869. ranAtLeastOneTest = YES;
  870. ++testPassCount;
  871. } else {
  872. ++testSkipCount;
  873. NSLog(@" [SKIPPED] Spec test: %@", name);
  874. NSString *comment = testDescription[@"comment"];
  875. if (comment) {
  876. NSLog(@" %@", comment);
  877. }
  878. }
  879. }];
  880. }
  881. NSLog(@"%@ completed; pass=%d skip=%d", NSStringFromClass([self class]), testPassCount,
  882. testSkipCount);
  883. XCTAssertTrue(ranAtLeastOneTest);
  884. }
  885. - (BOOL)anyTestsAreMarkedExclusive:(NSDictionary *)tests {
  886. __block BOOL found = NO;
  887. [tests enumerateKeysAndObjectsUsingBlock:^(id, id obj, BOOL *stop) {
  888. XCTAssertTrue([obj isKindOfClass:[NSDictionary class]]);
  889. NSDictionary *testDescription = (NSDictionary *)obj;
  890. NSArray<NSString *> *tags = testDescription[@"tags"];
  891. if ([tags indexOfObject:kExclusiveTag] != NSNotFound) {
  892. found = YES;
  893. *stop = YES;
  894. }
  895. }];
  896. return found;
  897. }
  898. @end
  899. NS_ASSUME_NONNULL_END