From afd72daea97cc54643528bf8833d3875a38267d8 Mon Sep 17 00:00:00 2001 From: Barry Moore Date: Thu, 3 Oct 2019 21:13:27 -0400 Subject: [PATCH 1/6] Initial diamond text support in test suite --- .../examples/success-standard/package.yaml | 1 + exercises/diamond/package.yaml | 3 ++- exercises/diamond/test/Tests.hs | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/exercises/diamond/examples/success-standard/package.yaml b/exercises/diamond/examples/success-standard/package.yaml index 05c4caea2..767a8874f 100644 --- a/exercises/diamond/examples/success-standard/package.yaml +++ b/exercises/diamond/examples/success-standard/package.yaml @@ -15,3 +15,4 @@ tests: - diamond - hspec - QuickCheck + - text diff --git a/exercises/diamond/package.yaml b/exercises/diamond/package.yaml index b24c564f7..07fb2b0db 100644 --- a/exercises/diamond/package.yaml +++ b/exercises/diamond/package.yaml @@ -1,5 +1,5 @@ name: diamond -version: 1.1.0.5 +version: 1.1.0.6 dependencies: - base @@ -20,3 +20,4 @@ tests: - diamond - hspec - QuickCheck + - text diff --git a/exercises/diamond/test/Tests.hs b/exercises/diamond/test/Tests.hs index 864896a1c..54167a41d 100644 --- a/exercises/diamond/test/Tests.hs +++ b/exercises/diamond/test/Tests.hs @@ -1,4 +1,6 @@ {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE FlexibleInstances #-} import Data.Char (isLetter, isPrint, isSpace) import Data.Foldable (for_) @@ -9,9 +11,19 @@ import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) import Test.QuickCheck (arbitraryASCIIChar, conjoin, counterexample, discard, elements, forAll, forAllShrink, Gen, Property, suchThat, Testable, (===)) +import Data.Text (Text, unpack) import Diamond (diamond) +class ToString a where + toString :: a -> String + +instance ToString String where + toString = id + +instance ToString Text where + toString = unpack + main :: IO () main = hspecWith defaultConfig {configFastFail = True} specs @@ -50,7 +62,7 @@ specs = describe "diamond" $ do where test Case{..} = it description assertion where - assertion = diamond input `shouldBe` Just expected + assertion = (fmap . fmap) toString (diamond input) `shouldBe` Just expected data Case = Case { description :: String @@ -151,7 +163,7 @@ genAlphaChar :: Gen Char genAlphaChar = elements ['A'..'Z'] genDiamond :: Gen (Maybe [String]) -genDiamond = diamond <$> genAlphaChar +genDiamond = (fmap . fmap . fmap) toString $ diamond <$> genAlphaChar forAllDiamond :: Testable prop => ([String] -> prop) -> Property forAllDiamond p = forAll genDiamond $ maybe discard p From a8ccd0efaebfa88207ba1f95b4849e7145110010 Mon Sep 17 00:00:00 2001 From: Barry Moore Date: Thu, 17 Oct 2019 19:47:28 -0400 Subject: [PATCH 2/6] Replace custom isString with string-conversions --- .../examples/success-standard/package.yaml | 1 + exercises/diamond/package.yaml | 1 + exercises/diamond/test/Tests.hs | 33 +++++++------------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/exercises/diamond/examples/success-standard/package.yaml b/exercises/diamond/examples/success-standard/package.yaml index 767a8874f..d2a798394 100644 --- a/exercises/diamond/examples/success-standard/package.yaml +++ b/exercises/diamond/examples/success-standard/package.yaml @@ -16,3 +16,4 @@ tests: - hspec - QuickCheck - text + - string-conversions diff --git a/exercises/diamond/package.yaml b/exercises/diamond/package.yaml index 07fb2b0db..affbac1b5 100644 --- a/exercises/diamond/package.yaml +++ b/exercises/diamond/package.yaml @@ -21,3 +21,4 @@ tests: - hspec - QuickCheck - text + - string-conversions diff --git a/exercises/diamond/test/Tests.hs b/exercises/diamond/test/Tests.hs index 54167a41d..44de0055f 100644 --- a/exercises/diamond/test/Tests.hs +++ b/exercises/diamond/test/Tests.hs @@ -2,28 +2,19 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE FlexibleInstances #-} -import Data.Char (isLetter, isPrint, isSpace) -import Data.Foldable (for_) -import Data.List (isSuffixOf) -import Data.Maybe (isJust, isNothing) -import Test.Hspec (Spec, describe, it, shouldBe) -import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) -import Test.QuickCheck (arbitraryASCIIChar, conjoin, counterexample, - discard, elements, forAll, forAllShrink, Gen, - Property, suchThat, Testable, (===)) -import Data.Text (Text, unpack) +import Data.Char (isLetter, isPrint, isSpace) +import Data.Foldable (for_) +import Data.List (isSuffixOf) +import Data.Maybe (isJust, isNothing) +import Test.Hspec (Spec, describe, it, shouldBe) +import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) +import Test.QuickCheck (arbitraryASCIIChar, conjoin, counterexample, + discard, elements, forAll, forAllShrink, Gen, + Property, suchThat, Testable, (===)) +import Data.String.Conversions (convertString) import Diamond (diamond) -class ToString a where - toString :: a -> String - -instance ToString String where - toString = id - -instance ToString Text where - toString = unpack - main :: IO () main = hspecWith defaultConfig {configFastFail = True} specs @@ -62,7 +53,7 @@ specs = describe "diamond" $ do where test Case{..} = it description assertion where - assertion = (fmap . fmap) toString (diamond input) `shouldBe` Just expected + assertion = (fmap . fmap) convertString (diamond input) `shouldBe` Just expected data Case = Case { description :: String @@ -163,7 +154,7 @@ genAlphaChar :: Gen Char genAlphaChar = elements ['A'..'Z'] genDiamond :: Gen (Maybe [String]) -genDiamond = (fmap . fmap . fmap) toString $ diamond <$> genAlphaChar +genDiamond = (fmap . fmap . fmap) convertString $ diamond <$> genAlphaChar forAllDiamond :: Testable prop => ([String] -> prop) -> Property forAllDiamond p = forAll genDiamond $ maybe discard p From 3bba1d14567dc35df7b9e7779aa02208f6d1840a Mon Sep 17 00:00:00 2001 From: Barry Moore Date: Thu, 17 Oct 2019 20:02:23 -0400 Subject: [PATCH 3/6] Add .meta/hints.md --- exercises/diamond/.meta/hints.md | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 exercises/diamond/.meta/hints.md diff --git a/exercises/diamond/.meta/hints.md b/exercises/diamond/.meta/hints.md new file mode 100644 index 000000000..88b391bef --- /dev/null +++ b/exercises/diamond/.meta/hints.md @@ -0,0 +1,39 @@ +## Hints + +You need to implement the `diamond` function which prints a diamond starting at +`A` with the given character at its widest points. You can use the provided +signature if you are unsure about the types, but don't let it restrict your +creativity: + +```haskell +diamond :: Char -> Maybe [String] +``` + +This exercise works with textual data. For historical reasons, Haskell's +`String` type is synonymous with `[Char]`, a list of characters. For more +efficient handling of textual data, the `Text` type can be used. + +As an optional extension to this exercise, you can + +- Read about [string types](https://haskell-lang.org/tutorial/string-types) in + Haskell. +- Add `- text` to your list of dependencies in package.yaml. +- Import `Data.Text` in [the following + way](https://hackernoon.com/4-steps-to-a-better-imports-list-in-haskell-43a3d868273c): + +```haskell +import qualified Data.Text as T +import Data.Text (Text) +``` + +- You can now write e.g. `diamond :: Char -> Maybe [Text]` and refer to + `Data.Text` combinators as e.g. `T.pack`, +- Look up the documentation for + [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html), +- You can then replace all occurrences of `String` with `Text` in Diamond.hs: + +```haskell +diamond :: Char -> Maybe [Text] +``` + +This part is entirely optional. From e8e3f0e9176dcdd265b4368d8c8097805ac098f0 Mon Sep 17 00:00:00 2001 From: Barry Moore Date: Thu, 17 Oct 2019 20:13:04 -0400 Subject: [PATCH 4/6] Run configlet to generate new README.md --- exercises/diamond/README.md | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/exercises/diamond/README.md b/exercises/diamond/README.md index d85616310..dd22e6d52 100644 --- a/exercises/diamond/README.md +++ b/exercises/diamond/README.md @@ -52,6 +52,47 @@ E·······E ····A···· ``` +## Hints + +You need to implement the `diamond` function which prints a diamond starting at +`A` with the given character at its widest points. You can use the provided +signature if you are unsure about the types, but don't let it restrict your +creativity: + +```haskell +diamond :: Char -> Maybe [String] +``` + +This exercise works with textual data. For historical reasons, Haskell's +`String` type is synonymous with `[Char]`, a list of characters. For more +efficient handling of textual data, the `Text` type can be used. + +As an optional extension to this exercise, you can + +- Read about [string types](https://haskell-lang.org/tutorial/string-types) in + Haskell. +- Add `- text` to your list of dependencies in package.yaml. +- Import `Data.Text` in [the following + way](https://hackernoon.com/4-steps-to-a-better-imports-list-in-haskell-43a3d868273c): + +```haskell +import qualified Data.Text as T +import Data.Text (Text) +``` + +- You can now write e.g. `diamond :: Char -> Maybe [Text]` and refer to + `Data.Text` combinators as e.g. `T.pack`, +- Look up the documentation for + [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html), +- You can then replace all occurrences of `String` with `Text` in Diamond.hs: + +```haskell +diamond :: Char -> Maybe [Text] +``` + +This part is entirely optional. + + ## Getting Started From 2a125f693211fcc012ceb9d8931fcba0b6ff7eb1 Mon Sep 17 00:00:00 2001 From: Simon Shine Date: Sat, 19 Oct 2019 19:05:31 +0200 Subject: [PATCH 5/6] Avoid hyperlinking to specific Hackage package versions Since #874 we've tried to avoid linking to specific versions of Hackage packages in Markdown files (see justification in #868). This wasn't enforced while this was worked on. --- exercises/diamond/.meta/hints.md | 2 +- exercises/diamond/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/diamond/.meta/hints.md b/exercises/diamond/.meta/hints.md index 88b391bef..223b53fa4 100644 --- a/exercises/diamond/.meta/hints.md +++ b/exercises/diamond/.meta/hints.md @@ -29,7 +29,7 @@ import Data.Text (Text) - You can now write e.g. `diamond :: Char -> Maybe [Text]` and refer to `Data.Text` combinators as e.g. `T.pack`, - Look up the documentation for - [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html), + [`Data.Text`](https://hackage.haskell.org/package/text/docs/Data-Text.html), - You can then replace all occurrences of `String` with `Text` in Diamond.hs: ```haskell diff --git a/exercises/diamond/README.md b/exercises/diamond/README.md index dd22e6d52..88e684e63 100644 --- a/exercises/diamond/README.md +++ b/exercises/diamond/README.md @@ -83,7 +83,7 @@ import Data.Text (Text) - You can now write e.g. `diamond :: Char -> Maybe [Text]` and refer to `Data.Text` combinators as e.g. `T.pack`, - Look up the documentation for - [`Data.Text`](https://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html), + [`Data.Text`](https://hackage.haskell.org/package/text/docs/Data-Text.html), - You can then replace all occurrences of `String` with `Text` in Diamond.hs: ```haskell From 25b8748643857300d1c958983861669b8b3fa38e Mon Sep 17 00:00:00 2001 From: Simon Shine Date: Sat, 19 Oct 2019 19:08:40 +0200 Subject: [PATCH 6/6] Order imports alphabetically --- exercises/diamond/test/Tests.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/diamond/test/Tests.hs b/exercises/diamond/test/Tests.hs index 44de0055f..6be83765e 100644 --- a/exercises/diamond/test/Tests.hs +++ b/exercises/diamond/test/Tests.hs @@ -6,12 +6,12 @@ import Data.Char (isLetter, isPrint, isSpace) import Data.Foldable (for_) import Data.List (isSuffixOf) import Data.Maybe (isJust, isNothing) +import Data.String.Conversions (convertString) import Test.Hspec (Spec, describe, it, shouldBe) import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith) import Test.QuickCheck (arbitraryASCIIChar, conjoin, counterexample, discard, elements, forAll, forAllShrink, Gen, Property, suchThat, Testable, (===)) -import Data.String.Conversions (convertString) import Diamond (diamond)