|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Arduino. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +/************************************************************************************** |
| 6 | + * INCLUDE |
| 7 | + **************************************************************************************/ |
| 8 | + |
| 9 | +#include <catch.hpp> |
| 10 | + |
| 11 | +#include <StreamMock.h> |
| 12 | + |
| 13 | +/************************************************************************************** |
| 14 | + * TEST CODE |
| 15 | + **************************************************************************************/ |
| 16 | + |
| 17 | +TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-01]") |
| 18 | +{ |
| 19 | + StreamMock mock; |
| 20 | + |
| 21 | + WHEN ("Only a integer is contained in stream") |
| 22 | + { |
| 23 | + mock << "1234"; |
| 24 | + REQUIRE(mock.parseInt() == 1234); |
| 25 | + } |
| 26 | + WHEN ("The integer is prepended by digits") |
| 27 | + { |
| 28 | + mock << "abcdef1234"; |
| 29 | + REQUIRE(mock.parseInt() == 1234); |
| 30 | + } |
| 31 | + WHEN ("The integer is prepended by whitespace chars") |
| 32 | + { |
| 33 | + mock << "\r\n\t 1234"; |
| 34 | + REQUIRE(mock.parseInt() == 1234); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-02]") |
| 39 | +{ |
| 40 | + StreamMock mock; |
| 41 | + |
| 42 | + WHEN ("Only a integer is contained in stream") |
| 43 | + { |
| 44 | + mock << "1234"; |
| 45 | + REQUIRE(mock.parseInt(SKIP_NONE) == 1234); |
| 46 | + REQUIRE(mock.readString() == arduino::String("")); |
| 47 | + } |
| 48 | + WHEN ("The integer is prepended by digits") |
| 49 | + { |
| 50 | + mock << "abcdef1234"; |
| 51 | + REQUIRE(mock.parseInt(SKIP_NONE) == 0); |
| 52 | + REQUIRE(mock.readString() == arduino::String("abcdef1234")); |
| 53 | + } |
| 54 | + WHEN ("The integer is prepended by whitespace chars") |
| 55 | + { |
| 56 | + mock << "\r\n\t 1234"; |
| 57 | + REQUIRE(mock.parseInt(SKIP_NONE) == 0); |
| 58 | + REQUIRE(mock.readString() == arduino::String("\r\n\t 1234")); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_WHITESPACE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseInt-03]") |
| 63 | +{ |
| 64 | + StreamMock mock; |
| 65 | + |
| 66 | + WHEN ("Only a integer is contained in stream") |
| 67 | + { |
| 68 | + mock << "1234"; |
| 69 | + REQUIRE(mock.parseInt(SKIP_WHITESPACE) == 1234); |
| 70 | + REQUIRE(mock.readString() == arduino::String("")); |
| 71 | + } |
| 72 | + WHEN ("The integer is prepended by digits") |
| 73 | + { |
| 74 | + mock << "abcdef1234"; |
| 75 | + REQUIRE(mock.parseInt(SKIP_WHITESPACE) == 0); |
| 76 | + REQUIRE(mock.readString() == arduino::String("abcdef1234")); |
| 77 | + } |
| 78 | + WHEN ("The integer is prepended by whitespace chars") |
| 79 | + { |
| 80 | + mock << "\r\n\t 1234"; |
| 81 | + REQUIRE(mock.parseInt(SKIP_WHITESPACE) == 1234); |
| 82 | + REQUIRE(mock.readString() == arduino::String("")); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +TEST_CASE ("Testing parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = 'a')", "[Stream-parseInt-04]") |
| 87 | +{ |
| 88 | + StreamMock mock; |
| 89 | + |
| 90 | + WHEN ("Only a integer is contained in stream") |
| 91 | + { |
| 92 | + mock << "1234"; |
| 93 | + REQUIRE(mock.parseInt(SKIP_ALL, 'a') == 1234); |
| 94 | + REQUIRE(mock.readString() == arduino::String("")); |
| 95 | + } |
| 96 | + WHEN ("The integer contains only ignore char values") |
| 97 | + { |
| 98 | + mock << "12a3a4a"; |
| 99 | + REQUIRE(mock.parseInt(SKIP_NONE, 'a') == 1234); |
| 100 | + REQUIRE(mock.readString() == arduino::String("")); |
| 101 | + } |
| 102 | + WHEN ("The integer contains other than ignore chars") |
| 103 | + { |
| 104 | + mock << "1bed234"; |
| 105 | + REQUIRE(mock.parseInt(SKIP_NONE, 'a') == 1); |
| 106 | + REQUIRE(mock.readString() == arduino::String("bed234")); |
| 107 | + } |
| 108 | +} |
0 commit comments