Skip to content

Commit

Permalink
Configure unit tests (#3)
Browse files Browse the repository at this point in the history
* ignore generated *.agdai files

* export Backend from Main

* move Main from src => app

* change cabal config add library and test-suite

* fix warnings in Backend

* test if backend is enabled

* add tests to CI

* update README with info how to run tests

* changelog for 0.1.0.2

* add missing newline in Main.hs
  • Loading branch information
lemastero authored Dec 14, 2023
1 parent c98b0b8 commit 90d4188
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ jobs:
- name: Build
run: cabal build all

- name: Run tests
run: cabal test all

- name: Run tests
run: cabal run -- agda2rust ./test/Hello.agda
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ _build
dist
dist-newstyle
*~
**/*.agdai
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@
* change upper bound for base to 4.20 to fix CI
* add CHANGELOG.md
* document how to compile example

## 0.1.0.2 -- 2023-12-14
* reorganise to library and executable and tests
* test if backend is enabled using HSpec
* cleanup warnings
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ cabal run -- agda2rust --help
cabal run -- agda2rust ./test/Hello.agda
cabal run -- agda2rust ./test/Test.agda
```
* Run tests

```sh
cabal test all
```
43 changes: 37 additions & 6 deletions agda2rust.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
cabal-version: 2.2
cabal-version: 3.8
-- Further docs http://haskell.org/cabal/users-guide/
name: agda2rust
version: 0.1.0.1
-- See the Haskell package versioning policy (PVP) for standards
-- https://pvp.haskell.org
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.2
description: Allows to export Rust source files from formal specification in Agda
license: MIT
license-file: LICENSE
Expand All @@ -16,12 +22,37 @@ source-repository head
type: git
location: https://github.com/lemastero/agda2rust.git

executable agda2rust
common warnings
ghc-options: -Wall

library
hs-source-dirs: src
main-is: Main.hs
other-modules: Paths_agda2rust
exposed-modules: Agda.Compiler.Rust.Backend Paths_agda2rust
autogen-modules: Paths_agda2rust
build-depends: base >= 4.10 && < 4.20,
Agda >= 2.6.4 && < 2.6.5,
deepseq >= 1.4.4 && < 1.6
deepseq >= 1.4.4 && < 1.6
default-language: Haskell2010
import: warnings

executable agda2rust
hs-source-dirs: app
main-is: Main.hs
other-modules: Paths_agda2rust
autogen-modules: Paths_agda2rust
build-depends: base >= 4.10 && < 4.20,
Agda >= 2.6.4 && < 2.6.5,
agda2rust
default-language: Haskell2010
import: warnings

test-suite agda2rust-test
import: warnings
default-language: Haskell2010
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: RustBackendTest.hs
build-depends: base >=4.10 && < 4.20,
Agda >= 2.6.4 && < 2.6.5,
HUnit >= 1.6.2.0,
agda2rust
6 changes: 6 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main where

import Agda.Compiler.Rust.Backend ( runRustBackend )

main :: IO ()
main = runRustBackend
16 changes: 10 additions & 6 deletions src/Main.hs → src/Agda/Compiler/Rust/Backend.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{-# LANGUAGE LambdaCase, RecordWildCards #-}
module Main where
module Agda.Compiler.Rust.Backend (
runRustBackend,
backend,
defaultOptions ) where

import Data.Maybe ( fromMaybe )
import Control.Monad ( unless )
Expand All @@ -25,7 +28,8 @@ import Agda.TypeChecking.Monad

import Agda.Main ( runAgda )

main = runAgda [Backend backend]
runRustBackend :: IO ()
runRustBackend = runAgda [Backend backend]

data Options = Options { optOutDir :: Maybe FilePath }

Expand All @@ -51,24 +55,24 @@ backend = Backend'
[ Option ['o'] ["out-dir"] (ReqArg outdirOpt "DIR")
"Write output files to DIR. (default: project root)"
]
, isEnabled = \ _ -> True
, isEnabled = const True
, preCompile = return
, postCompile = \ _ _ _ -> return ()
, preModule = moduleSetup
, postModule = writeModule
, compileDef = compile
, scopeCheckingSuffices = False
, mayEraseType = \ _ -> return True
, mayEraseType = const $ return True
}

moduleSetup :: Options -> IsMain -> TopLevelModuleName -> Maybe FilePath
-> TCM (Recompile ModuleEnv ModuleRes)
moduleSetup _ _ m _ = do
moduleSetup _ _ _ _ = do
setScope . iInsideScope =<< curIF
return $ Recompile ()

compile :: Options -> ModuleEnv -> IsMain -> Definition -> TCM CompiledDef
compile opts tlm _ Defn{..}
compile _ _ _ Defn{..}
= withCurrentModule (qnameModule defName)
$ getUniqueCompilerPragma "AGDA2RUST" defName >>= \case
Nothing -> return []
Expand Down
22 changes: 22 additions & 0 deletions test/RustBackendTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Main (main) where

import Agda.Compiler.Rust.Backend ( backend, defaultOptions )
import Test.HUnit (
Test(..)
, assertEqual
, failures
, runTestTT)
import System.Exit ( exitFailure , exitSuccess )
import Agda.Compiler.Backend ( isEnabled )

testIsEnabled :: Test
testIsEnabled = TestCase
(assertEqual "isEnabled" (isEnabled backend defaultOptions) True)

tests :: Test
tests = TestList [TestLabel "rustBackend" testIsEnabled]

main :: IO ()
main = do
result <- runTestTT tests
if failures result > 0 then exitFailure else exitSuccess

0 comments on commit 90d4188

Please sign in to comment.