Exemplo n.º 1
0
func command(args hackyslack.Args) hackyslack.D {
	rand.Seed(time.Now().UnixNano())
	result := roll.Parse(args.Text)
	for _, roll := range result {
		roll.Roll()
	}
	return formatRoll(args.UserName, result)
}
Exemplo n.º 2
0
func TestRollFormat(t *testing.T) {
	username := "******"
	for _, test := range FormatTests {
		rolls := roll.Parse(test)
		verify := fmt.Sprint("@", username, " rolled ")
		sum := 0
		for i, result := range rolls {
			result.Roll()
			// Use the last roll as the final.
			total := result.Total
			switch result.Operator {
			case "+":
				sum += result.Total
			case "-":
				sum -= result.Total
			case "*":
				sum *= result.Total
			case "/":
				sum /= result.Total
			}
			if i == 0 {
				verify += fmt.Sprint("*", total, "*")
			} else {
				verify += fmt.Sprint(" ", result.Operator, " *", total, "*")
			}
		}
		if len(rolls) > 1 {
			verify += fmt.Sprint(" = *", sum, "*")
		}
		resp := formatRoll(username, rolls)
		if resp["response_type"] != "in_channel" {
			t.Error("Incorrect response type", resp["response_type"])
		}
		attach := resp["attachments"].([]hackyslack.D)[0]
		if attach["color"] == "" {
			t.Error("Missing color")
		}
		if attach["text"] != verify {
			t.Error("Incorrect response text", attach["text"], "instead of", verify)
		}
	}
}