Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Nov 27, 2024
1 parent 41dbd77 commit 7d30713
Showing 1 changed file with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Dispose()
}

[Fact]
public async Task TestWriteTypeDeclaration()
public async Task WritesBaseUrlProperty()
{
var codeClass = new CodeClass
{
Expand All @@ -59,7 +59,7 @@ public async Task TestWriteTypeDeclaration()
};
codeClass.AddProperty(urlTemplateProperty);

// Add a new property named BaseUrl and set its value to the baseUrl string
// Add base url property
var baseUrlProperty = new CodeProperty
{
Name = "BaseUrl",
Expand Down Expand Up @@ -90,4 +90,138 @@ public async Task TestWriteTypeDeclaration()
Assert.Contains("# Base url for the server/host", result);
Assert.Contains("@url = https://example.com", result);
}

[Fact]
public async Task WritesRequestExecutorMethods()
{
var codeClass = new CodeClass
{
Name = "TestClass",
Kind = CodeClassKind.RequestBuilder
};
var urlTemplateProperty = new CodeProperty
{
Name = "urlTemplate",
Kind = CodePropertyKind.UrlTemplate,
DefaultValue = "\"{+baseurl}/posts\"",
Type = new CodeType
{
Name = "string",
IsExternal = true
},
Documentation = new CodeDocumentation
{
DescriptionTemplate = "The URL template for the request."
}
};
codeClass.AddProperty(urlTemplateProperty);

// Add base url property
var baseUrlProperty = new CodeProperty
{
Name = "BaseUrl",
Kind = CodePropertyKind.Custom,
Access = AccessModifier.Private,
DefaultValue = "https://example.com",
Type = new CodeType { Name = "string", IsExternal = true }
};
codeClass.AddProperty(baseUrlProperty);

var method = new CodeMethod
{
Name = "get",
Kind = CodeMethodKind.RequestExecutor,
Documentation = new CodeDocumentation { DescriptionTemplate = "GET method" },
ReturnType = new CodeType { Name = "void" }
};
codeClass.AddMethod(method);

var postMethod = new CodeMethod
{
Name = "post",
Kind = CodeMethodKind.RequestExecutor,
Documentation = new CodeDocumentation { DescriptionTemplate = "Post method" },
ReturnType = new CodeType { Name = "void" },
RequestBodyContentType = "application/json"
};


var typeDefinition = new CodeClass
{
Name = "PostParameter",
};

var properties = new List<CodeProperty>
{
new() {
Name = "body",
Kind = CodePropertyKind.Custom,
Type = new CodeType { Name = "string", IsExternal = true }
},
new() {
Name = "id",
Kind = CodePropertyKind.Custom,
Type = new CodeType { Name = "int", IsExternal = true }
},
new() {
Name = "title",
Kind = CodePropertyKind.Custom,
Type = new CodeType { Name = "string", IsExternal = true }
},
new() {
Name = "userId",
Kind = CodePropertyKind.Custom,
Type = new CodeType { Name = "int", IsExternal = true }
}
};

typeDefinition.AddProperty(properties.ToArray());

// Define the parameter with the specified properties
var postParameter = new CodeParameter
{
Name = "postParameter",
Kind = CodeParameterKind.RequestBody,
Type = new CodeType
{
Name = "PostParameter",
TypeDefinition = typeDefinition
}
};

// Add the parameter to the post method
postMethod.AddParameter(postParameter);

codeClass.AddMethod(postMethod);

var patchMethod = new CodeMethod
{
Name = "patch",
Kind = CodeMethodKind.RequestExecutor,
Documentation = new CodeDocumentation { DescriptionTemplate = "Patch method" },
ReturnType = new CodeType { Name = "void" }
};
codeClass.AddMethod(patchMethod);

root.AddClass(codeClass);

await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.HTTP }, root);

writer.Write(codeClass.StartBlock);
var result = tw.ToString();

// Check HTTP operations
Assert.Contains("GET {{url}}/posts HTTP/1.1", result);
Assert.Contains("PATCH {{url}}/posts HTTP/1.1", result);
Assert.Contains("POST {{url}}/posts HTTP/1.1", result);

// Check content type
Assert.Contains("Content-Type: application/json", result);

// check the request body
Assert.Contains("\"body\": \"string\"", result);
Assert.Contains("\"id\": 0", result);
Assert.Contains("\"title\": \"string\"", result);
Assert.Contains("\"userId\": 0", result);
}
}

0 comments on commit 7d30713

Please sign in to comment.