awtk/tests/digit_clock_test.cc

170 lines
5.8 KiB
C++
Raw Normal View History

#include "widgets/digit_clock.h"
2018-11-03 12:10:25 +08:00
#include "gtest/gtest.h"
#include <stdlib.h>
#include <string>
using std::string;
TEST(DigitClock, basic) {
value_t v1;
value_t v2;
widget_t* w = digit_clock_create(NULL, 10, 20, 30, 40);
digit_clock_t* t = DIGIT_CLOCK(w);
2018-11-14 09:57:08 +08:00
value_set_str(&v1, "M/D");
2018-12-01 18:30:03 +08:00
ASSERT_EQ(widget_set_prop(w, WIDGET_PROP_FORMAT, &v1), RET_OK);
ASSERT_EQ(widget_get_prop(w, WIDGET_PROP_FORMAT, &v2), RET_OK);
2018-11-03 12:10:25 +08:00
ASSERT_EQ(string(value_str(&v1)), string(value_str(&v2)));
ASSERT_EQ(string(value_str(&v1)), string(t->format));
widget_destroy(w);
}
2018-11-14 09:57:08 +08:00
TEST(DigitClock, format) {
date_time_t dt;
char str[128];
widget_t* w = digit_clock_create(NULL, 10, 20, 30, 40);
dt.year = 2018;
ASSERT_EQ(digit_clock_format_time(w, "Y", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("2018"));
2018-12-11 15:20:52 +08:00
ASSERT_EQ(digit_clock_format_time(w, "YY", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18"));
2018-11-14 09:57:08 +08:00
ASSERT_EQ(digit_clock_format_time(w, "Y-", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("2018-"));
2018-12-11 15:20:52 +08:00
ASSERT_EQ(digit_clock_format_time(w, "YY-", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-"));
dt.month = 9;
2018-11-14 09:57:08 +08:00
ASSERT_EQ(digit_clock_format_time(w, "Y-M", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9"));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09"));
2018-11-14 09:57:08 +08:00
ASSERT_EQ(digit_clock_format_time(w, "Y-M-", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-"));
2018-11-14 09:57:08 +08:00
2018-12-11 15:20:52 +08:00
dt.day = 3;
2018-11-14 09:57:08 +08:00
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3"));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM-DD", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09-03"));
2018-11-14 09:57:08 +08:00
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D ", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3 "));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM-DD ", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09-03 "));
2018-11-14 09:57:08 +08:00
dt.hour = 1;
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D h", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3 1"));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM-DD hh", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09-03 01"));
2018-11-14 09:57:08 +08:00
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D h:", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3 1:"));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM-DD hh:", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09-03 01:"));
2018-11-14 09:57:08 +08:00
dt.minute = 2;
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D h:m", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3 1:2"));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM-DD hh:mm", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09-03 01:02"));
2018-11-14 09:57:08 +08:00
dt.second = 3;
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D h:m:s", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3 1:2:3"));
ASSERT_EQ(digit_clock_format_time(w, "YY-MM-DD hh:mm:ss", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("18-09-03 01:02:03"));
2018-11-14 09:57:08 +08:00
dt.second = 32;
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D h:m:s", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
2018-12-11 15:20:52 +08:00
ASSERT_EQ(string(str), string("2018-9-3 1:2:32"));
ASSERT_EQ(digit_clock_format_time(w, "Y-M-D hh:mm:ss", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("2018-9-3 01:02:32"));
2018-11-14 09:57:08 +08:00
widget_destroy(w);
}
TEST(DigitClock, cast) {
widget_t* w = digit_clock_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(w, digit_clock_cast(w));
widget_destroy(w);
}
2019-03-11 17:57:27 +08:00
TEST(DigitClock, wday) {
date_time_t dt;
char str[128];
widget_t* w = digit_clock_create(NULL, 10, 20, 30, 40);
dt.wday = 0;
ASSERT_EQ(digit_clock_format_time(w, "W", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("Sun"));
dt.wday = 5;
ASSERT_EQ(digit_clock_format_time(w, "W", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("Fri"));
widget_destroy(w);
}
TEST(DigitClock, month) {
date_time_t dt;
char str[128];
widget_t* w = digit_clock_create(NULL, 10, 20, 30, 40);
dt.month = 1;
ASSERT_EQ(digit_clock_format_time(w, "MMM", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("Jan"));
dt.month = 6;
ASSERT_EQ(digit_clock_format_time(w, "MMM", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("Jun"));
dt.month = 12;
ASSERT_EQ(digit_clock_format_time(w, "MMM", &dt), RET_OK);
ASSERT_EQ(wstr_get_utf8(&(w->text), str, sizeof(str)), RET_OK);
ASSERT_EQ(string(str), string("Dec"));
widget_destroy(w);
}