/
DemienMedich
/
BodyweightBase
Обзор
Документация
Войти
/
DemienMedich
/
BodyweightBase
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/workout_session_runner_tests.cpp
150 строк
4 KB
DemienMedich
Replace C# app with C++ Qt version
20 июн 2026, 00:04
20 июн 2026, 00:04
e516ad2
Код
Авторство
О чём код?
#include "bodyweight/workout_session_runner.h" #include <QtTest> using namespace bodyweight; namespace { QVector<ExerciseDefinition> exercises() { return { {"squat", "Squat", ExerciseMetric::repetitions, 10, 5}, {"hold", "Hold", ExerciseMetric::seconds, 4, 0} }; } WorkoutPlan plan() { WorkoutPlan result; result.id = "test"; result.name = "Test"; result.steps = { {"squat", 12, 5, {}}, {"hold", 4, 0, {}} }; return result; } } // namespace class WorkoutSessionRunnerTests : public QObject { Q_OBJECT private slots: void completesMixedPlan(); void pauseFreezesCountdown(); void skipRecordsIncompleteStep(); void rejectsUnknownExercise(); void restoresPausedDraft(); void rejectsInvalidDraft(); }; void WorkoutSessionRunnerTests::completesMixedPlan() { WorkoutSessionRunner runner(exercises()); QVERIFY(runner.start(plan())); QCOMPARE(runner.snapshot().phase, SessionPhase::preparation); QCOMPARE(runner.snapshot().currentValue, 12); runner.advance(8); QCOMPARE(runner.snapshot().phase, SessionPhase::repetitionExercise); runner.advance(3); runner.setRepValue(13); runner.completeRepExercise(); QCOMPARE(runner.snapshot().phase, SessionPhase::rest); runner.advance(5); QCOMPARE(runner.snapshot().phase, SessionPhase::preparation); runner.advance(12); QCOMPARE(runner.snapshot().phase, SessionPhase::completed); const std::optional<FinishedSession> finished = runner.takeFinishedSession(); QVERIFY(finished.has_value()); QCOMPARE(finished->steps.size(), 2); QCOMPARE(finished->totalWorkSeconds, 7); QCOMPARE(finished->totalRestSeconds, 5); QVERIFY(finished->completedAll); } void WorkoutSessionRunnerTests::pauseFreezesCountdown() { WorkoutSessionRunner runner(exercises()); QVERIFY(runner.start(plan())); runner.advance(3); runner.pause(); QCOMPARE(runner.snapshot().phase, SessionPhase::paused); QCOMPARE(runner.snapshot().phaseElapsedSeconds, 3); runner.advance(100); QCOMPARE(runner.snapshot().phaseElapsedSeconds, 3); runner.resume(); runner.advance(5); QCOMPARE(runner.snapshot().phase, SessionPhase::repetitionExercise); } void WorkoutSessionRunnerTests::skipRecordsIncompleteStep() { WorkoutSessionRunner runner(exercises()); QVERIFY(runner.start(plan())); runner.skipCurrentExercise(); runner.skipCountdown(); runner.advance(8); runner.skipCurrentExercise(); const std::optional<FinishedSession> finished = runner.takeFinishedSession(); QVERIFY(finished.has_value()); QCOMPARE(finished->steps.size(), 2); QVERIFY(!finished->steps.first().completed); QVERIFY(!finished->steps.last().completed); } void WorkoutSessionRunnerTests::rejectsUnknownExercise() { WorkoutSessionRunner runner(exercises()); WorkoutPlan invalid = plan(); invalid.steps.first().exerciseId = "missing"; QString error; QVERIFY(!runner.start(invalid, &error)); QVERIFY(error.contains("missing")); QCOMPARE(runner.snapshot().phase, SessionPhase::idle); } void WorkoutSessionRunnerTests::restoresPausedDraft() { WorkoutSessionRunner source(exercises()); QVERIFY(source.start(plan())); source.advance(8); source.advance(3); source.setRepValue(11); source.pause(); const std::optional<SessionDraft> draft = source.createDraft(); QVERIFY(draft.has_value()); WorkoutSessionRunner restored(exercises()); QString error; QVERIFY2(restored.restore(*draft, &error), qPrintable(error)); QCOMPARE(restored.snapshot().phase, SessionPhase::paused); QCOMPARE(restored.snapshot().currentValue, 11); QCOMPARE(restored.snapshot().phaseElapsedSeconds, 3); restored.resume(); QCOMPARE(restored.snapshot().phase, SessionPhase::repetitionExercise); } void WorkoutSessionRunnerTests::rejectsInvalidDraft() { WorkoutSessionRunner runner(exercises()); SessionDraft draft; draft.plan = plan(); draft.stepIndex = 99; draft.phase = SessionPhase::preparation; draft.phaseDurationSeconds = 8; QString error; QVERIFY(!runner.restore(draft, &error)); QVERIFY(error.contains("index")); } QTEST_MAIN(WorkoutSessionRunnerTests) #include "workout_session_runner_tests.moc"