3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ずんだのハロウィン問題 Go編

Last updated at Posted at 2018-10-31

ずんだのハロウィン問題 Ruby編
https://qiita.com/tbpgr/items/1f2ed0e14e5d94775963

ルール

  • 入力として "trick", "treat", "sushi", "poyayou", "tamakatsu" がランダムに与えられる
  • 入力をそのまま標準出力する
  • 入力が "trick" または "treat" だったら "happy halloween" と標準出力する
  • 入力が "trick" または "treat" ではなかったら "sleepy" と標準出力する

プログラム(Go)

zunda.go

package main

import (
	"fmt"
	"io"
	"os"
)

var writer io.Writer = os.Stdout

func main() {
	var s string
	fmt.Scan(&s)
	Halloween(s)
}

func Halloween(s string) {
	fmt.Println(s)
	switch s {
	case "trick", "treat":
		fmt.Fprint(writer, "happy halloween")
	default:
		fmt.Fprint(writer, "sleepy")
	}
}

zunda_test.go

package main

import (
	"bytes"
	"testing"
)

var buffer *bytes.Buffer

func TestHalloween(t *testing.T) {
	buffer = &bytes.Buffer{}
	writer = buffer

	Halloween("trick")
	if buffer.String() != "happy halloween" {
		t.Fail()
	}

	buffer.Reset()
	Halloween("treat")
	if buffer.String() != "happy halloween" {
		t.Fail()
	}

	buffer.Reset()
	Halloween("sushi")
	if buffer.String() != "sleepy" {
		t.Fail()
	}

	buffer.Reset()
	Halloween("poyayou")
	if buffer.String() != "sleepy" {
		t.Fail()
	}

	buffer.Reset()
	Halloween("tamakatsu")
	if buffer.String() != "sleepy" {
		t.Fail()
	}
}

出力例

$ go run main.go
trick
happy halloween

$ go run main.go
poyayou
sleepy

$ go test -v
=== RUN   TestHalloween
trick
treat
sushi
poyayou
tamakatsu
--- PASS: TestHalloween (0.00s)
PASS
ok  	_/home/yukpiz/labo/temp/zunda	0.002s
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?