Improve error message when formula constant number lacks a unit.

pull/949/head
Fredrik Öhrström 2023-04-18 13:56:23 +02:00
rodzic 7ccbaeb154
commit fa725020e3
3 zmienionych plików z 61 dodań i 5 usunięć

Wyświetl plik

@ -613,6 +613,7 @@ size_t FormulaImplementation::parseOps(size_t i)
if (tok->type == TokenType::PLUS)
{
size_t next = parseOps(i+1);
if (!valid_) return next;
handleAddition(tok);
return next;
}
@ -620,6 +621,7 @@ size_t FormulaImplementation::parseOps(size_t i)
if (tok->type == TokenType::MINUS)
{
size_t next = parseOps(i+1);
if (!valid_) return next;
handleSubtraction(tok);
return next;
}
@ -627,6 +629,7 @@ size_t FormulaImplementation::parseOps(size_t i)
if (tok->type == TokenType::TIMES)
{
size_t next = parseOps(i+1);
if (!valid_) return next;
handleMultiplication(tok);
return next;
}
@ -634,6 +637,7 @@ size_t FormulaImplementation::parseOps(size_t i)
if (tok->type == TokenType::DIV)
{
size_t next = parseOps(i+1);
if (!valid_) return next;
handleDivision(tok);
return next;
}
@ -641,6 +645,7 @@ size_t FormulaImplementation::parseOps(size_t i)
if (tok->type == TokenType::EXP)
{
size_t next = parseOps(i+1);
if (!valid_) return next;
handleExponentiation(tok);
return next;
}
@ -648,6 +653,7 @@ size_t FormulaImplementation::parseOps(size_t i)
if (tok->type == TokenType::SQRT)
{
size_t next = parseOps(i+1);
if (!valid_) return next;
handleSquareRoot(tok);
return next;
}
@ -662,10 +668,14 @@ size_t FormulaImplementation::parseOps(size_t i)
return i;
}
if (next == NULL) return i;
if (tok->type == TokenType::NUMBER && next->type == TokenType::UNIT)
if (tok->type == TokenType::NUMBER)
{
if (next == NULL || next->type != TokenType::UNIT)
{
errors_.push_back(tostrprintf("Constant number %s lacks a unit!\n", tok->vals(formula_).c_str()));
valid_ = false;
return i;
}
handleConstant(tok, next);
return i+2;
}
@ -685,7 +695,7 @@ size_t FormulaImplementation::parsePar(size_t i)
if (tok == NULL) break;
if (tok->type == TokenType::RPAR) break;
size_t next = parseOps(i);
if (next == i) break;
if (!valid_ || next == i) break;
i = next;
}
@ -855,7 +865,7 @@ bool FormulaImplementation::go()
for (;;)
{
size_t next = parseOps(i);
if (next == i) break;
if (!valid_ || next == i) break;
i = next;
}

Wyświetl plik

@ -93,6 +93,9 @@ if [ "$?" != "0" ]; then RC="1"; fi
tests/test_formulas.sh $PROG
if [ "$?" != "0" ]; then RC="1"; fi
tests/test_formula_errors.sh $PROG
if [ "$?" != "0" ]; then RC="1"; fi
tests/test_calculate_dates.sh $PROG
if [ "$?" != "0" ]; then RC="1"; fi

Wyświetl plik

@ -0,0 +1,43 @@
#!/bin/sh
PROG="$1"
rm -rf testoutput
mkdir -p testoutput
TEST=testoutput
TESTNAME="Test cmdline formulas with errors --calculate_... "
TESTRESULT="ERROR"
$PROG --format=json \
--calculate_sumtemp_c='external_temperature_c*198' \
--calculate_addtemp_c='external_temperature_c + 1100' \
--format=fields --selectfields=name \
23442D2C998734761B168D2087D19EAD217F1779EDA86AB6_710008190000081900007F13 \
MyTapWater multical21 76348799 "" > $TEST/test_output.txt 2>&1
cat <<EOF > $TEST/test_expected.txt
Warning! Ignoring calculated field sumtemp because parse failed:
Constant number 198 lacks a unit!
Warning! Ignoring calculated field addtemp because parse failed:
Constant number 1100 lacks a unit!
MyTapWater
EOF
if [ "$?" = "0" ]
then
cat $TEST/test_output.txt | sed 's/"timestamp": "....-..-..T..:..:..Z"/"timestamp": "1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt
diff $TEST/test_expected.txt $TEST/test_responses.txt
if [ "$?" = "0" ]
then
echo "OK: $TESTNAME"
TESTRESULT="OK"
else
if [ "$USE_MELD" = "true" ]
then
meld $TEST/test_expected.txt $TEST/test_responses.txt
fi
fi
fi
if [ "$TESTRESULT" = "ERROR" ]; then echo ERROR: $TESTNAME; exit 1; fi