Antoine Goutenoir 959cf1c3c9
[GdScript] Templates for GdScript (Godot 4) (#19267)
* feat(gdscript): sketch implementation of gdscript target language

This does not really work yet, but it's a start.
Results are not denormalized, no support for enums nor datetimes,
and thousands of other features are missing.

I still don't know how we are going to denormalize JSON+LD
without writing a whole GDScript lib for it…

* feat: add an exhaustive list of keywords reserved in GDScript

I've also provided the small python script I used to generate the list.

* refacto(gdscript): start using partials in templates

Whilst I'm racking my brains trying to figure out integration testing…

* test(gdscript): prepare a demo and integration testing

* fix(gdscript): do not use subclasses, use plain POGO

(plain ol' godot object)

One: I don't know how they work under-the-hood.
Two: I'm very confused over-the-hood.
Tri: We do not need them.

* refacto(gdscript): move demo files to their own directory

I know I'm making a lot of commits for not much,
but now I'm opening the sample files with Godot as well,
and doing unholy things with filesystems,
so I'm not taking any chances.

It's all going to be squashed anyway.  :)

* fix(gdscript): sample as a Godot project

It works !  I can now write integration tests in GDScript.
The real work starts now.

/spend 25h

* feat(gdscript): serialize and send body params

The test suite is now past its first hurdle, the 415 HTTP status code,
and went straight into an unexpected error 500.

I suspect the server does not like me trying to set the pet id at 0,
because that's what we're trying to do right now.

Godot is crashing a lot, mostly because I don't know how to make Callable.NOOP
and my current solution hints at optional on_success and on_failure,
yet if we omit them the engine will ragequit.

* feat(gdscript): check request body for required yet missing properties

Now we'll get a nice error when we forget to set a required property.

The demo is now able to:
- connect
- create a user
- login as that user
- create a pet

* feat(gdscript): namespace core classes as well with apiPrefixName

This makes our usage of `class_name` a little more acceptable.

* feat(gdscript): support prefixes and suffixes for class names

This will crutch namespacing well enough for most uses.

* feat(gdscript): handle enums, naively

* feat(gdscript): support basic API endpoint param constraints

- minLength
- maxLength
- minItems
- maxItems
- minimum
- maximum
- pattern (no flags)

* feat(gdscript): handle header params and header customization

We also support serializing to application/x-www-form-urlencoded now.

Next up: DateTimes !

* feat(gdscript): handle Date and DateTime like Strings

There's no timezone support in Godot for DateTimes.

* feat(gdscript): support plain text responses

* feat(gdscript): support collections of models

Those are Arrays, not custom collection objects.

* feat(gdscript): configure default host from OAS

* feat(gdscript): some documentation and better config

We don't need no factories nor singletons ; config is enough.

* docs(gdscript): document usage a little

* feat(gdscript): add more reserved words, skip jsonld models and configure features

We can now generate a client for an OAS server running ApiPlatform (PHP).

* feat(gdscript): improve logging with a configurable log level

* feat(gdscript): add support for Basic Bearer and Header ApiKey

(but I can't find the `description` template handler)

* fix(gdscript)

Too late to amend >.<

* fix(gdscript)

dangsarnit

* chore(gdscript): clean up a sprint artifact

* fix: don't forget the HTTP error code when relevant

* feat: use Resource as base class for models

* fix. Default string values now with "quotes"

* temporary remove settings as godot api have changed

* kick ci

* docs: review gdscript java class

* feat: support for TLS, some refacto, some review

There's still a lot of holes, TODOs and FIXMEs.

* feat: experimental support of Request inline objects

The inline response objects are still not supported.

* feat(gdscript): support inline request and response objects

* chore(gdscript): review the templates

* fix(gdscript): unexpected nulls in default values

{{#if defaultValue}} evaluates to true for null if we call super here.

* refacto(gdscript): replace "bee" prefix by "bzz", use a constructor

Now we pass the config and the client via the constructor.
This reduces the area of the public surface a bit, for the better I think.
This commit also cleans up the class name shenanigans.

* fix(gdscript): add missing file

* test(gdscript): refactor the test project to use the generated lib as addon

Since there is no singleton in the generated client, the addon need not be enabled in the project configuration to be usable.

The --headless mode is broken for now, as things changed in Godot 4 since the beta.

* docs(gdscript): document petstore server ADR

* test(gdscript): add GUT and an integration test

We used the latest stable GUT, but a feature we're going to need was merged today, so we'll need to update it either to master or to the next release at some point.

* refacto(gdscript, breaking): use an ApiResponse object in success callbacks

/spent 6d since the beginning

* test(gdscript): update integration tests

/spend 2h

* docs(gdscript): explain the new ApiResponse

Also moving core templates to their own subdir, for clarity.

/spend 10m

* chore(gdscript): review, document, clean up

/spend 2h

* test(gdscript): test the delete operation as well

/spend 7m

* feat(gdscript): update GUT and exit with appropriate code

/spend 2h

* docs(gdscript): add Gdscript's PI

Hire me while I'm available !  :D
I'd rather code than make a CV.

* feat(gdscript): support reserved keywords

Also adding some more assertions,
and using our own OAS file now.

/spend 3h

* refacto(gdscript): use "base" instead of "bee"

/spend 1h

* feat(gdscript): improve descriptions

/spend 1h

* fix(gdscript): await before polling

Contributed by @jchu231

* docs(gdscript): review the template files

* docs(gdscript): review and generate docs

---------

Co-authored-by: Bagrat <b.saatsazov@gmail.com>
2024-11-17 10:48:19 +08:00

268 lines
7.6 KiB
GDScript

# ------------------------------------------------------------------------------
# Contains all the results of a single test. Allows for multiple asserts results
# and pending calls.
#
# When determining the status of a test, check for failing then passing then
# pending.
# ------------------------------------------------------------------------------
class Test:
var pass_texts = []
var fail_texts = []
var pending_texts = []
var orphans = 0
var line_number = 0
# must have passed an assert and not have any other status to be passing
func is_passing():
return pass_texts.size() > 0 and fail_texts.size() == 0 and pending_texts.size() == 0
# failing takes precedence over everything else, so any failures makes the
# test a failure.
func is_failing():
return fail_texts.size() > 0
# test is only pending if pending was called and the test is not failing.
func is_pending():
return pending_texts.size() > 0 and fail_texts.size() == 0
func did_something():
return is_passing() or is_failing() or is_pending()
# NOTE: The "failed" and "pending" text must match what is outputted by
# the logger in order for text highlighting to occur in summary.
func to_s():
var pad = ' '
var to_return = ''
for i in range(fail_texts.size()):
to_return += str(pad, '[Failed]: ', fail_texts[i], "\n")
for i in range(pending_texts.size()):
to_return += str(pad, '[Pending]: ', pending_texts[i], "\n")
return to_return
func get_status():
var to_return = 'no asserts'
if(pending_texts.size() > 0):
to_return = 'pending'
elif(fail_texts.size() > 0):
to_return = 'fail'
elif(pass_texts.size() > 0):
to_return = 'pass'
return to_return
# ------------------------------------------------------------------------------
# Contains all the results for a single test-script/inner class. Persists the
# names of the tests and results and the order in which the tests were run.
# ------------------------------------------------------------------------------
class TestScript:
var name = 'NOT_SET'
var was_skipped = false
var skip_reason = ''
var _tests = {}
var _test_order = []
func _init(script_name):
name = script_name
func get_pass_count():
var count = 0
for key in _tests:
count += _tests[key].pass_texts.size()
return count
func get_fail_count():
var count = 0
for key in _tests:
count += _tests[key].fail_texts.size()
return count
func get_pending_count():
var count = 0
for key in _tests:
count += _tests[key].pending_texts.size()
return count
func get_passing_test_count():
var count = 0
for key in _tests:
if(_tests[key].is_passing()):
count += 1
return count
func get_failing_test_count():
var count = 0
for key in _tests:
if(_tests[key].is_failing()):
count += 1
return count
func get_risky_count():
var count = 0
if(was_skipped):
count = 1
else:
for key in _tests:
if(!_tests[key].did_something()):
count += 1
return count
func get_test_obj(obj_name):
if(!_tests.has(obj_name)):
var to_add = Test.new()
_tests[obj_name] = to_add
_test_order.append(obj_name)
var to_return = _tests[obj_name]
return to_return
func add_pass(test_name, reason):
var t = get_test_obj(test_name)
t.pass_texts.append(reason)
func add_fail(test_name, reason):
var t = get_test_obj(test_name)
t.fail_texts.append(reason)
func add_pending(test_name, reason):
var t = get_test_obj(test_name)
t.pending_texts.append(reason)
func get_tests():
return _tests
# ------------------------------------------------------------------------------
# Summary Class
#
# This class holds the results of all the test scripts and Inner Classes that
# were run.
# ------------------------------------------------------------------------------
var _scripts = []
func add_script(name):
_scripts.append(TestScript.new(name))
func get_scripts():
return _scripts
func get_current_script():
return _scripts[_scripts.size() - 1]
func add_test(test_name):
# print('-- test_name = ', test_name)
# print('-- current script = ', get_current_script())
# print('-- test_obj = ', get_current_script().get_test_obj(test_name))
return get_current_script().get_test_obj(test_name)
func add_pass(test_name, reason = ''):
get_current_script().add_pass(test_name, reason)
func add_fail(test_name, reason = ''):
get_current_script().add_fail(test_name, reason)
func add_pending(test_name, reason = ''):
get_current_script().add_pending(test_name, reason)
func get_test_text(test_name):
return test_name + "\n" + get_current_script().get_test_obj(test_name).to_s()
# Gets the count of unique script names minus the .<Inner Class Name> at the
# end. Used for displaying the number of scripts without including all the
# Inner Classes.
func get_non_inner_class_script_count():
var counter = load('res://addons/gut/thing_counter.gd').new()
for i in range(_scripts.size()):
var ext_loc = _scripts[i].name.rfind('.gd.')
var to_add = _scripts[i].name
if(ext_loc != -1):
to_add = _scripts[i].name.substr(0, ext_loc + 3)
counter.add(to_add)
return counter.get_unique_count()
func get_totals():
var totals = {
passing = 0,
pending = 0,
failing = 0,
risky = 0,
tests = 0,
scripts = 0,
passing_tests = 0,
failing_tests = 0
}
for i in range(_scripts.size()):
# assert totals
totals.passing += _scripts[i].get_pass_count()
totals.pending += _scripts[i].get_pending_count()
totals.failing += _scripts[i].get_fail_count()
# test totals
totals.tests += _scripts[i]._test_order.size()
totals.passing_tests += _scripts[i].get_passing_test_count()
totals.failing_tests += _scripts[i].get_failing_test_count()
totals.risky += _scripts[i].get_risky_count()
totals.scripts = get_non_inner_class_script_count()
return totals
func log_summary_text(lgr):
var orig_indent = lgr.get_indent_level()
var found_failing_or_pending = false
for s in range(_scripts.size()):
lgr.set_indent_level(0)
if(_scripts[s].was_skipped or _scripts[s].get_fail_count() > 0 or _scripts[s].get_pending_count() > 0):
lgr.log("\n" + _scripts[s].name, lgr.fmts.underline)
if(_scripts[s].was_skipped):
lgr.inc_indent()
var skip_msg = str('[Risky] Script was skipped: ', _scripts[s].skip_reason)
lgr.log(skip_msg, lgr.fmts.yellow)
lgr.dec_indent()
for t in range(_scripts[s]._test_order.size()):
var tname = _scripts[s]._test_order[t]
var test = _scripts[s].get_test_obj(tname)
if(!test.is_passing()):
found_failing_or_pending = true
lgr.log(str('- ', tname))
lgr.inc_indent()
for i in range(test.fail_texts.size()):
lgr.failed(test.fail_texts[i])
for i in range(test.pending_texts.size()):
lgr.pending(test.pending_texts[i])
if(!test.did_something()):
lgr.log('[Risky] Did not assert', lgr.fmts.yellow)
lgr.dec_indent()
lgr.set_indent_level(0)
if(!found_failing_or_pending):
lgr.log('All tests passed', lgr.fmts.green)
# just picked a non-printable char, dunno if it is a good or bad choice.
var npws = PackedByteArray([31]).get_string_from_ascii()
lgr.log()
var _totals = get_totals()
lgr.log("Totals", lgr.fmts.yellow)
lgr.log(str('Scripts: ', get_non_inner_class_script_count()))
lgr.log(str('Passing tests ', _totals.passing_tests))
lgr.log(str('Failing tests ', _totals.failing_tests))
lgr.log(str('Risky tests ', _totals.risky))
var pnd=str('Pending: ', _totals.pending)
# add a non printable character so this "pending" isn't highlighted in the
# editor's output panel.
lgr.log(str(npws, pnd))
lgr.log(str('Asserts: ', _totals.passing, ' of ', _totals.passing + _totals.failing, ' passed'))
lgr.set_indent_level(orig_indent)