| 5 | #include "test.h" |
| 6 | |
| 7 | FL_TEST_FILE(FL_FILEPATH) { |
| 8 | |
| 9 | // ============================================================================= |
| 10 | // String View Optimization Tests |
| 11 | // ============================================================================= |
| 12 | |
| 13 | FL_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 | |
| 39 | FL_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 | |
| 56 | FL_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 | } |
nothing calls this directly
no test coverage detected