elm-format

pull/3/head
Bram 2023-12-21 23:17:34 +01:00
parent 88818abe20
commit 978d6c6315
3 changed files with 168 additions and 114 deletions

View File

@ -2,11 +2,11 @@ module Context exposing (..)
import Expect import Expect
import Fuzz exposing (Fuzzer) import Fuzz exposing (Fuzzer)
import Test exposing (..)
import Internal.Values.Context as Context exposing (Context)
import Internal.Config.Leaks as Leaks import Internal.Config.Leaks as Leaks
import Json.Encode as E import Internal.Values.Context as Context exposing (Context)
import Json.Decode as D import Json.Decode as D
import Json.Encode as E
import Test exposing (..)
fuzzer : Fuzzer Context fuzzer : Fuzzer Context
@ -16,14 +16,15 @@ fuzzer =
maybeString = maybeString =
Fuzz.maybe Fuzz.string Fuzz.maybe Fuzz.string
in in
Fuzz.map7 Context Fuzz.map7 Context
maybeString maybeString
maybeString maybeString
maybeString maybeString
maybeString maybeString
maybeString maybeString
maybeString maybeString
(Fuzz.maybe <| Fuzz.list Fuzz.string) (Fuzz.maybe <| Fuzz.list Fuzz.string)
{-| If a leak is spotted, make sure to change the leaking value and then test {-| If a leak is spotted, make sure to change the leaking value and then test
with the same seed to ensure it is not a (tiny) coincidence and a leak is in with the same seed to ensure it is not a (tiny) coincidence and a leak is in
@ -32,7 +33,9 @@ fact coming through.
leaks : Test leaks : Test
leaks = leaks =
describe "No leaks allowed" describe "No leaks allowed"
[ fuzz2 fuzzer Fuzz.string "Access token" [ fuzz2 fuzzer
Fuzz.string
"Access token"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -40,7 +43,9 @@ leaks =
|> Context.getAccessToken |> Context.getAccessToken
|> Expect.notEqual Leaks.accessToken |> Expect.notEqual Leaks.accessToken
) )
, fuzz2 fuzzer Fuzz.string "Base URL" , fuzz2 fuzzer
Fuzz.string
"Base URL"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -48,7 +53,9 @@ leaks =
|> Context.getBaseUrl |> Context.getBaseUrl
|> Expect.notEqual Leaks.baseUrl |> Expect.notEqual Leaks.baseUrl
) )
, fuzz2 fuzzer Fuzz.string "Transaction" , fuzz2 fuzzer
Fuzz.string
"Transaction"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -56,7 +63,9 @@ leaks =
|> Context.getTransaction |> Context.getTransaction
|> Expect.notEqual Leaks.transaction |> Expect.notEqual Leaks.transaction
) )
, fuzz2 fuzzer (Fuzz.list Fuzz.string) "Versions" , fuzz2 fuzzer
(Fuzz.list Fuzz.string)
"Versions"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -66,10 +75,13 @@ leaks =
) )
] ]
apiContext : Test apiContext : Test
apiContext = apiContext =
describe "Verify writing info" describe "Verify writing info"
[ fuzz2 fuzzer Fuzz.string "Access token" [ fuzz2 fuzzer
Fuzz.string
"Access token"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -77,7 +89,9 @@ apiContext =
|> Context.getAccessToken |> Context.getAccessToken
|> Expect.equal value |> Expect.equal value
) )
, fuzz2 fuzzer Fuzz.string "Base URL" , fuzz2 fuzzer
Fuzz.string
"Base URL"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -85,7 +99,9 @@ apiContext =
|> Context.getBaseUrl |> Context.getBaseUrl
|> Expect.equal value |> Expect.equal value
) )
, fuzz2 fuzzer Fuzz.string "Transaction" , fuzz2 fuzzer
Fuzz.string
"Transaction"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -93,7 +109,9 @@ apiContext =
|> Context.getTransaction |> Context.getTransaction
|> Expect.equal value |> Expect.equal value
) )
, fuzz2 fuzzer (Fuzz.list Fuzz.string) "Versions" , fuzz2 fuzzer
(Fuzz.list Fuzz.string)
"Versions"
(\context value -> (\context value ->
context context
|> Context.apiFormat |> Context.apiFormat
@ -103,17 +121,19 @@ apiContext =
) )
] ]
json : Test json : Test
json = json =
describe "JSON encode + JSON decode" describe "JSON encode + JSON decode"
[ test "Empty is {}" [ test "Empty is {}"
( Context.init (Context.init
|> Context.encode |> Context.encode
|> E.encode 0 |> E.encode 0
|> Expect.equal "{}" |> Expect.equal "{}"
|> always |> always
) )
, fuzz fuzzer "JSON recode" , fuzz fuzzer
"JSON recode"
(\context -> (\context ->
context context
|> Context.encode |> Context.encode

View File

@ -4,9 +4,8 @@ import Expect
import Fuzz exposing (Fuzzer) import Fuzz exposing (Fuzzer)
import Internal.Tools.Iddict as Iddict exposing (Iddict) import Internal.Tools.Iddict as Iddict exposing (Iddict)
import Json.Decode as D import Json.Decode as D
import Test exposing (..)
import Json.Encode as E import Json.Encode as E
import Internal.Tools.Iddict as Iddict import Test exposing (..)
fuzzer : Fuzzer a -> Fuzzer (Iddict a) fuzzer : Fuzzer a -> Fuzzer (Iddict a)
@ -94,6 +93,7 @@ empty =
) )
] ]
singleton : Test singleton : Test
singleton = singleton =
let let
@ -106,86 +106,100 @@ singleton =
) )
Fuzz.int Fuzz.int
in in
describe "singleton" describe "singleton"
[ fuzz singleFuzzer "not isEmpty" [ fuzz singleFuzzer
(\single -> "not isEmpty"
single (\single ->
|> Iddict.isEmpty single
|> Expect.equal False |> Iddict.isEmpty
) |> Expect.equal False
, fuzz Fuzz.int "singleton == insert empty" )
(\i -> , fuzz Fuzz.int
Iddict.empty "singleton == insert empty"
|> Iddict.insert i (\i ->
|> Expect.equal (Iddict.singleton i) Iddict.empty
) |> Iddict.insert i
, fuzz Fuzz.int "First item is key 0" |> Expect.equal (Iddict.singleton i)
(\i -> )
Iddict.singleton i , fuzz Fuzz.int
|> Tuple.first "First item is key 0"
|> Expect.equal 0 (\i ->
) Iddict.singleton i
, fuzz singleFuzzer "Key 0 is member" |> Tuple.first
(\single -> |> Expect.equal 0
single )
|> Iddict.member 0 , fuzz singleFuzzer
|> Expect.equal True "Key 0 is member"
) (\single ->
, fuzz Fuzz.int "Key 0 get returns Just value" single
(\i -> |> Iddict.member 0
Iddict.singleton i |> Expect.equal True
|> Tuple.second )
|> Iddict.get 0 , fuzz Fuzz.int
|> Expect.equal (Just i) "Key 0 get returns Just value"
) (\i ->
, fuzz singleFuzzer "Size == 1" Iddict.singleton i
(\single -> |> Tuple.second
single |> Iddict.get 0
|> Iddict.size |> Expect.equal (Just i)
|> Expect.equal 1 )
) , fuzz singleFuzzer
, fuzz Fuzz.int "Only key 0" "Size == 1"
(\i -> (\single ->
Iddict.singleton i single
|> Tuple.second |> Iddict.size
|> Iddict.keys |> Expect.equal 1
|> Expect.equal [ 0 ] )
) , fuzz Fuzz.int
, fuzz Fuzz.int "Only value value" "Only key 0"
(\i -> (\i ->
Iddict.singleton i Iddict.singleton i
|> Tuple.second |> Tuple.second
|> Iddict.values |> Iddict.keys
|> Expect.equal [ i ] |> Expect.equal [ 0 ]
) )
, fuzz singleFuzzer "JSON encode -> decode -> singleton" , fuzz Fuzz.int
(\single -> "Only value value"
single (\i ->
|> Iddict.encode E.int Iddict.singleton i
|> D.decodeValue (Iddict.decoder D.int) |> Tuple.second
|> Expect.equal (Ok single) |> Iddict.values
) |> Expect.equal [ i ]
, fuzz Fuzz.int "JSON encode" )
(\i -> , fuzz singleFuzzer
Iddict.singleton i "JSON encode -> decode -> singleton"
|> Tuple.second (\single ->
|> Iddict.encode E.int single
|> E.encode 0 |> Iddict.encode E.int
|> Expect.equal ("{\"cursor\":1,\"dict\":{\"0\":" ++ (String.fromInt i) ++ "}}") |> D.decodeValue (Iddict.decoder D.int)
) |> Expect.equal (Ok single)
, fuzz Fuzz.int "JSON decode" )
(\i -> , fuzz Fuzz.int
("{\"cursor\":1,\"dict\":{\"0\":" ++ (String.fromInt i) ++ "}}") "JSON encode"
|> D.decodeString (Iddict.decoder D.int) (\i ->
|> Tuple.pair 0 Iddict.singleton i
|> Expect.equal (Iddict.singleton i |> Tuple.mapSecond Ok) |> Tuple.second
) |> Iddict.encode E.int
] |> E.encode 0
|> Expect.equal ("{\"cursor\":1,\"dict\":{\"0\":" ++ String.fromInt i ++ "}}")
)
, fuzz Fuzz.int
"JSON decode"
(\i ->
("{\"cursor\":1,\"dict\":{\"0\":" ++ String.fromInt i ++ "}}")
|> D.decodeString (Iddict.decoder D.int)
|> Tuple.pair 0
|> Expect.equal (Iddict.singleton i |> Tuple.mapSecond Ok)
)
]
insert : Test insert : Test
insert = insert =
describe "insert" describe "insert"
[ fuzz2 (fuzzer Fuzz.int) Fuzz.int "Add something" [ fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Add something"
(\d i -> (\d i ->
case Iddict.insert i d of case Iddict.insert i d of
( key, dict ) -> ( key, dict ) ->
@ -193,14 +207,18 @@ insert =
|> Iddict.get key |> Iddict.get key
|> Expect.equal (Just i) |> Expect.equal (Just i)
) )
, fuzz2 (fuzzer Fuzz.int) Fuzz.int "Never isEmpty" , fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Never isEmpty"
(\d i -> (\d i ->
Iddict.insert i d Iddict.insert i d
|> Tuple.second |> Tuple.second
|> Iddict.isEmpty |> Iddict.isEmpty
|> Expect.equal False |> Expect.equal False
) )
, fuzz2 (fuzzer Fuzz.int) Fuzz.int "New key" , fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"New key"
(\d i -> (\d i ->
case Iddict.insert i d of case Iddict.insert i d of
( key, dict ) -> ( key, dict ) ->
@ -211,7 +229,9 @@ insert =
Expect.notEqual key newKey Expect.notEqual key newKey
) )
) )
, fuzz2 (fuzzer Fuzz.int) Fuzz.int "New dict" , fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"New dict"
(\d i -> (\d i ->
case Iddict.insert i d of case Iddict.insert i d of
( key, dict ) -> ( key, dict ) ->
@ -222,7 +242,9 @@ insert =
Expect.notEqual dict newDict Expect.notEqual dict newDict
) )
) )
, fuzz2 (fuzzer Fuzz.int) Fuzz.int "Inserted value is member" , fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Inserted value is member"
(\d i -> (\d i ->
case Iddict.insert i d of case Iddict.insert i d of
( key, dict ) -> ( key, dict ) ->
@ -230,7 +252,9 @@ insert =
|> Iddict.member key |> Iddict.member key
|> Expect.equal True |> Expect.equal True
) )
, fuzz2 (fuzzer Fuzz.int) Fuzz.int "Get inserted value" , fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"Get inserted value"
(\d i -> (\d i ->
case Iddict.insert i d of case Iddict.insert i d of
( key, dict ) -> ( key, dict ) ->
@ -238,12 +262,14 @@ insert =
|> Iddict.get key |> Iddict.get key
|> Expect.equal (Just i) |> Expect.equal (Just i)
) )
, fuzz2 (fuzzer Fuzz.int) Fuzz.int "size = size + 1" , fuzz2 (fuzzer Fuzz.int)
Fuzz.int
"size = size + 1"
(\d i -> (\d i ->
case Iddict.insert i d of case Iddict.insert i d of
( _, dict ) -> ( _, dict ) ->
Expect.equal Expect.equal
( Iddict.size dict ) (Iddict.size dict)
( Iddict.size d + 1 ) (Iddict.size d + 1)
) )
] ]

