<commit_before>﻿module TypeConversionSpec
open FSpec.Core.DslV2
open FSpec.Core.MatchersV2
open JsonFSharp
open JsonParser

type FooType = {
    foo : string;
    foo2: int
    }

type ChildType = {
    foo: int
    }

type ParentType = {
    child: ChildType;
    bar: int
    }

let getSuccess = function
    | Success(x) -> x
    | Failure(x) -> failwith x

let stringToJson = JsonInput.fromString >> parse >> getSuccess
let jsonToObj<'T> = toInstance<'T> >> getSuccess

let specs = 
    describe "Type conversions" [
        it "should return instance of specified type" <| fun () ->
            let value = 
                """{ "foo": "bar", "foo2": 42 }"""      
                |> stringToJson
                |> jsonToObj<FooType>
            value.foo |> should equal "bar"

        it "should return parent type" <| fun () ->
            let value =
                """{ "child": { "foo": 42 }, "bar": 43 }"""
                |> stringToJson
                |> jsonToObj<ParentType>
            value.child.foo |> should equal 42
            value.bar |> should equal 43
    ]<commit_msg>JsonToObject test split into two<commit_after>﻿module TypeConversionSpec
open FSpec.Core.DslV2
open FSpec.Core.MatchersV2
open JsonFSharp
open JsonParser

type FooTypeWithString = { foo : string; }
type FooTypeWithInt = { foo : int; }
type ParentType = {
    child: FooTypeWithInt;
    bar: int
    }

let getSuccess = function
    | Success(x) -> x
    | Failure(x) -> failwith x

let stringToJson = JsonInput.fromString >> parse >> getSuccess
let jsonToObj<'T> = toInstance<'T> >> getSuccess

let specs = 
    describe "Type conversions" [
        describe "Simple type conversions" [
            it "an initialize string values" <| fun () ->
                let value = 
                    """{ "foo": "bar" }"""      
                    |> stringToJson
                    |> jsonToObj<FooTypeWithString>
                value.foo |> should equal "bar"
            it "can initialize integer values" <| fun () ->
                let value = 
                    """{ "foo": 42 }"""      
                    |> stringToJson
                    |> jsonToObj<FooTypeWithInt>
                value.foo |> should equal 42
        ]

        it "should return parent type" <| fun () ->
            let value =
                """{ "child": { "foo": 42 }, "bar": 43 }"""
                |> stringToJson
                |> jsonToObj<ParentType>
            value.child.foo |> should equal 42
            value.bar |> should equal 43
    ]
{'fraction_non_alphanumeric': 0.11711356466876971, 'fraction_numerical': 0.011041009463722398, 'mean_word_length': 1.7397408207343412, 'pattern_counts': {'":': 10, '<': 17, '<?xml version=': 0, '>': 50, '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': '2482535', 'n_tokens_mistral': 785, 'n_tokens_neox': 713, 'n_words': 200}