Skip to content
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

Add applicative test for accessor function shorthand #3572

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ RUN apt-get update && apt-get install -y xz-utils

# Options
# Let it here to avoid conflicts with Microsoft options !!!
ARG DOTNET_VERSION=6.0.406
ARG DOTNET_VERSION=8.0.100
ARG NODE_VERSION=18.8.0
ARG PYTHON_VERSION=3.11.5
ARG DART_VERSION=3.1.2
Expand Down Expand Up @@ -103,4 +103,4 @@ RUN rm /home/vscode/install_dart.sh

# Force docker to load the zsh profile
# This should be the last steps
CMD [ "/bin/zsh" ]
CMD [ "/bin/zsh" ]
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
Expand All @@ -35,7 +35,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
Expand All @@ -56,7 +56,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
Expand All @@ -74,7 +74,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
Expand All @@ -96,7 +96,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup dotnet tools
run: dotnet tool restore
Expand Down Expand Up @@ -142,7 +142,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup Node.js environment
uses: actions/setup-node@v2.4.1
Expand All @@ -168,7 +168,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Setup Dart SDK
uses: dart-lang/setup-dart@v1.3
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.100",
"version": "8.0.0",
Copy link
Collaborator

@ncave ncave Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MangelMaxime Should be 8.0.100, no? Fixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right indeed, I suppose because we have "rollForward": "latestMinor" it didn't break on me.

But better to use a real version

"rollForward": "latestMajor"
}
}
4 changes: 2 additions & 2 deletions src/quicktest/Quicktest.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RollForward>Major</RollForward>
<!-- <LangVersion>Preview</LangVersion> -->
<LangVersion>Preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="QuickTest.fs" />
Expand All @@ -12,4 +12,4 @@
<ItemGroup>
<ProjectReference Include="..\Fable.Core\Fable.Core.fsproj" />
</ItemGroup>
</Project>
</Project>
47 changes: 47 additions & 0 deletions tests/Js/Main/ApplicativeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,9 @@
testCase "SRTP with inlined functions relying on generic info works" <| fun () -> // See #2135
let t2: int option =
Error 1
|> fun x -> (^``Applicative<'T>`` : (static member Return : ^T -> ^``Applicative<'T>``) x)

Check warning on line 1084 in tests/Js/Main/ApplicativeTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.
// |> Const.Return
|> Const.run

Check warning on line 1086 in tests/Js/Main/ApplicativeTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1086 in tests/Js/Main/ApplicativeTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.
|> First.run

equal None t2
Expand Down Expand Up @@ -1700,7 +1700,7 @@
static member inline map (Functor, f: 'a -> 'b, x: List<'a>) = List.map f x

let inline map (f: ^a -> ^b) (x: ^x) =
((^x or ^Functor): (static member map: ^Functor * (^a -> ^b) * ^x -> ^r) (Functor, f, x))

Check warning on line 1703 in tests/Js/Main/ApplicativeTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

type NonEmptyList<'T> = NonEmptyList of 'T * List<'T> with
static member inline map (Functor, f: 'a -> 'b, NonEmptyList(h, t): NonEmptyList<'a>) =
Expand Down Expand Up @@ -1732,6 +1732,52 @@
NonEmptyList("a", ["b"; "c"]) |> mapMyList |> equal (NonEmptyList("a_", ["b_"; "c_"]))
]

module AccessorFunctionShorthand =

type User =
{
Name : string
}

type Student =
{
Name : string
Age : int
}

let inline namePropertyGetter<'a when 'a:(member Name: string)> (x: 'a) = x |> _.Name

let tests =
[
testCase "Accessor function shorthand works for records" <| fun () ->
let people =
[
{ Name = "John" }
{ Name = "Jane" }
]

let names = people |> List.map _.Name
equal names ["John"; "Jane"]

testCase "Accessor function shorthand works for anonymous records" <| fun () ->
let people =
[
{| Name = "John" |}
{| Name = "Jane" |}
]

let names = people |> List.map _.Name
equal names ["John"; "Jane"]

testCase "Accessor function shorthand works with STRP syntax" <| fun () ->
let user =
{ Name = "John" }
let student =
{ Name = "Jane"; Age = 20 }

equal (namePropertyGetter user) "John"
equal (namePropertyGetter student) "Jane"
]
let tests =
testList "Applicative" (
tests1
Expand All @@ -1745,4 +1791,5 @@
@ Curry.tests
@ Uncurry.tests
@ MultipleInlines.tests
@ AccessorFunctionShorthand.tests
)
5 changes: 1 addition & 4 deletions tests/Js/Main/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,6 @@ let tests =
(3, 'c'); (3, 'd'); (3, 'e'); (3, 'f'); (4, 'a'); (4, 'b'); (4, 'c');
(4, 'd'); (4, 'e'); (4, 'f')]

// TODO: Remove conditional compilation after upgrading to dotnet SDK with F# 4.7
// #if FABLE_COMPILER
testCase "Implicit yields work" <| fun () ->
let makeList condition =
[
Expand All @@ -879,7 +877,6 @@ let tests =
]
makeList true |> List.sum |> equal 6
makeList false |> List.sum |> equal 3
// #endif

testCase "List.splitInto works" <| fun () ->
[1..10] |> List.splitInto 3 |> equal [ [1..4]; [5..7]; [8..10] ]
Expand Down Expand Up @@ -993,4 +990,4 @@ let tests =
throwsAnyError (fun () -> List.removeManyAt 0 2 [] |> ignore)
throwsAnyError (fun () -> List.removeManyAt -1 2 [1] |> ignore)
throwsAnyError (fun () -> List.removeManyAt 2 2 [1] |> ignore)
]
]
24 changes: 23 additions & 1 deletion tests/Js/Main/RecordTypeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type Time =
static member inline duration(value: {| from: int; until: int |}) = value.until - value.from
static member inline duration(value: {| from: int |}) = Time.duration {| value with until = 10 |}

