From 074c5872f7bf4ffc12c8171458d7c260d469aa9f Mon Sep 17 00:00:00 2001 From: Hanton Yang Date: Sun, 25 Aug 2019 13:39:49 +0800 Subject: [PATCH] Add tests for ASLayoutSpecUtilities --- AsyncDisplayKit.xcodeproj/project.pbxproj | 4 + Tests/ASLayoutSpecUtilitiesTests.mm | 244 ++++++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 Tests/ASLayoutSpecUtilitiesTests.mm diff --git a/AsyncDisplayKit.xcodeproj/project.pbxproj b/AsyncDisplayKit.xcodeproj/project.pbxproj index 9e719c0b5..270e26717 100644 --- a/AsyncDisplayKit.xcodeproj/project.pbxproj +++ b/AsyncDisplayKit.xcodeproj/project.pbxproj @@ -100,6 +100,7 @@ 3917EBD41E9C2FC400D04A01 /* _ASCollectionReusableView.h in Headers */ = {isa = PBXBuildFile; fileRef = 3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */; settings = {ATTRIBUTES = (Private, ); }; }; 3917EBD51E9C2FC400D04A01 /* _ASCollectionReusableView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.mm */; }; 3C9C128519E616EF00E942A0 /* ASTableViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */; }; + 407B8BAE2310E2ED00CB979E /* ASLayoutSpecUtilitiesTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 407B8BAD2310E2ED00CB979E /* ASLayoutSpecUtilitiesTests.mm */; }; 471D04B1224CB98600649215 /* ASImageNodeBackingSizeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 471D04B0224CB98600649215 /* ASImageNodeBackingSizeTests.mm */; }; 4E9127691F64157600499623 /* ASRunLoopQueueTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4E9127681F64157600499623 /* ASRunLoopQueueTests.mm */; }; 509E68601B3AED8E009B9150 /* ASScrollDirection.mm in Sources */ = {isa = PBXBuildFile; fileRef = 205F0E111B371BD7007741D0 /* ASScrollDirection.mm */; }; @@ -679,6 +680,7 @@ 3917EBD21E9C2FC400D04A01 /* _ASCollectionReusableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _ASCollectionReusableView.h; sourceTree = ""; }; 3917EBD31E9C2FC400D04A01 /* _ASCollectionReusableView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = _ASCollectionReusableView.mm; sourceTree = ""; }; 3C9C128419E616EF00E942A0 /* ASTableViewTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASTableViewTests.mm; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + 407B8BAD2310E2ED00CB979E /* ASLayoutSpecUtilitiesTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ASLayoutSpecUtilitiesTests.mm; sourceTree = ""; }; 464052191A3F83C40061C0BA /* ASDataController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = ASDataController.h; sourceTree = ""; }; 4640521A1A3F83C40061C0BA /* ASDataController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; lineEnding = 0; path = ASDataController.mm; sourceTree = ""; }; 4640521B1A3F83C40061C0BA /* ASTableLayoutController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ASTableLayoutController.h; sourceTree = ""; }; @@ -1392,6 +1394,7 @@ 83A7D95D1D446A6E00BF333E /* ASWeakMapTests.mm */, CC3B208D1C3F7D0A00798563 /* ASWeakSetTests.mm */, 695BE2541DC1245C008E6EA5 /* ASWrapperSpecSnapshotTests.mm */, + 407B8BAD2310E2ED00CB979E /* ASLayoutSpecUtilitiesTests.mm */, 057D02C01AC0A66700C7AC3C /* AsyncDisplayKitTestHost */, CC583ABF1EF9BAB400134156 /* Common */, 058D09C6195D04C000B7D73C /* Supporting Files */, @@ -2337,6 +2340,7 @@ ACF6ED601B178DC700DA7C62 /* ASLayoutSpecSnapshotTestsHelper.mm in Sources */, CC7FD9E11BB5F750005CCB2B /* ASPhotosFrameworkImageRequestTests.mm in Sources */, 052EE0661A159FEF002C6279 /* ASMultiplexImageNodeTests.mm in Sources */, + 407B8BAE2310E2ED00CB979E /* ASLayoutSpecUtilitiesTests.mm in Sources */, 058D0A3C195D057000B7D73C /* ASMutableAttributedStringBuilderTests.mm in Sources */, F325E48C21745F9E00AC93A4 /* ASButtonNodeTests.mm in Sources */, 9692B4FF219E12370060C2C3 /* ASCollectionViewThrashTests.mm in Sources */, diff --git a/Tests/ASLayoutSpecUtilitiesTests.mm b/Tests/ASLayoutSpecUtilitiesTests.mm new file mode 100644 index 000000000..fb099fa80 --- /dev/null +++ b/Tests/ASLayoutSpecUtilitiesTests.mm @@ -0,0 +1,244 @@ +// +// ASLayoutSpecUtilitiesTests.m +// AsyncDisplayKitTests +// +// Copyright (c) Pinterest, Inc. All rights reserved. +// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 +// + +#import +#import "ASLayoutSpecUtilities.h" + +@interface ASLayoutSpecUtilitiesTests : XCTestCase + +@end + +@implementation ASLayoutSpecUtilitiesTests + +- (void)testMapVectorPrimitive +{ + std::vector a = {3, 1, 4}; + std::vector b = AS::map(a, ^int(int i) { + return i * 2; + }); + std::vector c = {6, 2, 8}; + XCTAssertEqual(b, c); +} + +- (void)testMapVectorNSObject +{ + std::vector a = {@"2.16", @"3.14"}; + std::vector b = AS::map(a, ^float(NSString *str) { + return str.floatValue; + }); + std::vector c = {2.16, 3.14}; + XCTAssertEqual(b, c); +} + +- (void)testMapVectorStruct +{ + struct TestStruct + { + NSString *value; + + bool operator==(const TestStruct& rhs) const + { + return [value isEqualToString:rhs.value]; + } + }; + + std::vector a = { + {.value = @"2"}, + {.value = @"1"}, + {.value = @"6"} + }; + std::vector b = AS::map(a, ^NSInteger(TestStruct s) { + return s.value.integerValue; + }); + std::vector c = {2, 1, 6}; + XCTAssertEqual(b, c); +} + +- (void)testMapVectorClass +{ + class TestClass { + private: + NSString *_value; + + public: + TestClass (NSString *value) + { + _value = value; + } + + NSString *getValue() const noexcept + { + return _value; + } + + void setValue(NSString *value) noexcept + { + _value = value; + } + + bool operator==(const TestClass& rhs) const noexcept + { + return [getValue() isEqualToString:rhs.getValue()]; + } + }; + + std::vector a = { + TestClass(@"2"), + TestClass(@"1"), + TestClass(@"6") + }; + std::vector b = AS::map(a, ^NSInteger(TestClass c) { + return c.getValue().integerValue; + }); + std::vector c = {2, 1, 6}; + XCTAssertEqual(b, c); +} + +- (void)testMapVectorEmpty +{ + std::vector a = {}; + std::vector b = AS::map(a, ^int(NSString *str) { + return str.intValue; + }); + std::vector c = {}; + XCTAssertEqual(b, c); +} + +- (void)testMapNSFastEnumeration +{ + struct TestStruct { + NSString *value; + + bool operator==(const TestStruct& rhs) const + { + return [value isEqualToString:rhs.value]; + } + }; + + NSArray *a = @[@1, @2, @3]; + std::vector b = AS::map(a, ^TestStruct(NSNumber *num) { + return {.value = num.stringValue}; + }); + std::vector c = { + {.value = @"1"}, + {.value = @"2"}, + {.value = @"3"} + }; + XCTAssertEqual(b, c); +} + +- (void)testMapNSFastEnumerationEmpty +{ + NSArray *a = @[]; + std::vector b = AS::map(a, ^int(NSNumber *num) { + return num.intValue; + }); + std::vector c = {}; + XCTAssertEqual(b, c); +} + +- (void)testFilterVectorPrimitive +{ + std::vector a = {1, 2, 3, 4}; + std::vector b = AS::filter(a, ^BOOL(int num) { + return num < 3; + }); + std::vector c = {1, 2}; + XCTAssertEqual(b, c); +} + +- (void)testFilterVectorNSObject +{ + std::vector a = {@"9", @"2", @"6"}; + std::vector b = AS::filter(a, ^BOOL(NSString *str) { + return str.integerValue % 2 == 0; + }); + std::vector c = {@"2", @"6"}; + XCTAssertEqual(b, c); +} + +- (void)testFilterVectorStruct +{ + struct TestStruct { + NSString *value; + + bool operator==(const TestStruct& rhs) const + { + return [value isEqualToString:rhs.value]; + } + }; + + std::vector a = { + {.value = @"6"}, + {.value = @"8"}, + {.value = @"3"}, + {.value = @"9"} + }; + std::vector b = AS::filter(a, ^BOOL(TestStruct s) { + return s.value.integerValue == 3; + }); + std::vector c = { + {.value = @"3"} + }; + XCTAssertEqual(b, c); +} + +- (void)testFilterVectorClass +{ + class TestClass { + private: + NSString *_value; + + public: + TestClass (NSString *value) + { + _value = value; + } + + NSString *getValue() const noexcept + { + return _value; + } + + void setValue(NSString *value) noexcept + { + _value = value; + } + + bool operator==(const TestClass& rhs) const noexcept + { + return [getValue() isEqualToString:rhs.getValue()]; + } + }; + + std::vector a = { + TestClass(@"2"), + TestClass(@"1"), + TestClass(@"6") + }; + std::vector b = AS::filter(a, ^BOOL(TestClass c) { + return c.getValue().integerValue > 1; + }); + std::vector c = { + TestClass(@"2"), + TestClass(@"6") + }; + XCTAssertEqual(b, c); +} + +- (void)testFilterVectorEmpty +{ + std::vector a = {}; + std::vector b = AS::filter(a, ^BOOL(int num) { + return num < 2; + }); + std::vector c = {}; + XCTAssertEqual(b, c); +} + +@end