/// <summary> /// Generates test case code for the specified code model. /// </summary> /// <param name="cmg">The code model for which to create code.</param> /// <returns>Root node of the AST.</returns> public static Node GenerateTests(CodeModelGo cmg) { var file = new File(); var packageName = $"{cmg.Namespace}tests"; file.AddChild(new Comment($"Package {packageName} contains test functions for package {cmg.Namespace}.")); file.AddChild(new Package(packageName)); var imports = GetImports(cmg); file.AddChild(imports); file.AddChild(new Terminal()); TestFunctionRpc.TypeAddedEvent = (Core.Model.IModelType modelType) => { if (modelType is PrimaryTypeGo) { var ptg = modelType as PrimaryTypeGo; if (!string.IsNullOrWhiteSpace(ptg.Import)) { var imp = new ImportEntry(ptg.Import); if (!imports.Contains(imp)) { imports.AddChild(imp); } } } }; file.AddChild(VariableDecl.Generate(LoggerVarName, "*log.Logger")); file.AddChild(new Terminal()); var rpcTypeName = cmg.Name; var suiteDef = GetServerReceiver(rpcTypeName); file.AddChild(new Comment($"{suiteDef.Item2} is used to dispatch functions calls via JSON-RPC.")); file.AddChild(suiteDef.Item1); file.AddChild(new Terminal()); file.AddChild(new Comment("RegisterServer enables RPC.")); file.AddChild(GetRegisterServer(suiteDef.Item2)); file.AddChild(new Terminal()); var goMethods = cmg.Methods.Cast <MethodGo>().OrderBy(mg => mg.SerializedName.ToString()); foreach (var method in goMethods) { var testFunc = TestFunctionRpc.Generate(method, rpcTypeName, RawResponseTypeName, ToRawResponseFuncName, LoggerVarName); file.AddChild(new Comment($"{testFunc.Item2} is a test function for the matching operation ID.")); file.AddChild(testFunc.Item1); file.AddChild(new Terminal()); } return(file); }