-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add code generation * Add unit tests for code generation * Do minor changes (WIP) * Add requested changes * Update tests according to request * Use `ReplaceLineEndingsMethod()` instead of own functions
- Loading branch information
Showing
10 changed files
with
1,108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace OBO.NET.CodeGeneration | ||
|
||
|
||
open OBO.NET | ||
open FSharpAux | ||
open type System.Environment | ||
|
||
|
||
module CodeGeneration = | ||
|
||
[<Literal>] | ||
let baseString = """namespace ARCTokenization.StructuralOntology | ||
open ControlledVocabulary | ||
module <name> = | ||
""" | ||
|
||
/// Takes an OboTerm and returns its name but with all spaces replaced by underscores. | ||
let toUnderscoredName (term : OboTerm) = | ||
term.Name | ||
|> String.replace " " "_" | ||
|
||
/// Returns true if a string contains special characters or starts with a number. | ||
let checkForSpecialCharacters str = | ||
let spChs = System.Text.RegularExpressions.Regex(@"(^\d|[^a-zA-Z0-9_])") | ||
(spChs.Match str).Success | ||
|
||
/// Takes a string and returns it with back ticks ("``") at the beginning and the end. | ||
let addBackTicks str = | ||
$"``{str}``" | ||
|
||
/// Takes an OboTerm and returns its TermSourceRef as string. | ||
let toTermSourceRef (term : OboTerm) = | ||
term.Id | ||
|> String.takeWhile ((<>) ':') | ||
|
||
/// Takes an OboTerm and transforms it into an F# code string for structural ontology libraries. | ||
let toCodeString (term : OboTerm) = | ||
let underscoredName = toUnderscoredName term | ||
let curatedName = | ||
if checkForSpecialCharacters underscoredName then | ||
addBackTicks underscoredName | ||
else underscoredName | ||
$" let {curatedName} = CvTerm.create(\"{term.Id}\", \"{term.Name}\", \"{toTermSourceRef term}\"){NewLine}{NewLine}" | ||
|
||
/// Takes a module name and an OboOntology and returns the F# code of the whole term list for structural ontology libraries. | ||
let toSourceCode moduleName (onto : OboOntology) = | ||
let concattedSingleValues = String.init onto.Terms.Length (fun i -> $"{toCodeString onto.Terms[i]}") | ||
let updatedBaseString = String.replace "<name>" moduleName baseString | ||
$"{updatedBaseString}{concattedSingleValues}" | ||
|
||
/// Takes a module name and an OboOntology and writes the ontology's terms as F# code for structural ontology libraries as a source file at the given path. | ||
let toFile moduleName (onto : OboOntology) path = | ||
System.IO.File.WriteAllText(path, toSourceCode moduleName onto) | ||
|
||
/// Takes a module name and the path to an OBO file and writes the ontology's terms as F# code for structural ontology libraries as a source file at the given output path. | ||
let fromOboFileToSourceFile moduleName inputPath outputPath = | ||
OboOntology.fromFile false inputPath | ||
|> fun o -> toFile moduleName o outputPath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) --> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<!-- Optional: Embed source files that are not tracked by the source control manager in the PDB --> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<!-- Optional: Build symbol package (.snupkg) to distribute the PDB containing Source Link --> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\OBO.NET\OBO.NET.fsproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="CodeGeneration.fs" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<Authors>Oliver Maus, F# open source contributors</Authors> | ||
<Description>An OBO file format to F# source code generator, written in F#.</Description> | ||
<Summary>An OBO file format to F# source code generator, written in F#.</Summary> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageProjectUrl>https://github.com/CSBiology/OBO.NET</PackageProjectUrl> | ||
<PackageTags>ontology fsharp file obo codegeneration code generation</PackageTags> | ||
<RepositoryUrl>https://github.com/CSBiology/OBO.NET</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<FsDocsLicenseLink>https://github.com/CSBiology/OBO.NET/blob/main/LICENSE</FsDocsLicenseLink> | ||
<FsDocsReleaseNotesLink>https://github.com/CSBiology/OBO.NET/blob/main/RELEASE_NOTES.md</FsDocsReleaseNotesLink> | ||
</PropertyGroup> | ||
|
||
</Project> |
51 changes: 51 additions & 0 deletions
51
tests/OBO.NET.CodeGeneration.Tests/CodeGeneration.Tests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace OBO.NET.CodeGeneration.Tests | ||
|
||
open Expecto | ||
open OBO.NET | ||
open OBO.NET.CodeGeneration | ||
open FSharpAux | ||
open ARCTokenization.StructuralOntology | ||
open ARCTokenization.Terms | ||
open System.IO | ||
open type System.Environment | ||
|
||
|
||
module CodeGenerationTests = | ||
|
||
let refObo = OboOntology.fromFile false (Path.Combine(__SOURCE_DIRECTORY__, "References/ReferenceOboFile.obo")) | ||
let refSF = File.ReadAllText (Path.Combine(__SOURCE_DIRECTORY__, "References/ReferenceSourceFile.fs")) | ||
|
||
let toUnderscoredNameTest = | ||
testList "toUnderscoredName" [ | ||
testCase "returns correct underscored name" <| fun _ -> | ||
let expected = "Investigation_Metadata" | ||
let actual = List.head refObo.Terms |> CodeGeneration.toUnderscoredName | ||
Expect.equal actual expected "underscored name is not correct" | ||
] | ||
|
||
let toTermSourceRefTest = | ||
testList "toTermSourceRef" [ | ||
testCase "returns correct TermSourceRef" <| fun _ -> | ||
let expected = "INVMSO" | ||
let actual = List.head refObo.Terms |> CodeGeneration.toTermSourceRef | ||
Expect.equal actual expected "TermSourceRef is not correct" | ||
] | ||
|
||
let toCodeStringTest = | ||
testList "toCodeString" [ | ||
testCase "returns correct F# code" <| fun _ -> | ||
let expected = $" let Investigation_Metadata = CvTerm.create(\"INVMSO:00000001\", \"Investigation Metadata\", \"INVMSO\"){NewLine}{NewLine}" | ||
let actual = List.head refObo.Terms |> CodeGeneration.toCodeString | ||
Expect.equal actual expected "F# code is not correct" | ||
] | ||
|
||
let toSourceCodeTest = | ||
testList "toSourceCode" [ | ||
testCase "returns correct source code" <| fun _ -> | ||
let expected = refSF.ReplaceLineEndings() | ||
let actual = (CodeGeneration.toSourceCode "Investigation" refObo).ReplaceLineEndings() | ||
Expect.equal actual expected "Source code is not correct" | ||
] | ||
|
||
[<Tests>] | ||
let all = testList "CodeGeneration" [toUnderscoredNameTest; toTermSourceRefTest; toCodeStringTest; toSourceCodeTest] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
open OBO.NET.CodeGeneration.Tests | ||
|
||
open Expecto | ||
|
||
|
||
[<EntryPoint>] | ||
let main argv = Tests.runTestsInAssemblyWithCLIArgs [] argv |
32 changes: 32 additions & 0 deletions
32
tests/OBO.NET.CodeGeneration.Tests/OBO.NET.CodeGeneration.Tests.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
<GenerateProgramFile>false</GenerateProgramFile> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="references\ReferenceOboFile.obo" /> | ||
<Compile Include="References\ReferenceSourceFile.fs" /> | ||
<Compile Include="CodeGeneration.Tests.fs" /> | ||
<Compile Include="Main.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup /> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\OBO.NET\OBO.NET.fsproj" /> | ||
<ProjectReference Include="..\..\src\OBO.NET.CodeGeneration\OBO.NET.CodeGeneration.fsproj" /> | ||
<PackageReference Include="Expecto" Version="10.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" /> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
<PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.14.1" /> | ||
<PackageReference Include="ARCTokenization" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.