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

unit test for recipe coverage #1676

Merged
merged 2 commits into from
Dec 12, 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
2 changes: 2 additions & 0 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ test-suite swarm-unit
TestInventory
TestModel
TestPedagogy
TestRecipeCoverage
TestNotification
TestLanguagePipeline
TestOrdering
Expand All @@ -451,6 +452,7 @@ test-suite swarm-unit

build-depends: tasty >= 0.10 && < 1.6,
tasty-hunit >= 0.10 && < 0.11,
tasty-expected-failure >= 0.12 && < 0.13,
tasty-quickcheck >= 0.10 && < 0.11,
QuickCheck >= 2.14 && < 2.15,
-- Imports shared with the library don't need bounds
Expand Down
2 changes: 2 additions & 0 deletions test/unit/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import TestNotification (testNotification)
import TestOrdering (testOrdering)
import TestPedagogy (testPedagogy)
import TestPretty (testPrettyConst)
import TestRecipeCoverage (testDeviceRecipeCoverage)
import TestScoring (testHighScores)
import Witch (from)

Expand All @@ -55,6 +56,7 @@ tests s =
, testPrettyConst
, testBoolExpr
, testCommands
, testDeviceRecipeCoverage (s ^. runtimeState)
, testHighScores
, testEval (s ^. gameState)
, testModel
Expand Down
48 changes: 48 additions & 0 deletions test/unit/TestRecipeCoverage.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{-# LANGUAGE OverloadedStrings #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- Ensure recipe coverage for all entities that
-- grant capabilities (aka "devices").
module TestRecipeCoverage where

import Control.Lens ((^.))
import Data.Map qualified as M
import Data.Set qualified as Set
import Data.Text qualified as T
import Swarm.Game.Entity (EntityMap (entitiesByCap), entityName)
import Swarm.Game.Recipe (recipeOutputs)
import Swarm.TUI.Model (RuntimeState, stdEntityMap, stdRecipes)
import Swarm.Util (commaList, quote)
import Test.Tasty
import Test.Tasty.ExpectedFailure (expectFailBecause)
import Test.Tasty.HUnit

testDeviceRecipeCoverage :: RuntimeState -> TestTree
testDeviceRecipeCoverage rs =
testGroup
"Recipe coverage"
[ expectFailBecause "Need to come up with more recipes" checkCoverage
]
where
checkCoverage :: TestTree
checkCoverage =
testCase
"Ensure all devices have recipes (#1268)"
$ assertBool errMessage
$ null nonCoveredEntities
where
errMessage =
T.unpack $
T.unwords
[ "Missing recipes for:"
, commaList $ map quote $ Set.toList nonCoveredEntities
]

-- Only include entities that grant a capability:
entityNames = Set.fromList . map (^. entityName) . concat . M.elems . entitiesByCap $ rs ^. stdEntityMap

getOutputsForRecipe r = map ((^. entityName) . snd) $ r ^. recipeOutputs
recipeOutputEntities = Set.fromList . concatMap getOutputsForRecipe $ rs ^. stdRecipes
nonCoveredEntities = Set.difference entityNames recipeOutputEntities
Loading