|
| 1 | +" |
| 2 | +SUnit coverage for the arrow syntax font adapter, styler, installer, and global toggle behavior. |
| 3 | +" |
| 4 | +Class { |
| 5 | + #name : 'ASRArrowSyntaxRenderingTest', |
| 6 | + #superclass : 'TestCase', |
| 7 | + #category : 'ArrowSyntaxRendering-Tests-Tests', |
| 8 | + #package : 'ArrowSyntaxRendering-Tests', |
| 9 | + #tag : 'Tests' |
| 10 | +} |
| 11 | + |
| 12 | +{ #category : 'fixtures' } |
| 13 | +ASRArrowSyntaxRenderingTest >> newScrolledTextMorphForSample [ |
| 14 | + |
| 15 | + | model morph | |
| 16 | + model := RubScrolledTextModel new. |
| 17 | + model setInitialText: self sampleMethodSource asText. |
| 18 | + morph := RubScrolledTextMorph new. |
| 19 | + morph model: model. |
| 20 | + morph beForSmalltalkCode. |
| 21 | + ^ morph |
| 22 | +] |
| 23 | + |
| 24 | +{ #category : 'fixtures' } |
| 25 | +ASRArrowSyntaxRenderingTest >> newSmalltalkCodeDecorator [ |
| 26 | + |
| 27 | + | morph | |
| 28 | + morph := self newScrolledTextMorphForSample. |
| 29 | + morph textArea withDecoratorNamed: #shoutStyler. |
| 30 | + ^ morph textArea decoratorNamed: #shoutStyler |
| 31 | +] |
| 32 | + |
| 33 | +{ #category : 'fixtures' } |
| 34 | +ASRArrowSyntaxRenderingTest >> newSmalltalkMethodDecoratorForObject [ |
| 35 | + |
| 36 | + | morph decorator | |
| 37 | + morph := self newScrolledTextMorphForSample. |
| 38 | + morph textArea withDecoratorNamed: #shoutStyler. |
| 39 | + decorator := morph textArea decoratorNamed: #shoutStyler. |
| 40 | + decorator classOrMetaClass: Object. |
| 41 | + ^ decorator |
| 42 | +] |
| 43 | + |
| 44 | +{ #category : 'fixtures' } |
| 45 | +ASRArrowSyntaxRenderingTest >> parsedSampleMethod [ |
| 46 | + |
| 47 | + ^ OCParser parseFaultyMethod: self sampleMethodSource |
| 48 | +] |
| 49 | + |
| 50 | +{ #category : 'fixtures' } |
| 51 | +ASRArrowSyntaxRenderingTest >> sampleAssignment [ |
| 52 | + |
| 53 | + ^ self parsedSampleMethod allChildren detect: [ :node | |
| 54 | + node isAssignment ] |
| 55 | +] |
| 56 | + |
| 57 | +{ #category : 'fixtures' } |
| 58 | +ASRArrowSyntaxRenderingTest >> sampleMethodSource [ |
| 59 | + |
| 60 | + ^ ASRArrowSyntaxInstaller sampleSource |
| 61 | +] |
| 62 | + |
| 63 | +{ #category : 'fixtures' } |
| 64 | +ASRArrowSyntaxRenderingTest >> sampleReturn [ |
| 65 | + |
| 66 | + ^ self parsedSampleMethod allChildren detect: [ :node | |
| 67 | + node isReturn ] |
| 68 | +] |
| 69 | + |
| 70 | +{ #category : 'fixtures' } |
| 71 | +ASRArrowSyntaxRenderingTest >> styledSampleText [ |
| 72 | + |
| 73 | + | text styler | |
| 74 | + text := self sampleMethodSource asText. |
| 75 | + styler := ASRArrowSyntaxTextStyler new. |
| 76 | + styler classOrMetaClass: Object. |
| 77 | + styler style: text ast: self parsedSampleMethod. |
| 78 | + ^ text |
| 79 | +] |
| 80 | + |
| 81 | +{ #category : 'tests' } |
| 82 | +ASRArrowSyntaxRenderingTest >> testArrowStylerDoesNotForceLightThemeDefaultColor [ |
| 83 | + |
| 84 | + | attributes | |
| 85 | + attributes := ASRArrowSyntaxTextStyler attributesFor: #default. |
| 86 | + self assert: attributes notEmpty. |
| 87 | + self |
| 88 | + assert: attributes first |
| 89 | + equals: (SHRBTextStyler attributesFor: #default) first |
| 90 | +] |
| 91 | + |
| 92 | +{ #category : 'tests' } |
| 93 | +ASRArrowSyntaxRenderingTest >> testArrowStylerUsesActiveShoutStyleTable [ |
| 94 | + |
| 95 | + self |
| 96 | + assert: ASRArrowSyntaxTextStyler styleTableName |
| 97 | + equals: SHRBTextStyler styleTableName. |
| 98 | + self |
| 99 | + assert: (ASRArrowSyntaxTextStyler attributesFor: #default) |
| 100 | + equals: (SHRBTextStyler attributesFor: #default). |
| 101 | + self |
| 102 | + assert: (ASRArrowSyntaxTextStyler attributesFor: #selector) |
| 103 | + equals: (SHRBTextStyler attributesFor: #selector) |
| 104 | +] |
| 105 | + |
| 106 | +{ #category : 'tests' } |
| 107 | +ASRArrowSyntaxRenderingTest >> testAssignmentOperatorGetsArrowFont [ |
| 108 | + |
| 109 | + | text assignment font | |
| 110 | + text := self styledSampleText. |
| 111 | + assignment := self sampleAssignment. |
| 112 | + font := text |
| 113 | + fontAt: assignment assignmentPosition |
| 114 | + withStyle: ASRSubstitutionFont defaultBaseFont textStyle. |
| 115 | + self |
| 116 | + assert: (font displayCharacterFor: $:) |
| 117 | + equals: (Character value: 16r2190). |
| 118 | + self assert: (font widthOf: $=) equals: 0. |
| 119 | + self assert: (font widthOfString: ':=') equals: (font widthOf: $:) |
| 120 | +] |
| 121 | + |
| 122 | +{ #category : 'tests' } |
| 123 | +ASRArrowSyntaxRenderingTest >> testGlobalToggleCanDisableForNewRubricDecorator [ |
| 124 | + |
| 125 | + self withArrowRenderingEnabled: false do: [ |
| 126 | + | decorator | |
| 127 | + decorator := self newSmalltalkCodeDecorator. |
| 128 | + self assert: decorator styler class equals: SHRBTextStyler ] |
| 129 | +] |
| 130 | + |
| 131 | +{ #category : 'tests' } |
| 132 | +ASRArrowSyntaxRenderingTest >> testGlobalToggleCreatesArrowStylerForNewRubricDecorator [ |
| 133 | + |
| 134 | + self withArrowRenderingEnabled: true do: [ |
| 135 | + | decorator | |
| 136 | + decorator := self newSmalltalkCodeDecorator. |
| 137 | + self |
| 138 | + assert: decorator styler class |
| 139 | + equals: ASRArrowSyntaxTextStyler ] |
| 140 | +] |
| 141 | + |
| 142 | +{ #category : 'tests' } |
| 143 | +ASRArrowSyntaxRenderingTest >> testGlobalTogglePreservesMethodClassWhenRefreshingExistingDecorator [ |
| 144 | + |
| 145 | + self withArrowRenderingEnabled: false do: [ |
| 146 | + | decorator text assignment assignmentFont | |
| 147 | + decorator := self newSmalltalkMethodDecoratorForObject. |
| 148 | + self assert: decorator styler class equals: SHRBTextStyler. |
| 149 | + ASRArrowSyntaxInstaller enabled: true. |
| 150 | + decorator refreshStyling. |
| 151 | + text := decorator text. |
| 152 | + assignment := self sampleAssignment. |
| 153 | + assignmentFont := text |
| 154 | + fontAt: assignment assignmentPosition |
| 155 | + withStyle: |
| 156 | + ASRSubstitutionFont defaultBaseFont textStyle. |
| 157 | + self |
| 158 | + assert: decorator styler class |
| 159 | + equals: ASRArrowSyntaxTextStyler. |
| 160 | + self |
| 161 | + assert: (assignmentFont displayCharacterFor: $:) |
| 162 | + equals: (Character value: 16r2190) ] |
| 163 | +] |
| 164 | + |
| 165 | +{ #category : 'tests' } |
| 166 | +ASRArrowSyntaxRenderingTest >> testGlobalToggleRefreshesExistingRubricDecorator [ |
| 167 | + |
| 168 | + self withArrowRenderingEnabled: false do: [ |
| 169 | + | decorator | |
| 170 | + decorator := self newSmalltalkCodeDecorator. |
| 171 | + self assert: decorator styler class equals: SHRBTextStyler. |
| 172 | + ASRArrowSyntaxInstaller enabled: true. |
| 173 | + self |
| 174 | + assert: decorator styler class |
| 175 | + equals: ASRArrowSyntaxTextStyler. |
| 176 | + ASRArrowSyntaxInstaller enabled: false. |
| 177 | + self assert: decorator styler class equals: SHRBTextStyler ] |
| 178 | +] |
| 179 | + |
| 180 | +{ #category : 'tests' } |
| 181 | +ASRArrowSyntaxRenderingTest >> testGlobalToggleRendersArrowsInMethodEditor [ |
| 182 | + |
| 183 | + self withArrowRenderingEnabled: true do: [ |
| 184 | + | decorator text assignment return assignmentFont returnFont | |
| 185 | + decorator := self newSmalltalkMethodDecoratorForObject. |
| 186 | + decorator refreshStyling. |
| 187 | + text := decorator text. |
| 188 | + assignment := self sampleAssignment. |
| 189 | + return := self sampleReturn. |
| 190 | + assignmentFont := text |
| 191 | + fontAt: assignment assignmentPosition |
| 192 | + withStyle: |
| 193 | + ASRSubstitutionFont defaultBaseFont textStyle. |
| 194 | + returnFont := text |
| 195 | + fontAt: return start |
| 196 | + withStyle: |
| 197 | + ASRSubstitutionFont defaultBaseFont textStyle. |
| 198 | + self assert: text asString equals: self sampleMethodSource. |
| 199 | + self |
| 200 | + assert: (assignmentFont displayCharacterFor: $:) |
| 201 | + equals: (Character value: 16r2190). |
| 202 | + self |
| 203 | + assert: (returnFont displayCharacterFor: $^) |
| 204 | + equals: (Character value: 16r2191) ] |
| 205 | +] |
| 206 | + |
| 207 | +{ #category : 'tests' } |
| 208 | +ASRArrowSyntaxRenderingTest >> testInstallerUsesArrowStyler [ |
| 209 | + |
| 210 | + | morph decorator | |
| 211 | + morph := self newScrolledTextMorphForSample. |
| 212 | + decorator := ASRArrowSyntaxInstaller installOnScrolledTextMorph: |
| 213 | + morph. |
| 214 | + self deny: decorator isNil. |
| 215 | + self assert: decorator styler class equals: ASRArrowSyntaxTextStyler. |
| 216 | + self assert: morph text asString equals: self sampleMethodSource |
| 217 | +] |
| 218 | + |
| 219 | +{ #category : 'tests' } |
| 220 | +ASRArrowSyntaxRenderingTest >> testReturnGetsArrowFont [ |
| 221 | + |
| 222 | + | text return font | |
| 223 | + text := self styledSampleText. |
| 224 | + return := self sampleReturn. |
| 225 | + font := text |
| 226 | + fontAt: return start |
| 227 | + withStyle: ASRSubstitutionFont defaultBaseFont textStyle. |
| 228 | + self |
| 229 | + assert: (font displayCharacterFor: $^) |
| 230 | + equals: (Character value: 16r2191) |
| 231 | +] |
| 232 | + |
| 233 | +{ #category : 'tests' } |
| 234 | +ASRArrowSyntaxRenderingTest >> testSettingsDeclarationIsDiscoverable [ |
| 235 | + |
| 236 | + self assert: |
| 237 | + ((ASRArrowSyntaxInstaller class >> #settingsOn:) pragmas |
| 238 | + anySatisfy: [ :pragma | pragma keyword = #systemsettings ]) |
| 239 | +] |
| 240 | + |
| 241 | +{ #category : 'tests' } |
| 242 | +ASRArrowSyntaxRenderingTest >> testStylingPreservesSourceString [ |
| 243 | + |
| 244 | + | text | |
| 245 | + text := self styledSampleText. |
| 246 | + self assert: text asString equals: self sampleMethodSource |
| 247 | +] |
| 248 | + |
| 249 | +{ #category : 'fixtures' } |
| 250 | +ASRArrowSyntaxRenderingTest >> withArrowRenderingEnabled: aBoolean do: aBlock [ |
| 251 | + |
| 252 | + | oldValue | |
| 253 | + oldValue := ASRArrowSyntaxInstaller enabled. |
| 254 | + ^ [ |
| 255 | + ASRArrowSyntaxInstaller enabled: aBoolean. |
| 256 | + aBlock value ] ensure: [ |
| 257 | + ASRArrowSyntaxInstaller enabled: oldValue ] |
| 258 | +] |
0 commit comments