type CarInterior = { Seats: int }
type Car = { Interior: CarInterior }

let tests =
testList "RecordTypes" [

Expand Down Expand Up @@ -120,4 +123,23 @@ let tests =
let x'' = { x' with uniqueA = -10 }
equal -10 x''.uniqueA
equal -20 x''.uniqueB
]

testCase "Nested record fiel opy and update works for records" <| fun () ->
let car =
{ Interior = { Seats = 4 } }

let car2 =
{ car with Interior.Seats = 5 }

equal 5 car2.Interior.Seats

testCase "Nested record fiel copy and update works for anonymous records" <| fun () ->
let car =
{| Interior = {| Seats = 4 |} |}

let car2 =
{| car with Interior.Seats = 5 |}

equal 5 car2.Interior.Seats

]
24 changes: 23 additions & 1 deletion tests/Js/Main/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ let LINE_SEPARATOR = "\u2028"
let [<Literal>] aLiteral = "foo"
let notALiteral = "foo"

[<Literal>]
let formatCoordinateBody = "(%f,%f)"

[<Literal>]
let formatPrefix = "Person at coordinates"

[<Literal>]
let fullFormat = formatPrefix + formatCoordinateBody

type MyUnion = Bar of int * int | Foo1 of float | Foo3 | Foo4 of MyUnion

type Test(i: int) =
Expand Down Expand Up @@ -160,6 +169,13 @@ let tests =
sprintf "%d" -1L |> equal "-1"
sprintf "%.2f" -1. |> equal "-1.00"

testCase "format string can use and compose string literals" <| fun () ->
let renderedCoordinates = sprintf formatCoordinateBody 0.25 0.75
let renderedText = sprintf fullFormat 0.25 0.75

equal "(0.250000,0.750000)" renderedCoordinates
equal "Person at coordinates(0.250000,0.750000)" renderedText

testCase "Print.sprintf works" <| fun () -> // See #1216
let res = Printf.sprintf "%s" "abc"
equal "res: abc" ("res: " + res)
Expand Down Expand Up @@ -288,6 +304,12 @@ let tests =
$"b=(%O{b})" |> equal "b=(a=6)"
$"%O{b}%O{b}" |> equal "a=7a=8"

testCase "Extended string interpolation syntax" <| fun () ->
let classAttr = "item-panel"
let cssNew = $$""".{{classAttr}}:hover {background-color: #eee;}"""

cssNew |> equal ".item-panel:hover {background-color: #eee;}"

testCase "sprintf \"%A\" with lists works" <| fun () ->
let xs = ["Hi"; "Hello"; "Hola"]
(sprintf "%A" xs).Replace("\"", "") |> equal "[Hi; Hello; Hola]"
Expand Down Expand Up @@ -1098,4 +1120,4 @@ let tests =
let s3: FormattableString = $"""I have no holes"""
s3.GetStrings() |> toArray |> equal [|"I have no holes"|]
#endif
]
]
47 changes: 47 additions & 0 deletions tests/Python/TestApplicative.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,9 +1085,9 @@
let ``test SRTP with inlined functions relying on generic info works`` () = // See #2135
let t2: int option =
Error 1
|> fun x -> (^``Applicative<'T>`` : (static member Return : ^T -> ^``Applicative<'T>``) x)

