-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathSTTextViewControllerTests.swift
More file actions
143 lines (116 loc) · 5 KB
/
STTextViewControllerTests.swift
File metadata and controls
143 lines (116 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import XCTest
@testable import CodeEditTextView
import SwiftTreeSitter
import AppKit
final class STTextViewControllerTests: XCTestCase {
var controller: STTextViewController!
var theme: EditorTheme!
override func setUpWithError() throws {
theme = EditorTheme(
text: .textColor,
insertionPoint: .textColor,
invisibles: .gray,
background: .textBackgroundColor,
lineHighlight: .highlightColor,
selection: .selectedTextColor,
keywords: .systemPink,
commands: .systemBlue,
types: .systemMint,
attributes: .systemTeal,
variables: .systemCyan,
values: .systemOrange,
numbers: .systemYellow,
strings: .systemRed,
characters: .systemRed,
comments: .systemGreen
)
controller = STTextViewController(
text: .constant(""),
language: .default,
font: .monospacedSystemFont(ofSize: 11, weight: .medium),
theme: theme,
tabWidth: 4,
wrapLines: true,
editorOverscroll: 0.5,
useThemeBackground: true,
isEditable: true
)
}
func test_captureNames() throws {
// test for "keyword"
let captureName1 = "keyword"
let color1 = controller.attributesFor(CaptureName(rawValue: captureName1))[.foregroundColor] as? NSColor
XCTAssertEqual(color1, NSColor.systemPink)
// test for "comment"
let captureName2 = "comment"
let color2 = controller.attributesFor(CaptureName(rawValue: captureName2))[.foregroundColor] as? NSColor
XCTAssertEqual(color2, NSColor.systemGreen)
/* ... additional tests here ... */
// test for empty case
let captureName3 = ""
let color3 = controller.attributesFor(CaptureName(rawValue: captureName3))[.foregroundColor] as? NSColor
XCTAssertEqual(color3, NSColor.textColor)
// test for random case
let captureName4 = "abc123"
let color4 = controller.attributesFor(CaptureName(rawValue: captureName4))[.foregroundColor] as? NSColor
XCTAssertEqual(color4, NSColor.textColor)
}
func test_editorOverScroll() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .init(x: .zero,
y: .zero,
width: 100,
height: 100)
controller.editorOverscroll = 0
controller.contentInsets = nil
controller.reloadUI()
// editorOverscroll: 0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 0)
controller.editorOverscroll = 0.5
controller.reloadUI()
// editorOverscroll: 0.5
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 50.0)
controller.editorOverscroll = 1.0
controller.reloadUI()
// editorOverscroll: 1.0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 87.0)
}
func test_editorInsets() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .init(x: .zero,
y: .zero,
width: 100,
height: 100)
func assertInsetsEqual(_ lhs: NSEdgeInsets, _ rhs: NSEdgeInsets) throws {
XCTAssertEqual(lhs.top, rhs.top)
XCTAssertEqual(lhs.right, rhs.right)
XCTAssertEqual(lhs.bottom, rhs.bottom)
XCTAssertEqual(lhs.left, rhs.left)
}
controller.editorOverscroll = 0
controller.contentInsets = nil
controller.reloadUI()
// contentInsets: 0
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
// contentInsets: 16
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
controller.reloadUI()
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16))
// contentInsets: different
controller.contentInsets = NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1)
controller.reloadUI()
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 32.5, left: 12.3, bottom: 20, right: 1))
// contentInsets: 16
// editorOverscroll: 0.5
controller.contentInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
controller.editorOverscroll = 0.5
controller.reloadUI()
try assertInsetsEqual(scrollView.contentInsets, NSEdgeInsets(top: 16, left: 16, bottom: 16 + 50, right: 16))
}
func test_editorOverScroll_ZeroCondition() throws {
let scrollView = try XCTUnwrap(controller.view as? NSScrollView)
scrollView.frame = .zero
// editorOverscroll: 0
XCTAssertEqual(scrollView.contentView.contentInsets.bottom, 0)
}
}