Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
On TextTest
  • Loading branch information
hilaire committed Oct 2, 2019
1 parent b06f8ff commit 5592915
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/Text-Core/Text.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ Text >> string [
^string
]

{ #category : #attributes }
{ #category : #testing }
Text >> unembellished [
"Return true if the only emphases are the default font and bold"
| font1 bold |
Expand Down
133 changes: 0 additions & 133 deletions src/Text-Tests/TextAndTextStreamTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,6 @@ TextAndTextStreamTest >> example2 [
^ text
]

{ #category : #examples }
TextAndTextStreamTest >> replacementAtStartExample3 [

| text1 replacement length |

text1 := 'This is a simple text' copy asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
length := 'This' size.
replacement := 'Tht' asText.
text1 replaceFrom: 1
to: length
with: replacement
startingAt: 1
]

{ #category : #examples }
TextAndTextStreamTest >> replacementExample3 [

"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement
Here, the replacement text is shorteer than the text that is shall replace."

| text1 string replacement startPos length startPosInRep string2 |
text1 := (string := 'This is again simple text' copy) asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
startPos := string findString: 'simple'.
length := 'simple' size.
replacement := (string2 := 'both simple and short') asText.
startPosInRep := string2 findString: 'short'.
text1 replaceFrom: startPos
to: startPos + length - 1
with: replacement
startingAt: startPosInRep
]

{ #category : #tests }
TextAndTextStreamTest >> testAddStringToTextStream [
"It is possible to add a string into a TextStream.
Expand Down Expand Up @@ -398,103 +362,6 @@ TextAndTextStreamTest >> testTextEmphasisRangeDetection4 [
ifFalse: [range first = idx & range isEmpty]])
]

{ #category : #tests }
TextAndTextStreamTest >> testTextReplacement1 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement"

| text1 string replacement startPos length startPosInRep string2 |
text1 := (string := 'This is a simple text' copy) asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
startPos := string findString: 'simple'.
length := 'simple' size.
replacement := (string2 := 'both simple and short*') asText.
startPosInRep := string2 findString: 'short'.
text1 replaceFrom: startPos
to: startPos + length - 1
with: replacement
startingAt: startPosInRep.
self assert: text1 string size equals: text1 runs size
]

{ #category : #tests }
TextAndTextStreamTest >> testTextReplacement2 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement.
Here, the replacement text has trailing characters."

| text1 string replacement startPos length startPosInRep string2 |
text1 := (string := 'This is simple text' copy) asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
startPos := string findString: 'simple'.
length := 'simple' size.
replacement := (string2 := 'both simple and short*************') asText.
startPosInRep := string2 findString: 'short'.
text1 replaceFrom: startPos
to: startPos + length - 1
with: replacement
startingAt: startPosInRep.
self assert: text1 string size equals: text1 runs size
]

{ #category : #tests }
TextAndTextStreamTest >> testTextReplacement3 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement
Here, the replacement text is shorteer than the text that is shall replace."

self should: [ self replacementExample3 ] raise: Error
]

{ #category : #tests }
TextAndTextStreamTest >> testTextReplacementAtStartPos1 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement"

| text1 replacement length |
text1 := 'This is a simple text' copy asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
length := 'This' size.
replacement := 'That' asText.
text1 replaceFrom: 1
to: length
with: replacement
startingAt: 1.
self assert: text1 string size equals: text1 runs size
]

{ #category : #tests }
TextAndTextStreamTest >> testTextReplacementAtStartPos2 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement."

| text1 replacement length |
text1 := 'This is a simple text' copy asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
length := 'This' size.
replacement := 'That********' asText.
text1 replaceFrom: 1
to: length
with: replacement
startingAt: 1.
self assert: text1 string size equals: text1 runs size
]

{ #category : #tests }
TextAndTextStreamTest >> testTextReplacementAtStartPos3 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement
Here, the replacement text is shorter than the text that is shall replace."

self should: [self replacementAtStartExample3] raise: Error
]

{ #category : #tests }
TextAndTextStreamTest >> testTextStreamAdjacentRunsWithIdentitcalAttributes [
"This test verifies that adjacent runs with identical attributes are coalesced."
Expand Down
159 changes: 146 additions & 13 deletions src/Text-Tests/TextTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ Class {
#category : #'Text-Tests-Base'
}

{ #category : #examples }
TextTest >> replacementExample3 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement
Here, the replacement text is shorteer than the text that is shall replace."
| replacement startPos length startPosInRep string2 |
text := (string := 'This is again simple text' copy) asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
startPos := string findString: 'simple'.
length := 'simple' size.
replacement := (string2 := 'both simple and short') asText.
startPosInRep := string2 findString: 'short'.
text replaceFrom: startPos to: startPos + length - 1 with: replacement startingAt: startPosInRep
]

{ #category : #running }
TextTest >> setUp [
string := 'Pharo is cool'.
Expand All @@ -24,12 +40,10 @@ TextTest >> setUp [
{ #category : #tests }
TextTest >> testAppend [
| receiver argument result expectedResult |

"Append text without attribute"
"Append text without attribute"
receiver := 'xxx' asText addAttribute: TextEmphasis bold from: 1 to: 3.
argument := 'yyy'.
expectedResult := 'xxxyyy' asText
addAttribute: TextEmphasis bold from: 1 to: 3.
expectedResult := 'xxxyyy' asText addAttribute: TextEmphasis bold from: 1 to: 3.
result := receiver append: argument.
self assert: result == receiver.
self assert: result string equals: expectedResult string.
Expand All @@ -47,6 +61,22 @@ TextTest >> testAppend [
self assert: result runs equals: expectedResult runs.
]

{ #category : #tests }
TextTest >> testAsNumber [
self assert: '123' asText asNumber equals: 123.
self assert: '1.23e2' asText asNumber equals: 123.
self assert: '2r1000' asText asNumber equals: 8.


]

{ #category : #tests }
TextTest >> testAsString [
self assert: text asString equals: 'Pharo is cool'


]

{ #category : #tests }
TextTest >> testAt [
string withIndexDo: [ :char :index |
Expand Down Expand Up @@ -81,6 +111,17 @@ TextTest >> testFindStringStartingAtCaseSensitive [

]

{ #category : #tests }
TextTest >> testHowManyMatch [
self assert: (text howManyMatch: 'Pharo') equals: 5.
self assert: (text howManyMatch: 'Pha') equals: 3.
self assert: (text howManyMatch: 'P') equals: 1.
self assert: (text howManyMatch: 'p') equals: 0.
self assert: (text howManyMatch: 'is') equals: 0.
self assert: (text howManyMatch: 'cool') equals: 0.

]

{ #category : #tests }
TextTest >> testLineCount [
text := Text streamContents: [:str | str << 'Pharo' << Character cr << 'is' << Character cr << 'cool'].
Expand All @@ -91,23 +132,18 @@ TextTest >> testLineCount [
{ #category : #tests }
TextTest >> testPrepend [
| receiver argument result expectedResult |

"Prepend text without attribute"
receiver := 'xxx' asText
addAttribute: TextEmphasis bold from: 1 to: 3.
receiver := 'xxx' asText addAttribute: TextEmphasis bold from: 1 to: 3.
argument := 'yyy'.
expectedResult := 'yyyxxx' asText
addAttribute: TextEmphasis bold from: 4 to: 6.
expectedResult := 'yyyxxx' asText addAttribute: TextEmphasis bold from: 4 to: 6.
result := receiver prepend: argument.
self assert: result == receiver.
self assert: result string equals: expectedResult string.
self assert: result runs equals: expectedResult runs.

"Prepend text with attribute"
receiver := 'xxx' asText
addAttribute: TextEmphasis bold from: 1 to: 3.
argument := 'yyy' asText
addAttribute: TextEmphasis italic from: 1 to: 3.
receiver := 'xxx' asText addAttribute: TextEmphasis bold from: 1 to: 3.
argument := 'yyy' asText addAttribute: TextEmphasis italic from: 1 to: 3.
expectedResult := 'yyyxxx' asText
addAttribute: TextEmphasis italic from: 1 to: 3;
addAttribute: TextEmphasis bold from: 4 to: 6.
Expand Down Expand Up @@ -151,6 +187,48 @@ TextTest >> testReplaceFromToWith [
self assert: attr first equals: bold ]
]

{ #category : #tests }
TextTest >> testReplacementAtStartPos1 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement"
| replacement length |
text := 'This is a simple text' copy asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
length := 'This' size.
replacement := 'That' asText.
text replaceFrom: 1 to: length with: replacement startingAt: 1.
self assert: text string size equals: text runs size
]

{ #category : #tests }
TextTest >> testReplacementAtStartPos2 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement."
| replacement length |
text := 'This is a simple text' copy asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
length := 'This' size.
replacement := 'That********' asText.
text replaceFrom: 1 to: length with: replacement startingAt: 1.
self assert: text string size equals: text runs size
]

{ #category : #tests }
TextTest >> testReplacementAtStartPos3 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement
Here, the replacement text is shorter than the text that is shall replace."
| text1 replacement length |
text1 := 'This is a simple text' copy asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
length := 'This' size.
replacement := 'Tht' asText.
self should: [ text1 replaceFrom: 1 to: length with: replacement startingAt: 1] raise: Error
]

{ #category : #tests }
TextTest >> testRuns [
| attributes |
Expand All @@ -173,3 +251,58 @@ TextTest >> testSize [
TextTest >> testString [
self assert: text string equals: 'Pharo is cool'
]

{ #category : #tests }
TextTest >> testTextReplacement1 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement"
| replacement startPos length startPosInRep string2 |
text := (string := 'This is a simple text' copy) asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
startPos := string findString: 'simple'.
length := 'simple' size.
replacement := (string2 := 'both simple and short*') asText.
startPosInRep := string2 findString: 'short'.
text replaceFrom: startPos to: startPos + length - 1 with: replacement startingAt: startPosInRep.
self assert: text string size equals: text runs size
]

{ #category : #tests }
TextTest >> testTextReplacement2 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement.
Here, the replacement text has trailing characters."
| replacement startPos length startPosInRep string2 |
text := (string := 'This is simple text' copy) asText.
"Without the copy, we would modify a constant that the compiler attached at the compiled method."
startPos := string findString: 'simple'.
length := 'simple' size.
replacement := (string2 := 'both simple and short*************') asText.
startPosInRep := string2 findString: 'short'.
text replaceFrom: startPos to: startPos + length - 1 with: replacement startingAt: startPosInRep.
self assert: text string size equals: text runs size
]

{ #category : #tests }
TextTest >> testTextReplacement3 [
"For a Text t, the following assertion should always hold:
t string size = t run size
This test examines the preservation of this assertion for in-place replacement
Here, the replacement text is shorteer than the text that is shall replace."

self should: [ self replacementExample3 ] raise: Error
]

{ #category : #tests }
TextTest >> testUnembellished [
| anotherText |
self deny: text unembellished.
anotherText := 'Hello' asText
addAttribute: TextEmphasis bold.
self assert: anotherText unembellished.
anotherText addAttribute: TextFontChange font2.
self deny: anotherText unembellished.

]

0 comments on commit 5592915

Please sign in to comment.