1 module bamboo.integration;
2 
3 version (unittest)  :  // Example
4 import std.conv;
5 import std.file;
6 import std.stdio;
7 import pegged.parser;
8 import bamboo.astgen;
9 import bamboo.codegen;
10 import bamboo.parser;
11 import bamboo.types;
12 import bamboo.hashgen;
13 import bamboo.legacy_hash;
14 
15 unittest
16 {
17 
18     Module file = parseModule("tests/astron_example.dc");
19 
20     assert(file !is null);
21     HashGenerator gen;
22 
23     hashModule(gen, file);
24 
25     auto hash = gen.hash;
26 
27     assert(hash == 205_656_144, hash.to!string() ~ " != 205,656,144");
28 
29     file = parseModule("tests/simple_example.dc");
30     hash = computeHash(file);
31 
32     assert(hash == 4_275_592, hash.to!string() ~ " != 4,275,592");
33 }
34 
35 enum simple_example = `
36 // Some abstractions for readability.
37 typedef uint32 doId;
38 typedef uint32 zoneId;
39 typedef uint64 channel;
40 
41 // The Python Views for these distributed classes.
42 from simple_example.objects import LoginManager/AI/UD
43 from simple_example.objects import DistributedMaproot/AI/UD
44 from simple_example.objects import DistributedAvatar/AI/OV
45 
46 // A simple DOG for username/password authentication and handoff to
47 // the DistributedMaproot for avatar creation.
48 dclass LoginManager {
49   setMaproot(doId maproot);
50   login(string username, string password) clsend;
51 };
52 
53 // The root object of the map, container for the DistributedAvatars.
54 dclass DistributedMaproot {
55   createAvatar(channel client) airecv;
56 };
57 
58 // The actual class for avatar control.
59 // The idea is that a Client can set its intention for its heading
60 // and speed, but only the controlling AI can set its actual
61 // position and heading.
62 dclass DistributedAvatar {
63   string name broadcast required;
64 
65   setXYZH(float32, float32, float32, int16 % 360) broadcast required;
66   indicateIntent(float32 x, float32 y) ownsend airecv;
67 };
68 `;
69 
70 unittest
71 {
72     import std.stdio : File;
73 
74     Module file = parseModule("tests/simple_example.dc");
75 
76     string generated = generateFile(file, "simple_example");
77 
78     // Write to file
79     //File output = File("tests/simple_example.d", "w");
80     //output.writeln(generated);
81 
82     // These are needed for the dclass system to work.
83 
84     class DistributedObject
85     {
86     }
87 
88     enum Keyword
89     {
90         required,
91         broadcast,
92         ownrecv,
93         ram,
94         db,
95         clsend,
96         clrecv,
97         ownsend,
98         airecv,
99     }
100 
101     alias required = Keyword.required;
102     alias broadcast = Keyword.broadcast;
103     alias ownrecv = Keyword.ownrecv;
104     alias ram = Keyword.ram;
105     alias db = Keyword.db;
106     alias clsend = Keyword.clsend;
107     alias clrecv = Keyword.clrecv;
108     alias ownsend = Keyword.ownsend;
109     alias airecv = Keyword.airecv;
110 
111     // Mix in the module.
112 
113     enum str = generateModule(parseString(simple_example));
114     mixin(str);
115 
116     // Test that the types exist.
117     static assert(is(LoginManager));
118     static assert(is(DistributedMaproot));
119     static assert(is(DistributedAvatar));
120 
121     // Can we override one?
122     class DistributedAvatarTest : DistributedAvatar
123     {
124 
125     }
126 
127     auto obj = new DistributedAvatarTest();
128 
129     obj.xyzh = tuple(1, 1, 1, short(1));
130     assert(obj.xyzh == tuple(1f, 1f, 1f, short(1)));
131 }
132 
133 unittest
134 {
135     import std.stdio : File;
136 
137     Module file = parseModule("tests/direct.dc");
138 
139     string generated = generateFile(file);
140 
141     assert(file.symbol == "direct.distributed");
142 }