forked from loafle/openapi-generator-original
* 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>
322 lines
9.1 KiB
GDScript
322 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.
|
|
#
|
|
# ##############################################################################
|
|
# Description
|
|
# -----------
|
|
# ##############################################################################
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# A stroke of genius if I do say so. This allows for doubling a scene without
|
|
# having to write any files. By overloading the "instantiate" method we can
|
|
# make whatever we want.
|
|
# ------------------------------------------------------------------------------
|
|
class PackedSceneDouble:
|
|
extends PackedScene
|
|
var _script = null
|
|
var _scene = null
|
|
|
|
func set_script_obj(obj):
|
|
_script = obj
|
|
|
|
func instantiate(edit_state=0):
|
|
var inst = _scene.instantiate(edit_state)
|
|
if(_script != null):
|
|
inst.set_script(_script)
|
|
return inst
|
|
|
|
func load_scene(path):
|
|
_scene = load(path)
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# START Doubler
|
|
# ------------------------------------------------------------------------------
|
|
var _utils = load('res://addons/gut/utils.gd').get_instance()
|
|
var _base_script_text = _utils.get_file_as_text('res://addons/gut/double_templates/script_template.txt')
|
|
var _script_collector = _utils.ScriptCollector.new()
|
|
# used by tests for debugging purposes.
|
|
var print_source = false
|
|
var inner_class_registry = _utils.InnerClassRegistry.new()
|
|
|
|
# ###############
|
|
# Properties
|
|
# ###############
|
|
var _stubber = _utils.Stubber.new()
|
|
func get_stubber():
|
|
return _stubber
|
|
func set_stubber(stubber):
|
|
_stubber = stubber
|
|
|
|
var _lgr = _utils.get_logger()
|
|
func get_logger():
|
|
return _lgr
|
|
func set_logger(logger):
|
|
_lgr = logger
|
|
_method_maker.set_logger(logger)
|
|
|
|
var _spy = null
|
|
func get_spy():
|
|
return _spy
|
|
func set_spy(spy):
|
|
_spy = spy
|
|
|
|
var _gut = null
|
|
func get_gut():
|
|
return _gut
|
|
func set_gut(gut):
|
|
_gut = gut
|
|
|
|
var _strategy = null
|
|
func get_strategy():
|
|
return _strategy
|
|
func set_strategy(strategy):
|
|
_strategy = strategy
|
|
|
|
|
|
var _method_maker = _utils.MethodMaker.new()
|
|
func get_method_maker():
|
|
return _method_maker
|
|
|
|
var _ignored_methods = _utils.OneToMany.new()
|
|
func get_ignored_methods():
|
|
return _ignored_methods
|
|
|
|
# ###############
|
|
# Private
|
|
# ###############
|
|
func _init(strategy=_utils.DOUBLE_STRATEGY.SCRIPT_ONLY):
|
|
set_logger(_utils.get_logger())
|
|
_strategy = strategy
|
|
|
|
|
|
func _get_indented_line(indents, text):
|
|
var to_return = ''
|
|
for _i in range(indents):
|
|
to_return += "\t"
|
|
return str(to_return, text, "\n")
|
|
|
|
|
|
func _stub_to_call_super(parsed, method_name):
|
|
if(_utils.non_super_methods.has(method_name)):
|
|
return
|
|
|
|
var params = _utils.StubParams.new(parsed.script_path, method_name, parsed.subpath)
|
|
params.to_call_super()
|
|
_stubber.add_stub(params)
|
|
|
|
|
|
func _get_base_script_text(parsed, override_path, partial):
|
|
var path = parsed.script_path
|
|
if(override_path != null):
|
|
path = override_path
|
|
|
|
var stubber_id = -1
|
|
if(_stubber != null):
|
|
stubber_id = _stubber.get_instance_id()
|
|
|
|
var spy_id = -1
|
|
if(_spy != null):
|
|
spy_id = _spy.get_instance_id()
|
|
|
|
var gut_id = -1
|
|
if(_gut != null):
|
|
gut_id = _gut.get_instance_id()
|
|
|
|
var extends_text = parsed.get_extends_text()
|
|
|
|
var values = {
|
|
# Top sections
|
|
"extends":extends_text,
|
|
"constants":'',#obj_info.get_constants_text(),
|
|
"properties":'',#obj_info.get_properties_text(),
|
|
|
|
# metadata values
|
|
"path":path,
|
|
"subpath":_utils.nvl(parsed.subpath, ''),
|
|
"stubber_id":stubber_id,
|
|
"spy_id":spy_id,
|
|
"gut_id":gut_id,
|
|
"singleton_name":'',#_utils.nvl(obj_info.get_singleton_name(), ''),
|
|
"is_partial":partial,
|
|
}
|
|
|
|
return _base_script_text.format(values)
|
|
|
|
|
|
func _is_valid_double_method(parsed_script, parsed_method):
|
|
return !parsed_method.is_accessor() and \
|
|
!parsed_method.is_black_listed() and \
|
|
!_ignored_methods.has(parsed_script.resource, parsed_method.meta.name)
|
|
|
|
func _create_double(parsed, strategy, override_path, partial):
|
|
var base_script = _get_base_script_text(parsed, override_path, partial)
|
|
var super_name = ""
|
|
var path = ""
|
|
|
|
path = parsed.script_path
|
|
var dbl_src = ""
|
|
dbl_src += base_script
|
|
|
|
for method in parsed.get_local_methods():
|
|
if(_is_valid_double_method(parsed, method)):
|
|
var mthd = parsed.get_local_method(method.meta.name)
|
|
if(parsed.is_native):
|
|
dbl_src += _get_func_text(method.meta, parsed.resource, super_name)
|
|
else:
|
|
dbl_src += _get_func_text(method.meta, path, super_name)
|
|
|
|
if(strategy == _utils.DOUBLE_STRATEGY.INCLUDE_SUPER):
|
|
for method in parsed.get_super_methods():
|
|
if(_is_valid_double_method(parsed, method)):
|
|
_stub_to_call_super(parsed, method.meta.name)
|
|
if(parsed.is_native):
|
|
dbl_src += _get_func_text(method.meta, parsed.resource, super_name)
|
|
else:
|
|
dbl_src += _get_func_text(method.meta, path, super_name)
|
|
|
|
if(print_source):
|
|
print(_utils.add_line_numbers(dbl_src))
|
|
|
|
var DblClass = _utils.create_script_from_source(dbl_src)
|
|
if(_stubber != null):
|
|
_stub_method_default_values(DblClass, parsed, strategy)
|
|
|
|
return DblClass
|
|
|
|
|
|
func _stub_method_default_values(which, parsed, strategy):
|
|
for method in parsed.get_local_methods():
|
|
if(!method.is_black_listed() && !_ignored_methods.has(parsed.resource, method.meta.name)):
|
|
_stubber.stub_defaults_from_meta(parsed.script_path, method.meta)
|
|
|
|
|
|
|
|
func _double_scene_and_script(scene, strategy, partial):
|
|
var to_return = PackedSceneDouble.new()
|
|
to_return.load_scene(scene.get_path())
|
|
|
|
var script_obj = _utils.get_scene_script_object(scene)
|
|
if(script_obj != null):
|
|
var script_dbl = null
|
|
if(partial):
|
|
script_dbl = _partial_double(script_obj, strategy, scene.get_path())
|
|
else:
|
|
script_dbl = _double(script_obj, strategy, scene.get_path())
|
|
to_return.set_script_obj(script_dbl)
|
|
|
|
return to_return
|
|
|
|
|
|
func _get_inst_id_ref_str(inst):
|
|
var ref_str = 'null'
|
|
if(inst):
|
|
ref_str = str('instance_from_id(', inst.get_instance_id(),')')
|
|
return ref_str
|
|
|
|
|
|
func _get_func_text(method_hash, path, super_=""):
|
|
var override_count = null;
|
|
if(_stubber != null):
|
|
override_count = _stubber.get_parameter_count(path, method_hash.name)
|
|
|
|
var text = _method_maker.get_function_text(method_hash, path, override_count, super_) + "\n"
|
|
|
|
return text
|
|
|
|
|
|
func _parse_script(obj):
|
|
var parsed = null
|
|
|
|
if(_utils.is_inner_class(obj)):
|
|
if(inner_class_registry.has(obj)):
|
|
parsed = _script_collector.parse(inner_class_registry.get_base_resource(obj), obj)
|
|
else:
|
|
_lgr.error('Doubling Inner Classes requires you register them first. Call register_inner_classes passing the script that contains the inner class.')
|
|
else:
|
|
parsed = _script_collector.parse(obj)
|
|
|
|
return parsed
|
|
|
|
|
|
# Override path is used with scenes.
|
|
func _double(obj, strategy, override_path=null):
|
|
var parsed = _parse_script(obj)
|
|
if(parsed != null):
|
|
return _create_double(parsed, strategy, override_path, false)
|
|
|
|
|
|
func _partial_double(obj, strategy, override_path=null):
|
|
var parsed = _parse_script(obj)
|
|
if(parsed != null):
|
|
return _create_double(parsed, strategy, override_path, true)
|
|
|
|
|
|
# -------------------------
|
|
# Public
|
|
# -------------------------
|
|
|
|
# double a script/object
|
|
func double(obj, strategy=_strategy):
|
|
return _double(obj, strategy)
|
|
|
|
func partial_double(obj, strategy=_strategy):
|
|
return _partial_double(obj, strategy)
|
|
|
|
|
|
# double a scene
|
|
func double_scene(scene, strategy=_strategy):
|
|
return _double_scene_and_script(scene, strategy, false)
|
|
|
|
func partial_double_scene(scene, strategy=_strategy):
|
|
return _double_scene_and_script(scene, strategy, true)
|
|
|
|
|
|
func double_gdnative(which):
|
|
return _double(which, _utils.DOUBLE_STRATEGY.INCLUDE_SUPER)
|
|
|
|
func partial_double_gdnative(which):
|
|
return _partial_double(which, _utils.DOUBLE_STRATEGY.INCLUDE_SUPER)
|
|
|
|
|
|
func double_inner(parent, inner, strategy=_strategy):
|
|
var parsed = _script_collector.parse(parent, inner)
|
|
return _create_double(parsed, strategy, null, false)
|
|
|
|
|
|
func partial_double_inner(parent, inner, strategy=_strategy):
|
|
var parsed = _script_collector.parse(parent, inner)
|
|
return _create_double(parsed, strategy, null, true)
|
|
|
|
|
|
func add_ignored_method(obj, method_name):
|
|
_ignored_methods.add(obj, method_name)
|