-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code generation #32
Merged
Merged
Code generation #32
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8dff3e
Add code generation
omaus a804343
Add unit tests for code generation
omaus 7e603b2
Do minor changes (WIP)
omaus 2874a9d
Add requested changes
omaus 2144050
Update tests according to request
omaus e81e2ba
Use `ReplaceLineEndingsMethod()` instead of own functions
omaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
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 = String.replace "\r" "" refSF | ||
let actual = | ||
CodeGeneration.toSourceCode "Investigation" refObo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh lol, didn't know about that! 👍🏻😄 |
||
|> String.splitS NewLine | ||
|> String.concat "\n" | ||
|> String.replace "\r" "" | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would not use these ontologies but static files in this repo as references for tests