Tests/OrgPresentationTests/ImagePlacementTests.swift
45 lines · 2616 bytes
1import Foundation
2import OrgCore
3import Testing
4@testable import OrgPresentation
5
6struct ImagePlacementTests {
7 static func placements(_ text: String, includeLinked: Bool = false) -> [Presentation.ImagePlacement] {
8 let links = InlineImages.links(OrgParser.parse(text), in: 0..<(text as NSString).length, includeLinked: includeLinked)
9 return Presentation.imagePlacements(links, in: text as NSString)
10 }
11
12 @Test func aLinkAloneTakesItsLine() {
13 #expect(Self.placements("[[file:a.png]]\n [[file:b.png]] \t\n[[file:c.png]]").map(\.ownLine) == [true, true, true])
14 #expect(Self.placements("[[file:a.png][A figure]]\n", includeLinked: true).map(\.ownLine) == [true])
15 }
16
17 @Test func aLinkWithTextAroundItStandsInTheText() {
18 #expect(Self.placements("See [[file:a.png]]\n[[file:b.png]] here\n").map(\.ownLine) == [false, false])
19 #expect(Self.placements("[[file:a.png]] [[file:b.png]]\n").map(\.ownLine) == [false, false])
20 #expect(Self.placements("* [[file:h.png]]\n- [[file:i.png]]\n| [[file:t.png]] |\n").map(\.ownLine) == [false, false, false])
21 // The image stands for the description, inside the link.
22 #expect(Self.placements("[[https://example.com][file:thumb.png]]\n").map(\.ownLine) == [false])
23 }
24
25 /// A link past the text's end, from a tree older than the text, is left out.
26 @Test func linksPastTheTextAreLeftOut() {
27 let links = InlineImages.links(OrgParser.parse("text\n[[file:a.png]]\n"), in: 0..<20)
28 #expect(Presentation.imagePlacements(links, in: "text\n") == [])
29 }
30
31 @Test func sizeKeepsProportionsWithinTheWidth() {
32 let natural = CGSize(width: 400, height: 200)
33 #expect(Presentation.imageSize(natural: natural, width: nil, maxWidth: 600) == CGSize(width: 400, height: 200))
34 #expect(Presentation.imageSize(natural: natural, width: 100, maxWidth: 600) == CGSize(width: 100, height: 50))
35 #expect(Presentation.imageSize(natural: natural, width: 800, maxWidth: 600) == CGSize(width: 600, height: 300))
36 #expect(Presentation.imageSize(natural: natural, width: nil, maxWidth: 300) == CGSize(width: 300, height: 150))
37 #expect(Presentation.imageSize(natural: .zero, width: nil, maxWidth: 300) == .zero)
38 }
39
40 @Test func fontSizeGivesALineAsTallAsTheImage() {
41 // A 13-point font with a 16-point line: a 160-point image needs a 130-point font.
42 #expect(Presentation.imageFontSize(height: 160, pointSize: 13, lineHeight: 16) == 130)
43 #expect(Presentation.imageFontSize(height: 0, pointSize: 13, lineHeight: 16) == 0.01)
44 }
45}