mirror of
https://github.com/simonmichael/hledger.git
synced 2024-09-20 02:37:12 +03:00
Added a simple 'showMarketPrice' function to show market price directives in a journal-compatible way (#505)
* added showMarketPrice and Hledger.Data.MarketPrice module * showMarketPrice implemented using showDate * attempted to add tests to Hledger.Data.MarketPrice * moved MarketPrice test to Hledger.Read.JournalReader; fixed documentation on MarketPrice; added MarketPrice module to package.yaml
This commit is contained in:
parent
3564c929e1
commit
6bccb847d5
@ -15,6 +15,7 @@ module Hledger.Data (
|
||||
module Hledger.Data.Dates,
|
||||
module Hledger.Data.Journal,
|
||||
module Hledger.Data.Ledger,
|
||||
module Hledger.Data.MarketPrice,
|
||||
module Hledger.Data.Period,
|
||||
module Hledger.Data.Posting,
|
||||
module Hledger.Data.RawOptions,
|
||||
@ -34,6 +35,7 @@ import Hledger.Data.Commodity
|
||||
import Hledger.Data.Dates
|
||||
import Hledger.Data.Journal
|
||||
import Hledger.Data.Ledger
|
||||
import Hledger.Data.MarketPrice
|
||||
import Hledger.Data.Period
|
||||
import Hledger.Data.Posting
|
||||
import Hledger.Data.RawOptions
|
||||
@ -50,6 +52,7 @@ tests_Hledger_Data = TestList
|
||||
,tests_Hledger_Data_Amount
|
||||
,tests_Hledger_Data_Commodity
|
||||
,tests_Hledger_Data_Journal
|
||||
,tests_Hledger_Data_MarketPrice
|
||||
,tests_Hledger_Data_Ledger
|
||||
,tests_Hledger_Data_Posting
|
||||
-- ,tests_Hledger_Data_RawOptions
|
||||
|
32
hledger-lib/Hledger/Data/MarketPrice.hs
Normal file
32
hledger-lib/Hledger/Data/MarketPrice.hs
Normal file
@ -0,0 +1,32 @@
|
||||
{-|
|
||||
|
||||
A 'MarketPrice' represents a historical exchange rate between two
|
||||
commodities. (Ledger calls them historical prices.) For example, prices
|
||||
published by a stock exchange or the foreign exchange market. Some
|
||||
commands (balance, currently) can use this information to show the market
|
||||
value of things at a given date.
|
||||
|
||||
-}
|
||||
|
||||
{-# LANGUAGE OverloadedStrings, LambdaCase #-}
|
||||
|
||||
module Hledger.Data.MarketPrice
|
||||
where
|
||||
import qualified Data.Text as T
|
||||
import Test.HUnit
|
||||
|
||||
import Hledger.Data.Amount
|
||||
import Hledger.Data.Dates
|
||||
import Hledger.Data.Types
|
||||
|
||||
-- | Get the string representation of an market price, based on its
|
||||
-- commodity's display settings.
|
||||
showMarketPrice :: MarketPrice -> String
|
||||
showMarketPrice mp = unwords
|
||||
[ "P"
|
||||
, showDate (mpdate mp)
|
||||
, T.unpack (mpcommodity mp)
|
||||
, (showAmount . setAmountPrecision maxprecision) (mpamount mp)
|
||||
]
|
||||
|
||||
tests_Hledger_Data_MarketPrice = TestList []
|
@ -624,6 +624,12 @@ test_postingp = do
|
||||
|
||||
tests_Hledger_Read_JournalReader = TestList $ concat [
|
||||
-- test_numberp
|
||||
[
|
||||
"showParsedMarketPrice" ~: do
|
||||
let mp = parseWithState mempty marketpricedirectivep "P 2017/01/30 BTC $922.83\n"
|
||||
mpString = (fmap . fmap) showMarketPrice mp
|
||||
mpString `is` (Just (Right "P 2017/01/30 BTC $922.83"))
|
||||
]
|
||||
]
|
||||
|
||||
{- old hunit tests
|
||||
|
@ -110,6 +110,7 @@ library
|
||||
Hledger.Data.Dates
|
||||
Hledger.Data.Journal
|
||||
Hledger.Data.Ledger
|
||||
Hledger.Data.MarketPrice
|
||||
Hledger.Data.Period
|
||||
Hledger.Data.StringFormat
|
||||
Hledger.Data.Posting
|
||||
|
@ -91,6 +91,7 @@ library:
|
||||
- Hledger.Data.Dates
|
||||
- Hledger.Data.Journal
|
||||
- Hledger.Data.Ledger
|
||||
- Hledger.Data.MarketPrice
|
||||
- Hledger.Data.Period
|
||||
- Hledger.Data.StringFormat
|
||||
- Hledger.Data.Posting
|
||||
|
Loading…
Reference in New Issue
Block a user