View File

@ -2,40 +2,48 @@ module Vault exposing (..)
import Expect import Expect
import Fuzz exposing (Fuzzer) import Fuzz exposing (Fuzzer)
import Internal.Config.Default as Default
import Internal.Values.Envelope as Envelope
import Matrix import Matrix
import Matrix.Settings import Matrix.Settings
import Test exposing (..) import Test exposing (..)
import Types import Types
import Internal.Values.Envelope as Envelope
import Internal.Config.Default as Default
fuzzer : Fuzzer Matrix.Vault fuzzer : Fuzzer Matrix.Vault
fuzzer = fuzzer =
Fuzz.constant <| Types.Vault <| Envelope.init {} Fuzz.constant <| Types.Vault <| Envelope.init {}
settings : Test settings : Test
settings = settings =
describe "Edit settings" describe "Edit settings"
[ fuzz fuzzer "Default device name" [ fuzz fuzzer
"Default device name"
(\vault -> (\vault ->
vault vault
|> Matrix.Settings.getDeviceName |> Matrix.Settings.getDeviceName
|> Expect.equal Default.deviceName |> Expect.equal Default.deviceName
) )
, fuzz2 fuzzer Fuzz.string "Set device name" , fuzz2 fuzzer
Fuzz.string
"Set device name"
(\vault name -> (\vault name ->
vault vault
|> Matrix.Settings.setDeviceName name |> Matrix.Settings.setDeviceName name
|> Matrix.Settings.getDeviceName |> Matrix.Settings.getDeviceName
|> Expect.equal name |> Expect.equal name
) )
, fuzz fuzzer "Default sync time" , fuzz fuzzer
"Default sync time"
(\vault -> (\vault ->
vault vault
|> Matrix.Settings.getSyncTime |> Matrix.Settings.getSyncTime
|> Expect.equal Default.syncTime |> Expect.equal Default.syncTime
) )
, fuzz2 fuzzer Fuzz.int "Set sync time" , fuzz2 fuzzer
Fuzz.int
"Set sync time"
(\vault sync -> (\vault sync ->
vault vault
|> Matrix.Settings.setSyncTime sync |> Matrix.Settings.setSyncTime sync