Skip to content

Commit

Permalink
added invalid time parse unit test
Browse files Browse the repository at this point in the history
added some validation and checks to TimeParse
added ONLY_C_LOCALE=1 to build
  • Loading branch information
Southclaws committed Jun 16, 2018
1 parent 1b0e636 commit bdf4484
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_definitions(
-D_CRT_SECURE_NO_WARNINGS
-D_SILENCE_CXX17_UNCAUGHT_EXCEPTION_DEPRECATION_WARNING
-DUSE_SYSTEM_TZ_DB=ON
-DONLY_C_LOCALE=1
)

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/test/plugins)
Expand Down
54 changes: 32 additions & 22 deletions pawn.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
{
"user": "Southclaws",
"repo": "pawn-chrono",
"entry": "test.pwn",
"output": "test/gamemodes/test.amx",
"dependencies": ["Southclaws/samp-stdlib"],
"dev_dependencies": ["pawn-lang/YSI-Includes"],
"runtime": {
"plugins": ["Southclaws/pawn-chrono"]
},
"resources": [
{
"name": "chrono.so",
"archive": false,
"platform": "linux"
},
{
"name": "chrono.dll",
"archive": false,
"platform": "windows"
}
]
}
"user": "Southclaws",
"repo": "pawn-chrono",
"tag": "1.1.0",
"entry": "test.pwn",
"output": "test/gamemodes/test.amx",
"dependencies": [
"Southclaws/samp-stdlib"
],
"dev_dependencies": [
"pawn-lang/YSI-Includes",
"oscar-broman/strlib"
],
"runtime": {
"version": "0.3.7",
"mode": "server",
"plugins": [
"Southclaws/pawn-chrono"
],
"rcon_password": "password",
"port": 7777
},
"resources": [
{
"name": "chrono.so",
"platform": "linux"
},
{
"name": "chrono.dll",
"platform": "windows"
}
]
}
9 changes: 8 additions & 1 deletion src/natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ cell Natives::TimeParse(AMX* amx, cell* params)
std::istringstream is(string);
date::sys_seconds d;

date::from_stream(is, fmt.c_str(), d);
try {
if (date::from_stream(is, fmt.c_str(), d).fail()) {
return 1;
}
} catch (std::exception& e) {
logprintf("ERROR: date::from_stream failed: %s", e.what());
return 1;
}

*output = static_cast<cell>(d.time_since_epoch().count());

Expand Down
17 changes: 17 additions & 0 deletions test.pwn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "chrono.inc"

#include <a_samp>
#include <strlib>
#include <YSI\y_testing>


Expand Down Expand Up @@ -479,6 +480,22 @@ Test:TimeParse_ISO6801_FULL_UTC() {
ASSERT(ts == Timestamp:1527929232);
}

Test:TimeParse_Invalid() {
new
year = 1997,
month = 3,
day = 2,
ret,
Timestamp:seconds,
Seconds:agetime;

TimeParse(sprintf("%i%i%i", year, month, day), "%Y%m%d", seconds);
agetime = Now() - seconds;

printf("- output: %d: %d agetime: %d", ret, _:seconds, _:agetime);
ASSERT(seconds == Timestamp:0);
}

Test:TimestampDifference() {
new
Timestamp:future = Timestamp:1527932832,
Expand Down

0 comments on commit bdf4484

Please # to comment.