-
Notifications
You must be signed in to change notification settings - Fork 790
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
WIP: Adding a new DataAttribute to improve specifying realsig, optimize and baselines. #18161
Draft
KevinRansom
wants to merge
7
commits into
dotnet:main
Choose a base branch
from
KevinRansom:ImproveDirectoryAttribute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e2f20a
temp
KevinRansom fb43fa5
temp
KevinRansom 0114fd4
temp
KevinRansom 9bde075
temp
KevinRansom 690e4c0
Merge branch 'main' into ImproveDirectoryAttribute
KevinRansom ee66618
Update tests
KevinRansom fa57d9e
temp
KevinRansom 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
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,160 @@ | ||
namespace FSharp.Test | ||
|
||
open System | ||
open System.Diagnostics | ||
open System.Linq | ||
open System.IO | ||
open System.Reflection | ||
open System.Runtime.CompilerServices | ||
open System.Runtime.InteropServices | ||
|
||
open Xunit | ||
open Xunit.Abstractions | ||
open Xunit.Sdk | ||
|
||
open FSharp.Compiler.IO | ||
open FSharp.Test.Compiler | ||
open FSharp.Test.Utilities | ||
open TestFramework | ||
|
||
//[<AutoOpen>] | ||
//module Extensions = | ||
// let getCompilation (compilation: CompilationHelper): CompilationUnit = | ||
// let c = unbox<CompilationHelper> compilation | ||
// c.Value | ||
|
||
type BooleanOptions = | ||
| True = 1 | ||
| False = 2 | ||
| Both = 3 | ||
| None = 0 | ||
|
||
/// Attribute to use with Xunit's TheoryAttribute. | ||
/// Takes a file, relative to current test suite's root. | ||
/// Returns a CompilationUnit with encapsulated source code, error baseline and IL baseline (if any). | ||
[<NoComparison; NoEquality>] | ||
type FileInlineData (directory: string, filename: string) = | ||
inherit DataAttribute() | ||
|
||
let mutable optimize: BooleanOptions option = None | ||
let mutable realsig: BooleanOptions option = None | ||
|
||
static let computeBoolValues opt = | ||
match opt with | ||
| Some BooleanOptions.True -> [|Some true|] | ||
| Some BooleanOptions.False -> [|Some false|] | ||
| Some BooleanOptions.Both -> [|Some true; Some false|] | ||
| _ -> [|None|] | ||
|
||
static let convertToNullableBool (opt: bool option): obj = | ||
match opt with | ||
| None -> null | ||
| Some opt -> box opt | ||
|
||
member _.Optimize with set v = optimize <- Some v | ||
|
||
member _.Realsig with set v = realsig <- Some v | ||
|
||
override _.GetData _ = | ||
|
||
let getOptions realsig optimize = | ||
|
||
let compilationHelper = CompilationHelper(filename, directory, convertToNullableBool realsig, convertToNullableBool optimize) | ||
[| box (compilationHelper) |] | ||
|
||
let results = | ||
let rsValues = computeBoolValues realsig | ||
let optValues = computeBoolValues optimize | ||
[| | ||
for r in rsValues do | ||
for o in optValues do | ||
getOptions r o | ||
|] | ||
|
||
results | ||
|
||
// realsig and optimized are boxed so null = not set, true or false = set | ||
and CompilationHelper internal (filename: string, directory: string, realsig: obj, optimize: obj) = | ||
|
||
let mutable filename = filename | ||
let mutable directory = directory | ||
let mutable realsig = realsig | ||
let mutable optimize = optimize | ||
|
||
let setRealInternalSignature compilation = | ||
match realsig with | ||
| null -> compilation | ||
| realsig -> compilation |> withRealInternalSignature (unbox realsig) | ||
|
||
let setOptimization compilation = | ||
match optimize with | ||
| null -> compilation | ||
| optimize -> compilation |> withOptimization (unbox optimize) | ||
|
||
static let getBaseline (opt: obj) prefix = | ||
match opt with | ||
| :? bool as b when b = true -> Some $"{prefix}On" | ||
| :? bool as b when b = false -> Some $"{prefix}Off" | ||
| _ -> None | ||
|
||
static let combineBaselines realsigBsl optimizeBsl = | ||
match realsigBsl, optimizeBsl with | ||
| Some rs, Some opt -> Some $"{rs}{opt}" | ||
| Some rs, None -> Some $"{rs}" | ||
| None, Some opt -> Some $"{opt}" | ||
| _ -> None | ||
|
||
new() = CompilationHelper("", "", null, null) | ||
|
||
// Define the implicit conversion operator | ||
static member op_Implicit(helper: CompilationHelper) : CompilationUnit = | ||
helper.Value () | ||
|
||
member _.Value(): CompilationUnit = | ||
let frame = StackTrace().GetFrame(1) // Get the calling method's frame | ||
let methodInfo = frame.GetMethod() :?> MethodInfo | ||
|
||
let directoryAttribute = DirectoryAttribute(directory) | ||
directoryAttribute.Includes <- [| filename |] | ||
|
||
let realsigBsl = (getBaseline realsig ".RealInternalSignature") | ||
let optimizeBsl = (getBaseline optimize ".Optimize") | ||
|
||
match combineBaselines realsigBsl optimizeBsl with | ||
| Some baseline -> directoryAttribute.BaselineSuffix <- baseline | ||
| _ -> () | ||
|
||
let results = directoryAttribute.GetData methodInfo | ||
if results.Count() <> 1 then failwith $"directoryAttribute returned incorrect number of results requires only 1: actual: {results.Count()}" | ||
let arguments = results.First() | ||
if arguments.Count() <> 1 then failwith $"directoryAttribute returned incorrect number of arguments requires only 1: actual: {arguments.Count()}" | ||
let compilation = arguments.First() | ||
|
||
match compilation with | ||
| :? CompilationUnit as cu -> cu |> setRealInternalSignature |> setOptimization | ||
| _ -> failwith "No compilation created" | ||
|
||
override _.ToString(): string = | ||
let file = $"File: {filename}" | ||
let realsig = | ||
match realsig with | ||
| :? bool as b -> $" realsig: {b}" | ||
| _ -> "" | ||
let optimize = | ||
match optimize with | ||
| :? bool as b -> $" optimize: {b}" | ||
| _ -> "" | ||
file + realsig + optimize | ||
|
||
interface IXunitSerializable with | ||
member _.Serialize(info: IXunitSerializationInfo) = | ||
info.AddValue("filename", filename) | ||
info.AddValue("directory", directory) | ||
info.AddValue("realsig", realsig) | ||
info.AddValue("optimize", optimize) | ||
|
||
member _.Deserialize(info: IXunitSerializationInfo) = | ||
filename <- info.GetValue<string>("filename") | ||
directory <- info.GetValue<string>("directory") | ||
realsig <- info.GetValue<obj>("realsig") | ||
optimize <- info.GetValue<obj>("optimize") |
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
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.
Would it be possible to use https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/caller-information instead?
Why exactly do we need it?
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.
@T-Gro , no. The Xunit.Sdk.DataAttribute.GetData override requires a MethodInfo instance. The caller-information attributes return the names of the member, the source file and line numbers.
Here is the Xunit description:
https://csharp-tokyo.github.io/xUnit-Hands-on/class_xunit_1_1_sdk_1_1_data_attribute.html#ab74ea9efe0a313e19ac0e8d8136a9071
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.
We need it, because GetData _ = is how XUnit retrieves the test data instance, and in this case we are using the DirectoryAttribute to create a compiler instance, and so we need to 'pretend' we are being xunit. It is true that our DirectoryAttribute doesn't actually use it, but we do need an instance to even invoke the method. And there is a decent probablility that at some point in the future DirectoryAttribute.GetData() will use it, so I didn't want to pass Unchecked.DefaultOf.
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.
If we reuse the functionality of
DirectoryAttribute
possibly we could extractcreateCompilationUnit
from it into a module or maybe inherit?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.
@majocha --- I'm still trying to figure out what we need. But we can certainly refactor, if necessary once we understand the necessities.