Solidity-coverage: Error: truffle/DeployedAddresses.sol:20:119: ParserError: Expected ';' but got 'Number'

Created on 10 Apr 2020  路  6Comments  路  Source: sc-forks/solidity-coverage

Guys,

I'm trying to test the following library using [email protected] but I receive the error:

Compilation failed. See above. Error: truffle/DeployedAddresses.sol:20:119: ParserError: Expected ';' but got 'Number' function TestBokkyPooBahsDateTimeLibrary() public pure returns (address payable) { return 0x400DB523AA93053879b20F10.5.223b2076aC852; } ^^ at CompileError.ExtendableError (/home/fabianorodrigo/Projetos/bitbucket/solidity-test/workdir/actus-solidity/node_modules/truffle/build/webpack:/packages/truffle-error/index .js:10:1) at new CompileError (/home/fabianorodrigo/Projetos/bitbucket/solidity-test/workdir/actus-solidity/node_modules/truffle/build/webpack:/packages/truffle-compile/compileerror.js :13:1) at run (/home/fabianorodrigo/Projetos/bitbucket/solidity-test/workdir/actus-solidity/node_modules/truffle/build/webpack:/packages/truffle-compile/run.js:54:1)

LIBRARY UNDER TEST
`
pragma solidity ^0.5.2;

// ----------------------------------------------------------------------------
// BokkyPooBah's DateTime Library v1.01
//
// A gas-efficient Solidity date and time library
//
// https://github.com/bokkypoobah/BokkyPooBahsDateTimeLibrary
//
// Tested date range 1970/01/01 to 2345/12/31
//
// Conventions:
// Unit | Range | Notes
// :-------- |:-------------:|:-----
// timestamp | >= 0 | Unix timestamp, number of seconds since 1970/01/01 00:00:00 UTC
// year | 1970 ... 2345 |
// month | 1 ... 12 |
// day | 1 ... 31 |
// hour | 0 ... 23 |
// minute | 0 ... 59 |
// second | 0 ... 59 |
// dayOfWeek | 1 ... 7 | 1 = Monday, ..., 7 = Sunday
//
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2018-2019. The MIT Licence.
// ----------------------------------------------------------------------------

library BokkyPooBahsDateTimeLibrary {

uint constant SECONDS_PER_DAY = 24 * 60 * 60;
uint constant SECONDS_PER_HOUR = 60 * 60;
uint constant SECONDS_PER_MINUTE = 60;
int constant OFFSET19700101 = 2440588;

uint constant DOW_MON = 1;
uint constant DOW_TUE = 2;
uint constant DOW_WED = 3;
uint constant DOW_THU = 4;
uint constant DOW_FRI = 5;
uint constant DOW_SAT = 6;
uint constant DOW_SUN = 7;

// ------------------------------------------------------------------------
// Calculate the number of days from 1970/01/01 to year/month/day using
// the date conversion algorithm from
//   http://aa.usno.navy.mil/faq/docs/JD_Formula.php
// and subtracting the offset 2440588 so that 1970/01/01 is day 0
//
// days = day
//      - 32075
//      + 1461 * (year + 4800 + (month - 14) / 12) / 4
//      + 367 * (month - 2 - (month - 14) / 12 * 12) / 12
//      - 3 * ((year + 4900 + (month - 14) / 12) / 100) / 4
//      - offset
// ------------------------------------------------------------------------
function _daysFromDate(uint year, uint month, uint day) internal pure returns (uint _days) {
    require(year >= 1970);
    int _year = int(year);
    int _month = int(month);
    int _day = int(day);

    int __days = _day
      - 32075
      + 1461 * (_year + 4800 + (_month - 14) / 12) / 4
      + 367 * (_month - 2 - (_month - 14) / 12 * 12) / 12
      - 3 * ((_year + 4900 + (_month - 14) / 12) / 100) / 4
      - OFFSET19700101;

    _days = uint(__days);
}

// ------------------------------------------------------------------------
// Calculate year/month/day from the number of days since 1970/01/01 using
// the date conversion algorithm from
//   http://aa.usno.navy.mil/faq/docs/JD_Formula.php
// and adding the offset 2440588 so that 1970/01/01 is day 0
//
// int L = days + 68569 + offset
// int N = 4 * L / 146097
// L = L - (146097 * N + 3) / 4
// year = 4000 * (L + 1) / 1461001
// L = L - 1461 * year / 4 + 31
// month = 80 * L / 2447
// dd = L - 2447 * month / 80
// L = month / 11
// month = month + 2 - 12 * L
// year = 100 * (N - 49) + year + L
// ------------------------------------------------------------------------
function _daysToDate(uint _days) internal pure returns (uint year, uint month, uint day) {
    int __days = int(_days);

    int L = __days + 68569 + OFFSET19700101;
    int N = 4 * L / 146097;
    L = L - (146097 * N + 3) / 4;
    int _year = 4000 * (L + 1) / 1461001;
    L = L - 1461 * _year / 4 + 31;
    int _month = 80 * L / 2447;
    int _day = L - 2447 * _month / 80;
    L = _month / 11;
    _month = _month + 2 - 12 * L;
    _year = 100 * (N - 49) + _year + L;

    year = uint(_year);
    month = uint(_month);
    day = uint(_day);
}

function timestampFromDate(uint year, uint month, uint day) internal pure returns (uint timestamp) {
    timestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY;
}
function timestampFromDateTime(uint year, uint month, uint day, uint hour, uint minute, uint second) internal pure returns (uint timestamp) {
    timestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + hour * SECONDS_PER_HOUR + minute * SECONDS_PER_MINUTE + second;
}
function timestampToDate(uint timestamp) internal pure returns (uint year, uint month, uint day) {
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function timestampToDateTime(uint timestamp) internal pure returns (uint year, uint month, uint day, uint hour, uint minute, uint second) {
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    uint secs = timestamp % SECONDS_PER_DAY;
    hour = secs / SECONDS_PER_HOUR;
    secs = secs % SECONDS_PER_HOUR;
    minute = secs / SECONDS_PER_MINUTE;
    second = secs % SECONDS_PER_MINUTE;
}

function isValidDate(uint year, uint month, uint day) internal pure returns (bool valid) {
    if (year >= 1970 && month > 0 && month <= 12) {
        uint daysInMonth = _getDaysInMonth(year, month);
        if (day > 0 && day <= daysInMonth) {
            valid = true;
        }
    }
}
function isValidDateTime(uint year, uint month, uint day, uint hour, uint minute, uint second) internal pure returns (bool valid) {
    if (isValidDate(year, month, day)) {
        if (hour < 24 && minute < 60 && second < 60) {
            valid = true;
        }
    }
}
function isLeapYear(uint timestamp) internal pure returns (bool leapYear) {
    uint year;
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    leapYear = _isLeapYear(year);
}
function _isLeapYear(uint year) internal pure returns (bool leapYear) {
    leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
function isWeekDay(uint timestamp) internal pure returns (bool weekDay) {
    weekDay = getDayOfWeek(timestamp) <= DOW_FRI;
}
function isWeekEnd(uint timestamp) internal pure returns (bool weekEnd) {
    weekEnd = getDayOfWeek(timestamp) >= DOW_SAT;
}
function getDaysInMonth(uint timestamp) internal pure returns (uint daysInMonth) {
    uint year;
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    daysInMonth = _getDaysInMonth(year, month);
}
function _getDaysInMonth(uint year, uint month) internal pure returns (uint daysInMonth) {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        daysInMonth = 31;
    } else if (month != 2) {
        daysInMonth = 30;
    } else {
        daysInMonth = _isLeapYear(year) ? 29 : 28;
    }
}
// 1 = Monday, 7 = Sunday
function getDayOfWeek(uint timestamp) internal pure returns (uint dayOfWeek) {
    uint _days = timestamp / SECONDS_PER_DAY;
    dayOfWeek = (_days + 3) % 7 + 1;
}

function getYear(uint timestamp) internal pure returns (uint year) {
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function getMonth(uint timestamp) internal pure returns (uint month) {
    uint year;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function getDay(uint timestamp) internal pure returns (uint day) {
    uint year;
    uint month;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function getHour(uint timestamp) internal pure returns (uint hour) {
    uint secs = timestamp % SECONDS_PER_DAY;
    hour = secs / SECONDS_PER_HOUR;
}
function getMinute(uint timestamp) internal pure returns (uint minute) {
    uint secs = timestamp % SECONDS_PER_HOUR;
    minute = secs / SECONDS_PER_MINUTE;
}
function getSecond(uint timestamp) internal pure returns (uint second) {
    second = timestamp % SECONDS_PER_MINUTE;
}

function addYears(uint timestamp, uint _years) internal pure returns (uint newTimestamp) {
    uint year;
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    year += _years;
    uint daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
        day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + timestamp % SECONDS_PER_DAY;
    require(newTimestamp >= timestamp);
}
function addMonths(uint timestamp, uint _months) internal pure returns (uint newTimestamp) {
    uint year;
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    month += _months;
    year += (month - 1) / 12;
    month = (month - 1) % 12 + 1;
    uint daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
        day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + timestamp % SECONDS_PER_DAY;
    require(newTimestamp >= timestamp);
}
function addDays(uint timestamp, uint _days) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp + _days * SECONDS_PER_DAY;
    require(newTimestamp >= timestamp);
}
function addHours(uint timestamp, uint _hours) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp + _hours * SECONDS_PER_HOUR;
    require(newTimestamp >= timestamp);
}
function addMinutes(uint timestamp, uint _minutes) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp + _minutes * SECONDS_PER_MINUTE;
    require(newTimestamp >= timestamp);
}
function addSeconds(uint timestamp, uint _seconds) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp + _seconds;
    require(newTimestamp >= timestamp);
}

function subYears(uint timestamp, uint _years) internal pure returns (uint newTimestamp) {
    uint year;
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    year -= _years;
    uint daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
        day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + timestamp % SECONDS_PER_DAY;
    require(newTimestamp <= timestamp);
}
function subMonths(uint timestamp, uint _months) internal pure returns (uint newTimestamp) {
    uint year;
    uint month;
    uint day;
    (year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
    uint yearMonth = year * 12 + (month - 1) - _months;
    year = yearMonth / 12;
    month = yearMonth % 12 + 1;
    uint daysInMonth = _getDaysInMonth(year, month);
    if (day > daysInMonth) {
        day = daysInMonth;
    }
    newTimestamp = _daysFromDate(year, month, day) * SECONDS_PER_DAY + timestamp % SECONDS_PER_DAY;
    require(newTimestamp <= timestamp);
}
function subDays(uint timestamp, uint _days) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp - _days * SECONDS_PER_DAY;
    require(newTimestamp <= timestamp);
}
function subHours(uint timestamp, uint _hours) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp - _hours * SECONDS_PER_HOUR;
    require(newTimestamp <= timestamp);
}
function subMinutes(uint timestamp, uint _minutes) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp - _minutes * SECONDS_PER_MINUTE;
    require(newTimestamp <= timestamp);
}
function subSeconds(uint timestamp, uint _seconds) internal pure returns (uint newTimestamp) {
    newTimestamp = timestamp - _seconds;
    require(newTimestamp <= timestamp);
}

function diffYears(uint fromTimestamp, uint toTimestamp) internal pure returns (uint _years) {
    require(fromTimestamp <= toTimestamp);
    uint fromYear;
    uint fromMonth;
    uint fromDay;
    uint toYear;
    uint toMonth;
    uint toDay;
    (fromYear, fromMonth, fromDay) = _daysToDate(fromTimestamp / SECONDS_PER_DAY);
    (toYear, toMonth, toDay) = _daysToDate(toTimestamp / SECONDS_PER_DAY);
    _years = toYear - fromYear;
}
function diffMonths(uint fromTimestamp, uint toTimestamp) internal pure returns (uint _months) {
    require(fromTimestamp <= toTimestamp);
    uint fromYear;
    uint fromMonth;
    uint fromDay;
    uint toYear;
    uint toMonth;
    uint toDay;
    (fromYear, fromMonth, fromDay) = _daysToDate(fromTimestamp / SECONDS_PER_DAY);
    (toYear, toMonth, toDay) = _daysToDate(toTimestamp / SECONDS_PER_DAY);
    _months = toYear * 12 + toMonth - fromYear * 12 - fromMonth;
}
function diffDays(uint fromTimestamp, uint toTimestamp) internal pure returns (uint _days) {
    require(fromTimestamp <= toTimestamp);
    _days = (toTimestamp - fromTimestamp) / SECONDS_PER_DAY;
}
function diffHours(uint fromTimestamp, uint toTimestamp) internal pure returns (uint _hours) {
    require(fromTimestamp <= toTimestamp);
    _hours = (toTimestamp - fromTimestamp) / SECONDS_PER_HOUR;
}
function diffMinutes(uint fromTimestamp, uint toTimestamp) internal pure returns (uint _minutes) {
    require(fromTimestamp <= toTimestamp);
    _minutes = (toTimestamp - fromTimestamp) / SECONDS_PER_MINUTE;
}
function diffSeconds(uint fromTimestamp, uint toTimestamp) internal pure returns (uint _seconds) {
    require(fromTimestamp <= toTimestamp);
    _seconds = toTimestamp - fromTimestamp;
}

}
`
TEST FILE

