d2f6c128ec2538a6a9d9285bb9c3a816bb0c19cb
[nit.git] / src / doc / templates / tests / test_json_model.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module test_json_model is test
16
17 import json_model
18 import frontend
19
20 class TestModelSerialization
21 test
22
23 var suite_path: String = "NIT_TESTING_PATH".environ
24 var lib_path: String = "{suite_path.dirname}/../../../../tests/test_prog"
25
26 var mainmodule: MModule is noinit
27
28 private var model: Model do
29 var toolcontext = new ToolContext
30 var model = new Model
31 var mbuilder = new ModelBuilder(model, toolcontext)
32 var mmodules = mbuilder.parse_full([lib_path])
33 if mmodules.is_empty then return model
34 mbuilder.run_phases
35 toolcontext.run_global_phases(mmodules)
36 mainmodule = mmodules.first
37 return model
38 end
39
40 fun test_refs_to_full_json is test do
41 var mentities = new Array[MEntity]
42 mentities.add model.mpackages.first
43 mentities.add model.mmodules.first
44 mentities.add model.mclasses.first
45 for mentity in mentities do
46 print ((new MEntityRef(mentity)).serialize_to_json(pretty = true, plain = true))
47 end
48 end
49
50 fun test_packages_to_full_json is test do
51 for mentity in model.mpackages do
52 print mentity.serialize_to_json(pretty = true, plain = true)
53 end
54 end
55
56 fun test_groups_to_full_json is test do
57 for mpackage in model.mpackages do
58 for mentity in mpackage.mgroups do
59 print mentity.serialize_to_json(pretty = true, plain = true)
60 end
61 end
62 end
63
64 fun test_modules_to_full_json is test do
65 for mentity in model.mmodules do
66 print mentity.serialize_to_json(pretty = true, plain = true)
67 end
68 end
69
70 fun test_classes_to_full_json is test do
71 for mentity in model.mclasses do
72 print mentity.serialize_to_json(pretty = true, plain = true)
73 end
74 end
75
76 fun test_classdefs_to_full_json is test do
77 for mclass in model.mclasses do
78 for mentity in mclass.mclassdefs do
79 print mentity.serialize_to_json(pretty = true, plain = true)
80 end
81 end
82 end
83
84 fun test_props_to_full_json is test do
85 for mentity in model.mproperties do
86 print mentity.serialize_to_json(pretty = true, plain = true)
87 end
88 end
89
90 fun test_propdefs_to_full_json is test do
91 for mprop in model.mproperties do
92 for mentity in mprop.mpropdefs do
93 print mentity.serialize_to_json(pretty = true, plain = true)
94 end
95 end
96 end
97 end
98
99 redef class nitc::Location
100 serialize
101
102 # Avoid diff on location absolute path
103 redef fun core_serialize_to(v) do
104 v.serialize_attribute("column_end", column_end)
105 v.serialize_attribute("column_start", column_start)
106 v.serialize_attribute("line_end", line_end)
107 v.serialize_attribute("line_start", line_start)
108 var file = self.file
109 if file != null then
110 v.serialize_attribute("file", "test_location")
111 end
112 end
113 end