-
Notifications
You must be signed in to change notification settings - Fork 830
Added GetBoundValues and TryFindBoundValue APIs to FsiEvaluationSession #9068
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
Merged
KevinRansom
merged 2 commits into
dotnet:master
from
TIHan:feature/api/fsi-bound-values
Apr 29, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,243 @@ | ||
| module FSharp.Compiler.UnitTests.FsiTests | ||
|
|
||
| open System.IO | ||
| open FSharp.Compiler.Interactive.Shell | ||
| open NUnit.Framework | ||
|
|
||
| let createFsiSession () = | ||
| // Intialize output and input streams | ||
| let inStream = new StringReader("") | ||
| let outStream = new CompilerOutputStream() | ||
| let errStream = new CompilerOutputStream() | ||
|
|
||
| // Build command line arguments & start FSI session | ||
| let argv = [| "C:\\fsi.exe" |] | ||
| let allArgs = Array.append argv [|"--noninteractive"|] | ||
|
|
||
| let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration() | ||
| FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, new StreamWriter(outStream), new StreamWriter(errStream), collectible = true) | ||
|
|
||
| [<Test>] | ||
| let ``No bound values at the start of FSI session`` () = | ||
| use fsiSession = createFsiSession () | ||
| let values = fsiSession.GetBoundValues() | ||
| Assert.IsEmpty values | ||
|
|
||
| [<Test>] | ||
| let ``Bound value has correct name`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
|
|
||
| let boundValue = fsiSession.GetBoundValues() |> List.exactlyOne | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
|
|
||
| [<Test>] | ||
| let ``Bound value has correct value`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let y = 2") | ||
|
|
||
| let boundValue = fsiSession.GetBoundValues() |> List.exactlyOne | ||
|
|
||
| Assert.AreEqual(2, boundValue.Value.ReflectionValue) | ||
|
|
||
| [<Test>] | ||
| let ``Bound value has correct type`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let z = 3") | ||
|
|
||
| let boundValue = fsiSession.GetBoundValues() |> List.exactlyOne | ||
|
|
||
| Assert.AreEqual(typeof<int>, boundValue.Value.ReflectionType) | ||
|
|
||
| [<Test>] | ||
| let ``Seven bound values are ordered and have their correct name`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| fsiSession.EvalInteraction("let y = 2") | ||
| fsiSession.EvalInteraction("let z = 3") | ||
| fsiSession.EvalInteraction("let a = 4") | ||
| fsiSession.EvalInteraction("let ccc = 5") | ||
| fsiSession.EvalInteraction("let b = 6") | ||
| fsiSession.EvalInteraction("let aa = 7") | ||
|
|
||
| let names = fsiSession.GetBoundValues() |> List.map (fun x -> x.Name) | ||
|
|
||
| Assert.AreEqual(["a";"aa";"b";"ccc";"x";"y";"z"], names) | ||
|
|
||
| [<Test>] | ||
| let ``Seven bound values are ordered and have their correct value`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| fsiSession.EvalInteraction("let y = 2") | ||
| fsiSession.EvalInteraction("let z = 3") | ||
| fsiSession.EvalInteraction("let a = 4") | ||
| fsiSession.EvalInteraction("let ccc = 5") | ||
| fsiSession.EvalInteraction("let b = 6") | ||
| fsiSession.EvalInteraction("let aa = 7") | ||
|
|
||
| let values = fsiSession.GetBoundValues() |> List.map (fun x -> x.Value.ReflectionValue) | ||
|
|
||
| Assert.AreEqual([4;7;6;5;1;2;3], values) | ||
|
|
||
| [<Test>] | ||
| let ``Seven bound values are ordered and have their correct type`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| fsiSession.EvalInteraction("let y = 2") | ||
| fsiSession.EvalInteraction("let z = 3") | ||
| fsiSession.EvalInteraction("let a = 4.") | ||
| fsiSession.EvalInteraction("let ccc = 5") | ||
| fsiSession.EvalInteraction("let b = 6.f") | ||
| fsiSession.EvalInteraction("let aa = 7") | ||
|
|
||
| let types = fsiSession.GetBoundValues() |> List.map (fun x -> x.Value.ReflectionType) | ||
|
|
||
| Assert.AreEqual([typeof<float>;typeof<int>;typeof<float32>;typeof<int>;typeof<int>;typeof<int>;typeof<int>], types) | ||
|
|
||
| [<Test>] | ||
| let ``Able to find a bound value by the identifier`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| fsiSession.EvalInteraction("let y = 2") | ||
| fsiSession.EvalInteraction("let z = 3") | ||
| fsiSession.EvalInteraction("let a = 4") | ||
| fsiSession.EvalInteraction("let ccc = 5") | ||
| fsiSession.EvalInteraction("let b = 6") | ||
| fsiSession.EvalInteraction("let aa = 7") | ||
|
|
||
| let boundValueOpt = fsiSession.TryFindBoundValue "ccc" | ||
|
|
||
| Assert.IsTrue boundValueOpt.IsSome | ||
|
|
||
| [<Test>] | ||
| let ``Able to find a bound value by the identifier and has valid info`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1.") | ||
| fsiSession.EvalInteraction("let y = 2.") | ||
| fsiSession.EvalInteraction("let z = 3") | ||
| fsiSession.EvalInteraction("let a = 4.") | ||
| fsiSession.EvalInteraction("let ccc = 5.") | ||
| fsiSession.EvalInteraction("let b = 6.") | ||
| fsiSession.EvalInteraction("let aa = 7.") | ||
|
|
||
| let boundValue = (fsiSession.TryFindBoundValue "z").Value | ||
|
|
||
| Assert.AreEqual("z", boundValue.Name) | ||
| Assert.AreEqual(3, boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int>, boundValue.Value.ReflectionType) | ||
|
|
||
| [<Test>] | ||
| let ``Not Able to find a bound value by the identifier`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| fsiSession.EvalInteraction("let y = 2") | ||
| fsiSession.EvalInteraction("let z = 3") | ||
| fsiSession.EvalInteraction("let a = 4") | ||
| fsiSession.EvalInteraction("let ccc = 5") | ||
| fsiSession.EvalInteraction("let b = 6") | ||
| fsiSession.EvalInteraction("let aa = 7") | ||
|
|
||
| let boundValueOpt = fsiSession.TryFindBoundValue "aaa" | ||
|
|
||
| Assert.IsTrue boundValueOpt.IsNone | ||
|
|
||
| [<Test>] | ||
| let ``The 'it' value does not exist at the start of a FSI session`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| let boundValueOpt = fsiSession.TryFindBoundValue "it" | ||
|
|
||
| Assert.IsTrue boundValueOpt.IsNone | ||
|
|
||
| [<Test>] | ||
| let ``The 'it' bound value does exists after a value is not explicitly bound`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("456") | ||
|
|
||
| let boundValueOpt = fsiSession.TryFindBoundValue "it" | ||
|
|
||
| Assert.IsTrue boundValueOpt.IsSome | ||
|
|
||
| [<Test>] | ||
| let ``The 'it' value does exists after a value is not explicitly bound and has valid info`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("456") | ||
|
|
||
| let boundValue = (fsiSession.TryFindBoundValue "it").Value | ||
|
|
||
| Assert.AreEqual("it", boundValue.Name) | ||
| Assert.AreEqual(456, boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int>, boundValue.Value.ReflectionType) | ||
|
|
||
| [<Test>] | ||
| let ``The latest shadowed value is only available`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| let boundValue = fsiSession.GetBoundValues() |> List.exactlyOne | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
| Assert.AreEqual(1, boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int>, boundValue.Value.ReflectionType) | ||
|
|
||
| fsiSession.EvalInteraction("let x = (1, 2)") | ||
| let boundValue = fsiSession.GetBoundValues() |> List.exactlyOne | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
| Assert.AreEqual((1, 2), boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int * int>, boundValue.Value.ReflectionType) | ||
|
|
||
| [<Test>] | ||
| let ``The latest shadowed value is only available and can be found`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| let boundValue = (fsiSession.TryFindBoundValue "x").Value | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
| Assert.AreEqual(1, boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int>, boundValue.Value.ReflectionType) | ||
|
|
||
| fsiSession.EvalInteraction("let x = (1, 2)") | ||
| let boundValue = (fsiSession.TryFindBoundValue "x").Value | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
| Assert.AreEqual((1, 2), boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int * int>, boundValue.Value.ReflectionType) | ||
|
|
||
| [<Test>] | ||
| let ``Values are successfully shadowed even with intermediate interactions`` () = | ||
| use fsiSession = createFsiSession () | ||
|
|
||
| fsiSession.EvalInteraction("let x = 1") | ||
| fsiSession.EvalInteraction("let z = 100") | ||
| fsiSession.EvalInteraction("let x = (1, 2)") | ||
| fsiSession.EvalInteraction("let w = obj ()") | ||
|
|
||
| let boundValues = fsiSession.GetBoundValues() | ||
|
|
||
| Assert.AreEqual(3, boundValues.Length) | ||
|
|
||
| let boundValue = boundValues |> List.find (fun x -> x.Name = "x") | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
| Assert.AreEqual((1, 2), boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int * int>, boundValue.Value.ReflectionType) | ||
|
|
||
| let boundValue = (fsiSession.TryFindBoundValue "x").Value | ||
|
|
||
| Assert.AreEqual("x", boundValue.Name) | ||
| Assert.AreEqual((1, 2), boundValue.Value.ReflectionValue) | ||
| Assert.AreEqual(typeof<int * int>, boundValue.Value.ReflectionType) |
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.
When would
istate.boundValueshave an item, only to not actually be found bytryGetGeneratedValue?Uh oh!
There was an error while loading. Please reload this page.
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 think I see what you are saying. Why would there be a
tryin the first place? If it's there, then we don't need totry. I can always seetryGetGeneratedValuereturning something for each item inboundValues. We could throw an error instead of returning nothing, but I was following the existing pattern of handling results when looking up the generated values.