/
StdioStreamTest.class.st
145 lines (108 loc) · 3.39 KB
/
StdioStreamTest.class.st
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
144
145
"
Automated tests for StdioStream
Public API and Key Messages
- StdioStreamTest is a subclass of TestCase and has the same public api
Internal Representation and Key Implementation Points.
StdioStreamTest relies on a test stream constructed by StdioStreamTestResource.
"
Class {
#name : #StdioStreamTest,
#superclass : #TestCase,
#category : #'Files-Tests-Streams'
}
{ #category : #'tests - manual' }
StdioStreamTest class >> manualStdinTest [
"This test can be run headless to manually to check actual stdin.
It reads the input and confirms that #atEnd answers as expected and the number of characters read.
Example execution (from the shell prompt):
echo 'hello world' | pharo --headless Pharo.image eval 'StdioStreamTest manualStdinTest'
pharo --headless Pharo.image eval 'StdioStreamTest manualStdinTest' < /proc/cpuinfo
pharo --headless Pharo.image eval 'StdioStreamTest manualStdinTest'
# Type some text, enter and Ctrl-D"
| stdin stdout atEndBefore atEndAfter contents |
stdin := Stdio stdin.
stdout := ZnNewLineWriterStream on: Stdio stdout.
atEndBefore := stdin atEnd.
contents := stdin upToEnd.
atEndAfter := stdin atEnd.
stdout
<< '#atEnd (before): ';
<< atEndBefore displayString;
cr;
<< '#atEnd (after): ';
<< atEndAfter displayString;
cr;
<< 'Size: ';
<< contents size displayString;
cr;
<< 'Contents:';
cr; cr.
contents hexDumpOn: stdout max: 128.
stdout cr.
SmalltalkImage current snapshot: false andQuit: true.
]
{ #category : #accessing }
StdioStreamTest class >> resources [
^{ StdioStreamTestResource }.
]
{ #category : #accessing }
StdioStreamTest >> resource [
"Answer the receiver's resource object"
^StdioStreamTestResource current
]
{ #category : #running }
StdioStreamTest >> setUp [
"A stream is shared amongst all the tests to reduce file create, write, delete cycles.
Ensure the stream is in the expected state."
super setUp.
self resource stdioStream position: 0.
]
{ #category : #accessing }
StdioStreamTest >> stdioStream [
^self resource stdioStream
]
{ #category : #tests }
StdioStreamTest >> testAtEnd [
"Test that #atEnd isn't incorrectly answering true"
self deny: self stdioStream atEnd
]
{ #category : #tests }
StdioStreamTest >> testChangePosition [
"The shared stream should be put back to the start for each test"
| stream |
stream := self stdioStream.
stream position: 5.
self assert: stream position equals: 5.
self assert: stream next equals: $5 asciiValue
]
{ #category : #tests }
StdioStreamTest >> testContents [
self assert: self stdioStream contents equals: self resource contents asByteArray
]
{ #category : #tests }
StdioStreamTest >> testPeek [
| stream |
stream := self stdioStream.
self assert: stream peek equals: $0 asciiValue.
self assert: stream peek equals: stream next
]
{ #category : #tests }
StdioStreamTest >> testPosition [
"The shared stream should be put back to the start for each test"
self assert: self stdioStream position equals: 0
]
{ #category : #tests }
StdioStreamTest >> testSize [
self assert: self stdioStream size equals: self resource contents size
]
{ #category : #tests }
StdioStreamTest >> testUpToEnd [
| stream contents |
stream := self stdioStream.
contents := self resource contents.
"Position stream (zero based)"
stream position: 5.
"Copy from contents (one based)"
self assert: stream upToEnd
equals: (contents copyFrom: 6 to: contents size) asByteArray
]