<commit_before>{-# LANGUAGE OverloadedStrings #-}
-- | This module defines a test that tries to walk over the
-- CodeGeneratorRequest in `tests/data/schema-codegenreq`,
-- failing if any of the data is not as expected.
module Tests.WalkSchemaCodeGenRequest
    (walkSchemaCodeGenRequestTest)
  where

import Prelude hiding (length)

import Control.Monad.Quota
import Data.CapNProto.Untyped
import Tests.Util

import Control.Monad             (mapM_, when)
import Data.CapNProto.BasicTypes (Text(..))
import Test.Framework            (Test)
import Test.HUnit                (Assertion, assertEqual)

import qualified Data.ByteString                                     as BS
import qualified Data.CapNProto.Message                              as M
import qualified Prelude
import qualified Schema.CapNProto.Reader.Schema                      as Schema
import qualified Schema.CapNProto.Reader.Schema.CodeGeneratorRequest as CGReq
import qualified Schema.CapNProto.Reader.Schema.Node                 as Node


-- | TODO: make this an array; we're doing random access to it below.
-- I(@zenhack) am waiting on this, since at the time of writing @taktoa
-- is working on some array utilities that will get merged soonish, so
-- it probably makes sense to just wait for that.
nodeNames :: [BS.ByteString]
nodeNames =
    [ "Import"
    , "annotation"
    , "Value"
    , "Type"
    ]

-- TODO: This contains a bit of copypasta from some of the untyped tests; should
-- factor that out.
theAssert :: Assertion
theAssert = do
    bytes <- BS.readFile "tests/data/schema-codegenreq"
    msg <- M.decode bytes
    ((), endQuota) <- runQuotaT (rootPtr msg >>= reader) 1024
    assertEqual "Correct remaining quota" 648 endQuota
  where
    reader :: Struct BS.ByteString -> QuotaT IO ()
    reader root = do
        let req = Schema.CodeGeneratorRequest root
        Just nodes <- CGReq.nodes req
        Just requestedFiles <- CGReq.requestedFiles req
        let 37 = length nodes
        let 1 = length requestedFiles
        mapM_ (walkNode nodes) [0,1..36]
    walkNode nodes i = do
        node <- index i nodes
        -- None of the nodes in the schema have parameters:
        Nothing <- Node.parameters node
        -- And none of them are generic:
        False <- Node.isGeneric node

        Just (Text name) <- Node.displayName node
        prefixLen <- Node.displayNamePrefixLength node
        let baseName = BS.drop (fromIntegral prefixLen) name

        when (i < Prelude.length nodeNames && baseName /= (nodeNames !! i)) $
            error "Incorrect name."

        has <- Node.hasAnnotations node

        -- there are two annotations in all of the nodes, at these indicies:
        case (has, i `elem` [4, 9]) of
            (False, False) -> return ()
            (True, True) -> do
                1 <- length <$> Node.annotations node
                return ()
            (False, True) ->
                error $ "Node at index " ++ show i ++ " should have had" ++
                        "an annotation."
            (True, False) ->
                error $ "Node at index " ++ show i ++ " should not " ++
                        "have had an annotation."<commit_msg>Fix the rest of the tests<commit_after>{-# LANGUAGE OverloadedStrings #-}
-- | This module defines a test that tries to walk over the
-- CodeGeneratorRequest in `tests/data/schema-codegenreq`,
-- failing if any of the data is not as expected.
module Tests.WalkSchemaCodeGenRequest
    (walkSchemaCodeGenRequestTest)
  where

import Prelude hiding (length)

import Control.Monad.Quota
import Data.CapNProto.Untyped
import Tests.Util

import Control.Monad             (mapM_, when)
import Data.CapNProto.BasicTypes (Text(..))
import Test.Framework            (Test)
import Test.HUnit                (Assertion, assertEqual)

import qualified Data.ByteString                                     as BS
import qualified Data.CapNProto.Message                              as M
import qualified Prelude
import qualified Schema.CapNProto.Reader.Schema                      as Schema
import qualified Schema.CapNProto.Reader.Schema.CodeGeneratorRequest as CGReq
import qualified Schema.CapNProto.Reader.Schema.Node                 as Node


-- | TODO: make this an array; we're doing random access to it below.
-- I(@zenhack) am waiting on this, since at the time of writing @taktoa
-- is working on some array utilities that will get merged soonish, so
-- it probably makes sense to just wait for that.
nodeNames :: [BS.ByteString]
nodeNames =
    [ "Import"
    , "annotation"
    , "Value"
    , "Type"
    ]

-- TODO: This contains a bit of copypasta from some of the untyped tests; should
-- factor that out.
theAssert :: Assertion
theAssert = do
    bytes <- BS.readFile "tests/data/schema-codegenreq"
    msg <- M.decode bytes
    ((), endQuota) <- runQuotaT (rootPtr msg >>= reader) 1024
    assertEqual "Correct remaining quota" 641 endQuota
  where
    reader :: Struct BS.ByteString -> QuotaT IO ()
    reader root = do
        let req = Schema.CodeGeneratorRequest root
        nodes <- CGReq.nodes req
        requestedFiles <- CGReq.requestedFiles req
        let 37 = length nodes
        let 1 = length requestedFiles
        mapM_ (walkNode nodes) [0,1..36]
    walkNode nodes i = do
        node <- index i nodes
        -- None of the nodes in the schema have parameters:
        False <- Node.hasParameters node
        -- And none of them are generic:
        False <- Node.isGeneric node

        Text name <- Node.displayName node
        prefixLen <- Node.displayNamePrefixLength node
        let baseName = BS.drop (fromIntegral prefixLen) name

        when (i < Prelude.length nodeNames && baseName /= (nodeNames !! i)) $
            error "Incorrect name."

        has <- Node.hasAnnotations node

        -- there are two annotations in all of the nodes, at these indicies:
        case (has, i `elem` [4, 9]) of
            (False, False) -> return ()
            (True, True) -> do
                1 <- length <$> Node.annotations node
                return ()
            (False, True) ->
                error $ "Node at index " ++ show i ++ " should have had" ++
                        "an annotation."
            (True, False) ->
                error $ "Node at index " ++ show i ++ " should not " ++
                        "have had an annotation."
{'fraction_non_alphanumeric': 0.08793969849246232, 'fraction_numerical': 0.005339195979899497, 'mean_word_length': 2.132808657156911, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2975948', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1678, 'n_words': 641}