Add unit tests for buttons / relays; and a bunch of mocks :)
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
TARGET = test
|
||||
LIBS =
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall -I../include -I./
|
||||
CFLAGS = -g -Wall -I../include -I./ -I ./mongoose/
|
||||
|
||||
.PHONY: default all clean
|
||||
|
||||
|
@ -3,10 +3,15 @@
|
||||
int test_failures=0;
|
||||
int assert_count=0;
|
||||
|
||||
uint32_t mqtt_pub_count;
|
||||
uint32_t mqtt_sub_count;
|
||||
|
||||
int main() {
|
||||
test_widget();
|
||||
test_widget_mqtt();
|
||||
test_screen();
|
||||
test_buttons();
|
||||
test_relays();
|
||||
|
||||
if (test_failures) {
|
||||
LOG(LL_ERROR, ("%d test failures", test_failures));
|
||||
|
@ -9,5 +9,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "mgos_mock.h"
|
||||
#include "mgos_gpio.h"
|
||||
#include "mgos_net.h"
|
||||
#include "mgos_mqtt.h"
|
||||
|
||||
#endif // __MGOS_H
|
||||
|
@ -50,6 +50,26 @@ void mgos_clear_timer(mgos_timer_id id) {
|
||||
return;
|
||||
}
|
||||
|
||||
float mg_time() {
|
||||
double mg_time() {
|
||||
return (float) time(NULL);
|
||||
}
|
||||
|
||||
double mgos_uptime() {
|
||||
return (double) time(NULL);
|
||||
}
|
||||
|
||||
char *mgos_sys_ro_vars_get_mac_address() {
|
||||
return "00:11:22:33:44:55";
|
||||
}
|
||||
|
||||
char *mgos_sys_ro_vars_get_arch() {
|
||||
return "esp32";
|
||||
}
|
||||
|
||||
bool mgos_net_get_ip_info(enum mgos_net_if_type if_type, int if_instance, struct mgos_net_ip_info *ip_info) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void mgos_net_ip_to_str(const struct sockaddr_in *sin, char *out) {
|
||||
return;
|
||||
}
|
||||
|
@ -5,6 +5,8 @@
|
||||
*/
|
||||
|
||||
#include "mgos.h"
|
||||
#include <time.h>
|
||||
#include "mongoose/mongoose.h"
|
||||
|
||||
#define MGOS_APP "unittest"
|
||||
|
||||
@ -38,6 +40,9 @@ typedef void (*timer_callback)(void *param);
|
||||
mgos_timer_id mgos_set_timer(int msecs, int flags, timer_callback cb, void *cb_arg);
|
||||
void mgos_clear_timer(mgos_timer_id id);
|
||||
|
||||
float mg_time();
|
||||
double mgos_uptime();
|
||||
|
||||
char *mgos_sys_ro_vars_get_mac_address();
|
||||
char *mgos_sys_ro_vars_get_arch();
|
||||
|
||||
#endif // __MGOS_MOCK_H
|
||||
|
@ -7,6 +7,9 @@ uint32_t mqtt_sub_count=0;
|
||||
static sub_handler_t s_handler;
|
||||
static void *s_handler_ud;
|
||||
|
||||
static mg_event_handler_t s_global_handler;
|
||||
static void *s_global_handler_ud;
|
||||
|
||||
void mgos_mqtt_pub(char *t, char *m, int m_len, int flags, bool persist) {
|
||||
LOG(LL_INFO, ("Sending topic='%s' msg='%s' persist=%s", t, m, persist?"ON":"OFF"));
|
||||
mqtt_pub_count++;
|
||||
@ -24,3 +27,9 @@ void mgos_mqtt_inject(char *topic, char *msg) {
|
||||
if (s_handler)
|
||||
s_handler(NULL, topic, strlen(topic), msg, strlen(msg), s_handler_ud);
|
||||
}
|
||||
|
||||
void mgos_mqtt_add_global_handler(mqtt_event_handler_t handler, void *ud) {
|
||||
s_global_handler = handler;
|
||||
s_global_handler_ud = ud;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define __MGOS_MQTT_H
|
||||
|
||||
#include "mgos.h"
|
||||
#include "mongoose/mongoose.h"
|
||||
|
||||
struct mg_connection;
|
||||
|
||||
@ -9,9 +10,14 @@ typedef void (*sub_handler_t)(struct mg_connection *nc, const char *topic,
|
||||
int topic_len, const char *msg, int msg_len,
|
||||
void *ud);
|
||||
|
||||
typedef void (*mqtt_event_handler_t)(struct mg_connection *nc, int ev,
|
||||
void *ev_data, void *user_data);
|
||||
|
||||
void mgos_mqtt_pub(char *t, char *m, int m_len, int flags, bool persist);
|
||||
void mgos_mqtt_sub(char *t, sub_handler_t cb, void *ud);
|
||||
|
||||
void mgos_mqtt_inject(char *topic, char *msg);
|
||||
void mgos_mqtt_add_global_handler(mqtt_event_handler_t handler, void *ud);
|
||||
|
||||
|
||||
#endif // __MGOS_MQTT_H
|
||||
|
32
unittest/mgos_net.h
Normal file
32
unittest/mgos_net.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef __MGOS_NET_H
|
||||
#define __MGOS_NET_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#define MGOS_NET_IF_WIFI_STA 0
|
||||
#define MGOS_NET_IF_WIFI_AP 1
|
||||
|
||||
enum mgos_net_if_type {
|
||||
MGOS_NET_IF_TYPE_WIFI,
|
||||
MGOS_NET_IF_TYPE_ETHERNET,
|
||||
MGOS_NET_IF_TYPE_PPP,
|
||||
/* This is a sentinel in case all networking interface types are disabled. */
|
||||
MGOS_NET_IF_MAX,
|
||||
};
|
||||
|
||||
struct mgos_net_ip_info {
|
||||
struct sockaddr_in ip;
|
||||
struct sockaddr_in netmask;
|
||||
struct sockaddr_in gw;
|
||||
};
|
||||
|
||||
bool mgos_net_get_ip_info(enum mgos_net_if_type if_type, int if_instance,
|
||||
struct mgos_net_ip_info *ip_info);
|
||||
|
||||
|
||||
void mgos_net_ip_to_str(const struct sockaddr_in *sin, char *out);
|
||||
|
||||
|
||||
#endif // __MGOS_NET_H
|
6124
unittest/mongoose/mongoose.h
Normal file
6124
unittest/mongoose/mongoose.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -23,5 +23,7 @@ extern int assert_count;
|
||||
int test_widget(void);
|
||||
int test_widget_mqtt(void);
|
||||
int test_screen(void);
|
||||
int test_buttons(void);
|
||||
int test_relays(void);
|
||||
|
||||
#endif // __TEST_H
|
||||
|
49
unittest/test_buttons.c
Normal file
49
unittest/test_buttons.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include "test.h"
|
||||
#include "buttons.h"
|
||||
|
||||
extern uint32_t mqtt_pub_count;
|
||||
extern uint32_t mqtt_sub_count;
|
||||
|
||||
int test_buttons() {
|
||||
bool ret;
|
||||
uint8_t idx;
|
||||
float last_change;
|
||||
|
||||
ret = buttons_init("0,A");
|
||||
ASSERT(ret!=0, "buttons_init should not have taken '0,A'");
|
||||
ret = buttons_init("0,41");
|
||||
ASSERT(ret!=0, "buttons_init should not have taken '0,41'");
|
||||
ret = buttons_init("0,1,2,3,4,5,6,7,8");
|
||||
ASSERT(ret!=0, "buttons_init should not have taken '0,1,2,3,4,5,6,7,8'");
|
||||
ret = buttons_init("0");
|
||||
ASSERT(ret==0, "buttons_init should have taken '0'");
|
||||
|
||||
ret = relays_init("16,12,13,14");
|
||||
ret = buttons_init("0,2");
|
||||
ASSERT(ret==0, "buttons_init should have taken ' 0, 2, '");
|
||||
idx = buttons_find_by_gpio(1);
|
||||
ASSERT(idx==255, "should not have gpio=1");
|
||||
idx = buttons_find_by_gpio(2);
|
||||
ASSERT(idx==1, "should have gpio=2 at idx=1");
|
||||
last_change = buttons_get_last_change_by_gpio(2);
|
||||
ASSERT(last_change==-1, "button at gpio=2 should have last_change==-1");
|
||||
mqtt_pub_count=0;
|
||||
buttons_touch_by_gpio(2);
|
||||
ASSERT(mqtt_pub_count==2, "button should have send mqtt message");
|
||||
buttons_touch_by_gpio(0);
|
||||
ASSERT(mqtt_pub_count==4, "button should have send mqtt message");
|
||||
buttons_touch_by_gpio(1);
|
||||
ASSERT(mqtt_pub_count==4, "button should not have send mqtt message");
|
||||
last_change = buttons_get_last_change_by_gpio(2);
|
||||
ASSERT(last_change>0, "button at gpio=2 should have last_change>0");
|
||||
last_change = buttons_get_last_change_by_idx(0);
|
||||
ASSERT(last_change>0, "button at idx=0 should have last_change>0");
|
||||
last_change = buttons_get_last_change_by_idx(1);
|
||||
ASSERT(last_change>0, "button at idx=1 should have last_change>0");
|
||||
last_change = buttons_get_last_change_by_idx(2);
|
||||
ASSERT(last_change==-1, "button at idx=2 should have last_change==-1");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
46
unittest/test_relays.c
Normal file
46
unittest/test_relays.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include "test.h"
|
||||
#include "relays.h"
|
||||
|
||||
int test_relays() {
|
||||
bool ret;
|
||||
bool state=0;
|
||||
uint8_t idx;
|
||||
float last_change;
|
||||
|
||||
ret = relays_init("0,A");
|
||||
ASSERT(ret!=0, "relays_init should not have taken '0,A'");
|
||||
ret = relays_init("0,41");
|
||||
ASSERT(ret!=0, "relays_init should not have taken '0,41'");
|
||||
ret = relays_init("0,1,2,3,4,5,6,7,8");
|
||||
ASSERT(ret!=0, "relays_init should not have taken '0,1,2,3,4,5,6,7,8'");
|
||||
ret = relays_init(" 0, 2, ");
|
||||
ASSERT(ret==0, "relays_init should have taken ' 0, 2, '");
|
||||
|
||||
ret = relays_init("12,13,14,16");
|
||||
ASSERT(ret==0, "relays_init should have taken '12,13,14,16'");
|
||||
idx = relays_find_by_gpio(1);
|
||||
ASSERT(idx==255, "should not have gpio=1");
|
||||
idx = relays_find_by_gpio(12);
|
||||
ASSERT(idx==0, "should have gpio=12 at idx=0");
|
||||
last_change = relays_get_last_change_by_gpio(12);
|
||||
ASSERT(last_change==-1, "button at gpio=12 should have last_change==-1");
|
||||
relays_set_by_gpio(12, true);
|
||||
state = relays_get_by_gpio(12);
|
||||
ASSERT(state==true, "relay at gpio=12 should be state=true");
|
||||
state = relays_get_by_idx(0);
|
||||
ASSERT(state==true, "relay at idx=0 should be state=true");
|
||||
state = relays_get_by_idx(1);
|
||||
ASSERT(state==false, "relay at idx=1 should be state=false");
|
||||
last_change = relays_get_last_change_by_gpio(12);
|
||||
ASSERT(last_change>0, "button at gpio=12 should have last_change>0");
|
||||
relays_set_by_idx(0, false);
|
||||
state = relays_get_by_idx(0);
|
||||
ASSERT(state==false, "relay at idx=0 should be state=false");
|
||||
state = relays_get_by_gpio(12);
|
||||
ASSERT(state==false, "relay at gpio=12 should be state=false");
|
||||
last_change = relays_get_last_change_by_gpio(12);
|
||||
ASSERT(last_change>0, "button at gpio=12 should have last_change>0");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user