react-native-navigation的迁移库

RNNFontAttributesCreatorTest.m 4.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #import <XCTest/XCTest.h>
  2. #import "RNNFontAttributesCreator.h"
  3. @interface RNNFontAttributesCreatorTest : XCTestCase
  4. @end
  5. @implementation RNNFontAttributesCreatorTest
  6. - (void)testCreateWithFontFamily_shouldCreateAttributes {
  7. NSString* familyName = @"Helvetica";
  8. NSNumber* fontSize = @(20);
  9. UIColor* fontColor = UIColor.blueColor;
  10. NSDictionary* attributes = [RNNFontAttributesCreator createWithFontFamily:familyName fontSize:fontSize fontWeight:nil color:fontColor];
  11. UIFont* font = attributes[NSFontAttributeName];
  12. XCTAssertEqual(attributes[NSForegroundColorAttributeName], fontColor);
  13. XCTAssertTrue([familyName isEqualToString:font.familyName]);
  14. XCTAssertEqual(font.pointSize, fontSize.floatValue);
  15. }
  16. - (void)testCreateWithFontFamily_shouldIgnoreFontFamilyWhenFontWeightIsNotNil {
  17. NSString* familyName = @"Helvetica";
  18. NSString* fontWeight = @"bold";
  19. NSNumber* fontSize = @(20);
  20. UIColor* fontColor = UIColor.blueColor;
  21. NSDictionary* attributes = [RNNFontAttributesCreator createWithFontFamily:familyName fontSize:fontSize fontWeight:fontWeight color:fontColor];
  22. UIFont* font = attributes[NSFontAttributeName];
  23. XCTAssertEqual(attributes[NSForegroundColorAttributeName], fontColor);
  24. XCTAssertFalse([familyName isEqualToString:font.familyName]);
  25. XCTAssertEqual(font.pointSize, fontSize.floatValue);
  26. }
  27. - (void)testCreateWithFontFamilyWithDefault_shouldCreateDefaultAttributes {
  28. NSString* familyName = @"Helvetica";
  29. NSNumber* defaultFontSize = @(20);
  30. UIColor* defaultFontColor = UIColor.blueColor;
  31. NSDictionary* attributes = [RNNFontAttributesCreator createWithFontFamily:familyName fontSize:nil defaultFontSize:defaultFontSize fontWeight:nil color:nil defaultColor:defaultFontColor];
  32. UIFont* font = attributes[NSFontAttributeName];
  33. XCTAssertEqual(attributes[NSForegroundColorAttributeName], defaultFontColor);
  34. XCTAssertTrue([familyName isEqualToString:font.familyName]);
  35. XCTAssertEqual(font.pointSize, defaultFontSize.floatValue);
  36. }
  37. - (void)testcreateFromDictionary_shouldCreateAttributes {
  38. NSString* familyName = @"Helvetica";
  39. NSNumber* fontSize = @(20);
  40. UIColor* fontColor = UIColor.blueColor;
  41. NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:@{} fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:fontColor defaultColor:nil];
  42. UIFont* font = attributes[NSFontAttributeName];
  43. XCTAssertEqual(attributes[NSForegroundColorAttributeName], fontColor);
  44. XCTAssertTrue([familyName isEqualToString:font.familyName]);
  45. XCTAssertEqual(font.pointSize, fontSize.floatValue);
  46. }
  47. - (void)testCreateFromDictionary_shouldMergeWithDictionary {
  48. NSString* familyName = @"Helvetica";
  49. NSNumber* fontSize = @(20);
  50. NSDictionary* dictionary = @{NSForegroundColorAttributeName: UIColor.redColor};
  51. NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:dictionary fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:nil defaultColor:nil];
  52. UIFont* font = attributes[NSFontAttributeName];
  53. XCTAssertTrue([familyName isEqualToString:font.familyName]);
  54. XCTAssertEqual(font.pointSize, fontSize.floatValue);
  55. }
  56. - (void)testCreateFromDictionary_shouldOverrideColor {
  57. NSString* familyName = @"Helvetica";
  58. NSNumber* fontSize = @(20);
  59. NSDictionary* dictionary = @{NSForegroundColorAttributeName: UIColor.redColor};
  60. NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:dictionary fontFamily:familyName fontSize:fontSize defaultFontSize:nil fontWeight:nil color:nil defaultColor:nil];
  61. XCTAssertEqual(attributes[NSForegroundColorAttributeName], nil);
  62. }
  63. - (void)testCreateWithFontFamily_shouldNotChangeFontFamilyWhenOnlySizeAvailable {
  64. NSNumber* fontSize = @(20);
  65. UIFont* initialFont = [UIFont systemFontOfSize:10 weight:UIFontWeightHeavy];
  66. NSMutableDictionary* initialAttributes = [NSMutableDictionary new];
  67. initialAttributes[NSFontAttributeName] = initialFont;
  68. NSDictionary* attributes = [RNNFontAttributesCreator createFromDictionary:initialAttributes fontFamily:nil fontSize:fontSize defaultFontSize:nil fontWeight:nil color:nil defaultColor:nil];
  69. UIFont* font = attributes[NSFontAttributeName];
  70. XCTAssertEqual(font.pointSize, fontSize.floatValue);
  71. XCTAssertTrue([font.familyName isEqualToString:initialFont.familyName]);
  72. }
  73. @end