Check warning on line 1088 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.11)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.

Check warning on line 1088 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.10)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.

Check warning on line 1088 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.

Check warning on line 1088 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.11)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.

Check warning on line 1088 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.10)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.

Check warning on line 1088 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.12)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'T has been constrained to be type 'Result<'a,int>'.
// |> Const.Return
|> Const.run

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.11)

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.11)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.10)

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.10)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.11)

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.11)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.10)

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.10)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.12)

The instantiation of the generic type 'Const' is missing and can't be inferred from the arguments or return type of this member. Consider providing a type instantiation when accessing this type, e.g. 'Const<_,_>'.

Check warning on line 1090 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.12)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Applicative<'T> has been constrained to be type 'Const<'a,Result<'b,int>>'.
|> First.run

equal None t2
Expand Down Expand Up @@ -1650,7 +1650,7 @@
static member inline map (Functor, f: 'a -> 'b, x: List<'a>) = List.map f x

let inline map (f: ^a -> ^b) (x: ^x) =
((^x or ^Functor): (static member map: ^Functor * (^a -> ^b) * ^x -> ^r) (Functor, f, x))

Check warning on line 1653 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.11)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

Check warning on line 1653 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.10)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

Check warning on line 1653 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

Check warning on line 1653 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.11)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

Check warning on line 1653 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.10)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

Check warning on line 1653 in tests/Python/TestApplicative.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.12)

This construct causes code to be less generic than indicated by the type annotations. The type variable 'Functor has been constrained to be type 'Functor'.

type NonEmptyList<'T> = NonEmptyList of 'T * List<'T> with
static member inline map (Functor, f: 'a -> 'b, NonEmptyList(h, t): NonEmptyList<'a>) =
Expand Down Expand Up @@ -1684,3 +1684,50 @@
[<Fact>]
let ``test Identifiers from witnesses don't get duplicated when resolving inline expressions`` () = // See #2855
NonEmptyList("a", ["b"; "c"]) |> mapMyList |> equal (NonEmptyList("a_", ["b_"; "c_"]))

module AccessorFunctionShorthand =

type User =
{
Name : string
}

type Student =
{
Name : string
Age : int
}

let inline namePropertyGetter<'a when 'a:(member Name: string)> (x: 'a) = x |> _.Name

[<Fact>]
let ``test Accessor function shorthand works for records`` () =
let people : AccessorFunctionShorthand.User list =
[
{ Name = "John" }
{ Name = "Jane" }
]

let names = people |> List.map _.Name
equal names ["John"; "Jane"]

[<Fact>]
let ``test Accessor function shorthand works for anonymous records`` () =
let people =
[
{| Name = "John" |}
{| Name = "Jane" |}
]

let names = people |> List.map _.Name
equal names ["John"; "Jane"]

[<Fact>]
let ``test Accessor function shorthand works with STRP syntax`` =
let user : AccessorFunctionShorthand.User =
{ Name = "John" }
let student : AccessorFunctionShorthand.Student =
{ Name = "Jane"; Age = 20 }

equal (AccessorFunctionShorthand.namePropertyGetter user) "John"
equal (AccessorFunctionShorthand.namePropertyGetter student) "Jane"
23 changes: 23 additions & 0 deletions tests/Python/TestRecordType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Time =
static member inline duration(value: {| from: int; until: int |}) = value.until - value.from
static member inline duration(value: {| from: int |}) = Time.duration {| value with until = 10 |}

type CarInterior = { Seats: int }
type Car = { Interior: CarInterior }

[<Fact>]
let ``test Anonymous records work`` () =
let r = makeAnonRec()
Expand Down Expand Up @@ -125,3 +128,23 @@ let ``test Mutating records work`` () =
let x'' = { x' with uniqueA = -10 }
equal -10 x''.uniqueA
equal -20 x''.uniqueB

[<Fact>]
let ``test Nested record fiel copy and update works for records`` =
let car =
{ Interior = { Seats = 4 } }

let car2 =
{ car with Interior.Seats = 5 }

equal 5 car2.Interior.Seats

[<Fact>]
let ``test Nested record fiel copy and update works for anonymous records`` =
let car =
{| Interior = {| Seats = 4 |} |}

let car2 =
{| car with Interior.Seats = 5 |}

equal 5 car2.Interior.Seats
Loading
Loading