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

NO-ISSUE - add some tests #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 21 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# This Travis job script has been generated by a script via
#
# haskell-ci 'kontrakcja-templates.cabal'
# haskell-ci 'kontrakcja-templates.cabal' '--output' '.travis.yml'
#
# To regenerate the script (for example after adjusting tested-with) run
#
# haskell-ci regenerate
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.9.20200330
# version: 0.12.1
#
version: ~> 1.0
language: c
Expand All @@ -30,22 +34,22 @@ before_cache:
jobs:
include:
- compiler: ghc-8.10.1
addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.10.1","cabal-install-3.2"]}}
addons: {"apt":{"packages":["ghc-8.10.1","cabal-install-3.4"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main"}]}}
os: linux
- compiler: ghc-8.8.3
addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.8.3","cabal-install-3.0"]}}
addons: {"apt":{"packages":["ghc-8.8.3","cabal-install-3.4"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main"}]}}
os: linux
- compiler: ghc-8.6.5
addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.6.5","cabal-install-3.0"]}}
addons: {"apt":{"packages":["ghc-8.6.5","cabal-install-3.4"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main"}]}}
os: linux
- compiler: ghc-8.4.4
addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.4.4","cabal-install-3.0"]}}
addons: {"apt":{"packages":["ghc-8.4.4","cabal-install-3.4"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main"}]}}
os: linux
- compiler: ghc-8.2.2
addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.2.2","cabal-install-3.0"]}}
addons: {"apt":{"packages":["ghc-8.2.2","cabal-install-3.4"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main"}]}}
os: linux
- compiler: ghc-8.0.2
addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.0.2","cabal-install-3.0"]}}
addons: {"apt":{"packages":["ghc-8.0.2","cabal-install-3.4"],"sources":[{"key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286","sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main"}]}}
os: linux
before_install:
- HC=$(echo "/opt/$CC/bin/ghc" | sed 's/-/\//')
Expand All @@ -68,7 +72,7 @@ before_install:
- |
echo "verbose: normal +nowrap +markoutput" >> $CABALHOME/config
echo "remote-build-reporting: anonymous" >> $CABALHOME/config
echo "write-ghc-environment-files: always" >> $CABALHOME/config
echo "write-ghc-environment-files: never" >> $CABALHOME/config
echo "remote-repo-cache: $CABALHOME/packages" >> $CABALHOME/config
echo "logs-dir: $CABALHOME/logs" >> $CABALHOME/config
echo "world-file: $CABALHOME/world" >> $CABALHOME/config
Expand All @@ -95,8 +99,8 @@ install:
- touch cabal.project
- |
echo "packages: ." >> cabal.project
- echo 'package kontrakcja-templates' >> cabal.project
- "echo ' ghc-options: -Werror=missing-methods' >> cabal.project"
- if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo 'package kontrakcja-templates' >> cabal.project ; fi
- "if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
- |
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(kontrakcja-templates)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
- cat cabal.project || true
Expand All @@ -122,8 +126,8 @@ script:
- touch cabal.project
- |
echo "packages: ${PKGDIR_kontrakcja_templates}" >> cabal.project
- echo 'package kontrakcja-templates' >> cabal.project
- "echo ' ghc-options: -Werror=missing-methods' >> cabal.project"
- if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo 'package kontrakcja-templates' >> cabal.project ; fi
- "if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
- |
- "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(kontrakcja-templates)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
- cat cabal.project || true
Expand All @@ -133,7 +137,9 @@ script:
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all
# Building with tests and benchmarks...
# build & run tests, build benchmarks
- ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} all
- ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} all --write-ghc-environment-files=always
# Testing...
- ${CABAL} v2-test $WITHCOMPILER ${TEST} ${BENCH} all --test-show-details=direct
# cabal check...
- (cd ${PKGDIR_kontrakcja_templates} && ${CABAL} -vnormal check)
# haddock...
Expand All @@ -142,5 +148,5 @@ script:
- rm -f cabal.project.local
- ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all

# REGENDATA ("0.9.20200330",["kontrakcja-templates.cabal"])
# REGENDATA ("0.12.1",["kontrakcja-templates.cabal","--output",".travis.yml"])
# EOF
21 changes: 20 additions & 1 deletion kontrakcja-templates.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ Category: Web
Cabal-version: >=1.10
Tested-with: GHC ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.3 || ==8.10.1

extra-source-files:
test/fixtures/templates/*.st
test/fixtures/texts/fr/*.json
test/fixtures/texts/jp/*.json

Source-repository head
type: git
location: https://github.com/scrive/kontrakcja-templates.git


Library
Default-Language: Haskell2010
exposed-modules: Text.StringTemplates.Fields
Text.StringTemplates.Files
Text.StringTemplates.Templates
Expand All @@ -47,9 +54,21 @@ Library
html >= 1.0.1.2,
json >= 0.5

Default-language: Haskell2010
Default-Extensions: GeneralizedNewtypeDeriving
MultiParamTypeClasses
TypeFamilies
UndecidableInstances
FlexibleInstances

test-suite kontrakcja-templates-test
Default-Language: Haskell2010
type: exitcode-stdio-1.0
main-is: Main.hs
hs-source-dirs: test
build-depends: base >= 4.9 && < 5
, HStringTemplate >= 0.7.0
, kontrakcja-templates
, containers >= 0.4.2.1
, tasty
, tasty-hunit
Default-Extensions: ScopedTypeVariables
47 changes: 47 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Main where

import Test.Tasty
import Test.Tasty.HUnit
import qualified Data.Map as Map
import Text.StringTemplate
import qualified Control.Exception as Exception
import Control.Exception (SomeException(..))
import Data.List (isPrefixOf)

import Text.StringTemplates.TemplatesLoader (readGlobalTemplates, GlobalTemplates)

main :: IO ()
main = do
defaultMain $
testGroup "Templates Loader" $
[ testCase "fetch value from text templates" $ do
globalTemplates <- readGlobalTemplates textFixtures templateFixtures "fr"
let mRendered = render <$> (Map.lookup "fr" globalTemplates >>= getStringTemplate "_Hello")
assertEqual "assert same values" (Just "Bonjour .") mRendered
assertEqual "available languages" ["fr", "jp"] $ Map.keys globalTemplates

, testCase "fetch value from templates" $ do
globalTemplates <- readGlobalTemplates textFixtures templateFixtures "fr"
let mRendered = render <$> (Map.lookup "fr" globalTemplates >>= getStringTemplate "foo")
assertEqual "assert same values" (Just "hey") mRendered

, testCase "missing default language should raise an exception" $ do
eException <- Exception.try $ readGlobalTemplates textFixtures templateFixtures "cz"
case eException of
Left (SomeException e) -> do
assertBool "exception raised" $ "Default language cz is not defined." `isPrefixOf` (show e)
_ ->
assertFailure "missing language didn't raise an exception"

, testCase "fill template with default language values if missing values" $ do
globalTemplates <- readGlobalTemplates textFixtures templateFixtures "jp"
let mRendered = render <$> (Map.lookup "fr" globalTemplates >>= getStringTemplate "_Hello")
assertEqual "assert same values" (Just "Bonjour .") mRendered
assertEqual "available languages" ["fr", "jp"] $ Map.keys globalTemplates
]
where
templateFixtures :: String
templateFixtures = "test/fixtures/templates"

textFixtures :: String
textFixtures = "test/fixtures/texts"
3 changes: 3 additions & 0 deletions test/fixtures/templates/foo.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
####
foo=hey
####
4 changes: 4 additions & 0 deletions test/fixtures/templates/hello-page.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
###
adminElmMain=$foo()$
$_Hello$
####
3 changes: 3 additions & 0 deletions test/fixtures/texts/fr/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"_Hello":"Bonjour $name$."
}
1 change: 1 addition & 0 deletions test/fixtures/texts/jp/jp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}