Example #1
0
// --- `elos habit history` {{{
func TestHabitHistory(t *testing.T) {
	ui, db, user, c := newMockHabitCommand(t)

	t.Log("Creating a new test habit")
	hbt := newTestHabit(t, db, user, "hello")
	habit.CheckinFor(db, hbt, "first checkin", time.Now().Add(-24*time.Hour))
	habit.CheckinFor(db, hbt, "second checkin", time.Now())
	t.Log("Created")

	// select the first habit
	ui.InputReader = bytes.NewBufferString("0\n")

	t.Log("running: `elos habit history`")
	code := c.Run([]string{"history"})
	t.Log("command `history` terminated")

	errput := ui.ErrorWriter.String()
	output := ui.OutputWriter.String()
	t.Logf("Error output:\n%s", errput)
	t.Logf("Output:\n%s", output)

	// verify there were no errors
	if errput != "" {
		t.Fatalf("Expected no error output, got: %s", errput)
	}

	// verify success
	if code != success {
		t.Fatalf("Expected successful exit code along with empty error output.")
	}

	// verify some of the output
	if !strings.Contains(output, "0)") {
		t.Fatalf("Output should have contained a 0) for listing checkins")
	}

	// verify checkins appeared
	if !strings.Contains(output, "first checkin") {
		t.Fatalf("Output should have contained the text of the first checkin")
	}

	if !strings.Contains(output, "second checkin") {
		t.Fatalf("Output should have contained the text of the second checkin")
	}
}
Example #2
0
File: habit.go Project: elos/elos
func (c *HabitCommand) runCheckin(args []string) int {
	hbt, index := c.promptSelectHabit()
	if index < 0 {
		return failure
	}

	if _, err := habit.CheckinFor(c.DB, hbt, "", time.Now()); err != nil {
		c.errorf("while checking in: %s", err)
		return failure
	}

	return success
}
Example #3
0
// --- `elos habit today` {{{
func TestHabitToday(t *testing.T) {
	ui, db, user, c := newMockHabitCommand(t)

	t.Log("creating two habits")
	h1 := newTestHabit(t, db, user, "first")
	newTestHabit(t, db, user, "second")
	t.Log("created")
	t.Log("checking one off")
	if _, err := habit.CheckinFor(db, h1, "", time.Now()); err != nil {
		t.Fatal(err)
	}
	t.Log("checked off")

	t.Log("running: `elos habit today`")
	code := c.Run([]string{"today"})
	t.Log("command `today` terminated")

	errput := ui.ErrorWriter.String()
	output := ui.OutputWriter.String()
	t.Logf("Error output:\n%s", errput)
	t.Logf("Output:\n%s", output)

	// verify there were no errors
	if errput != "" {
		t.Fatalf("Expected no error output, got: %s", errput)
	}

	// verify success
	if code != success {
		t.Fatalf("Expected successful exit code along with empty error output.")
	}

	if !strings.Contains(output, "first") {
		t.Fatalf("output should contain name of first habit")
	}

	if !strings.Contains(output, "second") {
		t.Fatalf("output should contain name of second habit")
	}

	if !strings.Contains(output, "✓") {
		t.Fatal("Should have found a '✓' in the output")
	}
}