Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Basic tests for Text
  • Loading branch information
hilaire committed Sep 30, 2019
1 parent 6cd24d4 commit 78f001a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Text-Core/TextKern.class.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
"
A TextKern encodes a kerning change applicable over a given range of text. Positive values of kern spread letters out, negative kern will cause them to overlap more. Note that kerns other than 0 will display somewhat slower, as kerning is not yet supported in the text scanning primitive.
-----exemple-------
| stream |
stream := TextStream on: (Text new: 100).
stream
withAttribute: (TextKern kern: 1) do: [ stream nextPutAll: 'Pharo is cool'];
cr;
nextPutAll: 'Pharo is cool'; cr;
withAttribute: (TextKern kern: -1) do: [stream nextPutAll: 'Pharo is cool'].
TextMorph new
newContents: stream contents;
openInWindow.
"
Class {
#name : #TextKern,
Expand Down
76 changes: 76 additions & 0 deletions src/Text-Tests/TextTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ A TextTest is a test class for testing the behavior of Text
Class {
#name : #TextTest,
#superclass : #TestCase,
#instVars : [
'text',
'string'
],
#category : #'Text-Tests-Base'
}

{ #category : #running }
TextTest >> setUp [
string := 'Pharo is cool'.
text := string copy asText.
text addAttribute: TextEmphasis bold from: 1 to: 5.
text addAttribute: TextColor blue from: 1 to: 5.
text addAttribute: TextEmphasis underlined from: 7 to: 8.
text addAttribute: TextColor red from: 7 to: 13.
]

{ #category : #tests }
TextTest >> testAppend [
| receiver argument result expectedResult |
Expand All @@ -33,6 +47,47 @@ TextTest >> testAppend [
self assert: result runs equals: expectedResult runs.
]

{ #category : #tests }
TextTest >> testAt [
string withIndexDo: [ :char :index |
self assert: (text at: index) equals: char]
]

{ #category : #tests }
TextTest >> testAtPut [
text at: 2 put: $H.
text at: 3 put: $A.
text at: 4 put: $R.
text at: 5 put: $O.
self assert: text string equals: 'PHARO is cool'
]

{ #category : #tests }
TextTest >> testFindStringStartingAt [
self assert: (text findString: 'Pharo' startingAt: 1) equals: 1.
self assert: (text findString: 'cool' startingAt: 1) equals: 10.
self assert: (text findString: 'Pharo' startingAt: 2) equals: 0.

]

{ #category : #tests }
TextTest >> testFindStringStartingAtCaseSensitive [
self assert: (text findString: 'pharo' startingAt: 1 caseSensitive: false) equals: 1.
self assert: (text findString: 'pharo' startingAt: 1 caseSensitive: true) equals: 0.

self assert: (text findString: 'COOL' startingAt: 1 caseSensitive: false) equals: 10.
self assert: (text findString: 'COOL' startingAt: 1 caseSensitive: true) equals: 0.


]

{ #category : #tests }
TextTest >> testLineCount [
text := Text streamContents: [:str | str << 'Pharo' << Character cr << 'is' << Character cr << 'cool'].
self assert: text lineCount equals: 3

]

{ #category : #tests }
TextTest >> testPrepend [
| receiver argument result expectedResult |
Expand Down Expand Up @@ -62,3 +117,24 @@ TextTest >> testPrepend [
self assert: result runs equals: expectedResult runs

]

{ #category : #running }
TextTest >> testRangeOfStartingAt [
" text addAttribute: TextEmphasis bold from: 1 to: 5.
text addAttribute: TextColor blue from: 1 to: 5.
text addAttribute: TextEmphasis underlined from: 7 to: 8.
text addAttribute: TextColor red from: 7 to: 13."
self assert: (text rangeOf: TextEmphasis bold startingAt: 1) equals: (1 to: 5).
"even an index at beginning of the range, it returns the whole interval"
self assert: (text rangeOf: TextEmphasis bold startingAt: 2) equals: (1 to: 5).
self assert: (text rangeOf: TextEmphasis bold startingAt: 5) equals: (1 to: 5).
"but not when index is outside the range"
self deny: (text rangeOf: TextEmphasis bold startingAt: 6) equals: (1 to: 5).
"search for alternate attribute works too"
self assert: (text rangeOf: TextColor blue startingAt: 2) equals: (1 to: 5).
"attributes with different range"
self assert: (text rangeOf: TextColor red startingAt: 7) equals: (7 to: 13).
self assert: (text rangeOf: TextEmphasis underlined startingAt: 7) equals: (7 to: 8).
self assert: (text rangeOf: TextEmphasis underlined startingAt: 8) equals: (7 to: 8).
self deny: (text rangeOf: TextEmphasis underlined startingAt: 5) equals: (7 to: 8).
]

0 comments on commit 78f001a

Please sign in to comment.