<commit_before>module Fable.Tests.Convert

open System
open System.Globalization
open Util.Testing
open Fable.Tests.Util

//-------------------------------------
// Parse and TryParse
//-------------------------------------

let tryParse f initial (value: string) =
    #if FABLE_COMPILER
    f(value)
    #else
    let res = ref initial
    let success = f(value, NumberStyles.Number, CultureInfo("en-US"), res)
    (success, !res)
    #endif

let parse f (a: string) =
    #if FABLE_COMPILER
    f(a)
    #else
    f(a, CultureInfo("en-US"))
    #endif

let tests =
  testList "Convert" [
    testCase "System.Double.Parse works" <| fun () ->
        parse Double.Parse "1.5" |> equal 1.5

    testCase "System.Double.TryParse works" <| fun () ->
        tryParse Double.TryParse 0.0 "1" |> equal (true, 1.0)
        tryParse Double.TryParse 0.0 "    1     " |> equal (true, 1.0)
        tryParse Double.TryParse 0.0 "1.5" |> equal (true, 1.5)
        tryParse Double.TryParse 0.0 "    1.5     " |> equal (true, 1.5)
        tryParse Double.TryParse 0.0 "foo" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "      " |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "9X" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "X9" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "X9TRE34" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "9SayWhat12Huh" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "-1.5" |> equal (true, -1.5)

    testCase "System.Decimal.Parse works" <| fun () ->
        parse Decimal.Parse "1.5" |> equal 1.5M

    testCase "System.Decimal.TryParse works" <| fun () ->
        let equal expected (success, actual) =
            match expected with
            | Some expected -> equal true success; equal expected actual
            | None -> equal false success
        tryParse Decimal.TryParse 0.0M "1" |> equal (Some 1.0M)
        tryParse Decimal.TryParse 0.0M "    1     " |> equal (Some 1.0M)
        tryParse Decimal.TryParse 0.0M "1.5" |> equal (Some 1.5M)
        tryParse Decimal.TryParse 0.0M "    1.5     " |> equal (Some 1.5M)
        tryParse Decimal.TryParse 0.0M "foo" |> equal None
        tryParse Decimal.TryParse 0.0M "9X" |> equal None
        tryParse Decimal.TryParse 0.0M "X9" |> equal None
        tryParse Decimal.TryParse 0.0M "X9TRE34" |> equal None
        tryParse Decimal.TryParse 0.0M "9SayWhat12Huh" |> equal None
        tryParse Decimal.TryParse 0.0M "-1.5" |> equal (Some -1.5M)

    testCase "System.Single.Parse works" <| fun () ->
        parse Single.Parse "1.5" |> equal 1.5f

    testCase "System.Single.TryParse works" <| fun () ->
        tryParse Single.TryParse 0.0f "1" |> equal (true, 1.0f)
        tryParse Single.TryParse 0.0f "    1     " |> equal (true, 1.0f)
        tryParse Single.TryParse 0.0f "1.5" |> equal (true, 1.5f)
        tryParse Single.TryParse 0.0f "    1.5     " |> equal (true, 1.5f)
        tryParse Single.TryParse 0.0f "foo" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "9X" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "X9" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "X9TRE34" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "9SayWhat12Huh" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "-1.5" |> equal (true, -1.5f)

    testCase "System.SByte.Parse works" <| fun () ->
        SByte.Parse("5") |> equal 5y
        SByte.Parse("-5") |> equal -5y
        SByte.Parse("-128") |> equal -128y
        (fun () -> SByte.Parse("128")) |> throwsError ""
        (fun () -> SByte.Parse("5f")) |> throwsError "Input string was not in a correct format."
        (fun () -> SByte.Parse("F")) |> throwsError "Input string was not in a correct format."
        (fun () -> SByte.Parse("5o")) |> throwsError "Input string was not in a correct format."

    testCase "System.SByte.Parse with hex works" <| fun () ->
        SByte.Parse("55", System.Globalization.NumberStyles.HexNumber) |> equal 85y
        SByte.Parse("5f", System.Globalization.NumberStyles.HexNumber) |> equal 95y
        SByte.Parse("FF", System.Globalization.NumberStyles.HexNumber) |> equal -1y
        (fun () -> SByte.Parse("1FF", System.Globalization.NumberStyles.HexNumber)) |> throwsError ""
        (fun () -> SByte.Parse("5o", System.Globalization.NumberStyles.HexNumber)) |> throwsError "Input string was not in a correct format."
        (fun () -> SByte.Parse("o5", System.Globalization.NumberStyles.HexNumber)) |> throwsError "Input string was not in a correct format."

    testCase "System.Int16.Parse works" <| fun () ->
        Int16.Parse("5") |> equal 5s
        Int16.Parse("-5") |> equal -5s
        Int16.Parse("-32768") |> equal -32768s
        (fun () -> Int16.Parse("32768")) |> throwsError ""
        (fun () -> Int16.Parse("5f")) |> throwsError "Input string was not in a correct format."
        (fun () -> Int16.Parse("FFF")) |> throwsError "Input string was not in a correct format."<commit_msg>Add a couple of tests<commit_after>module Fable.Tests.Convert

open System
open System.Globalization
open Util.Testing
open Fable.Tests.Util

//-------------------------------------
// Parse and TryParse
//-------------------------------------

let tryParse f initial (value: string) =
    #if FABLE_COMPILER
    f(value)
    #else
    let res = ref initial
    let success = f(value, NumberStyles.Number, CultureInfo("en-US"), res)
    (success, !res)
    #endif

let parse f (a: string) =
    #if FABLE_COMPILER
    f(a)
    #else
    f(a, CultureInfo("en-US"))
    #endif

let tests =
  testList "Convert" [
    testCase "System.Double.Parse works" <| fun () ->
        parse Double.Parse "1.5" |> equal 1.5

    testCase "System.Double.TryParse works" <| fun () ->
        tryParse Double.TryParse 0.0 "1" |> equal (true, 1.0)
        tryParse Double.TryParse 0.0 "    1     " |> equal (true, 1.0)
        tryParse Double.TryParse 0.0 "1.5" |> equal (true, 1.5)
        tryParse Double.TryParse 0.0 "    1.5     " |> equal (true, 1.5)
        tryParse Double.TryParse 0.0 "foo" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "      " |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "9X" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "X9" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "X9TRE34" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "9SayWhat12Huh" |> equal (false, 0.0)
        tryParse Double.TryParse 0.0 "-1.5" |> equal (true, -1.5)

    testCase "System.Decimal.Parse works" <| fun () ->
        parse Decimal.Parse "1.5" |> equal 1.5M

    testCase "System.Decimal.TryParse works" <| fun () ->
        let equal expected (success, actual) =
            match expected with
            | Some expected -> equal true success; equal expected actual
            | None -> equal false success
        tryParse Decimal.TryParse 0.0M "1" |> equal (Some 1.0M)
        tryParse Decimal.TryParse 0.0M "    1     " |> equal (Some 1.0M)
        tryParse Decimal.TryParse 0.0M "1.5" |> equal (Some 1.5M)
        tryParse Decimal.TryParse 0.0M "    1.5     " |> equal (Some 1.5M)
        tryParse Decimal.TryParse 0.0M "foo" |> equal None
        tryParse Decimal.TryParse 0.0M "9X" |> equal None
        tryParse Decimal.TryParse 0.0M "X9" |> equal None
        tryParse Decimal.TryParse 0.0M "X9TRE34" |> equal None
        tryParse Decimal.TryParse 0.0M "9SayWhat12Huh" |> equal None
        tryParse Decimal.TryParse 0.0M "-1.5" |> equal (Some -1.5M)

    testCase "System.Single.Parse works" <| fun () ->
        parse Single.Parse "1.5" |> equal 1.5f

    testCase "System.Single.TryParse works" <| fun () ->
        tryParse Single.TryParse 0.0f "1" |> equal (true, 1.0f)
        tryParse Single.TryParse 0.0f "    1     " |> equal (true, 1.0f)
        tryParse Single.TryParse 0.0f "1.5" |> equal (true, 1.5f)
        tryParse Single.TryParse 0.0f "    1.5     " |> equal (true, 1.5f)
        tryParse Single.TryParse 0.0f "foo" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "9X" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "X9" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "X9TRE34" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "9SayWhat12Huh" |> equal (false, 0.0f)
        tryParse Single.TryParse 0.0f "-1.5" |> equal (true, -1.5f)

    testCase "System.Boolean.Parse works" <| fun () ->
        Boolean.Parse "true" |> equal true
        Boolean.Parse "True" |> equal true
        Boolean.Parse " true " |> equal true
        Boolean.Parse "false" |> equal false
        Boolean.Parse "False" |> equal false
        Boolean.Parse " false " |> equal false

        (fun () -> Boolean.Parse "tru") |> throwsError "String 'tru' was not recognized as a valid Boolean."
        (fun () -> Boolean.Parse "falsee") |> throwsError "String 'falsee' was not recognized as a valid Boolean."

    testCase "System.Boolean.Parse works" <| fun () ->
        Boolean.TryParse "true" |> equal (true, true)
        Boolean.TryParse "True" |> equal (true, true)
        Boolean.TryParse " true " |> equal (true, true)
        Boolean.TryParse "false" |> equal (true, false)
        Boolean.TryParse "False" |> equal (true, false)
        Boolean.TryParse " false " |> equal (true, false)

        Boolean.TryParse "tru" |> equal (false, false)
        Boolean.TryParse "falsee" |> equal (false, false)

    testCase "System.SByte.Parse works" <| fun () ->
        SByte.Parse("5") |> equal 5y
        SByte.Parse("-5") |> equal -5y
        SByte.Parse("-128") |> equal -128y
        (fun () -> SByte.Parse("128")) |> throwsError ""
        (fun () -> SByte.Parse("5f")) |> throwsError "Input string was not in a correct format."
        (fun () -> SByte.Parse("F")) |> throwsError "Input string was not in a correct format."
        (fun () -> SByte.Parse("5o")) |> throwsError "Input string was not in a correct format."

    testCase "System.SByte.Parse with hex works" <| fun () ->
        SByte.Parse("55", System.Globalization.NumberStyles.HexNumber) |> equal 85y
        SByte.Parse("5f", System.Globalization.NumberStyles.HexNumber) |> equal 95y
        SByte.Parse("FF", System.Globalization.NumberStyles.HexNumber) |> equal -1y
        (fun () -> SByte.Parse("1FF", System.Globalization.NumberStyles.HexNumber)) |> throwsError ""
        (fun () -> SByte.Parse("5o", System.Globalization.NumberStyles.HexNumber)) |> throwsError "Input string was not in a correct format."
        (fun () -> SByte.Parse("o5", System.Globalization.NumberStyles.HexNumber)) |> throwsError "Input string was not in a correct format."

    testCase "System.Int16.Parse works" <| fun () ->
        Int16.Parse("5") |> equal 5s
        Int16.Parse("-5") |> equal -5s
        Int16.Parse("-32768") |> equal -32768s
        (fun () -> Int16.Parse("32768")) |> throwsError ""
        (fun () -> Int16.Parse("5f")) |> throwsError "Input string was not in a correct format."
        (fun () -> Int16.Parse("FFF")) |> throwsError "Input string was not in a correct format."
{'fraction_non_alphanumeric': 0.15940090933404655, 'fraction_numerical': 0.04243558883837033, 'mean_word_length': 2.3738345864661654, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 173, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5256528', 'n_tokens_mistral': 3989, 'n_tokens_neox': 3788, 'n_words': 1023}