Skip to content

Commit bbddd70

Browse files
committed
GdUnit initial commit Beta 0.9.0
0 parents  commit bbddd70

File tree

199 files changed

+14918
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

199 files changed

+14918
-0
lines changed

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Project-specific ignores
2+
.history
3+
4+
# Godot-specific ignores
5+
.import/
6+
*.png.import
7+
*.jpg.import
8+
*.svg.import
9+
10+
# Mono-specific ignores
11+
.mono/
12+
data_*/
13+
mono_crash.*.json
14+
15+
# System/tool-specific ignores
16+
.directory
17+
*~
18+
19+
# temporary generated files
20+
addons/gdUnit3/GdUnitRunner.cfg

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Mike Schulze
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# GdUnit3 - Godot Unit Test Framework ![Godot v3.2](https://img.shields.io/badge/Godot-v3.2-%23478cbf?logo=godot-engine&logoColor=white)
2+
3+
4+
WIKI
5+
https://github.com/MikeSchulze/gdUnitWiki/wiki
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# This class provides Date/Time functionallity to Godot
2+
class_name LocalTime
3+
extends Resource
4+
5+
const SCRIPT_PATH = "res://addons/GdCommons/DateTime/src/LocalTime.gd"
6+
7+
const SECONDS_PER_MINUTE:int = 60
8+
const MINUTES_PER_HOUR:int = 60
9+
const HOURS_PER_DAY:int = 24
10+
const MILLIS_PER_SECOND:int = 1000
11+
const MILLIS_PER_MINUTE:int = MILLIS_PER_SECOND * SECONDS_PER_MINUTE
12+
const MILLIS_PER_HOUR:int = MILLIS_PER_MINUTE * MINUTES_PER_HOUR
13+
14+
var _time:int
15+
var _hour:int
16+
var _minute:int
17+
var _second:int
18+
var _millisecond:int
19+
20+
21+
static func _create(time_ms:int):
22+
var _instance = load(SCRIPT_PATH)
23+
return _instance.new(time_ms)
24+
25+
static func now() -> LocalTime:
26+
var time := OS.get_system_time_msecs()
27+
return _create(time)
28+
#return local_time(time.get("hour"), time.get("minute"), time.get("second"), 0)
29+
30+
static func of_unix_time(time_ms:int) -> LocalTime:
31+
return _create(time_ms)
32+
33+
static func local_time(hours:int, minutes:int, seconds:int, millis:int) -> LocalTime:
34+
return _create(MILLIS_PER_HOUR * hours\
35+
+ MILLIS_PER_MINUTE * minutes\
36+
+ MILLIS_PER_SECOND * seconds\
37+
+ millis)
38+
39+
func elapsed_since() -> String:
40+
return elapsed(OS.get_system_time_msecs() - _time)
41+
42+
func elapsed_since_ms() -> int:
43+
return OS.get_system_time_msecs() - _time
44+
45+
func plus( time_unit, value:int) -> LocalTime:
46+
var addValue:int = 0
47+
match time_unit:
48+
TimeUnit.MILLIS:
49+
addValue = value
50+
TimeUnit.SECOND:
51+
addValue = value * MILLIS_PER_SECOND
52+
TimeUnit.MINUTE:
53+
addValue = value * MILLIS_PER_MINUTE
54+
TimeUnit.HOUR:
55+
addValue = value * MILLIS_PER_HOUR
56+
57+
_init(_time + addValue)
58+
return self
59+
60+
static func elapsed(time_ms:int) -> String:
61+
var local_time = _create(time_ms)
62+
if local_time._hour > 0:
63+
return "%dh %dmin %ds %dms" % [local_time._hour, local_time._minute, local_time._second, local_time._millisecond]
64+
if local_time._minute > 0:
65+
return "%dmin %ds %dms" % [local_time._minute, local_time._second, local_time._millisecond]
66+
if local_time._second > 0:
67+
return "%ds %dms" % [local_time._second, local_time._millisecond]
68+
return "%dms" % local_time._millisecond
69+
70+
# create from epoch timestamp in ms
71+
func _init(time:int):
72+
_time = time
73+
_hour = (time / MILLIS_PER_HOUR) % 24
74+
_minute = (time / MILLIS_PER_MINUTE) % 60
75+
_second = (time / MILLIS_PER_SECOND) % 60
76+
_millisecond = time % 1000
77+
78+
func hour() -> int:
79+
return _hour
80+
81+
func minute() -> int:
82+
return _minute
83+
84+
func second() -> int:
85+
return _second
86+
87+
func millis() -> int:
88+
return _millisecond
89+
90+
func _to_string() -> String:
91+
return "%02d:%02d:%02d.%03d" % [_hour, _minute, _second, _millisecond]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class_name TimeUnit
2+
extends Resource
3+
4+
enum {
5+
MILLIS = 1,
6+
SECOND = 2,
7+
MINUTE = 3,
8+
HOUR = 4,
9+
DAY = 5,
10+
MONTH = 6,
11+
YEAR = 7
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# GdUnit generated TestSuite
2+
class_name LocalTimeTest
3+
extends GdUnitTestSuite
4+
5+
# TestSuite generated from
6+
const __source = 'res://addons/DateTime/src/LocalTime.gd'
7+
8+
9+
func test_time_constants():
10+
assert_int(LocalTime.MILLIS_PER_HOUR).is_equal(1000*60*60)
11+
assert_int(LocalTime.MILLIS_PER_MINUTE).is_equal(1000*60)
12+
assert_int(LocalTime.MILLIS_PER_SECOND).is_equal(1000)
13+
assert_int(LocalTime.HOURS_PER_DAY).is_equal(24)
14+
assert_int(LocalTime.MINUTES_PER_HOUR).is_equal(60)
15+
assert_int(LocalTime.SECONDS_PER_MINUTE).is_equal(60)
16+
17+
func test_now():
18+
var current = OS.get_time(true)
19+
var local_time := LocalTime.now()
20+
assert_int(local_time.hour()).is_equal(current.get("hour"))
21+
assert_int(local_time.minute()).is_equal(current.get("minute"))
22+
assert_int(local_time.second()).is_equal(current.get("second"))
23+
# OS.get_time() does not provide milliseconds
24+
#assert_that(local_time.millis()).is_equal(current.get("mili"))
25+
26+
func test_of_unix_time():
27+
var time = OS.get_unix_time()
28+
var local_time := LocalTime.of_unix_time(time)
29+
assert_int(local_time.hour()).is_equal((time / LocalTime.MILLIS_PER_HOUR) % 24)
30+
assert_int(local_time.minute()).is_equal((time / LocalTime.MILLIS_PER_MINUTE) % 60)
31+
assert_int(local_time.second()).is_equal((time / LocalTime.MILLIS_PER_SECOND) % 60)
32+
assert_int(local_time.millis()).is_equal(time % 1000)
33+
34+
func test_to_string():
35+
assert_str(LocalTime.local_time(10, 12, 22, 333)._to_string()).is_equal("10:12:22.333")
36+
assert_str(LocalTime.local_time(23, 59, 59, 999)._to_string()).is_equal("23:59:59.999")
37+
assert_str(LocalTime.local_time( 0, 0, 0, 000)._to_string()).is_equal("00:00:00.000")
38+
assert_str(LocalTime.local_time( 2, 4, 3, 10)._to_string()).is_equal("02:04:03.010")
39+
40+
func test_plus_seconds():
41+
var time := LocalTime.local_time(10, 12, 22, 333)
42+
assert_str(time.plus(TimeUnit.SECOND, 10)._to_string()).is_equal("10:12:32.333")
43+
assert_str(time.plus(TimeUnit.SECOND, 27)._to_string()).is_equal("10:12:59.333")
44+
assert_str(time.plus(TimeUnit.SECOND, 1)._to_string()).is_equal("10:13:00.333")
45+
46+
# test overflow
47+
var time2 := LocalTime.local_time(10, 59, 59, 333)
48+
var start_time = time2._time
49+
for iteration in 10000:
50+
51+
var t = LocalTime.of_unix_time(start_time)
52+
var seconds:int = rand_range(0, 1000)
53+
t.plus(TimeUnit.SECOND, seconds)
54+
var expected := LocalTime.of_unix_time(start_time + (seconds * LocalTime.MILLIS_PER_SECOND))
55+
assert_str(t._to_string()).is_equal(expected._to_string())
56+
57+
func test_elapsed():
58+
assert_str(LocalTime.elapsed(10)).is_equal("10ms")
59+
assert_str(LocalTime.elapsed(201)).is_equal("201ms")
60+
assert_str(LocalTime.elapsed(999)).is_equal("999ms")
61+
assert_str(LocalTime.elapsed(1000)).is_equal("1s 0ms")
62+
assert_str(LocalTime.elapsed(2000)).is_equal("2s 0ms")
63+
assert_str(LocalTime.elapsed(3040)).is_equal("3s 40ms")
64+
assert_str(LocalTime.elapsed(LocalTime.MILLIS_PER_MINUTE * 6 + 3040)).is_equal("6min 3s 40ms")

addons/GdCommons/utils/src/Result.gd

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class_name Result
2+
extends Reference
3+
4+
enum {
5+
SUCCESS,
6+
WARN,
7+
ERROR,
8+
EMPTY
9+
}
10+
11+
var _state
12+
var _warn_message := ""
13+
var _error_message := ""
14+
var _value = null
15+
16+
static func __instance() -> Result:
17+
return load("res://addons/GdCommons/utils/src/Result.gd").new() as Result
18+
19+
static func success(value) -> Result:
20+
assert(value != null, "The value must not be NULL")
21+
var result := __instance()
22+
result._value = value
23+
result._state = SUCCESS
24+
return result
25+
26+
static func warn(warn_message :String, value = null) -> Result:
27+
assert(not warn_message.empty(), "The message must not be empty")
28+
var result := __instance()
29+
result._value = value
30+
result._warn_message = warn_message
31+
result._state = WARN
32+
return result
33+
34+
static func error(error_message :String, error :int = 0) -> Result:
35+
assert(not error_message.empty(), "The message must not be empty")
36+
var result := __instance()
37+
result._value = null
38+
result._error_message = error_message
39+
result._state = ERROR
40+
return result
41+
42+
static func empty() -> Result:
43+
var result := __instance()
44+
result._state = EMPTY
45+
return result
46+
47+
48+
func is_success() -> bool:
49+
return _state == SUCCESS
50+
51+
func is_warn() -> bool:
52+
return _state == WARN
53+
54+
func is_error() -> bool:
55+
return _state == ERROR
56+
57+
func is_empty() -> bool:
58+
return _state == EMPTY
59+
60+
func value():
61+
return _value
62+
63+
func or_else(value):
64+
if not is_success():
65+
return value
66+
return value()
67+
68+
func error_message() -> String:
69+
return _error_message
70+
71+
func warn_message() -> String:
72+
return _warn_message
73+
74+
func _to_string() -> String:
75+
return str(serialize(self))
76+
77+
static func serialize(result :Result) -> Dictionary:
78+
if result == null:
79+
push_error("Can't serialize a Null object from type Result")
80+
return {
81+
"state" : result._state,
82+
"value" : var2str(result._value),
83+
"warn_msg" : result._warn_message,
84+
"err_msg" : result._error_message
85+
}
86+
87+
static func deserialize(config :Dictionary) -> Result:
88+
var result := __instance()
89+
result._value = str2var(config.get("value", ""))
90+
result._warn_message = config.get("warn_msg", null)
91+
result._error_message = config.get("err_msg", null)
92+
result._state = config.get("state")
93+
return result
94+
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# GdUnit generated TestSuite
2+
class_name ResultTest
3+
extends GdUnitTestSuite
4+
5+
# TestSuite generated from
6+
const __source = 'res://addons/GdCommons/utils/src/Result.gd'
7+
8+
9+
func test_serde():
10+
var value = {
11+
"info" : "test",
12+
"meta" : 42
13+
}
14+
var source := Result.success(value)
15+
var serialized_result = Result.serialize(source)
16+
var deserialised_result := Result.deserialize(serialized_result)
17+
assert_object(deserialised_result)\
18+
.is_instanceof(Result) \
19+
.is_equal(source)
20+
21+
func test_or_else_on_success():
22+
var result := Result.success("some value")
23+
assert_str(result.value()).is_equal("some value")
24+
assert_str(result.or_else("other value")).is_equal("some value")
25+
26+
func test_or_else_on_warning():
27+
var result := Result.warn("some warning message")
28+
assert_object(result.value()).is_null()
29+
assert_str(result.or_else("other value")).is_equal("other value")
30+
31+
func test_or_else_on_error():
32+
var result := Result.error("some error message")
33+
assert_object(result.value()).is_null()
34+
assert_str(result.or_else("other value")).is_equal("other value")

addons/gdUnit3/plugin.cfg

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[plugin]
2+
3+
name="gdUnit3"
4+
description="Unit Test Framework for Godot Scripts"
5+
author="Mike Schulze"
6+
version="0.9.0-Beta"
7+
script="plugin.gd"

0 commit comments

Comments
 (0)