From 62e4c68907d8d33709c2c1f92a161dff00b3d5f2 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 22:01:36 +0200 Subject: Adding upstream version 0.11.2. Signed-off-by: Daniel Baumann --- src/formats/logfmt/logfmt.parser.test.cc | 221 +++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 src/formats/logfmt/logfmt.parser.test.cc (limited to 'src/formats/logfmt/logfmt.parser.test.cc') diff --git a/src/formats/logfmt/logfmt.parser.test.cc b/src/formats/logfmt/logfmt.parser.test.cc new file mode 100644 index 0000000..2193bfe --- /dev/null +++ b/src/formats/logfmt/logfmt.parser.test.cc @@ -0,0 +1,221 @@ +/** + * Copyright (c) 2021, Timothy Stack + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of Timothy Stack nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @file logfmt.parser.test.cc + */ + +#include "config.h" + +#include + +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest/doctest.h" + +#include "logfmt.parser.hh" + +TEST_CASE("basic") +{ + static const char *line = "abc=def ghi=\"1 2 3 4\" time=333 empty1= tf=true empty2="; + + auto p = logfmt::parser{string_fragment{line}}; + + auto pair1 = p.step(); + + CHECK(pair1.is()); + CHECK(pair1.get().first == "abc"); + CHECK(pair1.get().second + .get().uv_value == "def"); + + auto pair2 = p.step(); + + CHECK(pair2.is()); + CHECK(pair2.get().first == "ghi"); + CHECK(pair2.get().second + .get().qv_value == "\"1 2 3 4\""); + + auto pair3 = p.step(); + + CHECK(pair3.is()); + CHECK(pair3.get().first == "time"); + CHECK(pair3.get().second + .get().iv_value == 333); + + auto pair4 = p.step(); + + CHECK(pair4.is()); + CHECK(pair4.get().first == "empty1"); + CHECK(pair4.get().second + .get().uv_value == ""); + + auto pair5 = p.step(); + + CHECK(pair5.is()); + CHECK(pair5.get().first == "tf"); + CHECK(pair5.get().second + .get().bv_value); + + auto pair6 = p.step(); + + CHECK(pair6.is()); + CHECK(pair6.get().first == "empty2"); + CHECK(pair6.get().second + .get().uv_value == ""); + + auto eoi = p.step(); + CHECK(eoi.is()); +} + +TEST_CASE("floats") +{ + static const char *line = "f1=1.0 f2=-2.0 f3=1.2e3 f4=1.2e-2 f5=2e1 f6=2e+1"; + + auto p = logfmt::parser{string_fragment{line}}; + + auto pair1 = p.step(); + + CHECK(pair1.is()); + CHECK(pair1.get().first == "f1"); + CHECK(pair1.get().second + .get().fv_value == 1.0); + + auto pair2 = p.step(); + + CHECK(pair2.is()); + CHECK(pair2.get().first == "f2"); + CHECK(pair2.get().second + .get().fv_value == -2.0); + + auto pair3 = p.step(); + + CHECK(pair3.is()); + CHECK(pair3.get().first == "f3"); + CHECK(pair3.get().second + .get().fv_value == 1200); + + auto pair4 = p.step(); + + CHECK(pair4.is()); + CHECK(pair4.get().first == "f4"); + CHECK(pair4.get().second + .get().fv_value == 0.012); + + auto pair5 = p.step(); + + CHECK(pair5.is()); + CHECK(pair5.get().first == "f5"); + CHECK(pair5.get().second + .get().fv_value == 20); + + auto pair6 = p.step(); + + CHECK(pair6.is()); + CHECK(pair6.get().first == "f6"); + CHECK(pair6.get().second + .get().fv_value == 20); +} + +TEST_CASE("bad floats") +{ + static const char *line = "bf1=- bf2=-1.2e bf3=1.2.3 bf4=1e2e4"; + + auto p = logfmt::parser{string_fragment{line}}; + + auto pair1 = p.step(); + + CHECK(pair1.is()); + CHECK(pair1.get().first == "bf1"); + CHECK(pair1.get().second + .get().uv_value == "-"); + + auto pair2 = p.step(); + + CHECK(pair2.is()); + CHECK(pair2.get().first == "bf2"); + CHECK(pair2.get().second + .get().uv_value == "-1.2e"); + + auto pair3 = p.step(); + + CHECK(pair3.is()); + CHECK(pair3.get().first == "bf3"); + CHECK(pair3.get().second + .get().uv_value == "1.2.3"); + + auto pair4 = p.step(); + + CHECK(pair4.is()); + CHECK(pair4.get().first == "bf4"); + CHECK(pair4.get().second + .get().uv_value == "1e2e4"); +} + +TEST_CASE("non-terminated string") +{ + static const char *line = "abc=\"12 2"; + + auto p = logfmt::parser{string_fragment{line}}; + auto pair1 = p.step(); + + CHECK(pair1.is()); + CHECK(pair1.get().e_offset == 9); + CHECK(pair1.get().e_msg == "non-terminated string"); +} + +TEST_CASE("missing equals") +{ + static const char *line = "abc"; + + auto p = logfmt::parser{string_fragment{line}}; + auto pair1 = p.step(); + + CHECK(pair1.is()); + CHECK(pair1.get().e_offset == 3); + CHECK(pair1.get().e_msg == "expecting '='"); +} + +TEST_CASE("missing key") +{ + static const char *line = "=def"; + + auto p = logfmt::parser{string_fragment{line}}; + auto pair1 = p.step(); + + CHECK(pair1.is()); + CHECK(pair1.get().e_offset == 0); + CHECK(pair1.get().e_msg == "expecting key followed by '='"); +} + +TEST_CASE("empty") +{ + static const char *line = ""; + + auto p = logfmt::parser{string_fragment{line}}; + auto pair1 = p.step(); + + CHECK(pair1.is()); +} -- cgit v1.2.3