xerbot 0.1.0 : le contrat xerbot v1 pour Arduino / PlatformIO (USB, ESP-NOW, Bluetooth), 6 exemples dont RuView CSI, test natif 8/8
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R75H65SynGwRG5vBSoYeZ1
This commit is contained in:
27
test/test.cpp
Normal file
27
test/test.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "Arduino.h"
|
||||
#include "../src/Xerbot.h"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
static int n = 0;
|
||||
#define OK(c, m) do { if (!(c)) { std::cout << "✗ " << m << "\n"; return 1; } n++; } while (0)
|
||||
static std::string vu;
|
||||
void cb(Xerbot& x, const XerbotCommande& c) { vu = c.cmd; char t[32]; c.texte("mode", t, sizeof t); vu += std::string("|") + t + "|" + std::to_string((int)c.nombre("w")); x.ack(c.id, true); }
|
||||
int main() {
|
||||
Stream s; Xerbot xb("cube-1", "capteur"); xb.battement(0); xb.begin(s);
|
||||
OK(s.out == "{\"dev\":\"cube-1\",\"type\":\"capteur\",\"state\":{\"online\":true}}\n", "begin annonce l'état");
|
||||
s.out.clear(); xb.onCommande(cb);
|
||||
xb.telemetrie().add("t", 21.5).add("n", 3).add("nom", "a\"b").add("ok", true).envoyer();
|
||||
OK(s.out == "{\"dev\":\"cube-1\",\"type\":\"capteur\",\"telemetry\":{\"t\":21.50,\"n\":3,\"nom\":\"a\\\"b\",\"ok\":true}}\n", "télémétrie (échappement)");
|
||||
s.out.clear(); s.in = "{\"dev\":\"cube-1\",\"cmd\":\"chauffe\",\"args\":{\"mode\":\"torse\",\"w\":15},\"id\":\"x1\"}\n"; xb.loop();
|
||||
OK(vu == "chauffe|torse|15", "commande + args : " + vu);
|
||||
OK(s.out == "{\"dev\":\"cube-1\",\"type\":\"capteur\",\"ack\":{\"id\":\"x1\",\"ok\":true}}\n", "ack corrélé par id");
|
||||
s.out.clear(); vu = ""; s.in = "{\"dev\":\"autre\",\"cmd\":\"x\",\"id\":\"y\"}\n"; xb.loop();
|
||||
OK(vu.empty() && s.out.empty(), "une commande pour un autre appareil est ignorée");
|
||||
s.in = "{\"cmd\":\"ping\",\"id\":\"p\"}\n"; xb.loop();
|
||||
OK(s.out.find("\"ack\":{\"id\":\"p\",\"ok\":true}") != std::string::npos, "ping répond tout seul");
|
||||
s.out.clear(); s.in = "{\"hello\":\"xerboxion\"}\n"; xb.loop();
|
||||
OK(s.out.find("\"state\":{\"online\":true}") != std::string::npos, "hello → l'état");
|
||||
s.out.clear(); std::string longue(5000, 'a'); s.in = longue + "\n{\"cmd\":\"ping\",\"id\":\"z\"}\n"; xb.loop();
|
||||
OK(s.out.find("\"id\":\"z\"") != std::string::npos, "une ligne trop longue est jetée, la suivante passe");
|
||||
std::cout << "xerbot : " << n << "/" << n << "\n"; return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user