#import #import "NSAttributedString+Markdown.h" @interface NSAttributedString_Markdown_Tests : XCTestCase @end @implementation NSAttributedString_Markdown_Tests - (NSAttributedString *)attributedString:(NSString *)text withTraits:(UIFontDescriptorSymbolicTraits)traits { UIFont *font = [UIFont systemFontOfSize: 17.0]; UIFontDescriptor *fontDescriptor = font.fontDescriptor; UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits; UIFontDescriptorSymbolicTraits newSymbolicTraits = symbolicTraits | traits; UIFontDescriptor *newFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:newSymbolicTraits]; UIFont *newFont = [UIFont fontWithDescriptor:newFontDescriptor size:font.pointSize]; return [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName: newFont}]; } - (void)applySymbolicTraits:(UIFontDescriptorSymbolicTraits)traits toAttributedString:(NSMutableAttributedString *)attributedString range:(NSRange)range { UIFont *font = [UIFont systemFontOfSize: 17.0]; UIFontDescriptor *fontDescriptor = font.fontDescriptor; UIFontDescriptorSymbolicTraits symbolicTraits = fontDescriptor.symbolicTraits; UIFontDescriptorSymbolicTraits newSymbolicTraits = symbolicTraits | traits; UIFontDescriptor *newFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:newSymbolicTraits]; UIFont *newFont = [UIFont fontWithDescriptor:newFontDescriptor size:font.pointSize]; [attributedString setAttributes:@{NSFontAttributeName: newFont} range:range]; } - (void)testItalic { NSAttributedString *attrString = [self attributedString:@"Italic" withTraits:UIFontDescriptorTraitItalic]; XCTAssertEqualObjects(attrString.markdownRepresentation, @"_Italic_"); } - (void)testBold { NSAttributedString *attrString = [self attributedString:@"Bold" withTraits:UIFontDescriptorTraitBold]; XCTAssertEqualObjects(attrString.markdownRepresentation, @"**Bold**"); } - (void)testBoldItalic { NSAttributedString *attrString = [self attributedString:@"Italic Bold" withTraits:UIFontDescriptorTraitItalic | UIFontDescriptorTraitBold]; XCTAssertEqualObjects(attrString.markdownRepresentation, @"**_Italic Bold_**"); } - (void)testSeparate { NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"This is italic and this is bold."]; [self applySymbolicTraits:UIFontDescriptorTraitItalic toAttributedString:attrString range:NSMakeRange(8, 6)]; [self applySymbolicTraits:UIFontDescriptorTraitBold toAttributedString:attrString range:NSMakeRange(27, 4)]; XCTAssertEqualObjects(attrString.markdownRepresentation, @"This is _italic_ and this is **bold**."); } - (void)testOverlap { NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:@"Italic Bold"]; [self applySymbolicTraits:UIFontDescriptorTraitItalic toAttributedString:attrString1 range:NSMakeRange(0, 11)]; [self applySymbolicTraits:UIFontDescriptorTraitItalic | UIFontDescriptorTraitBold toAttributedString:attrString1 range:NSMakeRange(7, 4)]; XCTAssertEqualObjects(attrString1.markdownRepresentation, @"_Italic **Bold**_"); NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] initWithString:@"Bold Italic"]; [self applySymbolicTraits:UIFontDescriptorTraitBold toAttributedString:attrString2 range:NSMakeRange(0, 11)]; [self applySymbolicTraits:UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic toAttributedString:attrString2 range:NSMakeRange(5, 6)]; XCTAssertEqualObjects(attrString2.markdownRepresentation, @"**Bold _Italic_**"); } - (void)testEscaping { for (NSString *character in @[@"\\", @"`", @"*", @"_", @"{", @"}", @"[", @"]", @"(", @")", @"#", @"+", @"-", @".", @"!", @"|"]) { NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:character]; NSString *expected = [@"\\" stringByAppendingString:character]; XCTAssertEqualObjects(attrString.markdownRepresentation, expected); } } @end