From 35a7f1f2b893587191bde0fd7ff51fc7d611f890 Mon Sep 17 00:00:00 2001 From: Phil de Joux Date: Sat, 26 Jun 2021 11:51:39 -0400 Subject: [PATCH 1/2] Add a test showing Alt in km as Alt in m. --- uom-plugin/test-suite-units/Tests.hs | 2 ++ uom-plugin/test-suite-units/Z.hs | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/uom-plugin/test-suite-units/Tests.hs b/uom-plugin/test-suite-units/Tests.hs index 53e451e4..b5a60e61 100644 --- a/uom-plugin/test-suite-units/Tests.hs +++ b/uom-plugin/test-suite-units/Tests.hs @@ -77,6 +77,7 @@ import Test.Tasty.HUnit import Defs () import ErrorTests import Z (z) +import qualified Z (tests) -- Some basic examples @@ -327,6 +328,7 @@ tests = testGroup "uom-plugin" , testCase "2.4 l/h in m" $ convert [u| 2.4 l/ha |] @?= [u| 2.4e-7 m |] , testCase "1 m^4 in l m" $ convert [u| 1 m^4 |] @?= [u| 1000 l m |] ] + , Z.tests , testGroup "errors" [ testCase "s/m ~ m/s" $ mismatch1 `throws` mismatch1_errors , testCase "m + s" $ mismatch2 `throws` mismatch2_errors diff --git a/uom-plugin/test-suite-units/Z.hs b/uom-plugin/test-suite-units/Z.hs index 3e727e84..7d72f83f 100644 --- a/uom-plugin/test-suite-units/Z.hs +++ b/uom-plugin/test-suite-units/Z.hs @@ -1,12 +1,21 @@ +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -fplugin Data.UnitsOfMeasure.Plugin #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} -module Z (z) where +module Z (Alt(..), z, tests) where -import Data.UnitsOfMeasure.Convert (convert) +import Test.Tasty +import Test.Tasty.HUnit + +import Data.UnitsOfMeasure (Quantity, u) +import Data.UnitsOfMeasure.Convert (Convertible, convert) +import Data.UnitsOfMeasure.Defs () -- Inferring this type used to lead to unit equations with occur-check @@ -18,3 +27,16 @@ import Data.UnitsOfMeasure.Convert (convert) -- -> Quantity a v {-# ANN z "HLint: ignore Eta reduce" #-} z q = convert q + +newtype Alt a = Alt a + +instance (Convertible u [u| m |], q ~ Quantity Double u) => Show (Alt q) where + show (Alt x) = show y + where + y :: Quantity Double [u| m |] + y = convert x + +tests :: TestTree +tests = testGroup "show via convert" + [ testCase "1.01km" $ show (Alt [u| 1.01 km |]) @?= "[u| 1010.0 m |]" + ] \ No newline at end of file From 7d44c5c012bde2395d890ed7ed0d8e846cc7bf2e Mon Sep 17 00:00:00 2001 From: Phil de Joux Date: Sat, 26 Jun 2021 12:04:08 -0400 Subject: [PATCH 2/2] =?UTF-8?q?Add=20a=20failure=20Pack=20(Unpack=20(Base?= =?UTF-8?q?=20"m"))=20/=3D=C2=A0Base=20"m".?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uom-plugin/test-suite-units/Z.hs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/uom-plugin/test-suite-units/Z.hs b/uom-plugin/test-suite-units/Z.hs index 7d72f83f..f09bcbd1 100644 --- a/uom-plugin/test-suite-units/Z.hs +++ b/uom-plugin/test-suite-units/Z.hs @@ -8,7 +8,7 @@ {-# OPTIONS_GHC -fplugin Data.UnitsOfMeasure.Plugin #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} -module Z (Alt(..), z, tests) where +module Z (z, tests) where import Test.Tasty import Test.Tasty.HUnit @@ -28,15 +28,23 @@ import Data.UnitsOfMeasure.Defs () {-# ANN z "HLint: ignore Eta reduce" #-} z q = convert q -newtype Alt a = Alt a +newtype A a = A a +newtype B a = B a -instance (Convertible u [u| m |], q ~ Quantity Double u) => Show (Alt q) where - show (Alt x) = show y +instance (Convertible u [u| m |], q ~ Quantity Double u) => Show (A q) where + show (A x) = show y + where + y :: Quantity Double [u| m |] + y = convert x + +instance (q ~ Quantity Double [u| m |]) => Show (B q) where + show (B x) = show y where y :: Quantity Double [u| m |] y = convert x tests :: TestTree tests = testGroup "show via convert" - [ testCase "1.01km" $ show (Alt [u| 1.01 km |]) @?= "[u| 1010.0 m |]" + [ testCase "A 1.01km" $ show (A [u| 1.01 km |]) @?= "[u| 1010.0 m |]" + , testCase "B 1010m" $ show (B [u| 1010.0 m |]) @?= "[u| 1010.0 m |]" ] \ No newline at end of file