# See the License for the specific language governing permissions and
# limitations under the License.
-import ::android::platform
+import ::android
import ::android::vibration
import asteronits
if dy > 0.0 then
# Bottom part of the joystick, turns left or right
if dx < 0.0 then
- ship.applied_rotation = -1.0
- else
ship.applied_rotation = 1.0
+ else
+ ship.applied_rotation = -1.0
end
else
# Upper part of the joystick, detect action using 45d angles
if dx < dy then
- ship.applied_rotation = -1.0
- else if dx > -dy then
ship.applied_rotation = 1.0
+ else if dx > -dy then
+ ship.applied_rotation = -1.0
else
ship.applied_thrust = 1.0
end
# Add the joystick to the UI
ui_sprites.add new Sprite(spritesheet_controls.forward,
- ui_camera.bottom_left.offset(joystick_x, -200.0, 0.0))
+ ui_camera.bottom_left.offset(joystick_x, 200.0, 0.0))
ui_sprites.add new Sprite(spritesheet_controls.left,
- ui_camera.bottom_left.offset(joystick_x-100.0, -joystick_y, 0.0))
+ ui_camera.bottom_left.offset(joystick_x-100.0, joystick_y, 0.0))
ui_sprites.add new Sprite(spritesheet_controls.right,
- ui_camera.bottom_left.offset(joystick_x+100.0, -joystick_y, 0.0))
+ ui_camera.bottom_left.offset(joystick_x+100.0, joystick_y, 0.0))
# Purely cosmetic joystick background
ui_sprites.add new Sprite(spritesheet_controls.joystick_back,
- ui_camera.bottom_left.offset(joystick_x, -joystick_y, -1.0)) # In the back
+ ui_camera.bottom_left.offset(joystick_x, joystick_y, -1.0)) # In the back
ui_sprites.add new Sprite(spritesheet_controls.joystick_down,
ui_camera.bottom_left.offset(joystick_x, 0.0, 1.0))
# Add the "open fire" button
ui_sprites.add new Sprite(spritesheet_controls.fire,
- ui_camera.bottom_right.offset(-150.0, -150.0, 0.0))
+ ui_camera.bottom_right.offset(-150.0, 150.0, 0.0))
end
end
import tnitter_app
-import android::ui
-import android::http_request
+import android
import android::portrait
redef class LabelAuthor
module android_calculator
import calculator
-import android::ui
+import android
redef class Button
init do set_android_style(native, (text or else "?").is_int)
only be used by low-level implementations of Nit on Android.
Its usefulness will be extended in the future to customize user applications.
-## Project entry points
+## Android implementation
+
+There is two core implementation for Nit apps on Android.
+`android::nit_activity` is used by apps with standard windows and native UI controls.
+`android::game` is used by, well, games and the game frameworks `mnit` and `gamnit`.
+
+Clients don't have to select the core implementation, it is imported by other relevant modules.
+For example, a module importing `app::ui` and `android` will trigger the importation of `android::nit_activity`.
+
+## Lock app orientation
Importing `android::landscape` or `android::portrait` locks the generated
application in the specified orientation. This can be useful for games and
module android
import platform
-import native_app_glue
import dalvik
private import log
-private import assets
-
-redef class App
- redef fun init_window
- do
- super
- on_create
- on_restore_state
- on_start
- end
-
- redef fun term_window
- do
- super
- on_stop
- end
-
- # Is the application currently paused?
- var paused = true
-
- redef fun pause
- do
- paused = true
- on_pause
- super
- end
-
- redef fun resume
- do
- paused = false
- on_resume
- super
- end
-
- redef fun save_state do on_save_state
-
- redef fun lost_focus
- do
- paused = true
- super
- end
-
- redef fun gained_focus
- do
- paused = false
- super
- end
-
- redef fun destroy do on_destroy
-end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Android services and implementation of app.nit for gamnit and mnit
+module game
+
+import platform
+import native_app_glue
+import dalvik
+private import log
+private import assets
+
+redef class App
+ redef fun init_window
+ do
+ super
+ on_create
+ on_restore_state
+ on_start
+ end
+
+ redef fun term_window
+ do
+ super
+ on_stop
+ end
+
+ # Is the application currently paused?
+ var paused = true
+
+ redef fun pause
+ do
+ paused = true
+ on_pause
+ super
+ end
+
+ redef fun resume
+ do
+ paused = false
+ on_resume
+ super
+ end
+
+ redef fun save_state do on_save_state
+
+ redef fun lost_focus
+ do
+ paused = true
+ super
+ end
+
+ redef fun gained_focus
+ do
+ paused = false
+ super
+ end
+
+ redef fun destroy do on_destroy
+end
module input_events
import mnit::input
-import android
+import android::game
in "C header" `{
#include <android/log.h>
android_manifest_activity """android:screenOrientation="sensorLandscape" """
end
-import platform
+import android
android:screenOrientation="portrait"
"""
-import platform
+import android
# See the License for the specific language governing permissions and
# limitations under the License.
-# This module is used to manipulate android sensors
-# The sensor support is implemented in android_app module, so the user can enable the type of sensor he wants to use.
-# There is an example of how you can use the android sensors in nit/examples/mnit_ballz :
+# Access Android sensors
+#
+# Sensors are to be enabled when `App` is created.
+# The following example enables all sensors.
+# The events (`SensorEvent`, `ASensorAccelerometer`, `ASensorMagneticField`...)
+# are sent to the `input` callback of `App`
#
# ~~~~nitish
-# #FIXME rewrite the example
# redef class App
-# sensors_support_enabled = true
-# accelerometer.enabled = true
-# accelerometer.eventrate = 10000
-# magnetic_field.enabled = true
-# gyroscope.enabled = true
-# light.enabled = true
-# proximity.enabled = true
+# init
+# do
+# sensors_support_enabled = true
+# accelerometer.enabled = true
+# accelerometer.eventrate = 10000
+# magnetic_field.enabled = true
+# gyroscope.enabled = true
+# light.enabled = true
+# proximity.enabled = true
+# end
# end
# ~~~~
-#
-# In this example, we enable the sensor support, then enable all types of sensors supported by the API, directly with `App` attributes
-# As a result, you get all type of SensorEvent (ASensorAccelerometer, ASensorMagneticField ...) in the `input` callback of `App`
module sensors
-import android
+import game
import mnit
in "C header" `{
redef class CheckBox
redef type NATIVE: Android_widget_CompoundButton
redef var native do return (new Android_widget_CheckBox(app.native_activity)).new_global_ref
+ init do set_callback_on_toggle(native)
redef fun is_checked do return native.is_checked
redef fun is_checked=(value) do native.set_checked(value)
+
+ private fun on_toggle do notify_observers new ToggleEvent(self)
+
+ private fun set_callback_on_toggle(view: NATIVE)
+ import on_toggle in "Java" `{
+ final int final_sender_object = self;
+ CheckBox_incr_ref(final_sender_object);
+
+ view.setOnCheckedChangeListener(
+ new android.widget.CompoundButton.OnCheckedChangeListener() {
+ @Override
+ public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
+ CheckBox_on_toggle(final_sender_object);
+ }
+ });
+ `}
end
redef class TextInput
import core::error
# Platform variations
-# TODO: move on the platform once qualified names are understand in the condition
import linux::audio is conditional(linux)
import android::audio is conditional(android)
import serialization
# Platform variations
-# TODO: move on the platform once qualified names are understand in the condition
import linux::data_store is conditional(linux)
import android::data_store is conditional(android)
import ios::data_store is conditional(ios)
import app_base
# Platform variations
-# TODO: move on the platform once qualified names are understand in the condition
import linux::ui is conditional(linux)
-import android::ui is conditional(android) # FIXME it should be conditional to `android::platform`
+import android::ui is conditional(android)
import ios::ui is conditional(ios)
redef class App
var is_checked = false is writable
end
+# Event sent from a `VIEW`
+class ViewEvent
+ super AppEvent
+
+ # The `VIEW` that raised this event
+ var sender: VIEW
+
+ # Type of the `sender`
+ type VIEW: View
+end
+
# A `Button` press event
class ButtonPressEvent
- super AppEvent
+ super ViewEvent
+
+ redef type VIEW: Button
+end
+
+# The `CheckBox` `sender` has been toggled
+class ToggleEvent
+ super ViewEvent
- # The `Button` that raised this event
- var sender: Button
+ redef type VIEW: CheckBox
end
# A layout to visually organize `Control`s
end
redef fun substring(from, count) do
+ var ln = _length
+ if count <= 0 then return ""
+ if (count + from) > ln then count = ln - from
if count <= 0 then return ""
-
if from < 0 then
count += from
- if count < 0 then return ""
+ if count <= 0 then return ""
from = 0
end
- var ln = _length
- if (count + from) > ln then count = ln - from
return new ASCIIFlatString.full_data(_items, count, from + _first_byte, count)
end
android_manifest """<uses-feature android:glEsVersion="0x00020000"/>"""
end
-import ::android
+import ::android::game
intrude import android::load_image
private import gamnit::egl
@interface NitCallbackReference: NSObject
// Nit object target of the callbacks from UI events
- @property (nonatomic) Button nit_button;
+ @property (nonatomic) View nit_view;
// Actual callback method
- -(void) nitOnEvent: (UIButton*) sender;
+ -(void) nitOnEvent: (UIView*) sender;
@end
@implementation NitCallbackReference
- -(void) nitOnEvent: (UIButton*) sender {
- Button_on_click(self.nit_button);
+ -(void) nitOnEvent: (UIView*) sender {
+ View_on_ios_event(self.nit_view);
}
@end
redef type NATIVE: UIView
redef var enabled = null is lazy
+
+ private fun on_ios_event do end
end
redef class CompositeControl
# `UISwitch` acting as the real check box
var ui_switch: UISwitch is noautoinit
- init do
+ redef fun on_ios_event do notify_observers new ToggleEvent(self)
+
+ init
+ do
# Tweak the layout so it is centered
layout.native.distribution = new UIStackViewDistribution.fill_proportionally
layout.native.alignment = new UIStackViewAlignment.center
var s = new UISwitch
native.add_arranged_subview s
ui_switch = s
+
+ ui_switch.set_callback self
end
redef fun text=(text) do lbl.text = text
redef fun is_checked=(value) do ui_switch.set_on_animated(value, true)
end
+redef class UISwitch
+ # Register callbacks on this switch to be relayed to `sender`
+ private fun set_callback(sender: View)
+ import View.on_ios_event in "ObjC" `{
+
+ NitCallbackReference *ncr = [[NitCallbackReference alloc] init];
+ ncr.nit_view = sender;
+
+ // Pin the objects in both Objective-C and Nit GC
+ View_incr_ref(sender);
+ ncr = (__bridge NitCallbackReference*)CFBridgingRetain(ncr);
+
+ [self addTarget:ncr action:@selector(nitOnEvent:)
+ forControlEvents:UIControlEventValueChanged];
+ `}
+end
+
redef class TextInput
redef type NATIVE: UITextField
init do native.set_callback self
+ redef fun on_ios_event do notify_observers new ButtonPressEvent(self)
+
redef fun text=(text) do if text != null then native.title = text.to_nsstring
redef fun text do return native.current_title.to_s
- private fun on_click do notify_observers new ButtonPressEvent(self)
-
redef fun enabled=(enabled) do native.enabled = enabled or else true
redef fun enabled do return native.enabled
end
redef class UIButton
# Register callbacks on this button to be relayed to `sender`
- private fun set_callback(sender: Button)
- import Button.on_click in "ObjC" `{
+ private fun set_callback(sender: View)
+ import View.on_ios_event in "ObjC" `{
NitCallbackReference *ncr = [[NitCallbackReference alloc] init];
- ncr.nit_button = sender;
+ ncr.nit_view = sender;
// Pin the objects in both Objective-C and Nit GC
- Button_incr_ref(sender);
+ View_incr_ref(sender);
ncr = (__bridge NitCallbackReference*)CFBridgingRetain(ncr);
[self addTarget:ncr action:@selector(nitOnEvent:)
redef type NATIVE: GtkCheckButton
redef var native = new GtkCheckButton
+ redef fun signal(sender, data) do notify_observers new ToggleEvent(self)
+ init do native.signal_connect("toggled", self, null)
+
redef fun text do return native.text
redef fun text=(value) do native.text = (value or else "").to_s
import mnit
import mnit::opengles1
-import ::android
+import ::android::game
intrude import ::android::input_events
in "C" `{
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2015-2016 Guilherme Mansur <guilhermerpmansur@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module native_postgres is pkgconfig("libpq")
+
+in "C header" `{
+ #include <libpq-fe.h>
+`}
+
+extern class ExecStatusType `{int`}
+ new empty `{ return PGRES_EMPTY_QUERY; `}
+ new command_ok `{ return PGRES_COMMAND_OK; `}
+ new tuples_ok `{ return PGRES_TUPLES_OK; `}
+ new copy_out `{ return PGRES_COPY_OUT; `}
+ new copy_in `{ return PGRES_COPY_IN; `}
+ new bad_response `{ return PGRES_BAD_RESPONSE; `}
+ new nonfatal_error `{ return PGRES_NONFATAL_ERROR; `}
+ new fatal_error `{ return PGRES_FATAL_ERROR; `}
+
+ fun is_ok: Bool `{return self == PGRES_TUPLES_OK || self == PGRES_COMMAND_OK; `}
+
+ redef fun to_s import NativeString.to_s `{
+ char * err = PQresStatus(self);
+ if(err == NULL) err = "";
+ return NativeString_to_s(err);
+ `}
+end
+
+extern class ConnStatusType `{int`}
+ new connection_ok `{ return CONNECTION_OK; `}
+ new connection_bad `{ return CONNECTION_BAD; `}
+
+ fun is_ok: Bool `{return self == CONNECTION_OK; `}
+end
+
+extern class PGResult `{PGresult *`}
+ # Frees the memory block associated with the result
+ fun clear `{PQclear(self); `}
+
+ # Returns the number of rows in the query result
+ fun ntuples:Int `{ return PQntuples(self); `}
+
+ # Returns the number of columns in each row of the query result
+ fun nfields:Int `{return PQnfields(self); `}
+
+ # Returns the ExecStatusType of a result
+ fun status: ExecStatusType `{ return PQresultStatus(self); `}
+
+ # Returns the field name of a given column_number
+ fun fname(column_number:Int):String import NativeString.to_s `{
+ return NativeString_to_s( PQfname(self, column_number));
+ `}
+
+ # Returns the column number associated with the column name
+ fun fnumber(column_name:String):Int import String.to_cstring `{
+ return PQfnumber(self, String_to_cstring(column_name));
+ `}
+
+ # Returns a single field value of one row of the result at row_number, column_number
+ fun value(row_number:Int, column_number:Int):String import NativeString.to_s `{
+ return NativeString_to_s(PQgetvalue(self, row_number, column_number));
+ `}
+
+ # Tests wether a field is a null value
+ fun is_null(row_number:Int, column_number: Int): Bool `{
+ return PQgetisnull(self, row_number, column_number);
+ `}
+
+end
+extern class NativePostgres `{PGconn *`}
+
+ # Connect to a new database using the conninfo string as a parameter
+ new connectdb(conninfo: String) import String.to_cstring `{
+ PGconn * self = NULL;
+ self = PQconnectdb(String_to_cstring(conninfo));
+ return self;
+ `}
+
+ # Submits a query to the server and waits for the result returns the ExecStatustype of the query
+ fun exec(query: String): PGResult import String.to_cstring `{
+ PGresult *res = PQexec(self, String_to_cstring(query));
+ return res;
+ `}
+
+ # Prepares a statement with the given parameters
+ fun prepare(stmt: String, query: String, nParams: Int):PGResult import String.to_cstring `{
+ const char * stmtName = String_to_cstring(stmt);
+ const char * queryStr = String_to_cstring(query);
+ PGresult * res = PQprepare(self, stmtName, queryStr, nParams, NULL);
+ return res;
+ `}
+
+ fun exec_prepared(stmt: String, nParams: Int, values: Array[String], pLengths: Array[Int], pFormats: Array[Int], resultFormat: Int):PGResult import String.to_cstring, Array[String].[], Array[Int].[] `{
+ const char * stmtName = String_to_cstring(stmt);
+ const char * paramValues[nParams];
+ int paramLengths[nParams];
+ int paramFormats[nParams];
+ int i;
+ for(i = 0; i < nParams; i++)
+ paramValues[i] = String_to_cstring(Array_of_String__index(values, i));
+ for(i = 0; i < nParams; i++)
+ paramLengths[i] = Array_of_Int__index(pLengths, i);
+ for(i = 0; i < nParams; i++)
+ paramFormats[i] = Array_of_Int__index(pFormats, i);
+ PGresult * res = PQexecPrepared(self, stmtName, nParams, paramValues, paramLengths, paramFormats, resultFormat);
+ return res;
+ `}
+
+ # Returns the error message of the last operation on the connection
+ fun error: String import NativeString.to_s `{
+ char * error = PQerrorMessage(self);
+ return NativeString_to_s(error);
+ `}
+
+ # Returns the status of this connection
+ fun status: ConnStatusType `{
+ return PQstatus(self);
+ `}
+
+ # Closes the connection to the server
+ fun finish `{
+ PQfinish(self);
+ `}
+
+ # Closes the connection to the server and attempts to reconnect with the previously used params
+ fun reset `{
+ PQreset(self);
+ `}
+end
--- /dev/null
+[package]
+name=postgresql
+tags=database,lib
+maintainer=Guilherme Mansur <guilhermerpmansur@gmail.com>
+license=Apache-2.0
+[upstream]
+browse=https://github.com/nitlang/nit/tree/master/lib/postgresql/
+git=https://github.com/nitlang/nit.git
+git.directory=lib/postgresql/
+homepage=http://nitlanguage.org
+issues=https://github.com/nitlang/nit/issues
\ No newline at end of file
[package]
name=rubix
tags=algo,lib
-maintainer=Lucas Bajolet<lucas.bajolet@hotmail.com>
+maintainer=Lucas Bajolet<r4pass@hotmail.com>
license=Apache-2.0
[upstream]
browse=https://github.com/nitlang/nit/tree/master/lib/rubix.nit
$ nitunit foo.md
+When testing, the environment variable `NIT_TESTING` is set to `true`.
+This flag can be used by libraries and program to prevent (or limit) the execution of dangerous pieces of code.
+
+~~~~~
+# NIT_TESTING is automatically set.
+#
+# assert "NIT_TESTING".environ == "true"
+~~~~
+
## Working with `TestSuites`
TestSuites are Nit files that define a set of TestCases for a particular module.
import model_views
+redef class MEntity
+
+ # Collect modifier keywords like `redef`, `private` etc.
+ fun collect_modifiers: Array[String] do
+ return new Array[String]
+ end
+end
+
+redef class MPackage
+ redef fun collect_modifiers do
+ var res = super
+ res.add "package"
+ return res
+ end
+end
+
+redef class MGroup
+ redef fun collect_modifiers do
+ var res = super
+ res.add "group"
+ return res
+ end
+end
+
redef class MModule
+ redef fun collect_modifiers do
+ var res = super
+ res.add "module"
+ return res
+ end
+
# Collect all transitive imports.
fun collect_ancestors(view: ModelView): Set[MModule] do
var res = new HashSet[MModule]
redef class MClass
+ redef fun collect_modifiers do return intro.collect_modifiers
+
# Collect direct parents of `self` with `visibility >= to min_visibility`.
fun collect_parents(view: ModelView): Set[MClass] do
var res = new HashSet[MClass]
return res
end
- # Collect modifiers like redef, private etc.
- fun collect_modifiers: Array[String] do
- var res = new Array[String]
+ redef fun collect_modifiers do
+ var res = super
if not is_intro then
res.add "redef"
else
end
end
+redef class MProperty
+ redef fun collect_modifiers do return intro.collect_modifiers
+end
+
redef class MPropDef
- # Collect modifiers like redef, private, abstract, intern, fun etc.
- fun collect_modifiers: Array[String] do
- var res = new Array[String]
+ redef fun collect_modifiers do
+ var res = super
if not is_intro then
res.add "redef"
else
else
res.add "fun"
end
+ else if mprop isa MAttributeDef then
+ res.add "var"
end
return res
end
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Make model entities Jsonable.
+#
+# To avoid cycles, every reference from a MEntity to another is replaced by a
+# MEntityRef.
+#
+# How subobjects are retrieved using the MEntityRef is the responsability of the
+# client. Json objects can be returned as this or inflated with concrete objet
+# rather than the refs.
+#
+# TODO consider serialization module?
+module model_json
+
+import model::model_collect
+import json
+import loader
+
+# A reference to another mentity.
+class MEntityRef
+ super MEntity
+
+ # MEntity to link to.
+ var mentity: MEntity
+
+ # Return `self` as a Json Object.
+ #
+ # By default, MEntity references contain only the `full_name` of the Mentity.
+ # You should redefine this method in your client to implement a different behavior.
+ redef fun json do
+ var obj = new JsonObject
+ obj["full_name"] = mentity.full_name
+ return obj
+ end
+end
+
+redef class MEntity
+ super Jsonable
+
+ # Return `self` as a JsonObject.
+ #
+ # By default, every reference to another MEntity is replaced by a pointer
+ # to the MEntity::json_id.
+ fun json: JsonObject do
+ var obj = new JsonObject
+ obj["name"] = name
+ obj["class_name"] = class_name
+ obj["full_name"] = full_name
+ obj["mdoc"] = mdoc_or_fallback
+ var modifiers = new JsonArray
+ for modifier in collect_modifiers do
+ modifiers.add modifier
+ end
+ obj["modifiers"] = modifiers
+ return obj
+ end
+
+ redef fun to_json do return json.to_json
+end
+
+redef class MDoc
+ super Jsonable
+
+ # Return `self` as a JsonObject.
+ fun json: JsonObject do
+ var obj = new JsonObject
+ obj["content"] = content.join("\n")
+ obj["location"] = location
+ return obj
+ end
+
+ redef fun to_json do return json.to_json
+end
+
+redef class Location
+ super Jsonable
+
+ # Return `self` as a JsonObject.
+ fun json: JsonObject do
+ var obj = new JsonObject
+ obj["column_end"] = column_end
+ obj["column_start"] = column_start
+ obj["line_end"] = line_end
+ obj["line_start"] = line_start
+ var file = self.file
+ if file != null then
+ obj["file"] = file.filename
+ end
+ return obj
+ end
+
+ redef fun to_json do return json.to_json
+end
+
+redef class MVisibility
+ super Jsonable
+
+ redef fun to_json do return to_s.to_json
+end
+
+redef class MPackage
+
+ redef fun json do
+ var obj = super
+ obj["visibility"] = public_visibility
+ if ini != null then
+ obj["ini"] = new JsonObject.from(ini.as(not null).to_map)
+ end
+ obj["root"] = to_mentity_ref(root)
+ obj["mgroups"] = to_mentity_refs(mgroups)
+ return obj
+ end
+end
+
+redef class MGroup
+ redef fun json do
+ var obj = super
+ obj["visibility"] = public_visibility
+ obj["is_root"] = is_root
+ obj["mpackage"] = to_mentity_ref(mpackage)
+ obj["default_mmodule"] = to_mentity_ref(default_mmodule)
+ obj["parent"] = to_mentity_ref(parent)
+ obj["mmodules"] = to_mentity_refs(mmodules)
+ obj["mgroups"] = to_mentity_refs(in_nesting.direct_smallers)
+ return obj
+ end
+end
+
+redef class MModule
+ redef fun json do
+ var obj = super
+ obj["location"] = location
+ obj["visibility"] = public_visibility
+ obj["mpackage"] = to_mentity_ref(mpackage)
+ obj["mgroup"] = to_mentity_ref(mgroup)
+ obj["intro_mclasses"] = to_mentity_refs(intro_mclasses)
+ obj["mclassdefs"] = to_mentity_refs(mclassdefs)
+ return obj
+ end
+end
+
+redef class MClass
+ redef fun json do
+ var obj = super
+ obj["visibility"] = visibility
+ var arr = new JsonArray
+ for mparameter in mparameters do arr.add mparameter
+ obj["mparameters"] = arr
+ obj["intro"] = to_mentity_ref(intro)
+ obj["intro_mmodule"] = to_mentity_ref(intro_mmodule)
+ obj["mpackage"] = to_mentity_ref(intro_mmodule.mpackage)
+ obj["mclassdefs"] = to_mentity_refs(mclassdefs)
+ return obj
+ end
+end
+
+redef class MClassDef
+ redef fun json do
+ var obj = super
+ obj["visibility"] = mclass.visibility
+ obj["location"] = location
+ obj["is_intro"] = is_intro
+ var arr = new JsonArray
+ for mparameter in mclass.mparameters do arr.add mparameter
+ obj["mparameters"] = arr
+ obj["mmodule"] = to_mentity_ref(mmodule)
+ obj["mclass"] = to_mentity_ref(mclass)
+ obj["mpropdefs"] = to_mentity_refs(mpropdefs)
+ obj["intro_mproperties"] = to_mentity_refs(intro_mproperties)
+ return obj
+ end
+end
+
+redef class MProperty
+ redef fun json do
+ var obj = super
+ obj["visibility"] = visibility
+ obj["intro"] = to_mentity_ref(intro)
+ obj["intro_mclassdef"] = to_mentity_ref(intro_mclassdef)
+ obj["mpropdefs"] = to_mentity_refs(mpropdefs)
+ return obj
+ end
+end
+
+redef class MMethod
+ redef fun json do
+ var obj = super
+ obj["is_init"] = is_init
+ obj["msignature"] = intro.msignature
+ return obj
+ end
+end
+
+redef class MAttribute
+ redef fun json do
+ var obj = super
+ obj["static_mtype"] = to_mentity_ref(intro.static_mtype)
+ return obj
+ end
+end
+
+redef class MVirtualTypeProp
+ redef fun json do
+ var obj = super
+ obj["mvirtualtype"] = to_mentity_ref(mvirtualtype)
+ obj["bound"] = to_mentity_ref(intro.bound)
+ return obj
+ end
+end
+
+redef class MPropDef
+ redef fun json do
+ var obj = super
+ obj["visibility"] = mproperty.visibility
+ obj["location"] = location
+ obj["is_intro"] = is_intro
+ obj["mclassdef"] = to_mentity_ref(mclassdef)
+ obj["mproperty"] = to_mentity_ref(mproperty)
+ return obj
+ end
+end
+
+redef class MMethodDef
+ redef fun json do
+ var obj = super
+ obj["msignature"] = msignature
+ return obj
+ end
+end
+
+redef class MAttributeDef
+ redef fun json do
+ var obj = super
+ obj["static_mtype"] = to_mentity_ref(static_mtype)
+ return obj
+ end
+end
+
+redef class MVirtualTypeDef
+ redef fun json do
+ var obj = super
+ obj["bound"] = to_mentity_ref(bound)
+ obj["is_fixed"] = is_fixed
+ return obj
+ end
+end
+
+redef class MSignature
+ redef fun json do
+ var obj = new JsonObject
+ obj["arity"] = arity
+ var arr = new JsonArray
+ for mparam in mparameters do arr.add mparam
+ obj["mparams"] = arr
+ obj["return_mtype"] = to_mentity_ref(return_mtype)
+ obj["vararg_rank"] = vararg_rank
+ return obj
+ end
+end
+
+redef class MParameterType
+ redef fun json do
+ var obj = new JsonObject
+ obj["name"] = name
+ obj["rank"] = rank
+ obj["mtype"] = to_mentity_ref(mclass.intro.bound_mtype.arguments[rank])
+ return obj
+ end
+end
+
+redef class MParameter
+ redef fun json do
+ var obj = new JsonObject
+ obj["is_vararg"] = is_vararg
+ obj["name"] = name
+ obj["mtype"] = to_mentity_ref(mtype)
+ return obj
+ end
+end
+
+# Create a ref to a `mentity`.
+fun to_mentity_ref(mentity: nullable MEntity): nullable MEntityRef do
+ if mentity == null then return null
+ return new MEntityRef(mentity)
+end
+
+# Return a collection of `mentities` as a JsonArray of MEntityRefs.
+fun to_mentity_refs(mentities: Collection[MEntity]): JsonArray do
+ var array = new JsonArray
+ for mentity in mentities do array.add to_mentity_ref(mentity)
+ return array
+end
return
end
else
- if mprop.is_broken then
- return
- end
+ if mprop.is_broken then return
if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, not self isa AMainMethPropdef, mprop) then return
check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
end
if mreadprop == null then
var mvisibility = new_property_visibility(modelbuilder, mclassdef, self.n_visibility)
mreadprop = new MMethod(mclassdef, readname, self.location, mvisibility)
- if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mreadprop) then return
+ if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mreadprop) then
+ mreadprop.is_broken = true
+ return
+ end
else
+ if mreadprop.is_broken then return
if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, true, mreadprop) then return
check_redef_property_visibility(modelbuilder, self.n_visibility, mreadprop)
end
if mvisibility > protected_visibility then mvisibility = protected_visibility
end
mwriteprop = new MMethod(mclassdef, writename, self.location, mvisibility)
- if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef, false, mwriteprop) then return
+ if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef, false, mwriteprop) then
+ mwriteprop.is_broken = true
+ return
+ end
mwriteprop.deprecation = mreadprop.deprecation
else
+ if mwriteprop.is_broken then return
if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef or else n_kwredef, true, mwriteprop) then return
if atwritable != null then
check_redef_property_visibility(modelbuilder, atwritable.n_visibility, mwriteprop)
break
end
else
+ if mprop.is_broken then return
assert mprop isa MVirtualTypeProp
check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
end
var mmodules = modelbuilder.parse_full(args)
modelbuilder.run_phases
-if opt_full.value then mmodules = model.mmodules
+if opt_full.value then mmodules = modelbuilder.parsed_modules
var dir = opt_dir.value
if dir != null then
exit(0)
end
+"NIT_TESTING".setenv("true")
+
var page = new HTMLTag("testsuites")
if toolcontext.opt_full.value then mmodules = model.mmodules
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import core::kernel
+
+class A
+ fun f do 1.output
+ fun f2: Int do return 2
+ fun f2=(i:Int) do i.output
+ fun f3=(i:Int) do i.output
+ var v = 4
+ type T: A
+ fun t(t: T): T do return t
+end
+
+class B
+ super A#alt1#
+ redef fun f do 10.output
+ redef var f2 = 20
+ var f3 = 30 is redef writable
+ redef var v = 40
+ redef type T: B
+end
+
+class C
+ super B#alt2#
+ redef fun f do 100.output
+ redef var f2 = 200
+ redef var f3 = 300
+ redef var v = 400
+ redef type T: C
+
+end
+
+var a = new A
+a.f
+a.f2 = -2
+a.f2.output
+a.f3 = -3
+a.t(a).f
+
+a = new B
+a.f
+a.f2 = -2
+a.f2.output
+a.f3 = -3
+a.t(a).f
+
+a = new C
+a.f
+a.f2 = -2
+a.f2.output
+a.f3 = -3
+a.t(a).f
--- /dev/null
+1
+-2
+2
+-3
+1
+10
+-2
+10
+100
+-2
+100
--- /dev/null
+alt/base_redef_alt1.nit:29,12: Error: no property `B::f` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt1.nit:30,12--13: Error: no property `B::f2` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt1.nit:31,6--7: Error: no property `B::f3=` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt1.nit:32,12: Error: no property `B::v` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt1.nit:33,2--16: Error: no property `B::T` is inherited. Remove the `redef` keyword to define a new property.
--- /dev/null
+alt/base_redef_alt2.nit:38,12: Error: no property `C::f` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt2.nit:39,12--13: Error: no property `C::f2` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt2.nit:40,12--13: Error: no property `C::f3` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt2.nit:41,12: Error: no property `C::v` is inherited. Remove the `redef` keyword to define a new property.
+alt/base_redef_alt2.nit:42,2--16: Error: no property `C::T` is inherited. Remove the `redef` keyword to define a new property.
--- /dev/null
+aname class sex
+Whale mammal 1
+Snake reptile 0
test.has_suffix("bt") => false
test.has_suffix("bat") => false
test.has_suffix("foot") => false
+........
+
+........
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2016 Guilherme Mansur <guilhermerpmansur@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+module test_postgres_native
+
+import postgresql::native_postgres
+
+var db = new NativePostgres.connectdb("dbname=postgres")
+assert postgres_open: db.status.is_ok else print_error db.error
+
+var result = db.exec("CREATE TABLE IF NOT EXISTS animals (aname TEXT PRIMARY KEY, class TEXT NOT NULL, sex INTEGER)")
+assert postgres_create_table: result.status.is_ok else print_error db.error
+
+result = db.exec("INSERT INTO animals VALUES('Whale', 'mammal', 1)")
+assert postgres_insert_1: result.status.is_ok else print_error db.error
+
+result = db.exec("INSERT INTO animals VALUES('Snake', 'reptile', 0)")
+assert postgres_insert_2: result.status.is_ok else print_error db.error
+
+result = db.exec("SELECT * FROM animals")
+assert postgres_select: result.status.is_ok else print_error db.error
+
+assert postgres_ntuples: result.ntuples == 2 else print_error db.error
+assert postgres_nfields: result.nfields == 3 else print_error db.error
+assert postgres_fname: result.fname(0) == "aname" else print_error db.error
+assert postgres_isnull: result.is_null(0,0) == false else print_error db.error
+assert postgres_value: result.value(0,0) == "Whale" else print_error db.error
+
+var cols: Int = result.nfields
+var rows: Int = result.ntuples
+var fields: String = ""
+for c in [0..cols[ do fields += result.fname(c) + " "
+print fields
+for i in [0..rows[ do
+ fields = ""
+ for j in [0..cols[ do fields += result.value(i, j) + " "
+ print fields
+end
+
+result = db.exec("DELETE FROM animals WHERE aname = 'Lioness'")
+assert postgres_delete_1: result.status.is_ok else print_error db.error
+
+result = db.exec("DELETE FROM animals WHERE aname = 'Snake'")
+assert postgres_delete_2: result.status.is_ok else print_error db.error
+
+result = db.prepare("PREPARED_INSERT", "INSERT INTO animals(aname, class, sex) VALUES ($1, $2, $3)", 3)
+assert postgres_prepare: result.status.is_ok else print_error db.error
+
+result = db.exec("DELETE FROM animals WHERE aname = 'Frog'")
+assert postgres_delete_3: result.status.is_ok else print_error db.error
+
+var values = ["Frog", "Anphibian", "1"]
+var lengths = [values[0].length, values[1].length, values[2].length]
+var formats = [0,0,0]
+result = db.exec_prepared("PREPARED_INSERT", 3, values, lengths, formats,0)
+assert postgres_exec_prepared: result.status.is_ok else print_error db.error
+
+result = db.exec("DROP TABLE animals")
+assert postgres_drop_table: result.status.is_ok else print_error db.error
+db.finish
print("test.has_suffix(\"bat\") => {test.has_suffix("bat")}")
print("test.has_suffix(\"foot\") => {test.has_suffix("foot")}")
+print "........"
+print "/".substring(7, 1)
+print "........"