`
pragma solidity ^0.5.2;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/external/BokkyPooBah/BokkyPooBahsDateTimeLibrary.sol";

contract TestBokkyPooBahsDateTimeLibrary {
uint nonce = 56;
function stringToBytes32(string memory source) public returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}

assembly {
    result := mload(add(source, 32))
}

}
using BokkyPooBahsDateTimeLibrary for uint;

//Should execute _daysFromDate
function test__daysFromDate0() public {
uint varLib = 1461001;
varLib._daysFromDate(30, 18);
}
//Should fail _daysFromDate when NOT comply with: year >= 1970
function test__daysFromDate1() public {
uint varLib = 1969;
varLib._daysFromDate(30, 18);
}
//Should execute _daysToDate
function test__daysToDate() public {
uint varLib = 130;
varLib._daysToDate();
}
//Should execute timestampFromDate
function test_timestampFromDate() public {
uint varLib = 1461;
varLib.timestampFromDate(111, 13);
}
//Should execute timestampFromDateTime
function test_timestampFromDateTime() public {
uint varLib = 2440588;
varLib.timestampFromDateTime(254, 70, 86399, 151, 2448);
}
//Should execute timestampToDate
function test_timestampToDate() public {
uint varLib = 79;
varLib.timestampToDate();
}
//Should execute timestampToDateTime
function test_timestampToDateTime() public {
uint varLib = 4799;
varLib.timestampToDateTime();
}
//Should execute isValidDate WHEN year>=1970,month>0,month<=12
function test_isValidDate0() public {
uint varLib = 2440588;
varLib.isValidDate(11, 146096);
}
//Should execute isValidDate WHEN month<=0
function test_isValidDate1() public {
uint varLib = 365;
varLib.isValidDate(0, 399);
}
//Should execute isValidDateTime
function test_isValidDateTime() public {
uint varLib = 50;
varLib.isValidDateTime(4901, 16, 79, 360, 49);
}
//Should execute isLeapYear
function test_isLeapYear() public {
uint varLib = 59;
varLib.isLeapYear();
}
//Should execute _isLeapYear
function test__isLeapYear() public {
uint varLib = 51;
varLib._isLeapYear();
}
//Should execute isWeekDay
function test_isWeekDay() public {
uint varLib = 13;
varLib.isWeekDay();
}
//Should execute isWeekEnd
function test_isWeekEnd() public {
uint varLib = 49;
varLib.isWeekEnd();
}
//Should execute getDaysInMonth
function test_getDaysInMonth() public {
uint varLib = 49;
varLib.getDaysInMonth();
}
//Should execute _getDaysInMonth WHEN month==1
function test__getDaysInMonth0() public {
uint varLib = 2440587;
varLib._getDaysInMonth(1);
}
//Should execute _getDaysInMonth WHEN month==3
function test__getDaysInMonth1() public {
uint varLib = 161;
varLib._getDaysInMonth(3);
}
//Should execute _getDaysInMonth WHEN month==5
function test__getDaysInMonth2() public {
uint varLib = 119;
varLib._getDaysInMonth(5);
}
//Should execute _getDaysInMonth WHEN month==7
function test__getDaysInMonth3() public {
uint varLib = 130;
varLib._getDaysInMonth(7);
}
//Should execute _getDaysInMonth WHEN month==8
function test__getDaysInMonth4() public {
uint varLib = 13;
varLib._getDaysInMonth(8);
}
//Should execute _getDaysInMonth WHEN month==10
function test__getDaysInMonth5() public {
uint varLib = 91;
varLib._getDaysInMonth(10);
}
//Should execute _getDaysInMonth WHEN month==12
function test__getDaysInMonth6() public {
uint varLib = 41;
varLib._getDaysInMonth(12);
}
//Should execute _getDaysInMonth WHEN month!=2
function test__getDaysInMonth7() public {
uint varLib = 18;
varLib._getDaysInMonth(51);
}
//Should execute _getDaysInMonth WHEN month==2,month!=1,month!=3,month!=5,month!=7,month!=8,month!=10,month!=12
function test__getDaysInMonth8() public {
uint varLib = 40;
varLib._getDaysInMonth(2);
}
//Should execute getDayOfWeek
function test_getDayOfWeek() public {
uint varLib = 4001;
varLib.getDayOfWeek();
}
//Should execute getYear
function test_getYear() public {
uint varLib = 400;
varLib.getYear();
}
//Should execute getMonth
function test_getMonth() public {
uint varLib = 69;
varLib.getMonth();
}
//Should execute getDay
function test_getDay() public {
uint varLib = 11;
varLib.getDay();
}
//Should execute getHour
function test_getHour() public {
uint varLib = 40;
varLib.getHour();
}
//Should execute getMinute
function test_getMinute() public {
uint varLib = 121;
varLib.getMinute();
}
//Should execute getSecond
function test_getSecond() public {
uint varLib = 2446;
varLib.getSecond();
}
//Should execute addYears
function test_addYears() public {
uint varLib = 1462;
varLib.addYears(68568);
}
//Should execute addMonths
function test_addMonths() public {
uint varLib = 1;
varLib.addMonths(3999);
}
//Should execute addDays
function test_addDays() public {
uint varLib = 400;
varLib.addDays(41);
}
//Should execute addHours
function test_addHours() public {
uint varLib = 8;
varLib.addHours(900);
}
//Should execute addMinutes
function test_addMinutes() public {
uint varLib = 10;
varLib.addMinutes(1461000);
}
//Should execute addSeconds
function test_addSeconds() public {
uint varLib = 0;
varLib.addSeconds(68569);
}
//Should execute subYears
function test_subYears() public {
uint varLib = 4800;
varLib.subYears(951);
}
//Should execute subMonths
function test_subMonths() public {
uint varLib = 20;
varLib.subMonths(3);
}
//Should execute subDays
function test_subDays() public {
uint varLib = 41;
varLib.subDays(4000);
}
//Should execute subHours
function test_subHours() public {
uint varLib = 3999;
varLib.subHours(15);
}
//Should execute subMinutes
function test_subMinutes() public {
uint varLib = 0;
varLib.subMinutes(901);
}
//Should execute subSeconds
function test_subSeconds() public {
uint varLib = 41;
varLib.subSeconds(68570);
}
//Should execute diffYears
function test_diffYears0() public {
uint varLib = 160;
varLib.diffYears(68568);
}
//Should fail diffYears when NOT comply with: fromTimestamp <= toTimestamp
function test_diffYears1() public {
uint varLib = 68569;
varLib.diffYears(68568);
}
//Should execute diffMonths
function test_diffMonths0() public {
uint varLib = 48;
varLib.diffMonths(99);
}
//Should fail diffMonths when NOT comply with: fromTimestamp <= toTimestamp
function test_diffMonths1() public {
uint varLib = 100;
varLib.diffMonths(99);
}
//Should execute diffDays
function test_diffDays0() public {
uint varLib = 50;
varLib.diffDays(1970);
}
//Should fail diffDays when NOT comply with: fromTimestamp <= toTimestamp
function test_diffDays1() public {
uint varLib = 1971;
varLib.diffDays(1970);
}
//Should execute diffHours
function test_diffHours0() public {
uint varLib = 146097;
varLib.diffHours(146097);
}
//Should fail diffHours when NOT comply with: fromTimestamp <= toTimestamp
function test_diffHours1() public {
uint varLib = 146098;
varLib.diffHours(146097);
}
//Should execute diffMinutes
function test_diffMinutes0() public {
uint varLib = 51;
varLib.diffMinutes(899);
}
//Should fail diffMinutes when NOT comply with: fromTimestamp <= toTimestamp
function test_diffMinutes1() public {
uint varLib = 900;
varLib.diffMinutes(899);
}
//Should execute diffSeconds
function test_diffSeconds0() public {
uint varLib = 9;
varLib.diffSeconds(25);
}
//Should fail diffSeconds when NOT comply with: fromTimestamp <= toTimestamp
function test_diffSeconds1() public {
uint varLib = 26;
varLib.diffSeconds(25);
}
}
`

0.6.0

All 6 comments

@fabianorodrigo Are you able use the latest version of Solidity-Coverage (0.7.4)? It uses an actively maintained parser and runs into fewer problems like this.

I see truffle/DeployedAddresses in your error message which suggests you might be trying to run native Solidity tests or have Solidity files in your test directory. If so you'll need to remove these when using 0.6.7.

Yes! I have some Solidity tests for testing libraries and functions with visibility 'internal'.

What versions of Solidity does solidity-coverage 0.7.4 support?

Thank you very much for your attention!

@fabianorodrigo 0.7.4 Should work with all versions of Solidity including latest. It isn't able to measure coverage for native Solidity tests but it also doesn't crash if you have those tests in your Truffle project.

There is an upgrade guide here if you're having difficulty migrating from an earlier version. Also please just lmk if you run into problems. If your project is public on Github I can help debug installation.

Closing. Please just ping if there's more to resolve here.

Is there any difference between the code in branch 0.6.x-final and the package published in npmjs.com?

@fabianorodrigo I believe they're the same.

Was this page helpful?
0 / 5 - 0 ratings