MCPcopy Create free account
hub / github.com/FastLED/FastLED / FL_TEST_FILE

Function FL_TEST_FILE

tests/fl/remote/transport/serial.cpp:7–223  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include "test.h"
6
7FL_TEST_FILE(FL_FILEPATH) {
8
9// =============================================================================
10// String View Optimization Tests
11// =============================================================================
12
13FL_TEST_CASE("Serial: createSerialRequestSource - basic JSON") {
14 // Test that valid JSON is parsed correctly
15 fl::string mockInput = R"({"method":"test","params":[],"id":1})";
16
17 // Simulate readSerialLine returning the mock input
18 struct MockSerial {
19 fl::string data;
20 int available() const { return data.empty() ? 0 : 1; }
21 int read() {
22 if (data.empty()) return -1;
23 char c = data[0];
24 data = data.substr(1);
25 return c;
26 }
27 };
28
29 auto requestSource = fl::createSerialRequestSource();
30
31 // Create a test by manually parsing (since we can't easily mock the serial input)
32 fl::optional<fl::json> result = fl::json::parse(mockInput);
33 FL_REQUIRE(result.has_value());
34 FL_REQUIRE(result->contains("method"));
35 FL_REQUIRE(result->contains("params"));
36 FL_REQUIRE(result->contains("id"));
37}
38
39FL_TEST_CASE("Serial: String view prefix stripping - zero copy") {
40 // Test that prefix stripping using string_view is zero-copy
41 fl::string input = "PREFIX: {\"method\":\"test\",\"params\":[]}";
42 fl::string_view view = input;
43
44 // Strip prefix using string_view (no allocation)
45 const char* prefix = "PREFIX: ";
46 if (view.starts_with(prefix)) {
47 view.remove_prefix(fl::strlen(prefix));
48 }
49
50 // View should point to the JSON part
51 FL_REQUIRE(view.size() == fl::strlen("{\"method\":\"test\",\"params\":[]}"));
52 FL_REQUIRE(view[0] == '{');
53 FL_REQUIRE(view.starts_with("{\"method\""));
54}
55
56FL_TEST_CASE("Serial: String view trimming - zero copy") {
57 // Test that trimming using string_view is zero-copy
58 fl::string input = " \t {\"test\":true} \r\n ";
59 fl::string_view view = input;
60
61 // Trim leading whitespace
62 while (!view.empty() && fl::isspace(view.front())) {
63 view.remove_prefix(1);
64 }

Callers

nothing calls this directly

Calls 15

strlenFunction · 0.85
isspaceFunction · 0.85
formatJsonResponseFunction · 0.85
remove_prefixMethod · 0.80
remove_suffixMethod · 0.80
trimMethod · 0.80
has_valueMethod · 0.45
containsMethod · 0.45
starts_withMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected