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

366 lines
9.1 KiB
GDScript

# ##############################################################################
#(G)odot (U)nit (T)est class
#
# ##############################################################################
# The MIT License (MIT)
# =====================
#
# Copyright (c) 2020 Tom "Butch" Wesley
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ##############################################################################
# This class wraps around the various printers and supplies formatting for the
# various message types (error, warning, etc).
# ##############################################################################
var types = {
debug = 'debug',
deprecated = 'deprecated',
error = 'error',
failed = 'failed',
info = 'info',
normal = 'normal',
orphan = 'orphan',
passed = 'passed',
pending = 'pending',
warn ='warn',
}
var fmts = {
red = 'red',
yellow = 'yellow',
green = 'green',
bold = 'bold',
underline = 'underline',
none = null
}
var _type_data = {
types.debug: {disp='DEBUG', enabled=true, fmt=fmts.none},
types.deprecated: {disp='DEPRECATED', enabled=true, fmt=fmts.none},
types.error: {disp='ERROR', enabled=true, fmt=fmts.red},
types.failed: {disp='Failed', enabled=true, fmt=fmts.red},
types.info: {disp='INFO', enabled=true, fmt=fmts.bold},
types.normal: {disp='NORMAL', enabled=true, fmt=fmts.none},
types.orphan: {disp='Orphans', enabled=true, fmt=fmts.yellow},
types.passed: {disp='Passed', enabled=true, fmt=fmts.green},
types.pending: {disp='Pending', enabled=true, fmt=fmts.yellow},
types.warn: {disp='WARNING', enabled=true, fmt=fmts.yellow},
}
var _logs = {
types.warn: [],
types.error: [],
types.info: [],
types.debug: [],
types.deprecated: [],
}
var _printers = {
terminal = null,
gui = null,
console = null
}
var _gut = null
var _utils = null
var _indent_level = 0
var _indent_string = ' '
var _skip_test_name_for_testing = false
var _less_test_names = false
var _yield_calls = 0
var _last_yield_text = ''
func _init():
_utils = load('res://addons/gut/utils.gd').get_instance()
_printers.terminal = _utils.Printers.TerminalPrinter.new()
_printers.console = _utils.Printers.ConsolePrinter.new()
# There were some problems in the timing of disabling this at the right
# time in gut_cmdln so it is disabled by default. This is enabled
# by plugin_control.gd based on settings.
_printers.console.set_disabled(true)
func get_indent_text():
var pad = ''
for i in range(_indent_level):
pad += _indent_string
return pad
func _indent_text(text):
var to_return = text
var ending_newline = ''
if(text.ends_with("\n")):
ending_newline = "\n"
to_return = to_return.left(to_return.length() -1)
var pad = get_indent_text()
to_return = to_return.replace("\n", "\n" + pad)
to_return += ending_newline
return pad + to_return
func _should_print_to_printer(key_name):
return _printers[key_name] != null and !_printers[key_name].get_disabled()
func _print_test_name():
if(_gut == null):
return
var cur_test = _gut.get_current_test_object()
if(cur_test == null):
return false
if(!cur_test.has_printed_name):
_output('* ' + cur_test.name + "\n")
cur_test.has_printed_name = true
func _output(text, fmt=null):
for key in _printers:
if(_should_print_to_printer(key)):
var info = ''#str(self, ':', key, ':', _printers[key], '| ')
_printers[key].send(info + text, fmt)
func _log(text, fmt=fmts.none):
_print_test_name()
var indented = _indent_text(text)
_output(indented, fmt)
# ---------------
# Get Methods
# ---------------
func get_warnings():
return get_log_entries(types.warn)
func get_errors():
return get_log_entries(types.error)
func get_infos():
return get_log_entries(types.info)
func get_debugs():
return get_log_entries(types.debug)
func get_deprecated():
return get_log_entries(types.deprecated)
func get_count(log_type=null):
var count = 0
if(log_type == null):
for key in _logs:
count += _logs[key].size()
else:
count = _logs[log_type].size()
return count
func get_log_entries(log_type):
return _logs[log_type]
# ---------------
# Log methods
# ---------------
func _output_type(type, text):
var td = _type_data[type]
if(!td.enabled):
return
_print_test_name()
if(type != types.normal):
if(_logs.has(type)):
_logs[type].append(text)
var start = str('[', td.disp, ']')
if(text != null and text != ''):
start += ': '
else:
start += ' '
var indented_start = _indent_text(start)
var indented_end = _indent_text(text)
indented_end = indented_end.lstrip(_indent_string)
_output(indented_start, td.fmt)
_output(indented_end + "\n")
func debug(text):
_output_type(types.debug, text)
# supply some text or the name of the deprecated method and the replacement.
func deprecated(text, alt_method=null):
var msg = text
if(alt_method):
msg = str('The method ', text, ' is deprecated, use ', alt_method , ' instead.')
return _output_type(types.deprecated, msg)
func error(text):
_output_type(types.error, text)
func failed(text):
_output_type(types.failed, text)
func info(text):
_output_type(types.info, text)
func orphan(text):
_output_type(types.orphan, text)
func passed(text):
_output_type(types.passed, text)
func pending(text):
_output_type(types.pending, text)
func warn(text):
_output_type(types.warn, text)
func log(text='', fmt=fmts.none):
end_yield()
if(text == ''):
_output("\n")
else:
_log(text + "\n", fmt)
return null
func lograw(text, fmt=fmts.none):
return _output(text, fmt)
# Print the test name if we aren't skipping names of tests that pass (basically
# what _less_test_names means))
func log_test_name():
# suppress output if we haven't printed the test name yet and
# what to print is the test name.
if(!_less_test_names):
_print_test_name()
# ---------------
# Misc
# ---------------
func get_gut():
return _gut
func set_gut(gut):
_gut = gut
if(_gut == null):
_printers.gui = null
else:
if(_printers.gui == null):
_printers.gui = _utils.Printers.GutGuiPrinter.new()
func get_indent_level():
return _indent_level
func set_indent_level(indent_level):
_indent_level = indent_level
func get_indent_string():
return _indent_string
func set_indent_string(indent_string):
_indent_string = indent_string
func clear():
for key in _logs:
_logs[key].clear()
func inc_indent():
_indent_level += 1
func dec_indent():
_indent_level = max(0, _indent_level -1)
func is_type_enabled(type):
return _type_data[type].enabled
func set_type_enabled(type, is_enabled):
_type_data[type].enabled = is_enabled
func get_less_test_names():
return _less_test_names
func set_less_test_names(less_test_names):
_less_test_names = less_test_names
func disable_printer(name, is_disabled):
_printers[name].set_disabled(is_disabled)
func is_printer_disabled(name):
return _printers[name].get_disabled()
func disable_formatting(is_disabled):
for key in _printers:
_printers[key].set_format_enabled(!is_disabled)
func disable_all_printers(is_disabled):
for p in _printers:
disable_printer(p, is_disabled)
func get_printer(printer_key):
return _printers[printer_key]
func _yield_text_terminal(text):
var printer = _printers['terminal']
if(_yield_calls != 0):
printer.clear_line()
printer.back(_last_yield_text.length())
printer.send(text, fmts.yellow)
func _end_yield_terminal():
var printer = _printers['terminal']
printer.clear_line()
printer.back(_last_yield_text.length())
func _yield_text_gui(text):
pass
# var lbl = _gut.get_gui().get_waiting_label()
# lbl.visible = true
# lbl.set_bbcode('[color=yellow]' + text + '[/color]')
func _end_yield_gui():
pass
# var lbl = _gut.get_gui().get_waiting_label()
# lbl.visible = false
# lbl.set_text('')
# This is used for displaying the "yield detected" and "yielding to" messages.
func yield_msg(text):
if(_type_data.warn.enabled):
self.log(text, fmts.yellow)
# This is used for the animated "waiting" message
func yield_text(text):
_yield_text_terminal(text)
_yield_text_gui(text)
_last_yield_text = text
_yield_calls += 1
# This is used for the animated "waiting" message
func end_yield():
if(_yield_calls == 0):
return
_end_yield_terminal()
_end_yield_gui()
_yield_calls = 0
_last_yield_text = ''
func get_gui_bbcode():
return _printers.gui.get_bbcode()