From 45eb4889b08e6290ae9c11f59fca9203bd75b8cc Mon Sep 17 00:00:00 2001 From: Ahn Kiwook Date: Wed, 1 Mar 2017 02:48:37 +0900 Subject: [PATCH] Add Nirum.Constructs.ModulePath.spreadModulePath --- src/Nirum/Constructs/ModulePath.hs | 8 ++++++++ test/Nirum/Constructs/ModulePathSpec.hs | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Nirum/Constructs/ModulePath.hs b/src/Nirum/Constructs/ModulePath.hs index 2139030..aac5508 100644 --- a/src/Nirum/Constructs/ModulePath.hs +++ b/src/Nirum/Constructs/ModulePath.hs @@ -7,6 +7,7 @@ module Nirum.Constructs.ModulePath ( ModulePath ( ModuleName , ancestors , fromFilePath , fromIdentifiers + , spreadModulePath ) where import Data.Char (toLower) @@ -66,3 +67,10 @@ instance IsList ModulePath where (fromIdentifiers identifiers) toList (ModuleName identifier) = [identifier] toList (ModulePath path' identifier) = toList path' ++ [identifier] + +spreadModulePath :: ModulePath -> S.Set [Identifier] +spreadModulePath modulePath' = S.fromList $ getPathList modulePath' + where + getPathList :: ModulePath -> [[Identifier]] + getPathList m@(ModulePath path' _) = toList m : getPathList path' + getPathList (ModuleName identifier) = [[identifier]] diff --git a/test/Nirum/Constructs/ModulePathSpec.hs b/test/Nirum/Constructs/ModulePathSpec.hs index 8a6b64b..681674b 100644 --- a/test/Nirum/Constructs/ModulePathSpec.hs +++ b/test/Nirum/Constructs/ModulePathSpec.hs @@ -3,8 +3,10 @@ module Nirum.Constructs.ModulePathSpec where import Control.Exception (evaluate) import Data.List (sort) +import Data.Maybe (fromJust) import GHC.Exts (IsList (fromList, toList)) +import qualified Data.Set as S import System.FilePath (()) import Test.Hspec.Meta @@ -13,6 +15,7 @@ import Nirum.Constructs.ModulePath ( ModulePath (ModuleName, ModulePath) , ancestors , fromFilePath , fromIdentifiers + , spreadModulePath ) spec :: Spec @@ -82,3 +85,9 @@ spec = fooBarBaz2 `shouldNotSatisfy` (<= fooBarBaz) sort [["abc"], foo, fooBar, fooBarBaz, fooBarBaz2] `shouldBe` [["abc"], foo, fooBar, fooBarBaz, fooBarBaz2] + specify "spreadModulePath" $ do + let foo' = fromJust $ fromIdentifiers ["foo", "bar", "baz"] + spreadModulePath foo' `shouldBe` S.fromList [ ["foo", "bar", "baz"] + , ["foo", "bar"] + , ["foo"] + ]