Пример #1
0
// --- `elos habit checkin` {{{
func TestHabitCheckin(t *testing.T) {
	ui, db, user, c := newMockHabitCommand(t)

	t.Log("Creating a new test habit")
	hbt := newTestHabit(t, db, user, "Test Habit")
	t.Log("Created")

	// checkin for the first habit
	// TODO: have this take notes
	ui.InputReader = bytes.NewBufferString("0\nCheckin notes\n")

	t.Log("running: `elos habit checkin`")
	code := c.Run([]string{"checkin"})
	t.Log("command `checkin` 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 habits")
	}

	// verify Test Habit was listed
	if !strings.Contains(output, "Test Habit") {
		t.Fatalf("Output should have contained the habit's name in some way")
	}

	t.Log("Reload the habit")
	// verify that the habit was checked off
	if err := db.PopulateByID(hbt); err != nil {
		t.Fatal(err)
	}
	t.Logf("Habit:\n%+v", hbt)

	if checkedIn, err := habit.DidCheckinOn(db, hbt, time.Now()); err != nil {
		t.Fatal("Error while checking if habit is checked off: %s", err)
	} else if !checkedIn {
		t.Fatalf("Habit should be checked off for today now")
	}
}
Пример #2
0
Файл: habit.go Проект: elos/elos
func (c *HabitCommand) runToday(args []string) int {
	c.printf("Here is today's lineup:")
	var complete string
	for _, h := range c.habits {
		if checkedIn, err := habit.DidCheckinOn(c.DB, h, time.Now()); err != nil {
			c.errorf("error checking if habit is complete: %s", err)
			return failure
		} else if checkedIn {
			complete = "✓"
		} else {
			complete = ""
		}

		c.printf("%s: %s", h.Name, complete)
	}
	return success
}