From e3e3551abe8d9a981f66a4ffa76918b819f474a8 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 21 Nov 2017 13:17:36 -0500 Subject: [PATCH] frontend: add `is example` test Signed-off-by: Alexandre Terrasa --- src/frontend/tests/test_examples.nit | 63 ++++++++++++++++++++ .../test_examples.sav/test_model_examples.res | 33 ++++++++++ tests/test_prog/examples/game_examples.nit | 32 ++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/frontend/tests/test_examples.nit create mode 100644 src/frontend/tests/test_examples.sav/test_model_examples.res create mode 100644 tests/test_prog/examples/game_examples.nit diff --git a/src/frontend/tests/test_examples.nit b/src/frontend/tests/test_examples.nit new file mode 100644 index 0000000..fbd4803 --- /dev/null +++ b/src/frontend/tests/test_examples.nit @@ -0,0 +1,63 @@ +# 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. + +module test_examples is test + +import frontend +import parse_examples +import model_views + +class TestExample + test + + # The path to the testunit being executed + # + # Used to retrieve the path to sources to compile. + var test_path: String = "NIT_TESTING_PATH".environ.dirname is lazy + + # Test program to compile + # + # Default is `$NIT_DIR/tests/test_prog`. + var test_src: String = test_path / "../../../tests/test_prog" is lazy + + fun test_model_examples is test do + var toolcontext = new ToolContext + + # build model + var model = new Model + var modelbuilder = new ModelBuilder(model, toolcontext) + var mmodules = modelbuilder.parse_full([test_src]) + + # process + modelbuilder.run_phases + toolcontext.run_global_phases(mmodules) + var mainmodule = toolcontext.make_main_module(mmodules) + + var filters = new ModelFilter(accept_example = true) + + var view = new ModelView(model, mainmodule, filters) + + for mentity in view.mentities do + if not mentity.is_example then continue + + var mexample = mentity.mexample + if mexample == null then continue + + print "{mentity.full_name} provides example for:" + for oentity, score in mexample.example_for do + print " * {oentity.full_name} ({score})" + end + end + end +end diff --git a/src/frontend/tests/test_examples.sav/test_model_examples.res b/src/frontend/tests/test_examples.sav/test_model_examples.res new file mode 100644 index 0000000..de733c8 --- /dev/null +++ b/src/frontend/tests/test_examples.sav/test_model_examples.res @@ -0,0 +1,33 @@ +test_prog::game_examples provides example for: + * test_prog::Game (4) + * test_prog$Game (4) + * test_prog$List (2) + * test_prog$Game$stop_game (1) + * test_prog$Game$pause_game (1) + * test_prog$Game$start_game (1) + * test_prog::game (1) + * test_prog>game> (1) + * test_prog (1) +test_prog$MyGame provides example for: + * test_prog::Game (4) + * test_prog$Game (4) + * test_prog$List (2) + * test_prog$Game$stop_game (1) + * test_prog$Game$pause_game (1) + * test_prog$Game$start_game (1) +test_prog$MyGame$Game::start_game provides example for: + * test_prog$Game$start_game (1) + * test_prog$Game (1) + * test_prog::Game (1) +test_prog$MyGame$Game::pause_game provides example for: + * test_prog$Game$pause_game (1) + * test_prog$Game (1) + * test_prog::Game (1) +test_prog$MyGame$Game::stop_game provides example for: + * test_prog$Game$stop_game (1) + * test_prog$Game (1) + * test_prog::Game (1) +test_prog$MyGame$_player_characters provides example for: + * test_prog$List (1) +test_prog$MyGame$_computer_characters provides example for: + * test_prog$List (1) diff --git a/tests/test_prog/examples/game_examples.nit b/tests/test_prog/examples/game_examples.nit new file mode 100644 index 0000000..68b16de --- /dev/null +++ b/tests/test_prog/examples/game_examples.nit @@ -0,0 +1,32 @@ +# 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. + +module game_examples is example + +import game + +# This is an example of how to implement the Game interface +class MyGame + super Game + + redef var player_characters = new List[Character] + + redef var computer_characters = new List[Character] + + redef fun start_game do end + + redef fun pause_game do end + + redef fun stop_game do end +end -- 1.7.9.5