Last active
June 27, 2017 16:33
-
-
Save st0012/465f94e04fd1e5dabe7d3cc732abdd02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func main() { | |
Start(os.Stdin, os.Stdout) | |
} | |
/* | |
This will behaves like: | |
>> 1 + 1 | |
#=> 2 | |
>> | |
*/ | |
func Start(in io.Reader, out io.Writer) { | |
scanner := bufio.NewScanner(in) | |
for { | |
out.Write([]byte(">> ")) | |
scanned := scanner.Scan() | |
if !scanned { | |
return | |
} | |
line := scanner.Text() | |
// Other logics | |
} | |
} | |
/* | |
Question: How to write unit test for Start()? | |
When testing no matter what io.Reader I use, the scanner closes right away. | |
I want to write something like: | |
func TestStart(t *testing.T) { | |
in := &bytes.Buffer{} | |
out := &bytes.Buffer{} | |
Start(in, out) | |
in.WriteString("1 + 1") | |
in.WrtieString("exit") // exit the input mode and continues test | |
result := out.String() | |
fmt.Println(result) | |
} | |
And the result should be: | |
>> 1 + 1 | |
#=> 2 | |
>> | |
But I only got: | |
>> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi~~